-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test case for DataEventCollector (#719)
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/DataEventCollectorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.itangcent.idea.plugin | ||
|
||
import com.google.inject.Inject | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.actionSystem.CommonDataKeys | ||
import com.intellij.openapi.actionSystem.DataKey | ||
import com.itangcent.intellij.context.ActionContext | ||
import com.itangcent.mock.BaseContextTest | ||
import com.itangcent.test.mock | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mockito | ||
|
||
/** | ||
* Test case for [DataEventCollector] | ||
* | ||
* @author tangcent | ||
*/ | ||
internal class DataEventCollectorTest : BaseContextTest() { | ||
|
||
@Inject | ||
private lateinit var dataEventCollector: DataEventCollector | ||
|
||
override fun bind(builder: ActionContext.ActionContextBuilder) { | ||
builder.mock(AnActionEvent::class) { | ||
Mockito.`when`(it.getData(CommonDataKeys.PROJECT)) | ||
.thenReturn(mockProject) | ||
Mockito.`when`(it.getData(DataKey.create<String>("hello"))) | ||
.thenReturn("world") | ||
} | ||
} | ||
|
||
@Test | ||
fun getData() { | ||
Assertions.assertEquals(mockProject, dataEventCollector.getData(CommonDataKeys.PROJECT)) | ||
//cached | ||
Assertions.assertEquals(mockProject, dataEventCollector.getData(CommonDataKeys.PROJECT)) | ||
|
||
Assertions.assertEquals("world", dataEventCollector.getData("hello")) | ||
//cached | ||
Assertions.assertEquals("world", dataEventCollector.getData("hello")) | ||
|
||
//create DataEventCollector by constructor | ||
DataEventCollector(actionContext.instance(AnActionEvent::class)).let { | ||
Assertions.assertEquals(mockProject, it.getData(CommonDataKeys.PROJECT)) | ||
Assertions.assertEquals("world", it.getData("hello")) | ||
} | ||
} | ||
|
||
@Test | ||
fun getDataWithDisableDataReachBeforeAccess() { | ||
dataEventCollector.disableDataReach() | ||
Assertions.assertNull(dataEventCollector.getData(CommonDataKeys.PROJECT)) | ||
Assertions.assertNull(dataEventCollector.getData("hello")) | ||
} | ||
|
||
@Test | ||
fun getDataWithDisableDataReachAfterAccess() { | ||
Assertions.assertEquals(mockProject, dataEventCollector.getData(CommonDataKeys.PROJECT)) | ||
Assertions.assertEquals("world", dataEventCollector.getData("hello")) | ||
dataEventCollector.disableDataReach() | ||
//cached | ||
Assertions.assertEquals(mockProject, dataEventCollector.getData(CommonDataKeys.PROJECT)) | ||
Assertions.assertEquals("world", dataEventCollector.getData("hello")) | ||
} | ||
} |