Skip to content

Commit

Permalink
test(dsl-core): generating JSON for links
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jul 13, 2019
1 parent c4d3660 commit 1d69d73
Show file tree
Hide file tree
Showing 3 changed files with 333 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import org.apache.commons.lang3.NotImplementedException
import org.eclipse.emf.ecore.EObject
import app.hypermedia.testing.dsl.Modifier
import app.hypermedia.testing.dsl.core.StatusStatement
import app.hypermedia.testing.dsl.core.RelaxedLinkBlock
import app.hypermedia.testing.dsl.core.StrictLinkBlock
import app.hypermedia.testing.dsl.core.LinkStatement

/**
* Generates code from your model files on save.
Expand Down Expand Up @@ -93,6 +96,40 @@ class CoreGenerator extends AbstractGenerator {
"code": «status»
}
'''
def dispatch step(RelaxedLinkBlock it)'''
{
"type": "Link",
"strict": false,
"rel": "«relation»",
"children": [
«FOR step:children SEPARATOR ","»
«step.step»
«ENDFOR»
]
}
'''
def dispatch step(StrictLinkBlock it)'''
{
"type": "Link",
"strict": true,
"rel": "«relation»",
"children": [
«FOR step:children SEPARATOR ","»
«step.step»
«ENDFOR»
]
}
'''
def dispatch step(LinkStatement it)'''
{
"type": "Link",
"strict": true,
"rel": "«relation»"
}
'''
def dispatch step(EObject step) {
throw new NotImplementedException(String.format("Unrecognized step %s", step.class))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
app.hypermedia.testing.dsl.tests.generator.core.LinkTest.canGenerateForClassLink=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"propertyId": "name",
"strict": true,
"type": "Property"
},
"empty": false
}
],
"empty": false
},
"rel": "author",
"strict": false,
"type": "Link"
},
"empty": false
}
],
"empty": false
},
"classId": "Article",
"type": "Class"
},
"empty": false
},
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"propertyId": "name",
"strict": true,
"type": "Property"
},
"empty": false
}
],
"empty": false
},
"rel": "author",
"strict": false,
"type": "Link"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]


app.hypermedia.testing.dsl.tests.generator.core.LinkTest.canGenerateForLinkStatement=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"rel": "author",
"strict": true,
"type": "Link"
},
"empty": false
}
],
"empty": false
},
"classId": "Article",
"type": "Class"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]


app.hypermedia.testing.dsl.tests.generator.core.LinkTest.canGenerateForStrictClassLink=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"propertyId": "name",
"strict": true,
"type": "Property"
},
"empty": false
}
],
"empty": false
},
"rel": "author",
"strict": true,
"type": "Link"
},
"empty": false
}
],
"empty": false
},
"classId": "Article",
"type": "Class"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]


app.hypermedia.testing.dsl.tests.generator.core.LinkTest.canGenerateForTopLevelLink=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"propertyId": "name",
"strict": true,
"type": "Property"
},
"empty": false
}
],
"empty": false
},
"rel": "author",
"strict": false,
"type": "Link"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package app.hypermedia.testing.dsl.tests.generator.core

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.core.Model
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.CoreInjectorProvider
import org.eclipse.xtext.generator.GeneratorContext

@ExtendWith(InjectionExtension)
@InjectWith(CoreInjectorProvider)
class LinkTest {
@Inject IGenerator2 generator
@Inject extension ParseHelper<Model>

@BeforeAll
static def beforeAll() {
start()
}

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

@Test
def canGenerateForTopLevelLink() {
// given
val model = '''
With Link "author" {
Expect Property "name"
}
'''.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 canGenerateForClassLink() {
// given
val model = '''
With Class "Article" {
With Link "author" {
Expect Property "name"
}
}
'''.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 canGenerateForStrictClassLink() {
// given
val model = '''
With Class "Article" {
Expect Link "author" {
Expect Property "name"
}
}
'''.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 canGenerateForLinkStatement() {
// given
val model = '''
With Class "Article" {
Expect Link "author"
}
'''.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()
}
}

0 comments on commit 1d69d73

Please sign in to comment.