示例#1
0
文件: test_edn.py 项目: jml/edn
 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}"]))
示例#2
0
 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))
示例#3
0
文件: test_edn.py 项目: jml/edn
 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))
示例#4
0
文件: test_edn.py 项目: jml/edn
 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'))
示例#5
0
文件: test_edn.py 项目: jml/edn
 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")))
示例#6
0
 def test_unknown_handler(self):
     output = dumps(Custom(42), default=repr)
     self.assertEqual('"<Custom(42)>"', output)
示例#7
0
文件: test_edn.py 项目: jml/edn
 def test_character(self):
     # XXX: No character support yet.  Need a type for it or something.
     self.assertEqual('"a"', dumps('a'))
示例#8
0
文件: test_edn.py 项目: jml/edn
 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))
示例#9
0
文件: test_edn.py 项目: jml/edn
 def test_uuid(self):
     uid = uuid.UUID("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")
     text = '#uuid "%s"' % (uid,)
     self.assertEqual(text, dumps(uid))
示例#10
0
文件: test_edn.py 项目: jml/edn
 def test_list(self):
     self.assertEqual("()", dumps([]))
     self.assertEqual("(() ())", dumps([[], []]))
     self.assertEqual("(a)", dumps([Symbol('a')]))
示例#11
0
文件: test_edn.py 项目: jml/edn
 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"}']))
示例#12
0
文件: test_edn.py 项目: jml/edn
 def test_long(self):
     self.assertEqual('10000N', dumps(10000L))
示例#13
0
文件: test_edn.py 项目: jml/edn
 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))
示例#14
0
文件: test_edn.py 项目: jml/edn
 def test_nil(self):
     self.assertEqual('nil', dumps(None))
示例#15
0
文件: test_edn.py 项目: jml/edn
 def test_integer(self):
     self.assertEqual('1', dumps(1))
示例#16
0
 def test_escaped_string(self):
     self.assertEqual('"foo\\nbar"', dumps('foo\nbar'))
示例#17
0
 def test_decimal(self):
     self.assertEqual('4.1234M', dumps(Decimal('4.1234')))
     self.assertEqual('4M', dumps(Decimal('4')))
示例#18
0
 def test_null(self):
     self.assertEqual('nil', dumps(None))
     self.assertEqual('("b" nil)', dumps(('b', None)))
示例#19
0
文件: test_edn.py 项目: jml/edn
 def test_keyword(self):
     self.assertEqual(":foo", dumps(Keyword("foo")))
     self.assertEqual(":my/foo", dumps(Keyword("foo", "my")))
示例#20
0
文件: test_edn.py 项目: jml/edn
 def test_booleans(self):
     self.assertEqual('true', dumps(True))
     self.assertEqual('false', dumps(False))
示例#21
0
文件: test_edn.py 项目: jml/edn
 def test_tuple(self):
     self.assertEqual("()", dumps(()))
     self.assertEqual("(() ())", dumps(((), ())))
     self.assertEqual("(a)", dumps((Symbol('a'),)))
示例#22
0
文件: test_edn.py 项目: jml/edn
 def test_simple_strings(self):
     self.assertEqual('"foo"', dumps('foo'))
示例#23
0
 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))
示例#24
0
    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'))],
    )
示例#25
0
文件: test_edn.py 项目: jml/edn
 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))
示例#26
0
文件: yel.py 项目: marianoguerra/y
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)
示例#27
0
文件: test_edn.py 项目: jml/edn
 def test_tagged_value(self):
     self.assertEqual(
         '#foo bar',
         dumps(TaggedValue(Symbol('foo'), Symbol('bar'))))
示例#28
0
文件: test_edn.py 项目: jml/edn
 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'))
示例#29
0
文件: test_edn.py 项目: jml/edn
 def test_unicode(self):
     snowman = u'\u2603'
     encoded = snowman.encode('utf-8')
     self.assertEqual('"' + encoded + '"', dumps(snowman))
示例#30
0
 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)
示例#31
0
文件: yel.py 项目: marianoguerra/y
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)
示例#32
0
 def to_edn(self, value):
     return edn.dumps(value)