Skip to content

Commit

Permalink
tests in TestTermDeserializer pass
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Jun 26, 2020
1 parent 8dcbae4 commit 5c710a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ class TestTermDeserializer {
deserializer.assertTermDeserializationWorks("\"other atom\"") {
atomOf("other atom")
}

}

@Test
fun testAtomDeserializationInYaml() {
val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, deserializer.mimeType)
deserializer.assertTermDeserializationWorks("--- \"hello\"") {
deserializer.assertTermDeserializationWorks("\"hello\"") {
atomOf("hello")
}
deserializer.assertTermDeserializationWorks("--- \"other atom\"") {
deserializer.assertTermDeserializationWorks("\"other atom\"") {
atomOf("other atom")
}

Expand All @@ -46,10 +45,10 @@ class TestTermDeserializer {
fun testNumericDeserializationInYAML() {
val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, deserializer.mimeType)
deserializer.assertTermDeserializationWorks("--- 2") {
deserializer.assertTermDeserializationWorks("2") {
numOf(2)
}
deserializer.assertTermDeserializationWorks("--- 3.1") {
deserializer.assertTermDeserializationWorks("3.1") {
numOf(3.1)
}
}
Expand All @@ -67,12 +66,11 @@ class TestTermDeserializer {
fun testListDeserializationInYAML() {
val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, deserializer.mimeType)
var actual = """---
val actual = """
|list:
|- "hello"
|- hello
|- 1
|- true
|
""".trimMargin()
deserializer.assertTermDeserializationWorks(actual) {
listOf(atomOf("hello"), numOf(1), truthOf(true))
Expand All @@ -92,7 +90,7 @@ class TestTermDeserializer {
fun testSetDeserializationInYAML() {
val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, deserializer.mimeType)
var actual = """---
val actual = """
|set:
|- "hello"
|- 1
Expand All @@ -112,9 +110,9 @@ class TestTermDeserializer {
structOf("f", atomOf("hello"), numOf(2))
}

deserializer.assertTermDeserializationWorks("{\"fun\":\"f\",\"args\":[\"prova 2\",3.0,{\"list\":[\"qua ci va una lista\",true]}]}") {
deserializer.assertTermDeserializationWorks("{\"fun\":\"f\",\"args\":[\"prova 2\",{\"real\":3.0},{\"list\":[\"qua ci va una lista\",true]}]}") {
structOf(
"f", atomOf("prova 2"), numOf(3.0),
"f", atomOf("prova 2"), realOf(3.0),
listOf(atomOf("qua ci va una lista"), truthOf(true))
)
}
Expand All @@ -124,30 +122,28 @@ class TestTermDeserializer {
fun testStructDeserializationInYAML() {
val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, deserializer.mimeType)
var actual = """---
|fun: "f"
var actual = """
|fun: f
|args:
|- "hello"
|- hello
|- 2
|
""".trimMargin()
deserializer.assertTermDeserializationWorks(actual) {
structOf("f", atomOf("hello"), numOf(2))
}

actual = """---
actual = """
|fun: "f"
|args:
|- "prova 2"
|- 3.0
|- real: 3.0
|- list:
| - "qua ci va una lista"
| - true
|
""".trimMargin()
deserializer.assertTermDeserializationWorks(actual) {
structOf(
"f", atomOf("prova 2"), numOf(3.0),
"f", atomOf("prova 2"), realOf(3.0),
listOf(atomOf("qua ci va una lista"), truthOf(true))
)
}
Expand All @@ -170,9 +166,8 @@ class TestTermDeserializer {

assertEquals(MimeType.Yaml, deserializer.mimeType)

val actual = """---
val actual = """
|var: "X"
|
""".trimMargin()
deserializer.assertTermDeserializationWorks(actual) {
varOf("X")
Expand All @@ -195,10 +190,10 @@ class TestTermDeserializer {
fun testConstantDeserializationInYAML() {
val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, deserializer.mimeType)
deserializer.assertTermDeserializationWorks("--- true\n") {
deserializer.assertTermDeserializationWorks("true") {
truthOf(true)
}
deserializer.assertTermDeserializationWorks("--- false\n") {
deserializer.assertTermDeserializationWorks("false") {
truthOf(false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ fun Serializer<Term>.assertTermSerializationWorks(expected: String, actualGenera
* Utility assertion method aimed at checking if a deserializer correctly works
*/
fun <T : Term> Deserializer<T>.assertDeserializationWorks(expected: T, actual: String) {
kotlin.test.assertTrue { termsRepresentationsAreEqual(expected, deserialize(actual)) }
val deserialized = deserialize(actual)
kotlin.test.assertTrue("""
|Expected:
| $expected
|got:
| $deserialized
|
""".trimMargin()) { termsRepresentationsAreEqual(expected, deserialized) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ class JsTermDeobjectifier : TermDeobjectifier<dynamic> {
private fun deobjectifyReal(value: dynamic): Term {
return when (val actualValue = value["real"]) {
is String -> scope.realOf(actualValue as String)
else -> deobjectifyNumber(actualValue)
is Double -> scope.realOf(actualValue as Double)
else -> throw DeobjectificationException(value)
}
}

private fun deobjectifyInteger(value: dynamic): Term {
return when (val actualValue = value["integer"]) {
is String -> scope.intOf(actualValue as String)
else -> deobjectifyNumber(actualValue)
is Int -> scope.intOf(actualValue as Int)
else -> throw DeobjectificationException(value)
}
}

Expand Down Expand Up @@ -114,15 +116,7 @@ class JsTermDeobjectifier : TermDeobjectifier<dynamic> {
}

private fun deobjectifyNumber(value: dynamic): Term {
return when (value) {
is Int -> scope.numOf(value as Int)
is Long -> scope.numOf(value as Long)
is Double -> scope.numOf(value as Double)
is Byte -> scope.numOf(value as Byte)
is Short -> scope.numOf(value as Short)
is Float -> scope.numOf(value as Float)
else -> throw DeobjectificationException(value)
}
return scope.numOf(value.toString())
}

}

0 comments on commit 5c710a8

Please sign in to comment.