blob: c24665cad9b74620e5a64bec1b80c444868bc028 [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
Rich Lane569338c2013-06-17 15:51:24 -070029import sys
30import os
Rich Lanea06d0c32013-03-25 08:52:03 -070031import unittest
Rich Lane569338c2013-06-17 15:51:24 -070032
33root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
34sys.path.insert(0, root_dir)
35
Rich Lanea06d0c32013-03-25 08:52:03 -070036import pyparsing
37import loxi_front_end.parser as parser
38
39class StructTests(unittest.TestCase):
40 def test_empty(self):
41 src = """\
42struct foo { };
43"""
44 ast = parser.parse(src)
Rich Lanebd431502013-06-21 16:30:20 -070045 self.assertEquals(ast, [['struct', 'foo', None, []]])
Rich Lanea06d0c32013-03-25 08:52:03 -070046
47 def test_one_field(self):
48 src = """\
49struct foo {
50 uint32_t bar;
51};
52"""
53 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -070054 self.assertEquals(ast,
Rich Lanebd431502013-06-21 16:30:20 -070055 [['struct', 'foo', None, [['data', 'uint32_t', 'bar']]]])
Rich Lanea06d0c32013-03-25 08:52:03 -070056
57 def test_multiple_fields(self):
58 src = """\
59struct foo {
60 uint32_t bar;
61 uint8_t baz;
62 uint64_t abc;
63};
64"""
65 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -070066 self.assertEquals(ast,
Rich Lanebd431502013-06-21 16:30:20 -070067 [['struct', 'foo', None,
Rich Lanef424e972013-05-09 21:00:13 -070068 [['data', 'uint32_t', 'bar'],
69 ['data', 'uint8_t', 'baz'],
70 ['data', 'uint64_t', 'abc']]]])
Rich Lanea06d0c32013-03-25 08:52:03 -070071
72 def test_array_type(self):
73 src = """\
74struct foo {
75 uint32_t[4] bar;
76};
77"""
78 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -070079 self.assertEquals(ast,
Rich Lanebd431502013-06-21 16:30:20 -070080 [['struct', 'foo', None, [['data', 'uint32_t[4]', 'bar']]]])
Rich Lanea06d0c32013-03-25 08:52:03 -070081
82 def test_list_type(self):
83 src = """\
84struct foo {
85 list(of_action_t) bar;
86};
87"""
88 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -070089 self.assertEquals(ast,
Rich Lanebd431502013-06-21 16:30:20 -070090 [['struct', 'foo', None, [['data', 'list(of_action_t)', 'bar']]]])
Rich Lanea06d0c32013-03-25 08:52:03 -070091
Rich Lane32142872013-05-09 21:16:47 -070092 def test_pad_member(self):
Rich Lanee4d04ea2013-05-09 11:27:06 -070093 src = """\
94struct foo {
95 pad(1);
96};
97"""
98 ast = parser.parse(src)
99 self.assertEquals(ast,
Rich Lanebd431502013-06-21 16:30:20 -0700100 [['struct', 'foo', None, [['pad', 1]]]])
Rich Lanee4d04ea2013-05-09 11:27:06 -0700101
Rich Lane32142872013-05-09 21:16:47 -0700102 def test_type_member(self):
103 src = """\
104struct foo {
105 uint16_t foo == 0x10;
106};
107"""
108 ast = parser.parse(src)
109 self.assertEquals(ast,
Rich Lanebd431502013-06-21 16:30:20 -0700110 [['struct', 'foo', None, [['type', 'uint16_t', 'foo', 0x10]]]])
111
112 def test_inheritance(self):
113 src = """\
114struct foo : bar {
115 uint16_t foo == 0x10;
116};
117"""
118 ast = parser.parse(src)
119 self.assertEquals(ast,
120 [['struct', 'foo', 'bar', [['type', 'uint16_t', 'foo', 0x10]]]])
Rich Lane32142872013-05-09 21:16:47 -0700121
Rich Lane517506c2013-04-08 14:08:31 -0700122class EnumTests(unittest.TestCase):
123 def test_empty(self):
124 src = """\
125enum foo {
126};
127"""
128 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700129 self.assertEquals(ast, [['enum', 'foo', []]])
Rich Lane517506c2013-04-08 14:08:31 -0700130
131 def test_one(self):
132 src = """\
133enum foo {
134 BAR = 1
135};
136"""
137 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700138 self.assertEquals(ast, [['enum', 'foo', [['BAR', 1]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700139
140 def test_multiple(self):
141 src = """\
142enum foo {
143 OFP_A = 1,
144 OFP_B = 2,
145 OFP_C = 3
146};
147"""
148 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700149 self.assertEquals(ast, [['enum', 'foo', [['OFP_A', 1], ['OFP_B', 2], ['OFP_C', 3]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700150
151 def test_trailing_comma(self):
152 src = """\
153enum foo {
154 OFP_A = 1,
155 OFP_B = 2,
156 OFP_C = 3,
157};
158"""
159 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700160 self.assertEquals(ast, [['enum', 'foo', [['OFP_A', 1], ['OFP_B', 2], ['OFP_C', 3]]]])
Rich Lane517506c2013-04-08 14:08:31 -0700161
Rich Lanea06d0c32013-03-25 08:52:03 -0700162class TestMetadata(unittest.TestCase):
163 def test_version(self):
164 src = """\
165#version 1
166"""
167 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700168 self.assertEquals(ast, [['metadata', 'version', '1']])
Rich Lanea06d0c32013-03-25 08:52:03 -0700169
170class TestToplevel(unittest.TestCase):
171 def test_multiple_structs(self):
172 src = """\
173struct foo { };
174struct bar { };
175"""
176 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700177 self.assertEquals(ast,
Rich Lanebd431502013-06-21 16:30:20 -0700178 [['struct', 'foo', None, []], ['struct', 'bar', None, []]])
Rich Lanea06d0c32013-03-25 08:52:03 -0700179
180 def test_comments(self):
181 src = """\
182// comment 1
183struct foo { //comment 2
184// comment 3
Andreas Wundsam53256162013-05-02 14:05:53 -0700185 uint32_t a; //comment 5
Rich Lanea06d0c32013-03-25 08:52:03 -0700186// comment 6
187};
188// comment 4
189"""
190 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700191 self.assertEquals(ast,
Rich Lanebd431502013-06-21 16:30:20 -0700192 [['struct', 'foo', None, [['data', 'uint32_t', 'a']]]])
Rich Lanea06d0c32013-03-25 08:52:03 -0700193
194 def test_mixed(self):
195 src = """\
196#version 1
197struct foo { };
198#version 2
199struct bar { };
200"""
201 ast = parser.parse(src)
Rich Lane43b2a902013-05-09 13:47:07 -0700202 self.assertEquals(ast,
Rich Lanea06d0c32013-03-25 08:52:03 -0700203 [['metadata', 'version', '1'],
Rich Lanebd431502013-06-21 16:30:20 -0700204 ['struct', 'foo', None, []],
Rich Lanea06d0c32013-03-25 08:52:03 -0700205 ['metadata', 'version', '2'],
Rich Lanebd431502013-06-21 16:30:20 -0700206 ['struct', 'bar', None, []]])
Rich Lanea06d0c32013-03-25 08:52:03 -0700207
208class TestErrors(unittest.TestCase):
209 def syntax_error(self, src, regex):
210 with self.assertRaisesRegexp(pyparsing.ParseSyntaxException, regex):
211 parser.parse(src)
212
213 def test_missing_struct_syntax(self):
214 self.syntax_error('struct { uint32_t bar; };',
215 'Expected identifier \(at char 7\)')
216 self.syntax_error('struct foo uint32_t bar; };',
217 'Expected "{" \(at char 11\)')
218 self.syntax_error('struct foo { uint32_t bar; ;',
219 'Expected "}" \(at char 27\)')
220 self.syntax_error('struct foo { uint32_t bar; }',
221 'Expected ";" \(at char 28\)')
222
223 def test_invalid_type_name(self):
224 self.syntax_error('struct foo { list<of_action_t> bar; }',
225 'Expected "\(" \(at char 17\)')
226 self.syntax_error('struct foo { uint32_t[10 bar; }',
227 'Expected "\]" \(at char 24\)')
228
229 def test_invalid_member_syntax(self):
230 self.syntax_error('struct foo { bar; }',
231 'Expected identifier \(at char 16\)')
232 self.syntax_error('struct foo { uint32_t bar baz; }',
233 'Expected ";" \(at char 26\)')
234
235
236if __name__ == '__main__':
237 unittest.main()