Пример #1
0
def test_scalars():
    s = Class("scalars", Klass, Field("string", String()),
              Field("integer", Integer()), Field("float", Float()))

    obj = {"string": "asdf", "integer": 3, "float": 3.14159}
    k1 = s.load("test", yaml.dump(obj))
    k2 = Klass(**obj)
    assert k1 == k2
Пример #2
0
def test_null():
    for s in (String(), Integer(), Float(), Constant("asdf")):
        assert s.load("test", "null") == None
Пример #3
0
    except ValueError, e:
        assert error in str(e), e


ABC = Union(
    String(),
    Class("a", "a docs", Field("type", Constant("a")), Field("a", String())),
    Class("b", "b docs", Field("type", Constant("b")), Field("b", String())),
    Class("c", "c docs", Field("type", Constant("c")), Field("c", String())))

ABC_FIELDS = Union(
    Class("a", "a docs", Field("a", Constant("x")), Field("aa", String())),
    Class("b", "b docs", Field("b", Constant("x")), Field("bb", String())),
    Class("c", "c docs", Field("c", Constant("x")), Field("cc", String())))

ABC_STR_CONSTANTS = Union(Integer(), Float(), Boolean(),
                          Class("a", "a docs", Field("a", Constant("y"))),
                          Constant("b"), Constant("c"))

UNION_ERRORS = (
    (ABC, "[]",
     "expecting one of (string|a:map{type=a}|b:map{type=b}|c:map{type=c}"),
    (ABC, "{type: a, b: blah}",
     "no such field: b"), (ABC, "{type: b, b: []}",
                           "expecting string, got sequence"),
    (ABC, "{}",
     "expecting one of (a:map{type=a}|b:map{type=b}|c:map{type=c})"),
    (ABC_FIELDS, "[]", "expecting one of (a:map{a=x}|b:map{b=x}|c:map{c=x})"),
    (ABC_FIELDS, "{a: x}", "required field 'aa' is missing"),
    (ABC_FIELDS, "{b: x}",
     "required field 'bb' is missing"), (ABC_FIELDS, "{c: x}",