-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
521 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
148 changes: 148 additions & 0 deletions
148
src/test/java/opwvhk/intellij/avro_idl/actions/AvroIdlActionsTest.java
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,148 @@ | ||
package opwvhk.intellij.avro_idl.actions; | ||
|
||
import com.intellij.openapi.actionSystem.*; | ||
import com.intellij.openapi.vfs.LocalFileSystem; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.openapi.vfs.VirtualFileWrapper; | ||
import com.intellij.testFramework.HeavyPlatformTestCase; | ||
import com.intellij.testFramework.MapDataContext; | ||
import com.intellij.testFramework.TestActionEvent; | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase; | ||
import com.intellij.testFramework.fixtures.TempDirTestFixture; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class AvroIdlActionsTest extends | ||
//HeavyPlatformTestCase { | ||
BasePlatformTestCase { | ||
|
||
private Path inputDirectory; | ||
private Path outputDirectory; | ||
private Path resultDirectory; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
Path testDirectory = Path.of(getTestDataPath(), getTestDirectoryName()); | ||
inputDirectory = testDirectory.resolve("input"); | ||
outputDirectory = testDirectory.resolve("output"); | ||
|
||
TempDirTestFixture tempDirFixture = myFixture.getTempDirFixture(); | ||
String tempDirPath = tempDirFixture.getTempDirPath(); | ||
VirtualFile testFilesDirectory = tempDirFixture.copyAll(getTestDirectoryName(), getTestDirectoryName()); | ||
VirtualFile resultVDirectory = tempDirFixture.findOrCreateDir(getTestDirectoryName() + "/results"); | ||
//VirtualFile resultVDirectory = getTempDir().createVirtualDir(getTestDirectoryName()); | ||
ConversionActionBase.targetDirectory = resultVDirectory; | ||
resultDirectory = resultVDirectory.toNioPath(); | ||
|
||
Path[] array = list(outputDirectory, s -> s.sorted(Comparator.comparing(Path::toString)).toArray(Path[]::new)); | ||
if (array.length == 1) { | ||
Path relative = outputDirectory.relativize(array[0]); | ||
Path expectedResultFile = resultDirectory.resolve(relative); | ||
ConversionActionBase.targetFile = new VirtualFileWrapper(expectedResultFile.toFile()); | ||
} | ||
} | ||
|
||
@Override | ||
public void tearDown() throws Exception { | ||
ConversionActionBase.targetDirectory = null; | ||
ConversionActionBase.targetFile = null; | ||
super.tearDown(); | ||
} | ||
|
||
protected String getTestDataPath() { | ||
return "src/test/testData/actions"; | ||
} | ||
|
||
public void testNoFiles() throws IOException { | ||
AnAction action = ActionManager.getInstance().getAction("AvroIdl.IdlToProtocol"); | ||
executeTest(action, false); | ||
} | ||
|
||
public void testNoSuitableFiles() throws IOException { | ||
AnAction action = ActionManager.getInstance().getAction("AvroIdl.IdlToProtocol"); | ||
executeTest(action, false); | ||
} | ||
|
||
public void testIdlToProtocol() throws IOException { | ||
AnAction action = ActionManager.getInstance().getAction("AvroIdl.IdlToProtocol"); | ||
executeTest(action, true); | ||
} | ||
|
||
public void testIdlToSchema() throws IOException { | ||
AnAction action = ActionManager.getInstance().getAction("AvroIdl.IdlToSchema"); | ||
executeTest(action, true); | ||
} | ||
|
||
public void testProtocolToIdl() throws IOException { | ||
AnAction action = ActionManager.getInstance().getAction("AvroIdl.ProtocolToIdl"); | ||
executeTest(action, true); | ||
} | ||
|
||
public void testSchemaToIdl() throws IOException { | ||
AnAction action = ActionManager.getInstance().getAction("AvroIdl.SchemaToIdl"); | ||
executeTest(action, true); | ||
} | ||
|
||
private void executeTest(AnAction action, boolean shouldExecute) throws IOException { | ||
MapDataContext dataContext = new MapDataContext(); | ||
LocalFileSystem vfs = LocalFileSystem.getInstance(); | ||
dataContext.put(LangDataKeys.VIRTUAL_FILE_ARRAY, | ||
list(inputDirectory, s -> s.map(vfs::refreshAndFindFileByNioFile).toArray(VirtualFile[]::new))); | ||
dataContext.put(CommonDataKeys.PROJECT, getProject()); | ||
|
||
TestActionEvent event = new TestActionEvent(dataContext, action); | ||
action.update(event); | ||
Presentation p = event.getPresentation(); | ||
assertThat(p.isEnabled()).as("event %s", p.isEnabled() ? "enabled" : "disabled").isEqualTo(shouldExecute); | ||
assertThat(p.isVisible()).as("event %s", p.isVisible() ? "visible" : "hidden").isEqualTo(shouldExecute); | ||
|
||
action.actionPerformed(event); // Outside if statement to verify it correctly handles null invocations | ||
if (shouldExecute) { | ||
assertSameTextContentRecursive(resultDirectory, outputDirectory); | ||
} else { | ||
try (Stream<Path> resultFiles = Files.list(resultDirectory)) { | ||
assertThat(resultFiles.count()).isEqualTo(0); | ||
} | ||
} | ||
} | ||
|
||
private void assertSameTextContentRecursive(Path actualPath, Path expectedPath) throws IOException { | ||
if (Files.isDirectory(expectedPath)) { | ||
assertThat(actualPath).isDirectory(); | ||
List<Path> actualPaths = list(actualPath, s -> s.map(actualPath::relativize).toList()); | ||
List<Path> expectedPaths = list(expectedPath, s -> s.map(expectedPath::relativize).toList()); | ||
assertThat(actualPaths).isEqualTo(expectedPaths); | ||
for (Path p : expectedPaths) { | ||
assertSameTextContentRecursive(actualPath.resolve(p), expectedPath.resolve(p)); | ||
} | ||
} else { | ||
assertThat(actualPath).isRegularFile().hasFileName(expectedPath.getFileName().toString()); | ||
String actualContent = Files.readString(actualPath); | ||
String expectedContent = Files.readString(expectedPath); | ||
assertThat(actualContent).as("content of %s", actualPath.getFileName().toString()) | ||
.isEqualTo(expectedContent); | ||
} | ||
} | ||
|
||
private static <T> T list(Path path, Function<Stream<Path>, T> function) throws IOException { | ||
try (Stream<Path> stream = Files.list(path)) { | ||
return function.apply(stream); | ||
} | ||
} | ||
|
||
private static List<Path> list(Stream<Path> stream) throws IOException { | ||
try (Stream<Path> s = stream) { | ||
return s.toList(); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/testData/actions/idlToProtocol/input/SimpleProtocol.avdl
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,29 @@ | ||
/** | ||
* Doc comment. | ||
*/ | ||
@namespace("tests") | ||
protocol SimpleProtocol { | ||
|
||
record SampleNode { | ||
int count = 0; | ||
array<SamplePair> subNodes; | ||
} | ||
|
||
record SamplePair { | ||
Method method; | ||
SampleNode node; | ||
} | ||
|
||
record Method { | ||
string name; | ||
string purpose; | ||
} | ||
|
||
record SelfRef { | ||
string something; | ||
array<SelfRef> subNodes = []; | ||
} | ||
|
||
SampleNode buildNodeTree(string declaration); | ||
SelfRef buildRefTree(string declaration); | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/testData/actions/idlToProtocol/output/SimpleProtocol.avpr
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,70 @@ | ||
{ | ||
"protocol" : "SimpleProtocol", | ||
"namespace" : "tests", | ||
"doc" : "Doc comment.", | ||
"types" : [ { | ||
"type" : "record", | ||
"name" : "SampleNode", | ||
"fields" : [ { | ||
"name" : "count", | ||
"type" : "int", | ||
"default" : 0 | ||
}, { | ||
"name" : "subNodes", | ||
"type" : { | ||
"type" : "array", | ||
"items" : { | ||
"type" : "record", | ||
"name" : "SamplePair", | ||
"fields" : [ { | ||
"name" : "method", | ||
"type" : { | ||
"type" : "record", | ||
"name" : "Method", | ||
"fields" : [ { | ||
"name" : "name", | ||
"type" : "string" | ||
}, { | ||
"name" : "purpose", | ||
"type" : "string" | ||
} ] | ||
} | ||
}, { | ||
"name" : "node", | ||
"type" : "SampleNode" | ||
} ] | ||
} | ||
} | ||
} ] | ||
}, { | ||
"type" : "record", | ||
"name" : "SelfRef", | ||
"fields" : [ { | ||
"name" : "something", | ||
"type" : "string" | ||
}, { | ||
"name" : "subNodes", | ||
"type" : { | ||
"type" : "array", | ||
"items" : "SelfRef" | ||
}, | ||
"default" : [ ] | ||
} ] | ||
} ], | ||
"messages" : { | ||
"buildNodeTree" : { | ||
"request" : [ { | ||
"name" : "declaration", | ||
"type" : "string" | ||
} ], | ||
"response" : "SampleNode" | ||
}, | ||
"buildRefTree" : { | ||
"request" : [ { | ||
"name" : "declaration", | ||
"type" : "string" | ||
} ], | ||
"response" : "SelfRef" | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/testData/actions/idlToSchema/input/SchemaCycles.avdl
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,21 @@ | ||
namespace tests; | ||
|
||
record SelfRef { | ||
string something; | ||
array<SelfRef> subNodes = []; | ||
} | ||
|
||
record SampleNode { | ||
int count = 0; | ||
array<SamplePair> subNodes; | ||
} | ||
|
||
record SamplePair { | ||
Method method; | ||
SampleNode node; | ||
} | ||
|
||
record Method { | ||
string name; | ||
string purpose; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/testData/actions/idlToSchema/output/SampleNode.avsc
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,36 @@ | ||
{ | ||
"type" : "record", | ||
"name" : "SampleNode", | ||
"namespace" : "tests", | ||
"fields" : [ { | ||
"name" : "count", | ||
"type" : "int", | ||
"default" : 0 | ||
}, { | ||
"name" : "subNodes", | ||
"type" : { | ||
"type" : "array", | ||
"items" : { | ||
"type" : "record", | ||
"name" : "SamplePair", | ||
"fields" : [ { | ||
"name" : "method", | ||
"type" : { | ||
"type" : "record", | ||
"name" : "Method", | ||
"fields" : [ { | ||
"name" : "name", | ||
"type" : "string" | ||
}, { | ||
"name" : "purpose", | ||
"type" : "string" | ||
} ] | ||
} | ||
}, { | ||
"name" : "node", | ||
"type" : "SampleNode" | ||
} ] | ||
} | ||
} | ||
} ] | ||
} |
Oops, something went wrong.