def test_equality(self): self.assertTrue("db/id" != Keyword("db/id")) self.assertTrue("db/id" != Symbol("db/id")) self.assertTrue(Symbol("db/id") != Keyword("db/id")) self.assertTrue("db/id" == "db/id") self.assertTrue(Keyword("db/id") == Keyword("db/id")) self.assertTrue(Symbol("db/id") == Symbol("db/id"))
def test_parser_multiple_expressions(self): for expected, edn_string in ( ([], ""), ([], " ,,,, ,, , "), ([1], ",,,,,,,1,,,,,,,,,"), ([1, 2], "1 2"), ([1, 2], "1 2"), ([True, 42, False, Symbol('end')], "true 42 false end"), ([Symbol("a*b"), 42], 'a*b 42'), ): self.check_parse_all(expected, edn_string) if expected: self.check_parse(expected[0], edn_string)
def test_parser(self): self.check_parse(1, "1") self.check_parse(Symbol("a*b"), 'a*b') self.check_parse("ab", '"ab"') self.check_parse('a"b', r'"a\"b"') self.check_parse("blah\n", '"blah\n"') self.check_parse([1, 2, 3], "[1 2 3]") self.check_parse({1, 2, 3}, "#{1 2 3}") self.check_parse([1, True, None], "[1 true nil]") self.check_parse("c", r"\c") self.check_parse("\n", r"\newline") self.check_parse(Keyword("abc"), ":abc") self.check_parse([Keyword("abc"), 1, True, None], "[:abc 1 true nil]") self.check_parse((Keyword("abc"), 1, True, None), "(:abc 1 true nil)") self.check_parse(tuple(), "()") self.check_parse(set(), "#{}") self.check_parse({}, "{}") self.check_parse([], "[]") self.check_parse({"a": [1, 2, 3]}, '{"a" [1 2 3]}') self.check_parse( datetime.datetime(2012, 12, 22, 19, 40, 18, 0, tzinfo=pytz.utc), '#inst "2012-12-22T19:40:18Z"') self.check_parse(datetime.date(2011, 10, 9), '#inst "2011-10-09"') self.check_parse("|", "\"|\"") self.check_parse("%", "\"%\"") self.check_parse(['bl"ah'], r"""["bl\"ah"]""") self.check_parse("blah\n", '"blah\n"') self.check_parse('"', r'"\""') self.check_parse('\\', r'"\\"') self.check_parse(["abc", "123"], '["abc", "123"]') self.check_parse({"key": "value"}, '{"key" "value"}') self.check_parse( frozenset({ImmutableList([u"ab", u"cd"]), ImmutableList([u"ef"])}), '#{["ab", "cd"], ["ef"]}')
def test_parser_single_expressions(self): for expected, edn_string in ( (1, "1"), (16768115, "0xFFDC73"), (Symbol("xFF"), "xFF"), (Symbol("a*b"), 'a*b'), ("ab", '"ab"'), ('a"b', r'"a\"b"'), ("blah\n", '"blah\n"'), ([1, 2, 3], "[1 2 3]"), ({1, 2, 3}, "#{1 2 3}"), ([1, True, None], "[1 true nil]"), ("c", r"\c"), ("\n", r"\newline"), (u"Σ", u"\\Σ"), (u"λ", r"\u03bB"), (Keyword("abc"), ":abc"), ([Keyword("abc"), 1, True, None], "[:abc 1 true nil]"), ((Keyword("abc"), 1, True, None), "(:abc 1 true nil)"), (tuple(), "()"), (set(), "#{}"), ({}, "{}"), ([], "[]"), ({ "a": [1, 2, 3] }, '{"a" [1 2 3]}'), (datetime.datetime(2012, 12, 22, 19, 40, 18, 0, tzinfo=pytz.utc), '#inst "2012-12-22T19:40:18Z"'), (datetime.date(2011, 10, 9), '#inst "2011-10-09"'), ("|", "\"|\""), ("%", "\"%\""), (['bl"ah'], r"""["bl\"ah"]"""), ("blah\n", '"blah\n"'), ('"', r'"\""'), ('\\', r'"\\"'), (["abc", "123"], '["abc", "123"]'), ({ "key": "value" }, '{"key" "value"}'), (frozenset({ImmutableList([u"ab", u"cd"]), ImmutableList([u"ef"])}), '#{["ab", "cd"], ["ef"]}'), (fractions.Fraction(2, 3), "2/3"), ((2, Symbol('/'), 3), "(2 / 3)"), ): self.check_parse(expected, edn_string) self.check_parse_all([expected], edn_string)
def test_keyword_keys(self): unchanged = ( None, True, 1, "foo", {}, {True: 42}, {25: 42}, {3.14: "test"}, {Keyword("foo"): "something"}, {Symbol("foo"): "something"}, ["foo", "bar"], ("foo", "bar"), {1: {2: 3}}, ImmutableDict({1: 2}), ) for case in unchanged: self.check_roundtrip(case, keyword_keys=True) keyworded_keys = ( ("{:foo 42}", {"foo": 42}), ("{:a {:b {:c 42} :d 1}}", {"a": {"b": {"c": 42}, "d": 1}}), ("[{:a 42} {:b 25}]", [{"a": 42}, {"b": 25}]), ("({:a 42} {:b 25})", ({"a": 42}, {"b": 25})), ("{1 [{:a 42}]}", {1: [{"a": 42}]}), ("{:foo 1}", ImmutableDict({"foo": 1})), ) for expected, data in keyworded_keys: self.assertEqual(expected, dumps(data, keyword_keys=True)) self.assertEqual( {Keyword("a"): {Keyword("b"): 2}, 3: 4}, loads(dumps({Keyword("a"): {"b": 2}, 3: 4}, keyword_keys=True)))
def test_hashing(self): pop_count = len( set(map(hash, ["db/id", Keyword("db/id"), Symbol("db/id")]))) self.assertEqual(pop_count, 3)
def test_symbol(self): self.check_parse('a*b', Symbol("a*b"))