Skip to content

Commit

Permalink
feat: added expect id step
Browse files Browse the repository at this point in the history
fixes #59
  • Loading branch information
tpluscode committed Oct 10, 2019
1 parent f375ea6 commit 1618fad
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ TopLevelStep:
RepresentationStep:
super |
OperationBlock |
RdfTypeStatement
RdfTypeStatement |
IdentifierStatement
;

@Override
Expand All @@ -29,6 +30,10 @@ RdfTypeStatement:
ExpectModifier 'Type' id=Identifier
;

IdentifierStatement:
ExpectModifier 'Id' id=Identifier
;

NamespaceDeclaration:
'PREFIX' prefix=PrefixDeclaration namespace=URI
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,6 @@ class CoreGenerator extends AbstractGenerator {
}

protected def getScenarioSteps(EList<EObject> s) {
return s.filter(CoreScenario).flatMap[cs | cs.steps]
return s.filter(CoreScenario).flatMap[cs |cs.steps]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -90,6 +84,13 @@ final Map<String, String> _namespaces
return buildStatement('Property', map)
}

def dispatch step(IdentifierStatement it) {
val map = new HashMap<String, Object>
map.put('value', id.identifier)

return buildStatement('Identifier', map)
}

def dispatch identifier(PrefixedName it) {
val pair = value.split(':')
val prefix = pair.get(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
]
Original file line number Diff line number Diff line change
@@ -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<HydraScenario>
@BeforeAll
static def beforeAll() {
start()
}

@AfterAll
static def afterAll() {
validateSnapshots();
}

@Test
def expectType_generatesStep() {
// given
val model = '''
With Class <http://example.com/CreateUser> {
Expect Id <http://example.com/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()
}

@Test
def expectTypeWithPrefix_generatesStep() {
// given
val model = '''
PREFIX ex: <http://example.com/>
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()
}
}
Original file line number Diff line number Diff line change
@@ -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<HydraScenario>

@Test
def void expectId_canBeUsedWithUri() {
// when
val result = '''
With Class <http://example.com/User> {
Expect Id <http://example.com/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('http://example.com/Tomasz')
}

@Test
def void expectId_canBeUsedWithPrefixedName() {
// when
val result = '''
PREFIX ex: <http://example.com/>
With Class <http://example.com/User> {
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')
}
}

0 comments on commit 1618fad

Please sign in to comment.