Exemple #1
0
    def test_indent_simple(self):
        s = String("foo\nbar\nche")

        s2 = s.indent("  ")
        self.assertNotEqual(s, s2)
        for line in s2.splitlines():
            self.assertTrue(line.startswith("  "))
Exemple #2
0
    def test_string(self):
        s = String(1)
        self.assertEqual("1", s)

        s = String(b"foo")
        self.assertEqual("foo", s)

        s = String({"foo": 1})
        self.assertEqual("{u'foo': 1}" if is_py2 else "{'foo': 1}", s)

        s = String((1, 2))
        self.assertEqual("(1, 2)", s)

        s = String([1, 2])
        self.assertEqual("[1, 2]", s)

        s = String(True)
        self.assertEqual("True", s)

        s = String(None)
        self.assertEqual("None", s)

        s = String("foo")
        self.assertEqual("foo", s)

        su = testdata.get_unicode()
        s = String(su)
        sb = bytes(s)
        s2 = String(sb)
        self.assertEqual(s, s2)
Exemple #3
0
    def test_read_2(self):
        t = self.create_instance("foo 07 19")
        t.seek(4)

        r = t.read(2)
        self.assertEqual(2, len(r))
        self.assertEqual("07", String(r[0]))
        self.assertEqual("19", String(r[1]))
        self.assertEqual(None, t.peek())
Exemple #4
0
 def test_hash(self):
     h1 = String("4356").hash("YYYY-MM-DD")
     h2 = String("4356").hash("YYYY-MM-DD")
     h3 = String("4356").hash("YYYZ-MM-DD")
     h4 = String("4356").hash("YYYY-MM-DD", nonce="foobar")
     self.assertEqual(h1, h2)
     self.assertNotEqual(h1, h3)
     self.assertNotEqual(h1, h4)
     self.assertNotEqual(h3, h4)
Exemple #5
0
    def test_next_2(self):
        t = self.create_instance("123 567")

        token = t.next()
        self.assertEqual("123", String(token))

        token = t.next()
        self.assertEqual("567", String(token))

        with self.assertRaises(StopIteration):
            t.next()
Exemple #6
0
    def test_peek(self):
        t = self.create_instance("foo bar. Che? Boom!")
        w = t.next()
        self.assertEqual("foo", String(w))
        self.assertEqual("bar", String(t.peek()))

        w = t.next()
        self.assertEqual("bar", String(w))

        self.assertEqual("Che", String(t.peek()))

        w = t.next()
        self.assertEqual("Che", String(w))
Exemple #7
0
 def test_truncate(self):
     """tests that we can truncate a string on a word boundary"""
     s = String("foo bar bang bing")
     self.assertEqual("foo", s.truncate(5, ""))
     self.assertEqual("foo bar", s.truncate(10, ""))
     self.assertEqual("foo bar bang", s.truncate(15, ""))
     self.assertEqual("foo...", s.truncate(8))
     self.assertEqual("foo bar...", s.truncate(14))
     self.assertEqual("foop", s.truncate(5, "p"))
     self.assertEqual(s, s.truncate(len(s) + 100, "..."))
Exemple #8
0
    def test_next_4(self):
        def cb(ch):
            return ch.isspace()

        t = self.create_instance("september 15-17, 2019", cb)
        self.assertEqual(["september", "15-17,", "2019"],
                         [String(w) for w in t])

        def cb(ch):
            return String(ch).ispunc() or ch.isspace()

        t = self.create_instance("september 15-17, 2019", cb)
        tokens = [w for w in t]
        self.assertEqual(["september", "15", "17", "2019"],
                         [String(w) for w in tokens])
        self.assertTrue("-", tokens[1].rdelim)
        self.assertTrue("-", tokens[2].ldelim)
Exemple #9
0
class Datatype(Datatype):
    Datatype.TAGS = FrozenDict(O=Object(None),
                               b=Binary64(),
                               b16=Binary16(),
                               b32=Binary32(),
                               b64=Binary64(),
                               f=Float(),
                               i=SignedInt(),
                               s=String(),
                               t=Timestamp(),
                               u=UnsignedInt())
