blob: ea30902e68a42b1305d9ac3841c6ea005c1b7f68 [file] [log] [blame]
Rich Lane1d5b0102013-05-10 16:28:17 -07001#!/usr/bin/env python
2# Copyright 2013, Big Switch Networks, Inc.
3#
4# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
5# the following special exception:
6#
7# LOXI Exception
8#
9# As a special exception to the terms of the EPL, you may distribute libraries
10# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
11# that copyright and licensing notices generated by LoxiGen are not altered or removed
12# from the LoxiGen Libraries and the notice provided below is (i) included in
13# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
14# documentation for the LoxiGen Libraries, if distributed in binary form.
15#
16# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
17#
18# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
19# a copy of the EPL at:
20#
21# http://www.eclipse.org/legal/epl-v10.html
22#
23# Unless required by applicable law or agreed to in writing, software
24# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26# EPL for the specific language governing permissions and limitations
27# under the EPL.
28
Rich Lane9a3f1fd2013-05-10 16:29:17 -070029import sys
Rich Lane1d5b0102013-05-10 16:28:17 -070030import difflib
Rich Lane5c4446e2013-05-16 13:53:51 -070031import re
32import os
33import unittest
Rich Lane9a3f1fd2013-05-10 16:29:17 -070034import test_data
Rich Lane1d5b0102013-05-10 16:28:17 -070035
36# Human-friendly format for binary strings. 8 bytes per line.
37def format_binary(buf):
38 byts = map(ord, buf)
39 lines = [[]]
40 for byt in byts:
41 if len(lines[-1]) == 8:
42 lines.append([])
43 lines[-1].append(byt)
44 return '\n'.join([' '.join(['%02x' % y for y in x]) for x in lines])
45
46def diff(a, b):
47 return '\n'.join(difflib.ndiff(a.splitlines(), b.splitlines()))
48
Rich Laned392eae2013-05-10 16:34:34 -070049# Test serialization / deserialization / reserialization of a sample object.
50# Depends in part on the __eq__ method being correct.
Rich Lane1d5b0102013-05-10 16:28:17 -070051def test_serialization(obj, buf):
52 packed = obj.pack()
53 if packed != buf:
54 a = format_binary(buf)
55 b = format_binary(packed)
56 raise AssertionError("Serialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \
57 (type(obj).__name__, a, b, diff(a, b)))
58 unpacked = type(obj).unpack(buf)
59 if obj != unpacked:
60 a = obj.show()
61 b = unpacked.show()
62 raise AssertionError("Deserialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \
63 (type(obj).__name__, a, b, diff(a, b)))
Rich Laned392eae2013-05-10 16:34:34 -070064 packed = unpacked.pack()
65 if packed != buf:
66 a = format_binary(buf)
67 b = format_binary(packed)
68 raise AssertionError("Reserialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \
69 (type(obj).__name__, a, b, diff(a, b)))
Rich Lane9a3f1fd2013-05-10 16:29:17 -070070
71def test_pretty(obj, expected):
72 pretty = obj.show()
73 if expected != pretty:
74 raise AssertionError("Pretty printing of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \
75 (type(obj).__name__, expected, pretty, diff(expected, pretty)))
76
77# Run test_serialization and possibly test_pretty against the named data file
Rich Lane5c4446e2013-05-16 13:53:51 -070078def test_datafile(name, ofp):
Rich Lane9a3f1fd2013-05-10 16:29:17 -070079 data = test_data.read(name)
Rich Lane5c4446e2013-05-16 13:53:51 -070080 if not 'python' in data:
81 raise unittest.SkipTest("no python section in datafile")
Rich Lane9a3f1fd2013-05-10 16:29:17 -070082 binary = data['binary']
83 python = data['python']
Rich Lane5c4446e2013-05-16 13:53:51 -070084 obj = eval(python, { 'ofp': ofp })
Rich Lane9a3f1fd2013-05-10 16:29:17 -070085 test_serialization(obj, binary)
86 if 'python pretty-printer' in data:
87 test_pretty(obj, data['python pretty-printer'])
Rich Lane5c4446e2013-05-16 13:53:51 -070088
89# Add test_datafile tests for each datafile matching the given regex
90# The argument 'klass' should be a subclass of TestCase which will have the
91# test_* methods added to it.
92#
93# It would be cleaner to do this by constructing a TestSuite instance and
94# adding individual TestCase objects, but the TestLoader wouldn't pick it
95# up. We could use the load_tests protocol but that isn't available before
96# Python 2.7.
97def add_datafiles_tests(klass, regex, ofp):
98 for filename in test_data.list_files():
99 match = re.match(regex, filename)
100 if not match:
101 continue
102 def make_test(filename):
103 def fn(self):
104 test_datafile(filename, ofp)
105 return fn
106 setattr(klass, 'test_' + os.path.splitext(filename)[0], make_test(filename))