serde.tests.sprak
##################
# equality.sprak #
##################
bool equal_array(array first, array second)
if Count(first) == Count(second)
loop key in GetIndexes(first)
if !HasIndex(second, key)
return false
else if !equal(first[key], second[key])
return false
end
end
return true
end
return false
end
bool equal(var first, var second)
string type_first = Type(first)
if type_first == Type(second)
if type_first != "array"
return first == second
end
return equal_array(first, second)
end
return false
end
##############
# test.sprak #
##############
void run_tests(string name, array tests)
number count = 0
loop test in tests
if !equal(test[0], test[1])
Print(name + " " + count + ": fail")
Print("left: " + test[0])
Print("right: " + test[1])
end
count += 1
end
Print(name + " tests done.")
end
#############
# Functions #
#############
void test_serialise_string()
string name = "serialise_string"
array tests = []
Append(tests, [serialise_string('a'), '"a"'])
Append(tests, [serialise_string(''), '""'])
Append(tests, [serialise_string('\'), '"\\"'])
Append(tests, [serialise_string('"'), '"\""'])
Append(tests, [serialise_string('\\'), '"\\\\"'])
run_tests(name, tests)
end
void test_serialise_array()
string name = "serialise_array"
array tests = []
Append(tests, [serialise_array([false]), '{0:f}'])
Append(tests, [serialise_array([-273.15]), '{0:-273.15}'])
Append(tests, [serialise_array([""]), '{0:""}'])
Append(tests, [serialise_array([[]]), '{0:{}}'])
Append(tests, [serialise_array([]), '{}'])
run_tests(name, tests)
end
void test_serialise()
string name = "serialise"
array tests = []
Append(tests, [serialise(false), 'f'])
Append(tests, [serialise(-273.15), '-273.15'])
Append(tests, [serialise(""), '""'])
Append(tests, [serialise([]), '{}'])
array a = [false]
array keys = GetIndexes(a)
Append(tests, [serialise(keys[0]), '0'])
run_tests(name, tests)
end
void test_deserialise_string()
string name = "deserialise_string"
array tests = []
Append(tests, [deserialise_string('"a"'), 'a'])
Append(tests, [deserialise_string('""'), ''])
Append(tests, [deserialise_string('"\\"'), '\'])
Append(tests, [deserialise_string('"\""'), '"'])
Append(tests, [deserialise_string('"\\\\"'), '\\'])
run_tests(name, tests)
end
void test_is_digit()
string name = "is_digit"
array tests = []
Append(tests, [is_digit("0"), true])
Append(tests, [is_digit("1"), true])
Append(tests, [is_digit("2"), true])
Append(tests, [is_digit("3"), true])
Append(tests, [is_digit("4"), true])
Append(tests, [is_digit("5"), true])
Append(tests, [is_digit("6"), true])
Append(tests, [is_digit("7"), true])
Append(tests, [is_digit("8"), true])
Append(tests, [is_digit("9"), true])
Append(tests, [is_digit("a"), false])
run_tests(name, tests)
end
void test_deserialise_array()
string name = "deserialise_array"
array tests = []
array a
array b
array c
a = deserialise_array('{}')
Append(tests, [a, []])
a = deserialise_array('{0:f}')
Append(tests, [a, [false]])
a = deserialise_array('{0:-273.15}')
Append(tests, [a, [-273.15]])
a = deserialise_array('{0:"abc"}')
Append(tests, [a, ["abc"]])
a = deserialise_array('{0:{}}')
Append(tests, [a, [[]]])
a = deserialise_array('{0:f,1:t}')
Append(tests, [a, [false,true]])
a = deserialise_array('{0:0,1:-273.15}')
Append(tests, [a, [0,-273.15]])
a = deserialise_array('{0:"abc",1:"def"}')
Append(tests, [a, ["abc","def"]])
a = deserialise_array('{0:{},1:{}}')
Append(tests, [a, [[],[]]])
a = deserialise_array('{f:f,t:t}')
b = []
b[false] = false
b[true] = true
Append(tests, [a, b])
a = deserialise_array('{f:0,t:-273.15}')
b = []
b[false] = 0
b[true] = -273.15
Append(tests, [a, b])
a = deserialise_array('{f:"abc",t:"def"}')
b = []
b[false] = "abc"
b[true] = "def"
Append(tests, [a, b])
a = deserialise_array('{f:{},t:{}}')
b = []
b[false] = []
b[true] = []
Append(tests, [a, b])
a = deserialise_array('{0:f,-273.15:t}')
b = []
b[0] = false
b[-273.15] = true
Append(tests, [a, b])
a = deserialise_array('{0:0,-273.15:-273.15}')
b = []
b[0] = 0
b[-273.15] = -273.15
Append(tests, [a, b])
a = deserialise_array('{0:"abc",-273.15:"def"}')
b = []
b[0] = "abc"
b[-273.15] = "def"
Append(tests, [a, b])
a = deserialise_array('{0:{},-273.15:{}}')
b = []
b[0] = []
b[-273.15] = []
Append(tests, [a, b])
a = deserialise_array('{"abc":f,"def":t}')
b = []
b["abc"] = false
b["def"] = true
Append(tests, [a, b])
a = deserialise_array('{"abc":0,"def":-273.15}')
b = []
b["abc"] = 0
b["def"] = -273.15
Append(tests, [a, b])
a = deserialise_array('{"abc":"abc","def":"def"}')
b = []
b["abc"] = "abc"
b["def"] = "def"
Append(tests, [a, b])
a = deserialise_array('{"abc":{},"def":{}}')
b = []
b["abc"] = []
b["def"] = []
Append(tests, [a, b])
a = deserialise_array('{"a":{"b":f}}')
b = []
c = []
c["b"] = false
b["a"] = c
Append(tests, [a, b])
run_tests(name, tests)
end
void test_deserialise()
string name = "deserialise"
array tests = []
Append(tests, [deserialise('f'), false])
Append(tests, [deserialise('-273.15'), -273.15])
Append(tests, [deserialise('""'), ""])
Append(tests, [deserialise('{}'), []])
array a = [false]
array keys = GetIndexes(a)
number b = keys[0]
Append(tests, [deserialise('0'), b])
run_tests(name, tests)
end
DisconnectAll()
ClearText()
test_serialise_string()
test_serialise_array()
test_serialise()
test_deserialise_string()
test_is_digit()
test_deserialise_array()
test_deserialise()