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) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 45 | self.assertEquals(ast, [['struct', 'foo', []]]) |
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, |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 55 | [['struct', 'foo', [['data', 'uint32_t', 'bar']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 56 | |
| 57 | def test_multiple_fields(self): |
| 58 | src = """\ |
| 59 | struct foo { |
| 60 | uint32_t bar; |
| 61 | uint8_t baz; |
| 62 | uint64_t abc; |
| 63 | }; |
| 64 | """ |
| 65 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 66 | self.assertEquals(ast, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 67 | [['struct', 'foo', |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 68 | [['data', 'uint32_t', 'bar'], |
| 69 | ['data', 'uint8_t', 'baz'], |
| 70 | ['data', 'uint64_t', 'abc']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 71 | |
| 72 | def test_array_type(self): |
| 73 | src = """\ |
| 74 | struct foo { |
| 75 | uint32_t[4] bar; |
| 76 | }; |
| 77 | """ |
| 78 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 79 | self.assertEquals(ast, |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 80 | [['struct', 'foo', [['data', 'uint32_t[4]', 'bar']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 81 | |
| 82 | def test_list_type(self): |
| 83 | src = """\ |
| 84 | struct foo { |
| 85 | list(of_action_t) bar; |
| 86 | }; |
| 87 | """ |
| 88 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 89 | self.assertEquals(ast, |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 90 | [['struct', 'foo', [['data', 'list(of_action_t)', 'bar']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 91 | |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 92 | def test_pad_member(self): |
Rich Lane | e4d04ea | 2013-05-09 11:27:06 -0700 | [diff] [blame] | 93 | src = """\ |
| 94 | struct foo { |
| 95 | pad(1); |
| 96 | }; |
| 97 | """ |
| 98 | ast = parser.parse(src) |
| 99 | self.assertEquals(ast, |
| 100 | [['struct', 'foo', [['pad', 1]]]]) |
| 101 | |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 102 | def test_type_member(self): |
| 103 | src = """\ |
| 104 | struct foo { |
| 105 | uint16_t foo == 0x10; |
| 106 | }; |
| 107 | """ |
| 108 | ast = parser.parse(src) |
| 109 | self.assertEquals(ast, |
| 110 | [['struct', 'foo', [['type', 'uint16_t', 'foo', 0x10]]]]) |
| 111 | |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 112 | class EnumTests(unittest.TestCase): |
| 113 | def test_empty(self): |
| 114 | src = """\ |
| 115 | enum foo { |
| 116 | }; |
| 117 | """ |
| 118 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 119 | self.assertEquals(ast, [['enum', 'foo', []]]) |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 120 | |
| 121 | def test_one(self): |
| 122 | src = """\ |
| 123 | enum foo { |
| 124 | BAR = 1 |
| 125 | }; |
| 126 | """ |
| 127 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 128 | self.assertEquals(ast, [['enum', 'foo', [['BAR', 1]]]]) |
Rich Lane | 517506c | 2013-04-08 14:08:31 -0700 | [diff] [blame] | 129 | |
| 130 | def test_multiple(self): |
| 131 | src = """\ |
| 132 | enum foo { |
| 133 | OFP_A = 1, |
| 134 | OFP_B = 2, |
| 135 | OFP_C = 3 |
| 136 | }; |
| 137 | """ |
| 138 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 139 | 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] | 140 | |
| 141 | def test_trailing_comma(self): |
| 142 | src = """\ |
| 143 | enum foo { |
| 144 | OFP_A = 1, |
| 145 | OFP_B = 2, |
| 146 | OFP_C = 3, |
| 147 | }; |
| 148 | """ |
| 149 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 150 | 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] | 151 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 152 | class TestMetadata(unittest.TestCase): |
| 153 | def test_version(self): |
| 154 | src = """\ |
| 155 | #version 1 |
| 156 | """ |
| 157 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 158 | self.assertEquals(ast, [['metadata', 'version', '1']]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 159 | |
| 160 | class TestToplevel(unittest.TestCase): |
| 161 | def test_multiple_structs(self): |
| 162 | src = """\ |
| 163 | struct foo { }; |
| 164 | struct bar { }; |
| 165 | """ |
| 166 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 167 | self.assertEquals(ast, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 168 | [['struct', 'foo', []], ['struct', 'bar', []]]) |
| 169 | |
| 170 | def test_comments(self): |
| 171 | src = """\ |
| 172 | // comment 1 |
| 173 | struct foo { //comment 2 |
| 174 | // comment 3 |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 175 | uint32_t a; //comment 5 |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 176 | // comment 6 |
| 177 | }; |
| 178 | // comment 4 |
| 179 | """ |
| 180 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 181 | self.assertEquals(ast, |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 182 | [['struct', 'foo', [['data', 'uint32_t', 'a']]]]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 183 | |
| 184 | def test_mixed(self): |
| 185 | src = """\ |
| 186 | #version 1 |
| 187 | struct foo { }; |
| 188 | #version 2 |
| 189 | struct bar { }; |
| 190 | """ |
| 191 | ast = parser.parse(src) |
Rich Lane | 43b2a90 | 2013-05-09 13:47:07 -0700 | [diff] [blame] | 192 | self.assertEquals(ast, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 193 | [['metadata', 'version', '1'], |
| 194 | ['struct', 'foo', []], |
| 195 | ['metadata', 'version', '2'], |
| 196 | ['struct', 'bar', []]]) |
| 197 | |
| 198 | class TestErrors(unittest.TestCase): |
| 199 | def syntax_error(self, src, regex): |
| 200 | with self.assertRaisesRegexp(pyparsing.ParseSyntaxException, regex): |
| 201 | parser.parse(src) |
| 202 | |
| 203 | def test_missing_struct_syntax(self): |
| 204 | self.syntax_error('struct { uint32_t bar; };', |
| 205 | 'Expected identifier \(at char 7\)') |
| 206 | self.syntax_error('struct foo uint32_t bar; };', |
| 207 | 'Expected "{" \(at char 11\)') |
| 208 | self.syntax_error('struct foo { uint32_t bar; ;', |
| 209 | 'Expected "}" \(at char 27\)') |
| 210 | self.syntax_error('struct foo { uint32_t bar; }', |
| 211 | 'Expected ";" \(at char 28\)') |
| 212 | |
| 213 | def test_invalid_type_name(self): |
| 214 | self.syntax_error('struct foo { list<of_action_t> bar; }', |
| 215 | 'Expected "\(" \(at char 17\)') |
| 216 | self.syntax_error('struct foo { uint32_t[10 bar; }', |
| 217 | 'Expected "\]" \(at char 24\)') |
| 218 | |
| 219 | def test_invalid_member_syntax(self): |
| 220 | self.syntax_error('struct foo { bar; }', |
| 221 | 'Expected identifier \(at char 16\)') |
| 222 | self.syntax_error('struct foo { uint32_t bar baz; }', |
| 223 | 'Expected ";" \(at char 26\)') |
| 224 | |
| 225 | |
| 226 | if __name__ == '__main__': |
| 227 | unittest.main() |