import os, cStringIO from libpathod import rparse, utils import tutils rparse.TESTING = True class TestMisc: def test_generators(self): v = rparse.Value.parseString("'val'")[0] g = v.get_generator({}) assert g[:] == "val" def test_randomgenerator(self): g = rparse.RandomGenerator("one", 100) assert len(g[:10]) == 10 assert len(g[1:10]) == 9 assert len(g[:1000]) == 100 assert len(g[1000:1001]) == 0 assert g[0] def test_literalgenerator(self): g = rparse.LiteralGenerator("one") assert g == "one" assert g[:] == "one" assert g[1] == "n" def test_filegenerator(self): with tutils.tmpdir() as t: path = os.path.join(t, "foo") f = open(path, "w") f.write("x"*10000) f.close() g = rparse.FileGenerator(path) assert len(g) == 10000 assert g[0] == "x" assert g[-1] == "x" assert g[0:5] == "xxxxx" def test_valueliteral(self): v = rparse.ValueLiteral("foo") assert v.expr() assert str(v) def test_valuenakedliteral(self): v = rparse.ValueNakedLiteral("foo") assert v.expr() def test_file_value(self): v = rparse.Value.parseString("<'one two'")[0] assert str(v) assert v.path == "one two" v = rparse.Value.parseString(" 100 x = [(1, 5), (0, 5)] assert rparse.ready_actions(100, x) == sorted(x) class TestResponse: def dummy_response(self): return rparse.parse_response({}, "400'msg'") def test_response(self): r = rparse.parse_response({}, "400'msg'") assert r.code == 400 assert r.msg == "msg" r = rparse.parse_response({}, "400'msg':b@100b") assert r.msg == "msg" assert r.body[:] assert str(r) def test_render(self): s = cStringIO.StringIO() r = rparse.parse_response({}, "400'msg'") assert r.serve(s) def test_length(self): def testlen(x): s = cStringIO.StringIO() x.serve(s) assert x.length() == len(s.getvalue()) testlen(rparse.parse_response({}, "400'msg'")) testlen(rparse.parse_response({}, "400'msg':h'foo'='bar'")) testlen(rparse.parse_response({}, "400'msg':h'foo'='bar':b@100b"))