diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt index b1dc5eac5..b2d83ce91 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestTermDeserializer.kt @@ -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") } @@ -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) } } @@ -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)) @@ -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 @@ -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)) ) } @@ -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)) ) } @@ -170,9 +166,8 @@ class TestTermDeserializer { assertEquals(MimeType.Yaml, deserializer.mimeType) - val actual = """--- + val actual = """ |var: "X" - | """.trimMargin() deserializer.assertTermDeserializationWorks(actual) { varOf("X") @@ -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) } } diff --git a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt index fa2703b11..ca7aee4be 100644 --- a/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt +++ b/serialize-core/src/commonTest/kotlin/it/unibo/tuprolog/serialize/TestUtils.kt @@ -39,7 +39,14 @@ fun Serializer.assertTermSerializationWorks(expected: String, actualGenera * Utility assertion method aimed at checking if a deserializer correctly works */ fun Deserializer.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) } } /** diff --git a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt index 59b48587d..3c1ffde7b 100644 --- a/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt +++ b/serialize-core/src/jsMain/kotlin/it/unibo/tuprolog/serialize/JsTermDeobjectifier.kt @@ -36,14 +36,16 @@ class JsTermDeobjectifier : TermDeobjectifier { 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) } } @@ -114,15 +116,7 @@ class JsTermDeobjectifier : TermDeobjectifier { } 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()) } } \ No newline at end of file