Exemple #10
0
    def test_prev_1(self):
        s = "0123 567 9ABC"
        t = self.create_instance(s)

        t.seek(0)
        token = t.prev()
        self.assertEqual(None, token)

        t.seek(4)
        token = t.prev()
        self.assertEqual("0123", String(token))

        t.seek(2)
        token = t.prev()
        self.assertEqual(None, token)

        t.seek(6)
        token = t.prev()
        self.assertEqual("0123", String(token))

        t.seek(len(s))
        token = t.prev()
        self.assertEqual("9ABC", String(token))

        t.seek(len(s))
        token = t.prev()
        self.assertEqual("9ABC", String(token))
        token = t.prev()
        self.assertEqual("567", String(token))
        token = t.prev()
        self.assertEqual("0123", String(token))
        token = t.prev()
        self.assertEqual(None, token)
Exemple #11
0
    def test_create(self):
        d1 = Datetime.utcnow()
        self.assertTrue(isinstance(d1, Datetime))

        d2 = Datetime()

        self.assertEqual(d1.strftime(Datetime.FORMAT_PRECISION_SECONDS),
                         d2.strftime(Datetime.FORMAT_PRECISION_SECONDS))

        d3 = Datetime(2018, 10, 5)
        d4 = Datetime(String(d3))
        self.assertEqual(d3.strftime(Datetime.FORMAT_PRECISION_DAY),
                         d4.strftime(Datetime.FORMAT_PRECISION_DAY))
Exemple #12
0
    def test_next_3(self):
        def callback(ch):
            return ch == "A"

        t = self.create_instance("fooAbarAcheAbooAbaz", callback)

        w = t.next()
        self.assertEqual("foo", String(w))

        w = t.next()
        self.assertEqual("bar", String(w))

        w = t.next()
        self.assertEqual("che", String(w))

        w = t.next()
        self.assertEqual("boo", String(w))

        w = t.next()
        self.assertEqual("baz", String(w))

        with self.assertRaises(StopIteration):
            t.next()
Exemple #13
0
    def test_read_1(self):
        t = self.create_instance("0123 567  ABC")

        t.stream.seek(0)
        tokens = t.read(2)
        self.assertEqual(2, len(tokens))
        self.assertEqual("0123", String(tokens[0]))
        self.assertEqual("567", String(tokens[1]))

        t.stream.seek(0)
        tokens = t.read(5)
        self.assertEqual(3, len(tokens))
        self.assertEqual("0123", String(tokens[0]))
        self.assertEqual("567", String(tokens[1]))
        self.assertEqual("ABC", String(tokens[2]))

        t.stream.seek(0)
        tokens = t.read()
        self.assertEqual(3, len(tokens))
        self.assertEqual("0123", String(tokens[0]))
        self.assertEqual("567", String(tokens[1]))
        self.assertEqual("ABC", String(tokens[2]))
Exemple #14
0
    def test_prev_2(self):
        t = self.create_instance("foo bar che")
        self.assertIsNone(t.prev())

        foo = t.next()
        self.assertEqual("foo", String(foo))
        self.assertEqual("foo", String(t.prev()))

        t.next()  # t.prev() would move us back to before foo, so skip it
        bar = t.next()
        self.assertEqual("bar", String(bar))
        self.assertEqual("bar", String(t.prev()))

        t.next()
        che = t.next()
        self.assertEqual("che", String(che))
        self.assertEqual("che", String(t.prev()))
Exemple #15
0
 def test_bytes(self):
     d = HTTPHeaders()
     name = testdata.get_unicode()
     val = ByteString(testdata.get_unicode())
     d[name] = val
     self.assertEqual(d[name], String(val))
Exemple #16
0
 def test_indent_count(self):
     s = String("foo")
     self.assertTrue(s.indent(".", 3).startswith("..."))
     self.assertFalse(s.indent(".", 2).startswith("..."))
