def test_map_pickle(): def p(v): assert pickle.loads(pickle.dumps(v)) == v p(Map()) p(Map(a=1)) p(Map(a=1, b=2)) p(Map(d=Map(a=1, b=2)))
def test_annotation_int(): exp = Map({ 'num_constraints': Map({ 'max': 12, 'min': 1 }), }) assert DatetimeService.__nirum_method_annotations__['delta_month'] == exp
def test_annotation_union(): exp = Map({ 'annot': Map({ 'number_arg': 2, 'text_arg': u'Nirum \'\ub2c8\ub984\'', }), 'docs': Map({'docs': u'Docs annotation.\n'}), }) assert UnionWithAnnotation.__nirum_annotations__ == exp
def test_service_method_annotation_metadata(): expect = Map({ 'docs': Map({'docs': u'Method docs.'}), 'http_resource': Map({ 'method': u'GET', 'path': u'/ping' }), 'quote': Map({ 'single': u"'", 'triple': u"'''" }), 'unicode': Map({'unicode': u'\uc720\ub2c8\ucf54\ub4dc'}), }) assert PingService.__nirum_method_annotations__['ping'] == expect
def test_map_getitem(): m = Map(a=1, b=2) assert m['a'] == m.get('a') == m.get('a', 0) == 1 assert m['b'] == m.get('b') == m.get('b', 0) == 2 with raises(KeyError): m['c'] assert m.get('c') is None assert m.get('c', 0) == 0
def test_map_equality(fx_record_type, fx_unboxed_type): Point = fx_record_type Offset = fx_unboxed_type a = Map([ (Offset(1.), Point(left=Offset(1.), top=Offset(2.0))), (Offset(3.), Point(left=Offset(3.), top=Offset(4.0))), ]) b = Map([ (Offset(1.), Point(left=Offset(1.), top=Offset(2.0))), (Offset(3.), Point(left=Offset(3.), top=Offset(4.0))), ]) c = Map([ (Offset(1.), Point(left=Offset(1.), top=Offset(2.0))), (Offset(3.), Point(left=Offset(3.), top=Offset(5.0))), ]) assert a == b assert not (a != b) assert hash(a) == hash(b) assert b != c assert not (b == c) assert hash(b) != hash(c) assert a != c assert not (a == c)
def test_map_repr(): assert repr(Map()) == 'nirum.datastructures.Map()' assert repr(Map(a=1)) == "nirum.datastructures.Map({'a': 1})" assert repr(Map(a=1, b=2)) == "nirum.datastructures.Map({'a': 1, 'b': 2})"
def test_map_bool(): assert not Map() assert Map(a=1) assert Map(a=1, b=2)
def test_map_init(): assert list(Map()) == [] assert (sorted(Map([ ('a', 1), ('b', 2) ]).items()) == sorted(Map({ 'a': 1, 'b': 2 }).items()) == sorted(Map(Map({ 'a': 1, 'b': 2 })).items()) == sorted(Map(a=1, b=2).items()) == sorted( Map([('a', 1)], b=2).items()) == sorted(Map({ 'a': 1 }, b=2).items()) == sorted(Map(Map([('a', 1)]), b=2).items()) == [('a', 1), ('b', 2)]) assert isinstance(Map(), collections.Mapping) assert not isinstance(Map(), collections.MutableMapping)
def test_map_contains(): m = Map(a=1, b=2) assert 'a' in m assert 'b' in m assert 'c' not in m
def test_map_len(): assert len(Map()) == 0 assert len(Map(a=1)) == 1 assert len(Map(a=1, b=2)) == 2
def test_map_iter(): assert list(Map()) == [] assert list(Map(a=1)) == ['a'] assert list(Map(a=1, b=2)) in (['a', 'b'], ['b', 'a'])