import os from libpathod import language from libpathod.language import base, exceptions import tutils import nose.tools as nt def parse_request(s): return language.parse_pathoc(s).next() def test_times(): reqs = list(language.parse_pathoc("get:/:x5")) assert len(reqs) == 5 assert not reqs[0].times def test_caseless_literal(): class CL(base.CaselessLiteral): TOK = "foo" v = CL("foo") assert v.expr() assert v.values(language.Settings()) class TestTokValueNakedLiteral: def test_expr(self): v = base.TokValueNakedLiteral("foo") assert v.expr() def test_spec(self): v = base.TokValueNakedLiteral("foo") assert v.spec() == repr(v) == "foo" v = base.TokValueNakedLiteral("f\x00oo") assert v.spec() == repr(v) == r"f\x00oo" class TestTokValueLiteral: def test_espr(self): v = base.TokValueLiteral("foo") assert v.expr() assert v.val == "foo" v = base.TokValueLiteral("foo\n") assert v.expr() assert v.val == "foo\n" assert repr(v) def test_spec(self): v = base.TokValueLiteral("foo") assert v.spec() == r"'foo'" v = base.TokValueLiteral("f\x00oo") assert v.spec() == repr(v) == r"'f\x00oo'" v = base.TokValueLiteral("\"") assert v.spec() == repr(v) == '\'"\'' def roundtrip(self, spec): e = base.TokValueLiteral.expr() v = base.TokValueLiteral(spec) v2 = e.parseString(v.spec()) nt.assert_equal(v.val, v2[0].val) nt.assert_equal(v.spec(), v2[0].spec()) def test_roundtrip(self): self.roundtrip("'") self.roundtrip('\'') self.roundtrip("a") self.roundtrip("\"") # self.roundtrip("\\") self.roundtrip("200:b'foo':i23,'\\''") self.roundtrip("\a") class TestTokValueGenerate: def test_basic(self): v = base.TokValue.parseString("@10b")[0] assert v.usize == 10 assert v.unit == "b" assert v.bytes() == 10 v = base.TokValue.parseString("@10")[0] assert v.unit == "b" v = base.TokValue.parseString("@10k")[0] assert v.bytes() == 10240 v = base.TokValue.parseString("@10g")[0] assert v.bytes() == 1024 ** 3 * 10 v = base.TokValue.parseString("@10g,digits")[0] assert v.datatype == "digits" g = v.get_generator({}) assert g[:100] v = base.TokValue.parseString("@10,digits")[0] assert v.unit == "b" assert v.datatype == "digits" def test_spec(self): v = base.TokValueGenerate(1, "b", "bytes") assert v.spec() == repr(v) == "@1" v = base.TokValueGenerate(1, "k", "bytes") assert v.spec() == repr(v) == "@1k" v = base.TokValueGenerate(1, "k", "ascii") assert v.spec() == repr(v) == "@1k,ascii" v = base.TokValueGenerate(1, "b", "ascii") assert v.spec() == repr(v) == "@1,ascii" def test_freeze(self): v = base.TokValueGenerate(100, "b", "ascii") f = v.freeze(language.Settings()) assert len(f.val) == 100 class TestTokValueFile: def test_file_value(self): v = base.TokValue.parseString("<'one two'")[0] assert str(v) assert v.path == "one two" v = base.TokValue.parseString("