Skip to content

Commit

Permalink
feat(core-dsl): extend property statement with other data types
Browse files Browse the repository at this point in the history
re #50
  • Loading branch information
tpluscode committed Jul 28, 2019
1 parent a2369d7 commit 1671363
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,27 @@ PropertyBlock:
;

PropertyStatement:
ExpectModifier 'Property' name=Identifier (value=STRING)?
ExpectModifier 'Property' name=Identifier expectation=(
IntValue |
DecimalValue |
BooleanValue |
StringValue)?
;

DecimalValue:
value=DECIMAL
;

BooleanValue:
value=('true' | 'false')
;

IntValue:
value=INT
;

StringValue:
value=STRING
;

StatusStatement:
Expand Down Expand Up @@ -115,3 +135,5 @@ terminal HYPHENATED_NAME: ('a'..'z' | 'A'..'Z') ('-' | 'a'..'z' | 'A'..'Z')*;
terminal MULTILINE_BLOCK:
'```' -> '```'
;

terminal DECIMAL: INT '.' INT;
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import app.hypermedia.testing.dsl.core.FollowStatement
import app.hypermedia.testing.dsl.core.Identifier
import app.hypermedia.testing.dsl.core.CoreScenario
import org.eclipse.emf.common.util.EList
import app.hypermedia.testing.dsl.core.StringValue
import app.hypermedia.testing.dsl.core.BooleanValue
import app.hypermedia.testing.dsl.core.IntValue
import app.hypermedia.testing.dsl.core.DecimalValue
import java.math.BigDecimal

/**
* Generates code from your model files on save.
Expand Down Expand Up @@ -93,8 +98,8 @@ class CoreGenerator extends AbstractGenerator {
map.put('propertyId', name.identifier)
map.put('strict', true)

if(value !== null) {
map.put('value', value)
if(expectation !== null) {
map.put('value', expectation.propertyValue)
}

return buildStatement('Property', map)
Expand Down Expand Up @@ -164,6 +169,22 @@ class CoreGenerator extends AbstractGenerator {
def dispatch identifier(Identifier it) {
return value
}

def dispatch Object propertyValue(StringValue it) {
return value
}

def dispatch Object propertyValue(BooleanValue it) {
return value == 'true' ? true : false
}

def dispatch Object propertyValue(IntValue it) {
return value
}

def dispatch Object propertyValue(DecimalValue it) {
return new BigDecimal(value)
}

protected def getSteps(EList<EObject> s) {
return s.filter(CoreScenario).flatMap[cs | cs.steps]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import org.junit.jupiter.api.^extension.ExtendWith
import app.hypermedia.testing.dsl.core.PropertyBlock
import app.hypermedia.testing.dsl.core.PropertyStatement
import static org.assertj.core.api.Assertions.*;
import app.hypermedia.testing.dsl.core.StringValue
import app.hypermedia.testing.dsl.core.IntValue
import app.hypermedia.testing.dsl.core.BooleanValue
import app.hypermedia.testing.dsl.core.DecimalValue

@ExtendWith(InjectionExtension)
@InjectWith(CoreInjectorProvider)
Expand Down Expand Up @@ -70,7 +74,7 @@ class PropertyParsingTest {

val classBlock = result.steps.get(0) as ClassBlock
val propertyStmt = classBlock.classChildren.get(0) as PropertyStatement
assertThat(propertyStmt.value).isEqualTo("TOMASZ")
assertThat((propertyStmt.expectation as StringValue).value).isEqualTo("TOMASZ")
}

@Test
Expand Down Expand Up @@ -105,4 +109,55 @@ class PropertyParsingTest {
// then
TestHelpers.assertModelParsingFailed(result)
}

@Test
def void expectProperty_succesfullyParsesInt() {
// when
val result = parseHelper.parse('''
With Class "Book" {
Expect Property "pageCount" 1098
}
''')

// then
TestHelpers.assertModelParsedSuccessfully(result)

val classBlock = result.steps.get(0) as ClassBlock
val propertyStatement = classBlock.classChildren.get(0) as PropertyStatement
assertThat(propertyStatement.expectation).isInstanceOf(IntValue)
}

@Test
def void expectProperty_succesfullyParsesBoolean() {
// when
val result = parseHelper.parse('''
With Class "Book" {
Expect Property "read" false
}
''')

// then
TestHelpers.assertModelParsedSuccessfully(result)

val classBlock = result.steps.get(0) as ClassBlock
val propertyStatement = classBlock.classChildren.get(0) as PropertyStatement
assertThat(propertyStatement.expectation).isInstanceOf(BooleanValue)
}

@Test
def void expectProperty_succesfullyParsesDecimal() {
// when
val result = parseHelper.parse('''
With Class "Book" {
Expect Property "price" 10.99
}
''')

// then
TestHelpers.assertModelParsedSuccessfully(result)

val classBlock = result.steps.get(0) as ClassBlock
val propertyStatement = classBlock.classChildren.get(0) as PropertyStatement
assertThat(propertyStatement.expectation).isInstanceOf(DecimalValue)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
app.hypermedia.testing.dsl.tests.generator.core.PropertyTest.expectNonStringValues_generatesValue=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"propertyId": "age",
"strict": true,
"type": "Property",
"value": 21
},
"empty": false
},
{
"map": {
"propertyId": "salary",
"strict": true,
"type": "Property",
"value": 12.5
},
"empty": false
},
{
"map": {
"propertyId": "employed",
"strict": true,
"type": "Property",
"value": true
},
"empty": false
}
],
"empty": false
},
"classId": "Person",
"type": "Class"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]


app.hypermedia.testing.dsl.tests.generator.core.PropertyTest.expectPropertyValue_generatesValue=[
{
"map": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,25 @@ class PropertyTest {
val file = new JSONObject(fsa.textFiles.values.get(0).toString)
expect(file).toMatchSnapshot()
}

@Test
def expectNonStringValues_generatesValue() {
// given
val model = parseHelper.parse('''
With Class "Person" {
Expect Property "age" 21
Expect Property "salary" 12.5
Expect Property "employed" true
}
''')

// 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 1671363

Please sign in to comment.