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 | |
| 29 | import unittest |
| 30 | import pyparsing |
| 31 | import loxi_front_end.parser as parser |
| 32 | |
| 33 | class StructTests(unittest.TestCase): |
| 34 | def test_empty(self): |
| 35 | src = """\ |
| 36 | struct foo { }; |
| 37 | """ |
| 38 | ast = parser.parse(src) |
| 39 | self.assertEquals(ast.asList(), [['struct', 'foo', []]]) |
| 40 | |
| 41 | def test_one_field(self): |
| 42 | src = """\ |
| 43 | struct 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 = """\ |
| 53 | struct 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 = """\ |
| 68 | struct 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 = """\ |
| 78 | struct 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 | |
| 86 | class TestMetadata(unittest.TestCase): |
| 87 | def test_version(self): |
| 88 | src = """\ |
| 89 | #version 1 |
| 90 | """ |
| 91 | ast = parser.parse(src) |
| 92 | self.assertEquals(ast.asList(), [['metadata', 'version', '1']]) |
| 93 | |
| 94 | class TestToplevel(unittest.TestCase): |
| 95 | def test_multiple_structs(self): |
| 96 | src = """\ |
| 97 | struct foo { }; |
| 98 | struct bar { }; |
| 99 | """ |
| 100 | ast = parser.parse(src) |
| 101 | self.assertEquals(ast.asList(), |
| 102 | [['struct', 'foo', []], ['struct', 'bar', []]]) |
| 103 | |
| 104 | def test_comments(self): |
| 105 | src = """\ |
| 106 | // comment 1 |
| 107 | struct foo { //comment 2 |
| 108 | // comment 3 |
| 109 | uint32_t a; //comment 5 |
| 110 | // comment 6 |
| 111 | }; |
| 112 | // comment 4 |
| 113 | """ |
| 114 | ast = parser.parse(src) |
| 115 | self.assertEquals(ast.asList(), |
| 116 | [['struct', 'foo', [['uint32_t', 'a']]]]) |
| 117 | |
| 118 | def test_mixed(self): |
| 119 | src = """\ |
| 120 | #version 1 |
| 121 | struct foo { }; |
| 122 | #version 2 |
| 123 | struct bar { }; |
| 124 | """ |
| 125 | ast = parser.parse(src) |
| 126 | self.assertEquals(ast.asList(), |
| 127 | [['metadata', 'version', '1'], |
| 128 | ['struct', 'foo', []], |
| 129 | ['metadata', 'version', '2'], |
| 130 | ['struct', 'bar', []]]) |
| 131 | |
| 132 | class TestErrors(unittest.TestCase): |
| 133 | def syntax_error(self, src, regex): |
| 134 | with self.assertRaisesRegexp(pyparsing.ParseSyntaxException, regex): |
| 135 | parser.parse(src) |
| 136 | |
| 137 | def test_missing_struct_syntax(self): |
| 138 | self.syntax_error('struct { uint32_t bar; };', |
| 139 | 'Expected identifier \(at char 7\)') |
| 140 | self.syntax_error('struct foo uint32_t bar; };', |
| 141 | 'Expected "{" \(at char 11\)') |
| 142 | self.syntax_error('struct foo { uint32_t bar; ;', |
| 143 | 'Expected "}" \(at char 27\)') |
| 144 | self.syntax_error('struct foo { uint32_t bar; }', |
| 145 | 'Expected ";" \(at char 28\)') |
| 146 | |
| 147 | def test_invalid_type_name(self): |
| 148 | self.syntax_error('struct foo { list<of_action_t> bar; }', |
| 149 | 'Expected "\(" \(at char 17\)') |
| 150 | self.syntax_error('struct foo { uint32_t[10 bar; }', |
| 151 | 'Expected "\]" \(at char 24\)') |
| 152 | |
| 153 | def test_invalid_member_syntax(self): |
| 154 | self.syntax_error('struct foo { bar; }', |
| 155 | 'Expected identifier \(at char 16\)') |
| 156 | self.syntax_error('struct foo { uint32_t bar baz; }', |
| 157 | 'Expected ";" \(at char 26\)') |
| 158 | |
| 159 | |
| 160 | if __name__ == '__main__': |
| 161 | unittest.main() |