def test_custom_tags(self): @tag("dog") def parse_dog(name): return { "kind": "dog", "name": name, "message": "woof-woof", } @tag("cat") class Cat(TaggedElement): def __init__(self, name): self.name = name dog = loads("#dog \"Max\"") self.assertEqual({ "kind": "dog", "name": "Max", "message": "woof-woof" }, dog) cat = loads("#cat \"Alex\"") self.assertIsInstance(cat, Cat) self.assertEqual("Alex", cat.name) remove_tag("cat") self.assertRaises(NotImplementedError, lambda: loads("#cat \"Alex\"")) add_tag("cat", Cat) cat = loads("#cat \"Alex\"") self.assertIsInstance(cat, Cat) self.assertEqual("Alex", cat.name)
def test_round_trip_same(self): EDN_LITERALS = ("nil", "true", "false", '"hello world"', ":keyword", ":+", ":!", ":-", ":_", ":$", ":&", ":=", ":.", ":abc/def", "symbol", "123", "-123", "32.23", "32.23M", "-32.23M", "-3.903495E-73M", "3.23e-10", "3e+20", "3E+20M", '["abc"]', '[1]', '[1 "abc"]', '[1 "abc" true]', '[:ghi]', '(:ghi)', '[1 "abc" true :ghi]', '(1 "abc" true :ghi)', '{"a" 2}', '#inst "1985-04-12T23:20:50.000000Z"', '#inst "2011-10-09"', '#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"', '#date "19/07/1984"', '#{{"a" 1}}', '#{{"a" #{{:b 2}}}}', '"|"') class TagDate(TaggedElement): def __init__(self, value): super(TagDate, self).__init__() self.name = 'date' self.value = datetime.datetime.strptime(value, "%d/%m/%Y").date() def __str__(self): return '#{} "{}"'.format(self.name, self.value.strftime("%d/%m/%Y")) add_tag('date', TagDate) for literal in EDN_LITERALS: step1 = literal step2 = loads(step1) step3 = dumps(step2) self.assertEqual(step1, step3)
def test_round_trip_same(self): EDN_LITERALS = ( "nil", "true", "false", '"hello world"', ":keyword", ":+", ":!", ":-", ":_", ":$", ":&", ":=", ":.", ":abc/def", "symbol", "123", "-123", "32.23", "32.23M", "-32.23M", "3.23e-10", '["abc"]', '[1]', '[1 "abc"]', '[1 "abc" true]', '[:ghi]', '(:ghi)', '[1 "abc" true :ghi]', '(1 "abc" true :ghi)', '{"a" 2}', '#inst "1985-04-12T23:20:50Z"', '#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"', '#date "19/07/1984"', '#{{"a" 1}}', '#{{"a" #{{:b 2}}}}', '"|"' ) class TagDate(TaggedElement): def __init__(self, value): self.name = 'date' self.value = datetime.datetime.strptime( value, "%d/%m/%Y").date() def __str__(self): return '#{} "{}"'.format( self.name, self.value.strftime("%d/%m/%Y")) add_tag('date', TagDate) for literal in EDN_LITERALS: step1 = literal step2 = loads(step1) step3 = dumps(step2) # print step1, "->", step2, "->", step3 self.assertEqual(step1, step3)
def test_round_trip_same(self): EDN_LITERALS = ( "nil", "true", "false", '"hello world"', ":keyword", ":+", ":!", ":-", ":_", ":$", ":&", ":=", ":.", ":abc/def", #"symbol", "123", "-123", "32.23", "32.23M", "-32.23M", "3.23e-10", '["abc"]', '[1]', '[1 "abc"]', '[1 "abc" true]', '[:ghi]', '(:ghi)', '[1 "abc" true :ghi]', '(1 "abc" true :ghi)', #'#myapp/Person {:first "Fred" :last "Mertz', '#inst "1985-04-12T23:20:50Z"', '#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"', '#date "19/07/1984"') class TagDate(TaggedElement): def __init__(self, value): self.name = 'date' self.value = datetime.datetime.strptime(value, "%d/%m/%Y").date() def __str__(self): return '#{} "{}"'.format(self.name, self.value.strftime("%d/%m/%Y")) add_tag('date', TagDate) for literal in EDN_LITERALS: step1 = literal step2 = loads(step1) step3 = dumps(step2) # print step1, "->", step2, "->", step3 self.assertEqual(step1, step3)
def test_tag(self): class TagDate(TaggedElement): def __init__(self, value): self.name = 'date' self.value = datetime.datetime.strptime( value, "%d/%m/%Y").date() def __str__(self): return '#{} "{}"'.format( self.name, self.value.strftime("%d/%m/%Y")) add_tag('date', TagDate) self.check_roundtrip('#date "19/07/1984"') self.check_roundtrip('#inst "1985-04-12T23:20:50Z"') self.check_roundtrip('#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"')
import decimal import argparse parser = argparse.ArgumentParser(description="Test valid edn") parser.add_argument("-d", "--debug", action="store_true", help="debug") debug = parser.parse_args().debug class MyappPerson(edn_format.TaggedElement): def __init__(self, name, value): self.name = name self.value = value def __str__(self): return "#myapp/Person " + edn_format.dump(self.value) edn_format.add_tag("myapp/Person", MyappPerson) validEdnDir = "../../../../valid-edn" results = {} for ednFile in [f for f in listdir(validEdnDir) if isfile(join(validEdnDir, f))]: ednFileName = ednFile.split(".")[0] validEdn = open(join(validEdnDir, ednFileName + ".edn"), "r").read() expectedPy = open(join("..", ednFileName + ".py"), "r").read() if debug: print ednFileName try: expected = eval(expectedPy) parsed = edn_format.loads(validEdn) result = expected == parsed if not result and debug:
file_type = FileType(ftype) return cls( dict(path=path, size=size, uid=uid, gid=gid, atime=atime, mtime=mtime, mode=mode, type=file_type)) def __str__(self): return "#y.File {}".format(edn_format.dumps(self.to_dict())) def to_dict(self): return dict(self.items()) edn_format.add_tag("y.E", Error) edn_format.add_tag("y.O", Options) edn_format.add_tag("y.Uid", Uid) edn_format.add_tag("y.Gid", Gid) edn_format.add_tag("y.File", File) edn_format.add_tag("y.FileSize", FileSize) edn_format.add_tag("y.FileType", FileType) edn_format.add_tag("y.Path", Path) edn_format.add_tag("y.Timestamp", Timestamp) edn_format.add_tag("y.UnixPerms", UnixPerms)
elif os.path.isdir(fpath): ftype = "d" elif os.path.islink(fpath): ftype = "l" elif os.path.ismount(fpath): ftype = "m" else: ftype = "?" file_type = FileType(ftype) return cls(dict(path=path, size=size, uid=uid, gid=gid, atime=atime, mtime=mtime, mode=mode, type=file_type)) def __str__(self): return "#y.File {}".format(edn_format.dumps(self.to_dict())) def to_dict(self): return dict(self.items()) edn_format.add_tag("y.E", Error) edn_format.add_tag("y.O", Options) edn_format.add_tag("y.Uid", Uid) edn_format.add_tag("y.Gid", Gid) edn_format.add_tag("y.File", File) edn_format.add_tag("y.FileSize", FileSize) edn_format.add_tag("y.FileType", FileType) edn_format.add_tag("y.Path", Path) edn_format.add_tag("y.Timestamp", Timestamp) edn_format.add_tag("y.UnixPerms", UnixPerms)