Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | #!/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 Lane | 569338c | 2013-06-17 15:51:24 -0700 | [diff] [blame] | 29 | import sys |
| 30 | import os |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 31 | import unittest |
Rich Lane | 569338c | 2013-06-17 15:51:24 -0700 | [diff] [blame] | 32 | |
| 33 | root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..') |
| 34 | sys.path.insert(0, root_dir) |
| 35 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 36 | import pyparsing |
| 37 | import loxi_front_end.parser as parser |
| 38 | |
| 39 | class StructTests(unittest.TestCase): |
| 40 | def test_empty(self): |
| 41 | src = """\ |
| 42 | struct foo { }; |
| 43 | """ |
| 44 | ast = parser.parse(src) |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 45 | self.assertEquals(ast, [['struct', 'foo', [], None, []]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 46 | |
| 47 | def test_one_field(self): |
| 48 | src = """\ |
| 49 | struct foo { |
| 50 | uint32_t bar; |
| 51 | }; |
| 52 | """ |
| 53 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 54 | self.assertEquals(ast, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 55 | [['struct', 'foo', [], None, [['data', ['scalar', 'uint32_t'], 'bar']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 56 | |
Andreas Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 57 | def test_struct_align_arg(self): |
| 58 | src = """\ |
| 59 | struct foo(align=8) { |
| 60 | uint32_t bar; |
| 61 | }; |
| 62 | """ |
| 63 | ast = parser.parse(src) |
| 64 | self.assertEquals(ast, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 65 | [['struct', 'foo', [['align', '8']], None, [['data', ['scalar', 'uint32_t'], 'bar']]]]) |
Andreas Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 66 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 67 | def test_multiple_fields(self): |
| 68 | src = """\ |
| 69 | struct foo { |
| 70 | uint32_t bar; |
| 71 | uint8_t baz; |
| 72 | uint64_t abc; |
| 73 | }; |
| 74 | """ |
| 75 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 76 | self.assertEquals(ast, |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 77 | [['struct', 'foo', [], None, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 78 | [['data', ['scalar', 'uint32_t'], 'bar'], |
| 79 | ['data', ['scalar', 'uint8_t'], 'baz'], |
| 80 | ['data', ['scalar', 'uint64_t'], 'abc']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 81 | |
| 82 | def test_array_type(self): |
| 83 | src = """\ |
| 84 | struct foo { |
| 85 | uint32_t[4] bar; |
| 86 | }; |
| 87 | """ |
| 88 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 89 | self.assertEquals(ast, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 90 | [['struct', 'foo', [], None, [['data', ['array', 'uint32_t[4]'], 'bar']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 91 | |
| 92 | def test_list_type(self): |
| 93 | src = """\ |
| 94 | struct foo { |
| 95 | list(of_action_t) bar; |
| 96 | }; |
| 97 | """ |
| 98 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 99 | self.assertEquals(ast, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 100 | [['struct', 'foo', [], None, [['data', ['list', 'list(of_action_t)'], 'bar']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 101 | |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 102 | def test_pad_member(self): |
Rich Lane | e4d04ea | 2013-05-09 11:27:06 -0700 | [diff] [blame] | 103 | src = """\ |
| 104 | struct foo { |
| 105 | pad(1); |
| 106 | }; |
| 107 | """ |
| 108 | ast = parser.parse(src) |
| 109 | self.assertEquals(ast, |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 110 | [['struct', 'foo', [], None, [['pad', 1]]]]) |
Rich Lane | e4d04ea | 2013-05-09 11:27:06 -0700 | [diff] [blame] | 111 | |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 112 | def test_type_member(self): |
| 113 | src = """\ |
| 114 | struct foo { |
| 115 | uint16_t foo == 0x10; |
| 116 | }; |
| 117 | """ |
| 118 | ast = parser.parse(src) |
| 119 | self.assertEquals(ast, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 120 | [['struct', 'foo', [], None, [['type', ['scalar', 'uint16_t'], 'foo', 0x10]]]]) |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 121 | |
| 122 | def test_inheritance(self): |
| 123 | src = """\ |
| 124 | struct foo : bar { |
| 125 | uint16_t foo == 0x10; |
| 126 | }; |
| 127 | """ |
| 128 | ast = parser.parse(src) |
| 129 | self.assertEquals(ast, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 130 | [['struct', 'foo', [], 'bar', [['type', ['scalar', 'uint16_t'], 'foo', 0x10]]]]) |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 131 | |
Andreas Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 132 | def test_discriminator(self): |
| 133 | src = """\ |
| 134 | struct foo { |
| 135 | uint16_t foo == ?; |
| 136 | }; |
| 137 | """ |
| 138 | ast = parser.parse(src) |
| 139 | self.assertEquals(ast, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 140 | [['struct', 'foo', [], None, [['discriminator', ['scalar', 'uint16_t'], 'foo']]]]) |
Andreas Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 141 | |
Murat Parlakisik | 3486338 | 2016-12-05 00:53:17 -0800 | [diff] [blame^] | 142 | def test_field_length(self): |
| 143 | src = """\ |
| 144 | struct foo { |
| 145 | uint16_t list_len == length(list); |
| 146 | list(of_uint32_t) list; |
| 147 | }; |
| 148 | """ |
| 149 | ast = parser.parse(src) |
| 150 | self.assertEquals(ast, |
| 151 | [['struct', 'foo', [], None, [ |
| 152 | ['field_length', ['scalar', 'uint16_t'], 'list_len', 'list'], |
| 153 | ['data', ['list', 'list(of_uint32_t)'], 'list']]]]) |
| 154 | |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 155 | class EnumTests(unittest.TestCase): |
| 156 | def test_empty(self): |
| 157 | src = """\ |
| 158 | enum foo { |
| 159 | }; |
| 160 | """ |
| 161 | ast = parser.parse(src) |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 162 | self.assertEquals(ast, [['enum', 'foo', [], []]]) |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 163 | |
| 164 | def test_one(self): |
| 165 | src = """\ |
| 166 | enum foo { |
| 167 | BAR = 1 |
| 168 | }; |
| 169 | """ |
| 170 | ast = parser.parse(src) |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 171 | self.assertEquals(ast, [['enum', 'foo', [], [['BAR', [], 1]]]]) |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 172 | |
Andreas Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 173 | def test_params(self): |
| 174 | src = """\ |
| 175 | enum foo(wire_type=uint32, bitmask=False, complete=False) { |
| 176 | BAR = 1 |
| 177 | }; |
| 178 | """ |
| 179 | ast = parser.parse(src) |
| 180 | self.assertEquals(ast, [['enum', 'foo', |
| 181 | [ ['wire_type', 'uint32'], ['bitmask','False'], ['complete', 'False']], |
| 182 | [['BAR', [], 1]]]]) |
| 183 | |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 184 | def test_multiple(self): |
| 185 | src = """\ |
| 186 | enum foo { |
| 187 | OFP_A = 1, |
| 188 | OFP_B = 2, |
| 189 | OFP_C = 3 |
| 190 | }; |
| 191 | """ |
| 192 | ast = parser.parse(src) |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 193 | self.assertEquals(ast, [['enum', 'foo', [], [['OFP_A', [], 1], ['OFP_B', [], 2], ['OFP_C', [], 3]]]]) |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 194 | |
| 195 | def test_trailing_comma(self): |
| 196 | src = """\ |
| 197 | enum foo { |
| 198 | OFP_A = 1, |
| 199 | OFP_B = 2, |
| 200 | OFP_C = 3, |
| 201 | }; |
| 202 | """ |
| 203 | ast = parser.parse(src) |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 204 | self.assertEquals(ast, [['enum', 'foo', [], [['OFP_A', [], 1], ['OFP_B', [], 2], ['OFP_C', [], 3]]]]) |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 205 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 206 | class TestMetadata(unittest.TestCase): |
| 207 | def test_version(self): |
| 208 | src = """\ |
| 209 | #version 1 |
| 210 | """ |
| 211 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 212 | self.assertEquals(ast, [['metadata', 'version', '1']]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 213 | |
| 214 | class TestToplevel(unittest.TestCase): |
| 215 | def test_multiple_structs(self): |
| 216 | src = """\ |
| 217 | struct foo { }; |
| 218 | struct bar { }; |
| 219 | """ |
| 220 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 221 | self.assertEquals(ast, |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 222 | [['struct', 'foo', [], None, []], ['struct', 'bar', [], None, []]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 223 | |
| 224 | def test_comments(self): |
| 225 | src = """\ |
| 226 | // comment 1 |
| 227 | struct foo { //comment 2 |
| 228 | // comment 3 |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 229 | uint32_t a; //comment 5 |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 230 | // comment 6 |
| 231 | }; |
| 232 | // comment 4 |
| 233 | """ |
| 234 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 235 | self.assertEquals(ast, |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 236 | [['struct', 'foo', [], None, [['data', ['scalar', 'uint32_t'], 'a']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 237 | |
| 238 | def test_mixed(self): |
| 239 | src = """\ |
| 240 | #version 1 |
| 241 | struct foo { }; |
| 242 | #version 2 |
| 243 | struct bar { }; |
| 244 | """ |
| 245 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 246 | self.assertEquals(ast, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 247 | [['metadata', 'version', '1'], |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 248 | ['struct', 'foo', [], None, []], |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 249 | ['metadata', 'version', '2'], |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 250 | ['struct', 'bar', [], None, []]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 251 | |
| 252 | class TestErrors(unittest.TestCase): |
| 253 | def syntax_error(self, src, regex): |
| 254 | with self.assertRaisesRegexp(pyparsing.ParseSyntaxException, regex): |
| 255 | parser.parse(src) |
| 256 | |
| 257 | def test_missing_struct_syntax(self): |
| 258 | self.syntax_error('struct { uint32_t bar; };', |
| 259 | 'Expected identifier \(at char 7\)') |
| 260 | self.syntax_error('struct foo uint32_t bar; };', |
| 261 | 'Expected "{" \(at char 11\)') |
| 262 | self.syntax_error('struct foo { uint32_t bar; ;', |
| 263 | 'Expected "}" \(at char 27\)') |
| 264 | self.syntax_error('struct foo { uint32_t bar; }', |
| 265 | 'Expected ";" \(at char 28\)') |
| 266 | |
| 267 | def test_invalid_type_name(self): |
| 268 | self.syntax_error('struct foo { list<of_action_t> bar; }', |
| 269 | 'Expected "\(" \(at char 17\)') |
| 270 | self.syntax_error('struct foo { uint32_t[10 bar; }', |
| 271 | 'Expected "\]" \(at char 24\)') |
| 272 | |
| 273 | def test_invalid_member_syntax(self): |
| 274 | self.syntax_error('struct foo { bar; }', |
| 275 | 'Expected identifier \(at char 16\)') |
| 276 | self.syntax_error('struct foo { uint32_t bar baz; }', |
| 277 | 'Expected ";" \(at char 26\)') |
| 278 | |
| 279 | |
| 280 | if __name__ == '__main__': |
| 281 | unittest.main() |