def test_set(self): self.assertEqual("#{}", dumps(frozenset())) self.assertIn( dumps(set([1, 2, 3])), set(["#{1 2 3}", "#{1 3 2}", "#{2 1 3}", "#{2 3 1}", "#{3 1 2}", "#{3 2 1}"]))
def test_complex(self): writers = ( (datetime.date, Symbol('day'), lambda x: x.strftime('%Y-%m-%d')), ) uid = uuid.uuid4() data = [ ('username', "joe.random"), ('time_finished', datetime.date(2013, 9, 21)), ('specified_end_date', None), ('time_started', datetime.date(2013, 9, 21)), ('end_date', datetime.date(2013, 9, 21)), ('data_file', "/tmp/path/output/%s" % (str(uid,))), ("specified_start_date", None), ("start_date", datetime.date(2013, 9, 1)), ("id", str(uid)), ("foo", "bar"), ] expected = ( '[("username" "joe.random") ("time_finished" #day "2013-09-21") ' '("specified_end_date" nil) ("time_started" #day "2013-09-21") ' '("end_date" #day "2013-09-21") ' '("data_file" "/tmp/path/output/%s") ' '("specified_start_date" nil) ("start_date" #day "2013-09-01") ' '("id" "%s") ("foo" "bar")]' ) % (str(uid), str(uid)) self.assertEqual(expected, dumps(data, writers))
def test_arbitrary_namedtuple(self): # Documenting a potentially unexpected behaviour. Because dumps # figures out how to write something based on type, namedtuples will # be dumped as lists. Since they are very often used for specific types, # that might be surprising. foo = namedtuple('foo', 'x y') a = foo(1, 2) self.assertEqual('(1 2)', dumps(a))
def test_newlines(self): # It doesn't have to be this way. EDN allows literal newlines in # strings, but the equality definition says that 'foo\nbar' is not # equal to 'foo # bar'. Thus, it's equally valid to not escape the newline but to # instead insert a literal space. This is possibly a bug in the # spec. self.assertEqual('"foo\\nbar"', dumps('foo\nbar'))
def test_symbol(self): self.assertEqual("foo", dumps(Symbol("foo"))) self.assertEqual(".foo", dumps(Symbol(".foo"))) self.assertEqual("/", dumps(Symbol("/"))) self.assertEqual("foo/bar", dumps(Symbol("bar", "foo")))
def test_unknown_handler(self): output = dumps(Custom(42), default=repr) self.assertEqual('"<Custom(42)>"', output)
def test_character(self): # XXX: No character support yet. Need a type for it or something. self.assertEqual('"a"', dumps('a'))
def test_datetime_with_tz(self): sometime = datetime.datetime( 2012, 5, 12, 14, 30, 0, tzinfo=pytz.FixedOffset(60)) self.assertEqual('#inst "2012-05-12T14:30:00+01:00"', dumps(sometime))
def test_uuid(self): uid = uuid.UUID("f81d4fae-7dec-11d0-a765-00a0c91e6bf6") text = '#uuid "%s"' % (uid,) self.assertEqual(text, dumps(uid))
def test_list(self): self.assertEqual("()", dumps([])) self.assertEqual("(() ())", dumps([[], []])) self.assertEqual("(a)", dumps([Symbol('a')]))
def test_dict(self): self.assertEqual("{}", dumps({})) self.assertEqual('{:foo "bar"}', dumps({Keyword('foo'): 'bar'})) self.assertIn( dumps({Keyword('foo'): 'bar', Keyword('baz'): 'qux'}), set(['{:foo "bar" :baz "qux"}', '{:baz "qux" :foo "bar"}']))
def test_long(self): self.assertEqual('10000N', dumps(10000L))
def test_float(self): # FIXME: At least I have some idea of how ignorant this makes me look. # Figure out what's required to do this rigorously. self.assertEqual('0.3', dumps(0.3))
def test_nil(self): self.assertEqual('nil', dumps(None))
def test_integer(self): self.assertEqual('1', dumps(1))
def test_escaped_string(self): self.assertEqual('"foo\\nbar"', dumps('foo\nbar'))
def test_decimal(self): self.assertEqual('4.1234M', dumps(Decimal('4.1234'))) self.assertEqual('4M', dumps(Decimal('4')))
def test_null(self): self.assertEqual('nil', dumps(None)) self.assertEqual('("b" nil)', dumps(('b', None)))
def test_keyword(self): self.assertEqual(":foo", dumps(Keyword("foo"))) self.assertEqual(":my/foo", dumps(Keyword("foo", "my")))
def test_booleans(self): self.assertEqual('true', dumps(True)) self.assertEqual('false', dumps(False))
def test_tuple(self): self.assertEqual("()", dumps(())) self.assertEqual("(() ())", dumps(((), ()))) self.assertEqual("(a)", dumps((Symbol('a'),)))
def test_simple_strings(self): self.assertEqual('"foo"', dumps('foo'))
def test_datetime_with_tz(self): tz = iso8601.iso8601.FixedOffset(1, 0, '+01:00') sometime = datetime.datetime(2012, 5, 12, 14, 30, 0, tzinfo=tz) self.assertEqual('#inst "2012-05-12T14:30:00+01:00"', dumps(sometime))
for i in range(n): word = random.choice(WORDS) try: yield word.decode('ascii') except UnicodeDecodeError: continue def random_decimal(): value = random.randint(-500000, 500000) / 100.0 return decimal.Decimal(value).quantize(decimal.Decimal('0.01')) def random_day(): return datetime.date(2013, 1, 1) + datetime.timedelta(random.randint(0, 365)) def make_element(): return {edn.Keyword('description'): ' '.join(random_words(3)), edn.Keyword('amount'): random_decimal(), edn.Keyword('date'): random_day()} num = int(sys.argv[1]) for i in range(num): print edn.dumps( make_element(), [(datetime.date, edn.Symbol('day'), lambda x: x.strftime('%Y-%m-%d'))], )
def test_datetime(self): sometime = datetime.datetime(2012, 5, 12, 14, 30, 0, tzinfo=pytz.UTC) self.assertEqual('#inst "2012-05-12T14:30:00+00:00"', dumps(sometime))
def run_command_edn_in_out(command, env, args, kwargs, edn_iter=sys.stdin): for result in run_command_edn_in(command, env, args, kwargs, edn_iter): yield edn.dumps(result)
def test_tagged_value(self): self.assertEqual( '#foo bar', dumps(TaggedValue(Symbol('foo'), Symbol('bar'))))
def test_escaping(self): self.assertEqual('"foo\\rbar"', dumps('foo\rbar')) self.assertEqual(r'"foo\\bar"', dumps(r'foo\bar')) self.assertEqual('"foo\\"bar"', dumps('foo"bar'))
def test_unicode(self): snowman = u'\u2603' encoded = snowman.encode('utf-8') self.assertEqual('"' + encoded + '"', dumps(snowman))
def test_custom_writer(self): point = namedtuple('point', 'x y') writer = lambda p: (p.x, p.y) output = dumps(point(2, 3), [(point, Symbol('point'), writer)]) self.assertEqual('#point (2 3)', output)
def to_edn(self, value): return edn.dumps(value)