blob: da262a43aa804e2790b65cbdcdb0aa0aef106be7 [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)
Rich Lane43b2a902013-05-09 13:47:07 -070039 self.assertEquals(ast, [['struct', 'foo', []]])
Rich Lanea06d0c32013-03-25 08:52:03 -070040
41 def test_one_field(self):
42 src = """\
43struct foo {
44 uint32_t bar;
45};
46"""
47 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -070048 self.assertEquals(ast,
Rich Lanea06d0c32013-03-25 08:52:03 -070049 [['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)
Rich Lane43b2a902013-05-09 13:47:07 -070060 self.assertEquals(ast,
Rich Lanea06d0c32013-03-25 08:52:03 -070061 [['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)
Rich Lane43b2a902013-05-09 13:47:07 -070073 self.assertEquals(ast,
Rich Lanea06d0c32013-03-25 08:52:03 -070074 [['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)
Rich Lane43b2a902013-05-09 13:47:07 -070083 self.assertEquals(ast,
Rich Lanea06d0c32013-03-25 08:52:03 -070084 [['struct', 'foo', [['list(of_action_t)', 'bar']]]])
85
Rich Lanee4d04ea2013-05-09 11:27:06 -070086 def test_pad_type(self):
87 src = """\
88struct foo {
89 pad(1);
90};
91"""
92 ast = parser.parse(src)
93 self.assertEquals(ast,
94 [['struct', 'foo', [['pad', 1]]]])
95
Rich Lane517506c2013-04-08 14:08:31 -070096class EnumTests(unittest.TestCase):
97 def test_empty(self):
98 src = """\
99enum foo {
100};
101"""
102 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700103 self.assertEquals(ast, [['enum', 'foo', []]])
Rich Lane517506c2013-04-08 14:08:31 -0700104
105 def test_one(self):
106 src = """\
107enum foo {
108 BAR = 1
109};
110"""
111 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700112 self.assertEquals(ast, [['enum', 'foo', [['BAR', 1]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700113
114 def test_multiple(self):
115 src = """\
116enum foo {
117 OFP_A = 1,
118 OFP_B = 2,
119 OFP_C = 3
120};
121"""
122 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700123 self.assertEquals(ast, [['enum', 'foo', [['OFP_A', 1], ['OFP_B', 2], ['OFP_C', 3]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700124
125 def test_trailing_comma(self):
126 src = """\
127enum foo {
128 OFP_A = 1,
129 OFP_B = 2,
130 OFP_C = 3,
131};
132"""
133 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700134 self.assertEquals(ast, [['enum', 'foo', [['OFP_A', 1], ['OFP_B', 2], ['OFP_C', 3]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700135
Rich Lanea06d0c32013-03-25 08:52:03 -0700136class TestMetadata(unittest.TestCase):
137 def test_version(self):
138 src = """\
139#version 1
140"""
141 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700142 self.assertEquals(ast, [['metadata', 'version', '1']])
Rich Lanea06d0c32013-03-25 08:52:03 -0700143
144class TestToplevel(unittest.TestCase):
145 def test_multiple_structs(self):
146 src = """\
147struct foo { };
148struct bar { };
149"""
150 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700151 self.assertEquals(ast,
Rich Lanea06d0c32013-03-25 08:52:03 -0700152 [['struct', 'foo', []], ['struct', 'bar', []]])
153
154 def test_comments(self):
155 src = """\
156// comment 1
157struct foo { //comment 2
158// comment 3
Andreas Wundsam53256162013-05-02 14:05:53 -0700159 uint32_t a; //comment 5
Rich Lanea06d0c32013-03-25 08:52:03 -0700160// comment 6
161};
162// comment 4
163"""
164 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700165 self.assertEquals(ast,
Rich Lanea06d0c32013-03-25 08:52:03 -0700166 [['struct', 'foo', [['uint32_t', 'a']]]])
167
168 def test_mixed(self):
169 src = """\
170#version 1
171struct foo { };
172#version 2
173struct bar { };
174"""
175 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700176 self.assertEquals(ast,
Rich Lanea06d0c32013-03-25 08:52:03 -0700177 [['metadata', 'version', '1'],
178 ['struct', 'foo', []],
179 ['metadata', 'version', '2'],
180 ['struct', 'bar', []]])
181
182class TestErrors(unittest.TestCase):
183 def syntax_error(self, src, regex):
184 with self.assertRaisesRegexp(pyparsing.ParseSyntaxException, regex):
185 parser.parse(src)
186
187 def test_missing_struct_syntax(self):
188 self.syntax_error('struct { uint32_t bar; };',
189 'Expected identifier \(at char 7\)')
190 self.syntax_error('struct foo uint32_t bar; };',
191 'Expected "{" \(at char 11\)')
192 self.syntax_error('struct foo { uint32_t bar; ;',
193 'Expected "}" \(at char 27\)')
194 self.syntax_error('struct foo { uint32_t bar; }',
195 'Expected ";" \(at char 28\)')
196
197 def test_invalid_type_name(self):
198 self.syntax_error('struct foo { list<of_action_t> bar; }',
199 'Expected "\(" \(at char 17\)')
200 self.syntax_error('struct foo { uint32_t[10 bar; }',
201 'Expected "\]" \(at char 24\)')
202
203 def test_invalid_member_syntax(self):
204 self.syntax_error('struct foo { bar; }',
205 'Expected identifier \(at char 16\)')
206 self.syntax_error('struct foo { uint32_t bar baz; }',
207 'Expected ";" \(at char 26\)')
208
209
210if __name__ == '__main__':
211 unittest.main()