Exemple #17
0
    def test_next_1(self):
        t = self.create_instance(" 123 567  ABC")

        t.stream.seek(10)
        token = t.next()
        self.assertEqual("  ", String(token.ldelim))
        self.assertEqual("ABC", String(token))
        self.assertEqual(None, token.rdelim)

        t.stream.seek(6)
        token = t.next()
        self.assertEqual(" ", String(token.ldelim))
        self.assertEqual("567", String(token))
        self.assertEqual("  ", String(token.rdelim))

        t.stream.seek(9)
        token = t.next()
        self.assertEqual("  ", String(token.ldelim))
        self.assertEqual("ABC", String(token))
        self.assertEqual(None, token.rdelim)

        t.stream.seek(0)
        token = t.next()
        self.assertEqual(" ", String(token.ldelim))
        self.assertEqual("123", String(token))
        self.assertEqual(" ", String(token.rdelim))

        t.stream.seek(2)
        token = t.next()
        self.assertEqual(" ", String(token.ldelim))
        self.assertEqual("123", String(token))
        self.assertEqual(" ", String(token.rdelim))

        t = self.create_instance("0123 567  ABC")

        t.stream.seek(0)
        token = t.next()
        self.assertEqual(None, token.ldelim)
        self.assertEqual("0123", String(token))
        self.assertEqual(" ", String(token.rdelim))

        t.stream.seek(3)
        token = t.next()
        self.assertEqual(None, token.ldelim)
        self.assertEqual("0123", String(token))
        self.assertEqual(" ", String(token.rdelim))

        t = self.create_instance("")
        t.stream.seek(0)
        with self.assertRaises(StopIteration):
            t.next()

        t = self.create_instance("0123456789")

        t.stream.seek(0)
        token = t.next()
        self.assertEqual(None, token.ldelim)
        self.assertEqual("0123456789", String(token))
        self.assertEqual(None, token.rdelim)

        t.stream.seek(4)
        token = t.next()
        self.assertEqual(None, token.ldelim)
        self.assertEqual("0123456789", String(token))
        self.assertEqual(None, token.rdelim)

        t.stream.seek(9)
        token = t.next()
        self.assertEqual(None, token.ldelim)
        self.assertEqual("0123456789", String(token))
        self.assertEqual(None, token.rdelim)

        t = self.create_instance("0123456789   ")

        t.stream.seek(9)
        token = t.next()
        self.assertEqual(None, token.ldelim)
        self.assertEqual("0123456789", String(token))
        self.assertEqual("   ", String(token.rdelim))

        t.stream.seek(10)
        with self.assertRaises(StopIteration):
            t.next()
Exemple #18
0
 def test_string_tokenize(self):
     s = String("foo bar che")
     tokens = [t for t in s.tokenize()]
     self.assertEqual(["foo", "bar", "che"], tokens)
Exemple #19
0
    def test_ispunc(self):
        s = String("{.}")
        self.assertTrue(s.ispunc())

        s = String(".A.")
        self.assertFalse(s.ispunc())
Exemple #20
0
 def test___iter__(self):
     t = self.create_instance("foo bar")
     r = ""
     for w in t:
         r += String(w)
     self.assertEqual("foobar", r)
Exemple #21
0
 def test_xmlescape(self):
     s = String("<&")
     se = s.xmlescape()
     self.assertEqual("&lt;&amp;", se)
Exemple #22
0
 def test_regex(self):
     s = String("foo bar foo")
     r = s.regex(r"foo").count()
     self.assertEqual(2, r)
     self.assertEqual(2, len(s.regex(r"foo")))
     self.assertEqual(1, len(s.regex(r"bar")))
Exemple #23
0
 def cb(ch):
     return String(ch).ispunc() or ch.isspace()
Exemple #24
0
 def test_string_int(self):
     i = testdata.get_int(0, 1000)
     s = String(i)
     self.assertEqual(str(i), String(i))
Exemple #25
0
 def test_unicode_1(self):
     s = String(testdata.get_unicode())
     s2 = ByteString(s)
     s3 = String(s2)
     self.assertEqual(s, s3)
Exemple #26
0
 def test_unicode_2(self):
     d = {"foo": testdata.get_unicode()}
     s2 = String(d)
     s3 = ByteString(s2)
     s4 = String(s3)
     self.assertEqual(s2, s4)