From 1618fadc50c5cb671fc618b1adaa60890fbe7efe Mon Sep 17 00:00:00 2001 From: tpluscode Date: Thu, 10 Oct 2019 16:35:19 +0200 Subject: [PATCH] feat: added expect id step fixes #59 --- .../app/hypermedia/testing/dsl/Hydra.xtext | 7 +- .../testing/dsl/generator/CoreGenerator.xtend | 2 +- .../dsl/generator/HydraGenerator.xtend | 15 ++-- .../tests/generator/hydra/IdentifierTest.snap | 64 ++++++++++++++++ .../generator/hydra/IdentifierTest.xtend | 73 +++++++++++++++++++ .../dsl/tests/hydra/ExpectIdParsingTest.xtend | 59 +++++++++++++++ 6 files changed, 211 insertions(+), 9 deletions(-) create mode 100644 app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/IdentifierTest.snap create mode 100644 app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/IdentifierTest.xtend create mode 100644 app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/hydra/ExpectIdParsingTest.xtend diff --git a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/Hydra.xtext b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/Hydra.xtext index 6d891ba..fe95885 100644 --- a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/Hydra.xtext +++ b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/Hydra.xtext @@ -16,7 +16,8 @@ TopLevelStep: RepresentationStep: super | OperationBlock | - RdfTypeStatement + RdfTypeStatement | + IdentifierStatement ; @Override @@ -29,6 +30,10 @@ RdfTypeStatement: ExpectModifier 'Type' id=Identifier ; +IdentifierStatement: + ExpectModifier 'Id' id=Identifier +; + NamespaceDeclaration: 'PREFIX' prefix=PrefixDeclaration namespace=URI ; diff --git a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/CoreGenerator.xtend b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/CoreGenerator.xtend index 48cee36..1b32677 100644 --- a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/CoreGenerator.xtend +++ b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/CoreGenerator.xtend @@ -291,6 +291,6 @@ class CoreGenerator extends AbstractGenerator { } protected def getScenarioSteps(EList s) { - return s.filter(CoreScenario).flatMap[cs | cs.steps] + return s.filter(CoreScenario).flatMap[cs |cs.steps] } } diff --git a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/HydraGenerator.xtend b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/HydraGenerator.xtend index 8109c1e..416c3e6 100644 --- a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/HydraGenerator.xtend +++ b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/HydraGenerator.xtend @@ -3,22 +3,16 @@ */ package app.hypermedia.testing.dsl.generator -import app.hypermedia.testing.dsl.hydra.OperationBlock -import app.hypermedia.testing.dsl.hydra.InvocationBlock +import app.hypermedia.testing.dsl.hydra.* import app.hypermedia.testing.dsl.Modifier import java.util.HashMap import org.json.JSONObject -import app.hypermedia.testing.dsl.hydra.UriName -import app.hypermedia.testing.dsl.hydra.PrefixedName import org.eclipse.xtext.generator.IFileSystemAccess2 import org.eclipse.xtext.generator.IGeneratorContext import org.eclipse.emf.ecore.resource.Resource -import app.hypermedia.testing.dsl.hydra.NamespaceDeclaration import java.util.Map -import app.hypermedia.testing.dsl.hydra.HydraScenario import org.eclipse.emf.common.util.EList import org.eclipse.emf.ecore.EObject -import app.hypermedia.testing.dsl.hydra.RdfTypeStatement /** * Generates code from your model files on save. @@ -90,6 +84,13 @@ final Map _namespaces return buildStatement('Property', map) } + def dispatch step(IdentifierStatement it) { + val map = new HashMap + map.put('value', id.identifier) + + return buildStatement('Identifier', map) + } + def dispatch identifier(PrefixedName it) { val pair = value.split(':') val prefix = pair.get(0) diff --git a/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/IdentifierTest.snap b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/IdentifierTest.snap new file mode 100644 index 0000000..02ac72d --- /dev/null +++ b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/IdentifierTest.snap @@ -0,0 +1,64 @@ +app.hypermedia.testing.dsl.tests.generator.hydra.IdentifierTest.expectTypeWithPrefix_generatesStep=[ + { + "map": { + "steps": { + "myArrayList": [ + { + "map": { + "children": { + "myArrayList": [ + { + "map": { + "type": "Identifier", + "value": "http://example.com/NewUser" + }, + "empty": false + } + ], + "empty": false + }, + "classId": "http://example.com/CreateUser", + "type": "Class" + }, + "empty": false + } + ], + "empty": false + } + }, + "empty": false + } +] + + +app.hypermedia.testing.dsl.tests.generator.hydra.IdentifierTest.expectType_generatesStep=[ + { + "map": { + "steps": { + "myArrayList": [ + { + "map": { + "children": { + "myArrayList": [ + { + "map": { + "type": "Identifier", + "value": "http://example.com/NewUser" + }, + "empty": false + } + ], + "empty": false + }, + "classId": "http://example.com/CreateUser", + "type": "Class" + }, + "empty": false + } + ], + "empty": false + } + }, + "empty": false + } +] \ No newline at end of file diff --git a/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/IdentifierTest.xtend b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/IdentifierTest.xtend new file mode 100644 index 0000000..db97ddd --- /dev/null +++ b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/IdentifierTest.xtend @@ -0,0 +1,73 @@ +package app.hypermedia.testing.dsl.tests.generator.hydra + +import org.eclipse.xtext.generator.InMemoryFileSystemAccess +import org.junit.jupiter.api.^extension.ExtendWith +import org.eclipse.xtext.testing.extensions.InjectionExtension +import org.eclipse.xtext.testing.InjectWith +import com.google.inject.Inject +import org.eclipse.xtext.testing.util.ParseHelper +import app.hypermedia.testing.dsl.hydra.HydraScenario +import org.junit.jupiter.api.Test +import static io.github.jsonSnapshot.SnapshotMatcher.* +import org.eclipse.xtext.generator.IGenerator2 +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.AfterAll +import org.json.JSONObject +import app.hypermedia.testing.dsl.tests.HydraInjectorProvider +import org.eclipse.xtext.generator.GeneratorContext + +@ExtendWith(InjectionExtension) +@InjectWith(HydraInjectorProvider) +class IdentifierTest { + @Inject IGenerator2 generator + @Inject extension ParseHelper + @BeforeAll + static def beforeAll() { + start() + } + + @AfterAll + static def afterAll() { + validateSnapshots(); + } + + @Test + def expectType_generatesStep() { + // given + val model = ''' + With Class { + Expect Id + } + '''.parse + + // when + val fsa = new InMemoryFileSystemAccess() + generator.doGenerate(model.eResource, fsa, new GeneratorContext()) + println(fsa.textFiles) + + // then + val file = new JSONObject(fsa.textFiles.values.get(0).toString) + expect(file).toMatchSnapshot() + } + + @Test + def expectTypeWithPrefix_generatesStep() { + // given + val model = ''' + PREFIX ex: + + With Class ex:CreateUser { + Expect Id ex:NewUser + } + '''.parse + + // when + val fsa = new InMemoryFileSystemAccess() + generator.doGenerate(model.eResource, fsa, new GeneratorContext()) + println(fsa.textFiles) + + // then + val file = new JSONObject(fsa.textFiles.values.get(0).toString) + expect(file).toMatchSnapshot() + } +} diff --git a/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/hydra/ExpectIdParsingTest.xtend b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/hydra/ExpectIdParsingTest.xtend new file mode 100644 index 0000000..645ead1 --- /dev/null +++ b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/hydra/ExpectIdParsingTest.xtend @@ -0,0 +1,59 @@ +/* + * generated by Xtext 2.18.0 + */ +package app.hypermedia.testing.dsl.tests.hydra + +import app.hypermedia.testing.dsl.hydra.HydraScenario +import app.hypermedia.testing.dsl.tests.HydraInjectorProvider +import com.google.inject.Inject +import org.eclipse.xtext.testing.InjectWith +import org.eclipse.xtext.testing.extensions.InjectionExtension +import org.eclipse.xtext.testing.util.ParseHelper +import org.junit.jupiter.api.^extension.ExtendWith +import org.junit.jupiter.api.Test +import static org.assertj.core.api.Assertions.* +import app.hypermedia.testing.dsl.tests.TestHelpers +import app.hypermedia.testing.dsl.hydra.IdentifierStatement +import app.hypermedia.testing.dsl.core.ClassBlock + +@ExtendWith(InjectionExtension) +@InjectWith(HydraInjectorProvider) +class ExpectIdParsingTest { + @Inject extension ParseHelper + + @Test + def void expectId_canBeUsedWithUri() { + // when + val result = ''' + With Class { + Expect Id + } + '''.parse + + // then + TestHelpers.assertModelParsedSuccessfully(result) + + val clas = result.steps.get(0) as ClassBlock + val expectation = clas.children.get(0) as IdentifierStatement + assertThat(expectation.id.value).isEqualTo('http://example.com/Tomasz') + } + + @Test + def void expectId_canBeUsedWithPrefixedName() { + // when + val result = ''' + PREFIX ex: + + With Class { + Expect Id ex:Tomasz + } + '''.parse + + // then + TestHelpers.assertModelParsedSuccessfully(result) + + val clas = result.steps.get(0) as ClassBlock + val expectation = clas.children.get(0) as IdentifierStatement + assertThat(expectation.id.value).isEqualTo('ex:Tomasz') + } +}