2019-04-09 20:43:42 -05:00
|
|
|
let t = import "std/testing.ucg";
|
2019-01-20 08:32:05 -06:00
|
|
|
let schema = import "std/schema.ucg";
|
|
|
|
|
|
|
|
assert t.ok{
|
2019-01-20 16:22:32 -06:00
|
|
|
test = schema.must(schema.any{val=1, types=[1.0, 1]}, "Must be a float or an int"),
|
2019-01-20 08:32:05 -06:00
|
|
|
desc = "1 is an int",
|
|
|
|
};
|
|
|
|
|
2019-04-17 19:34:53 -05:00
|
|
|
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",
|
|
|
|
};
|
|
|
|
|
2019-01-20 08:32:05 -06:00
|
|
|
assert t.not_ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.any{val=1, types=[1.0, ""]},
|
2019-01-20 08:32:05 -06:00
|
|
|
desc = "1 is not a float or string",
|
2019-01-20 16:22:32 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
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{
|
2019-01-24 20:04:40 -06:00
|
|
|
left = schema.base_type_of(func(arg) => arg),
|
|
|
|
right = "func",
|
2019-01-20 16:22:32 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
assert t.equal{
|
|
|
|
left = schema.base_type_of(schema.any),
|
|
|
|
right = "module",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.equal{
|
2019-02-24 08:16:07 -06:00
|
|
|
left = schema.shaped{val="foo", shape=""},
|
2019-01-20 16:22:32 -06:00
|
|
|
right = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val="foo", shape=""},
|
2019-01-20 16:22:32 -06:00
|
|
|
desc = "\"foo\" is same shape as \"\"",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.not_ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val="foo", shape=0},
|
2019-01-20 16:22:32 -06:00
|
|
|
desc = "\"foo\" is not the same shape as 0",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val={foo="bar"}, shape={foo=""}},
|
2019-01-20 16:22:32 -06:00
|
|
|
desc = "shaped for simple tuples works",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val={foo="bar", count=1}, shape={foo=""}},
|
2019-01-20 16:22:32 -06:00
|
|
|
desc = "shaped for partial tuples works",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.not_ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{partial=false, val={foo="bar", count=1}, shape={foo=""}},
|
2019-01-20 16:22:32 -06:00
|
|
|
desc = "shaped for non partial tuples also works",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val={foo="bar", inner={one=1}}, shape={foo="", inner={one=0}}},
|
2019-01-20 16:22:32 -06:00
|
|
|
desc = "shaped for nested tuples works",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val={foo="bar", inner=[1, 2]}, shape={foo="", inner=[0]}},
|
2019-01-20 16:22:32 -06:00
|
|
|
desc = "shaped for nested list in tuple works",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.not_ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val={inner={foo="bar"}}, shape={inner={foo=1}}},
|
2019-01-20 16:22:32 -06:00
|
|
|
desc = "shaped fails when the shape doesn't match",
|
2019-02-18 18:47:52 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
assert t.ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val={list=[1, "foo"]}, shape={list=[0, ""]}},
|
2019-02-18 18:47:52 -06:00
|
|
|
desc="inner list with valid types matches shape",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.not_ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val={list=[1, "foo", true]}, shape={list=[0, ""]}},
|
2019-02-18 18:47:52 -06:00
|
|
|
desc="inner list with invalid types does not match shape",
|
|
|
|
};
|
|
|
|
|
|
|
|
assert t.ok{
|
2019-02-24 08:16:07 -06:00
|
|
|
test = schema.shaped{val={list=[1, "foo"]}, shape={list=[]}},
|
2019-02-18 18:47:52 -06:00
|
|
|
desc="inner list with valid types matches empty list shape",
|
|
|
|
};
|
2019-04-17 19:34:53 -05:00
|
|
|
|
2019-04-17 22:17:06 -05:00
|
|
|
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",
|
|
|
|
};
|
|
|
|
|
2019-04-17 19:34:53 -05:00
|
|
|
assert t.not_ok{
|
|
|
|
test = schema.shaped{val={foo="bar"}, shaped=1.0},
|
|
|
|
desc = "a tuple is not a float",
|
|
|
|
};
|
2019-04-17 21:02:09 -05:00
|
|
|
|
|
|
|
assert t.not_ok{
|
2019-04-17 22:17:06 -05:00
|
|
|
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)",
|
2019-04-17 21:02:09 -05:00
|
|
|
};
|