blob: 33ba5454e9fd4a36121ad0c2655caa88fed376bc [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -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
29import unittest
30import pyparsing
31import loxi_front_end.parser as parser
32
33class StructTests(unittest.TestCase):
34 def test_empty(self):
35 src = """\
36struct foo { };
37"""
38 ast = parser.parse(src)
39 self.assertEquals(ast.asList(), [['struct', 'foo', []]])
40
41 def test_one_field(self):
42 src = """\
43struct foo {
44 uint32_t bar;
45};
46"""
47 ast = parser.parse(src)
48 self.assertEquals(ast.asList(),
49 [['struct', 'foo', [['uint32_t', 'bar']]]])
50
51 def test_multiple_fields(self):
52 src = """\
53struct foo {
54 uint32_t bar;
55 uint8_t baz;
56 uint64_t abc;
57};
58"""
59 ast = parser.parse(src)
60 self.assertEquals(ast.asList(),
61 [['struct', 'foo',
62 [['uint32_t', 'bar'],
63 ['uint8_t', 'baz'],
64 ['uint64_t', 'abc']]]])
65
66 def test_array_type(self):
67 src = """\
68struct foo {
69 uint32_t[4] bar;
70};
71"""
72 ast = parser.parse(src)
73 self.assertEquals(ast.asList(),
74 [['struct', 'foo', [['uint32_t[4]', 'bar']]]])
75
76 def test_list_type(self):
77 src = """\
78struct foo {
79 list(of_action_t) bar;
80};
81"""
82 ast = parser.parse(src)
83 self.assertEquals(ast.asList(),
84 [['struct', 'foo', [['list(of_action_t)', 'bar']]]])
85
Rich Lane517506c2013-04-08 14:08:31 -070086class EnumTests(unittest.TestCase):
87 def test_empty(self):
88 src = """\
89enum foo {
90};
91"""
92 ast = parser.parse(src)
93 self.assertEquals(ast.asList(), [['enum', 'foo', []]])
94
95 def test_one(self):
96 src = """\
97enum foo {
98 BAR = 1
99};
100"""
101 ast = parser.parse(src)
Rich Lanefe735bc2013-04-08 16:58:49 -0700102 self.assertEquals(ast.asList(), [['enum', 'foo', [['BAR', 1]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700103
104 def test_multiple(self):
105 src = """\
106enum foo {
107 OFP_A = 1,
108 OFP_B = 2,
109 OFP_C = 3
110};
111"""
112 ast = parser.parse(src)
Rich Lanefe735bc2013-04-08 16:58:49 -0700113 self.assertEquals(ast.asList(), [['enum', 'foo', [['OFP_A', 1], ['OFP_B', 2], ['OFP_C', 3]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700114
115 def test_trailing_comma(self):
116 src = """\
117enum foo {
118 OFP_A = 1,
119 OFP_B = 2,
120 OFP_C = 3,
121};
122"""
123 ast = parser.parse(src)
Rich Lanefe735bc2013-04-08 16:58:49 -0700124 self.assertEquals(ast.asList(), [['enum', 'foo', [['OFP_A', 1], ['OFP_B', 2], ['OFP_C', 3]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700125
Rich Lanea06d0c32013-03-25 08:52:03 -0700126class TestMetadata(unittest.TestCase):
127 def test_version(self):
128 src = """\
129#version 1
130"""
131 ast = parser.parse(src)
132 self.assertEquals(ast.asList(), [['metadata', 'version', '1']])
133
134class TestToplevel(unittest.TestCase):
135 def test_multiple_structs(self):
136 src = """\
137struct foo { };
138struct bar { };
139"""
140 ast = parser.parse(src)
141 self.assertEquals(ast.asList(),
142 [['struct', 'foo', []], ['struct', 'bar', []]])
143
144 def test_comments(self):
145 src = """\
146// comment 1
147struct foo { //comment 2
148// comment 3
149 uint32_t a; //comment 5
150// comment 6
151};
152// comment 4
153"""
154 ast = parser.parse(src)
155 self.assertEquals(ast.asList(),
156 [['struct', 'foo', [['uint32_t', 'a']]]])
157
158 def test_mixed(self):
159 src = """\
160#version 1
161struct foo { };
162#version 2
163struct bar { };
164"""
165 ast = parser.parse(src)
166 self.assertEquals(ast.asList(),
167 [['metadata', 'version', '1'],
168 ['struct', 'foo', []],
169 ['metadata', 'version', '2'],
170 ['struct', 'bar', []]])
171
172class TestErrors(unittest.TestCase):
173 def syntax_error(self, src, regex):
174 with self.assertRaisesRegexp(pyparsing.ParseSyntaxException, regex):
175 parser.parse(src)
176
177 def test_missing_struct_syntax(self):
178 self.syntax_error('struct { uint32_t bar; };',
179 'Expected identifier \(at char 7\)')
180 self.syntax_error('struct foo uint32_t bar; };',
181 'Expected "{" \(at char 11\)')
182 self.syntax_error('struct foo { uint32_t bar; ;',
183 'Expected "}" \(at char 27\)')
184 self.syntax_error('struct foo { uint32_t bar; }',
185 'Expected ";" \(at char 28\)')
186
187 def test_invalid_type_name(self):
188 self.syntax_error('struct foo { list<of_action_t> bar; }',
189 'Expected "\(" \(at char 17\)')
190 self.syntax_error('struct foo { uint32_t[10 bar; }',
191 'Expected "\]" \(at char 24\)')
192
193 def test_invalid_member_syntax(self):
194 self.syntax_error('struct foo { bar; }',
195 'Expected identifier \(at char 16\)')
196 self.syntax_error('struct foo { uint32_t bar baz; }',
197 'Expected ";" \(at char 26\)')
198
199
200if __name__ == '__main__':
201 unittest.main()