ucg/std/tests/schema_test.ucg

179 lines
4.5 KiB
Plaintext

let t = import "std/testing.ucg";
let schema = import "std/schema.ucg";
assert t.ok{
test = schema.must(schema.any{val=1, types=[1.0, 1]}, "Must be a float or an int"),
desc = "1 is an int",
};
assert t.not_ok{
test = schema.any{val=1, types=[1.0, {foo="bar"}]},
desc = "1 is not a float or a tuple with a foo string field",
};
assert t.ok{
test = schema.any{val={foo="bar"}, types=[1.0, {foo=""}]},
desc = "1 is not a float or a tuple with a foo string field",
};
assert t.not_ok{
test = schema.any{val=1, types=[1.0, ""]},
desc = "1 is not a float or string",
};
assert t.equal{
left = schema.base_type_of("foo"),
right = "str",
};
assert t.equal{
left = schema.base_type_of(1),
right = "int",
};
assert t.equal{
left = schema.base_type_of(1.0),
right = "float",
};
assert t.equal{
left = schema.base_type_of(1.0),
right = "float",
};
assert t.equal{
left = schema.base_type_of(true),
right = "bool",
};
assert t.equal{
left = schema.base_type_of(NULL),
right = "null",
};
assert t.equal{
left = schema.base_type_of({}),
right = "tuple",
};
assert t.equal{
left = schema.base_type_of([]),
right = "list",
};
assert t.equal{
left = schema.base_type_of(func(arg) => arg),
right = "func",
};
assert t.equal{
left = schema.base_type_of(schema.any),
right = "module",
};
assert t.equal{
left = schema.shaped{val="foo", shape=""},
right = true,
};
assert t.ok{
test = schema.shaped{val="foo", shape=""},
desc = "\"foo\" is same shape as \"\"",
};
assert t.not_ok{
test = schema.shaped{val="foo", shape=0},
desc = "\"foo\" is not the same shape as 0",
};
assert t.ok{
test = schema.shaped{val={foo="bar"}, shape={foo=""}},
desc = "shaped for simple tuples works",
};
assert t.ok{
test = schema.shaped{val={foo="bar", count=1, fuzzy={bear="claws"}}, shape={foo=""}},
desc = "shaped for partial tuples works",
};
assert t.not_ok{
test = schema.shaped{partial=false, val={foo="bar", count=1}, shape={foo=""}},
desc = "shaped for non partial tuples also works",
};
assert t.ok{
test = schema.shaped{val={foo="bar", inner={one=1}}, shape={foo="", inner={one=0}}},
desc = "shaped for nested tuples works",
};
assert t.ok{
test = schema.shaped{val={foo="bar", inner=[1, 2]}, shape={foo="", inner=[0]}},
desc = "shaped for nested list in tuple works",
};
assert t.not_ok{
test = schema.shaped{val={inner={foo="bar"}}, shape={inner={foo=1}}},
desc = "shaped fails when the shape doesn't match",
};
assert t.ok{
test = schema.shaped{val={list=[1, "foo"]}, shape={list=[0, ""]}},
desc="inner list with valid types matches shape",
};
assert t.not_ok{
test = schema.shaped{val={list=[1, "foo", true]}, shape={list=[0, ""]}},
desc="inner list with invalid types does not match shape",
};
assert t.ok{
test = schema.shaped{val={list=[1, "foo"]}, shape={list=[]}},
desc="inner list with valid types matches empty list shape",
};
assert t.not_ok{
test = schema.shaped{val={list=[1, "foo"]}, shape={list="foo"}},
desc="inner list with with non list shape does not match",
};
assert t.not_ok{
test = schema.shaped{val={foo="bar"}, shaped=1.0},
desc = "a tuple is not a float",
};
assert t.not_ok{
test = schema.any{val={foo="bar", quux="baz"}, types=[1.0, {foo="bar"}]},
desc = "any doesn't match against missing fields for tuples for value tuple.",
};
assert t.ok{
test = schema.any{val={foo="bar", quux="baz"}, types=[1.0, {foo="bar"}], partial=true},
desc = "any can do partial matching against missing fields for tuples shapes.",
};
assert t.not_ok{
test = schema.any{val={foo="bar"}, types=[1.0, {foo="", quux=""}]},
desc = "any does match against missing fields for tuples shape tuple.",
};
assert t.not_ok{
test = schema.shaped{val={foo="bar", baz="quux"}, shape={bear=""}, partial=true},
desc = "even with partial all of the shape fields must be present.",
};
assert t.ok{
test = schema.all{val={foo="bar", baz="quux"}, types=[{foo=""}, {baz=""}]},
desc = "all enforces that all of the valid types are partial matches (success)",
};
assert t.not_ok{
test = schema.all{val={foo="bar", baz="quux"}, types=[{foo=""}, {baz=""}, {quux=""}]},
desc = "all enforces that all of the valid shapes must be partial matches (fail)",
};
assert t.ok{
test = schema.shaped{val={
name = "simple",
}, shape={name="",}, partial=true},
desc = "partial shapes match okay",
};