Skip to content

Commit

Permalink
format code in :serialization-core
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Sep 14, 2020
1 parent fdbfeac commit 9009a60
Show file tree
Hide file tree
Showing 34 changed files with 140 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package it.unibo.tuprolog.serialize

import it.unibo.tuprolog.core.exception.TuPrologException

class DeobjectificationException(`object`: Any) : TuPrologException("Error while deobjectifying $`object`")
class DeobjectificationException(`object`: Any) : TuPrologException("Error while deobjectifying $`object`")
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ interface Deobjectifier<T> {

@JsName("deobjectifyMany")
fun deobjectifyMany(`object`: Any): Iterable<T>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ interface Deserializer<T> {

@JsName("deserializeMany")
fun deserializeMany(string: String): Iterable<T>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ sealed class MimeType(
object Yaml : MimeType("application", "yaml")

object Xml : MimeType("application", "xml")

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ interface Objectifier<T> {
fun objectifyMany(values: Sequence<T>): Any =
objectifyMany(values.asIterable())
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ expect object ObjectsUtils {
fun parseAsObject(string: String, mimeType: MimeType): Any

fun deeplyEqual(obj1: Any?, obj2: Any?): Boolean
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package it.unibo.tuprolog.serialize
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.exception.TuPrologException

class SerializationException(term: Term) : TuPrologException("Error while serialising $term")
class SerializationException(term: Term) : TuPrologException("Error while serialising $term")
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ interface Serializer<T> {
@JsName("serializeManySequence")
fun serializeMany(values: Sequence<T>): String =
serializeMany(values.asIterable())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ interface TermDeobjectifier : Deobjectifier<Term> {
val default: TermDeobjectifier
get() = termDeobjectifier()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ interface TermDeserializer : Deserializer<Term> {
return termDeserializer(mimeType)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ interface TermObjectifier : Objectifier<Term>, TermVisitor<Any> {
val default: TermObjectifier
get() = termObjectifier()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ expect fun termDeserializer(mimeType: MimeType): TermDeserializer

expect fun termObjectifier(): TermObjectifier

expect fun termDeobjectifier(): TermDeobjectifier
expect fun termDeobjectifier(): TermDeobjectifier
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ interface TermSerializer : Serializer<Term> {
return termSerializer(mimeType)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class TestTermDeserializer {
deserializer.assertTermDeserializationWorks("\"other atom\"") {
atomOf("other atom")
}

}

@Test
Expand Down Expand Up @@ -96,7 +95,8 @@ class TestTermDeserializer {
fun testListDeserializationInYAML() {
val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, deserializer.mimeType)
val actual = """
val actual =
"""
|list:
|- hello
|- 1
Expand All @@ -120,7 +120,8 @@ class TestTermDeserializer {
fun testSetDeserializationInYAML() {
val deserializer: TermDeserializer = TermDeserializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, deserializer.mimeType)
val actual = """
val actual =
"""
|set:
|- "hello"
|- 1
Expand All @@ -142,7 +143,9 @@ class TestTermDeserializer {

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

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

assertEquals(MimeType.Yaml, deserializer.mimeType)

val actual = """
val actual =
"""
|var: "X"
""".trimMargin()
deserializer.assertTermDeserializationWorks(actual) {
Expand Down Expand Up @@ -227,4 +235,4 @@ class TestTermDeserializer {
truthOf(false)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class TestTermSerializer {

assertEquals(MimeType.Yaml, serializer.mimeType)

var expected = """
var expected =
"""
|list:
|- hello
|- false
Expand All @@ -100,7 +101,8 @@ class TestTermSerializer {
listOf(atomOf("hello"), truthOf(false))
}

expected = """
expected =
"""
|list:
|- hello
|- 1
Expand Down Expand Up @@ -146,7 +148,8 @@ class TestTermSerializer {

assertEquals(MimeType.Yaml, serializer.mimeType)

var expected = """
var expected =
"""
|set:
|- hello
|- false
Expand All @@ -155,7 +158,8 @@ class TestTermSerializer {
setOf(atomOf("hello"), truthOf(false))
}

expected = """
expected =
"""
|set:
|- hello
|- 1
Expand All @@ -181,7 +185,8 @@ class TestTermSerializer {

assertEquals(MimeType.Yaml, serializer.mimeType)

val expected = """
val expected =
"""
|fun: f
|args:
|- hello
Expand Down Expand Up @@ -228,7 +233,6 @@ class TestTermSerializer {
val serializer: TermSerializer = TermSerializer.of(MimeType.Json)
assertEquals(MimeType.Json, serializer.mimeType)


serializer.assertTermSerializationWorks("{\"var\":\"Y\"}") {
varOf("Y")
}
Expand All @@ -243,12 +247,13 @@ class TestTermSerializer {
val serializer: TermSerializer = TermSerializer.of(MimeType.Yaml)
assertEquals(MimeType.Yaml, serializer.mimeType)

val expected = """
val expected =
"""
|var: "Y"
""".trimMargin()

serializer.assertTermSerializationWorks(expected) {
varOf("Y")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.unibo.tuprolog.serialize

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.serialize.ObjectsUtils.deeplyEqual
import it.unibo.tuprolog.serialize.ObjectsUtils.parseAsObject

Expand All @@ -10,13 +11,15 @@ import it.unibo.tuprolog.serialize.ObjectsUtils.parseAsObject
fun <T : Term> Serializer<T>.assertSerializationWorks(expected: String, actual: T) {
val expectedObj = parseAsObject(expected, mimeType)
val actualObj = TermObjectifier.default.objectify(actual)
kotlin.test.assertTrue("""
kotlin.test.assertTrue(
"""
|Expected:
| $expectedObj
|got instead:
| $actualObj
|
""".trimMargin()) { deeplyEqual(expectedObj, actualObj) }
""".trimMargin()
) { deeplyEqual(expectedObj, actualObj) }
}

/**
Expand All @@ -42,13 +45,15 @@ fun Serializer<Term>.assertTermSerializationWorks(expected: String, actualGenera
*/
fun <T : Term> Deserializer<T>.assertDeserializationWorks(expected: T, actual: String) {
val deserialized = deserialize(actual)
kotlin.test.assertTrue("""
kotlin.test.assertTrue(
"""
|Expected:
| $expected
|got:
| $deserialized
|
""".trimMargin()) { expected.equals(deserialized, false) }
""".trimMargin()
) { expected.equals(deserialized, false) }
}

/**
Expand All @@ -67,4 +72,4 @@ fun <T : Term> Deserializer<T>.assertDeserializationWorks(expected: T, actual: S
*/
fun Deserializer<Term>.assertTermDeserializationWorks(actual: String, expectedGenerator: Scope.() -> Term) {
assertDeserializationWorks(Scope.empty().expectedGenerator(), actual)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,31 @@ internal class JsTermDeobjectifier : TermDeobjectifier {

private fun deobjectifyTuple(value: dynamic): Term {
val items = value["tuple"] as? Array<*> ?: throw DeobjectificationException(value)
return scope.tupleOf(items.map {
deobjectify(it ?: throw DeobjectificationException(value))
})
return scope.tupleOf(
items.map {
deobjectify(it ?: throw DeobjectificationException(value))
}
)
}

private fun deobjectifySet(value: dynamic): Term {
val items = value["set"] as? Array<*> ?: throw DeobjectificationException(value)
return scope.setOf(items.map {
deobjectify(it ?: throw DeobjectificationException(value))
})
return scope.setOf(
items.map {
deobjectify(it ?: throw DeobjectificationException(value))
}
)
}

private fun deobjectifyStructure(value: dynamic): Term {
val name = value["fun"] as? String ?: throw DeobjectificationException(value)
val args = value["args"] as? Array<*> ?: throw DeobjectificationException(value)
return scope.structOf(name, args.map {
deobjectify(it ?: throw DeobjectificationException(value))
})
return scope.structOf(
name,
args.map {
deobjectify(it ?: throw DeobjectificationException(value))
}
)
}

private fun deobjectifyVariable(value: dynamic): Term {
Expand All @@ -125,5 +132,4 @@ internal class JsTermDeobjectifier : TermDeobjectifier {
private fun deobjectifyNumber(value: dynamic): Term {
return scope.numOf(value.toString())
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ internal class JsTermDeserializer(override val mimeType: MimeType) : TermDeseria
is MimeType.Json -> JSON.parse(string)
is MimeType.Yaml -> YAML.parse(string)
} as Any
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package it.unibo.tuprolog.serialize

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Cons
import it.unibo.tuprolog.core.Directive
import it.unibo.tuprolog.core.EmptyList
import it.unibo.tuprolog.core.EmptySet
import it.unibo.tuprolog.core.Fact
import it.unibo.tuprolog.core.Indicator
import it.unibo.tuprolog.core.Integer
import it.unibo.tuprolog.core.List
import it.unibo.tuprolog.core.Real
import it.unibo.tuprolog.core.Rule
import it.unibo.tuprolog.core.Set
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Truth
import it.unibo.tuprolog.core.Tuple
import it.unibo.tuprolog.core.Var

internal class JsTermObjectifier : TermObjectifier {
override fun defaultValue(term: Term): Any {
Expand Down Expand Up @@ -107,4 +123,4 @@ internal class JsTermObjectifier : TermObjectifier {
jsObject(
"body" to term.body.accept(this)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ internal class JsTermSerializer(override val mimeType: MimeType) : TermSerialize
is MimeType.Json -> JSON.stringify(objectified)
is MimeType.Yaml -> YAML.stringify(objectified)
}
}
}
Loading

0 comments on commit 9009a60

Please sign in to comment.