Example #1
0
 def test_example(self):
     self.assertTrue('./example.data' in test_data.list_files())
     data = test_data.read('example.data')
     self.assertEquals(sorted(['section1', 'section2', 'binary']), sorted(data.keys()))
     self.assertEquals(' abc def\nghi', data['section1'])
     self.assertEquals('123\n456\n789', data['section2'])
     self.assertEquals('\x00\x01\x02\x03\x04\x05\x06\x07\x77\x66\x55\x44\x33\x22\x11\x00',
                       data['binary'])
Example #2
0
def test_datafile(name, ofp):
    data = test_data.read(name)
    if not 'python' in data:
        raise unittest.SkipTest("no python section in datafile")
    binary = data['binary']
    python = data['python']
    obj = eval(python, {'ofp': ofp})
    test_serialization(obj, binary)
    if 'python pretty-printer' in data:
        test_pretty(obj, data['python pretty-printer'])
Example #3
0
def test_datafile(name, ofp):
    data = test_data.read(name)
    if not 'python' in data:
        raise unittest.SkipTest("no python section in datafile")
    binary = data['binary']
    python = data['python']
    obj = eval(python, { 'ofp': ofp })
    test_serialization(obj, binary)
    if 'python pretty-printer' in data:
        test_pretty(obj, data['python pretty-printer'])
Example #4
0
def check_datafile(filename):
    name = os.path.splitext(os.path.basename(filename))[0]
    data = test_data.read(filename)
    if not 'lua' in data:
        raise SkipTest("no lua section in datafile")

    tmpdir = tempfile.mkdtemp(prefix="xdr-test-lua.")

    bindir = os.path.join(root, "bin")
    outdir = os.path.join(tmpdir, "xdr")
    filename = os.path.join(tmpdir, "test.xdr")
    with open(filename, 'w') as f:
        f.write(data['xdr'])
    subprocess.check_call(
        [bindir + "/xdr", "-t", "lua", "-o", outdir, filename])

    with open(os.path.join(tmpdir, "expected.lua"), 'w') as f:
        f.write("return " + data['lua'])

    # Convert the Python list to Lua syntax
    with open(os.path.join(tmpdir, "raw.lua"), 'w') as f:
        f.write('return { ')
        for x in data['raw']:
            if isinstance(x, int):
                f.write(repr(x) + ', ')
            elif isinstance(x, str):

                def lua_escape(c):
                    if c in string.printable and c != '"':
                        return c
                    else:
                        return "\%03d" % ord(c)

                f.write('"' + ''.join(lua_escape(c) for c in x) + '", ')
            else:
                assert (False)
        f.write('}')

    with open(os.path.join(tmpdir, "testutils.lua"), 'w') as f:
        f.write(testutils_lua)

    try:
        subprocess.check_output(["lua", "-l", "testutils", "-e", "runtest()"],
                                cwd=tmpdir,
                                stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError, e:
        print e.output
        print "Temporary directory:", tmpdir
        raise AssertionError("Test failed")
Example #5
0
def test_datafile(name, ofp, pyversion):
    data = test_data.read(name)
    if pyversion == 3:
        key = 'python3'
        if key not in data:
            # default to the 'python' section
            key = 'python'
    else:
        key = 'python'
    if key not in data:
        raise unittest.SkipTest("no %s section in datafile" % key)
    binary = data['binary']
    python = data[key]
    obj = eval(python, { 'ofp': ofp })
    test_serialization(obj, binary)
    keyprettyprint = key + ' pretty-printer'
    if keyprettyprint in data:
        test_pretty(obj, data[keyprettyprint])
Example #6
0
def check_datafile(filename):
    """
    Check that the generated Python code can serialize and deserialize correctly
    """
    data = test_data.read(filename)
    if not 'python' in data:
        raise SkipTest("no python section in datafile")

    mod = load_xdr(data['xdr'])
    x = eval(data['python'], { 'xdr': mod })
    assert_equal(x.pack(), data['binary'])
    y = mod.root.unpack(data['binary'])
    assert_equal(x, y)
    assert_equal(y.pack(), data['binary'])

    if 'python string' in data:
        assert_equal(str(x), data['python string'])
        assert_equal(str(y), data['python string'])
Example #7
0
def check_datafile(filename):
    """
    Check that the generated Python code can serialize and deserialize correctly
    """
    data = test_data.read(filename)
    if not 'python' in data:
        raise SkipTest("no python section in datafile")

    mod, tmpdir = load_xdr(data['xdr'])
    x = eval(data['python'], {'xdr': mod})
    assert_equal(x.pack(), data['binary'])
    y = mod.root.unpack(data['binary'])
    assert_equal(x, y)
    assert_equal(y.pack(), data['binary'])

    if 'python string' in data:
        assert_equal(str(x), data['python string'])
        assert_equal(str(y), data['python string'])

    shutil.rmtree(tmpdir)
Example #8
0
 def test_data(self):
     return test_data.read(self.data_file_name)
Example #9
0
 def test_all(self):
     for name in test_data.list_files():
         test_data.read(name)
Example #10
0
 def test_all(self):
     for name in test_data.list_files():
         test_data.read(name)