import os, cStringIO from libpathod import language, utils import tutils language.TESTING = True class TestValueNakedLiteral: def test_expr(self): v = language.ValueNakedLiteral("foo") assert v.expr() def test_spec(self): v = language.ValueNakedLiteral("foo") assert v.spec() == repr(v) == "foo" v = language.ValueNakedLiteral("f\x00oo") assert v.spec() == repr(v) == r"f\x00oo" class TestValueLiteral: def test_espr(self): v = language.ValueLiteral("foo") assert v.expr() assert v.val == "foo" v = language.ValueLiteral(r"foo\n") assert v.expr() assert v.val == "foo\n" assert repr(v) def test_spec(self): v = language.ValueLiteral("foo") assert v.spec() == r'"foo"' v = language.ValueLiteral("f\x00oo") assert v.spec() == repr(v) == r'"f\x00oo"' class TestValueGenerate: def test_basic(self): v = language.Value.parseString("@10b")[0] assert v.usize == 10 assert v.unit == "b" assert v.bytes() == 10 v = language.Value.parseString("@10")[0] assert v.unit == "b" v = language.Value.parseString("@10k")[0] assert v.bytes() == 10240 v = language.Value.parseString("@10g")[0] assert v.bytes() == 1024**3 * 10 v = language.Value.parseString("@10g,digits")[0] assert v.datatype == "digits" g = v.get_generator({}) assert g[:100] v = language.Value.parseString("@10,digits")[0] assert v.unit == "b" assert v.datatype == "digits" def test_spec(self): v = language.ValueGenerate(1, "b", "bytes") assert v.spec() == repr(v) == "@1" v = language.ValueGenerate(1, "k", "bytes") assert v.spec() == repr(v) == "@1k" v = language.ValueGenerate(1, "k", "ascii") assert v.spec() == repr(v) == "@1k,ascii" v = language.ValueGenerate(1, "b", "ascii") assert v.spec() == repr(v) == "@1,ascii" class TestValueFile: def test_file_value(self): v = language.Value.parseString("<'one two'")[0] assert str(v) assert v.path == "one two" v = language.Value.parseString("= len(s.getvalue()) r = language.parse_response({}, "400:m'msg':b@100:d0") testlen(r) r = language.parse_response({}, "400:m'msg':b@100:d0:i0,'foo'") testlen(r) r = language.parse_response({}, "400:m'msg':b@100:d0:i0,'foo'") testlen(r) def test_render(self): r = language.parse_response({}, "400:p0,100:dr") assert r.actions[0].spec() == "p0,100" assert len(r.preview_safe()) == 1 assert not r.actions[0].spec().startswith("p") def test_read_file(): tutils.raises(language.FileAccessDenied, language.read_file, {}, "=/foo") p = tutils.test_data.path("data") d = dict(staticdir=p) assert language.read_file(d, "+./file").strip() == "testfile" assert language.read_file(d, "+file").strip() == "testfile" tutils.raises(language.FileAccessDenied, language.read_file, d, "+./nonexistent") tutils.raises(language.FileAccessDenied, language.read_file, d, "+/nonexistent") tutils.raises(language.FileAccessDenied, language.read_file, d, "+../test_language.py") d["unconstrained_file_access"] = True assert language.read_file(d, "+../test_language.py")