blob: cb22a9c2ee2e200a1849496fe10505b2e62e5dbd [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2013, Big Switch Networks, Inc.
#
# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
# the following special exception:
#
# LOXI Exception
#
# As a special exception to the terms of the EPL, you may distribute libraries
# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
# that copyright and licensing notices generated by LoxiGen are not altered or removed
# from the LoxiGen Libraries and the notice provided below is (i) included in
# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
# documentation for the LoxiGen Libraries, if distributed in binary form.
#
# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
#
# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
# a copy of the EPL at:
#
# http://www.eclipse.org/legal/epl-v10.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# EPL for the specific language governing permissions and limitations
# under the EPL.
import unittest
import pyparsing
import loxi_front_end.parser as parser
class StructTests(unittest.TestCase):
def test_empty(self):
src = """\
struct foo { };
"""
ast = parser.parse(src)
self.assertEquals(ast, [['struct', 'foo', []]])
def test_one_field(self):
src = """\
struct foo {
uint32_t bar;
};
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['struct', 'foo', [['data', 'uint32_t', 'bar']]]])
def test_multiple_fields(self):
src = """\
struct foo {
uint32_t bar;
uint8_t baz;
uint64_t abc;
};
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['struct', 'foo',
[['data', 'uint32_t', 'bar'],
['data', 'uint8_t', 'baz'],
['data', 'uint64_t', 'abc']]]])
def test_array_type(self):
src = """\
struct foo {
uint32_t[4] bar;
};
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['struct', 'foo', [['data', 'uint32_t[4]', 'bar']]]])
def test_list_type(self):
src = """\
struct foo {
list(of_action_t) bar;
};
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['struct', 'foo', [['data', 'list(of_action_t)', 'bar']]]])
def test_pad_member(self):
src = """\
struct foo {
pad(1);
};
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['struct', 'foo', [['pad', 1]]]])
def test_type_member(self):
src = """\
struct foo {
uint16_t foo == 0x10;
};
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['struct', 'foo', [['type', 'uint16_t', 'foo', 0x10]]]])
class EnumTests(unittest.TestCase):
def test_empty(self):
src = """\
enum foo {
};
"""
ast = parser.parse(src)
self.assertEquals(ast, [['enum', 'foo', []]])
def test_one(self):
src = """\
enum foo {
BAR = 1
};
"""
ast = parser.parse(src)
self.assertEquals(ast, [['enum', 'foo', [['BAR', 1]]]])
def test_multiple(self):
src = """\
enum foo {
OFP_A = 1,
OFP_B = 2,
OFP_C = 3
};
"""
ast = parser.parse(src)
self.assertEquals(ast, [['enum', 'foo', [['OFP_A', 1], ['OFP_B', 2], ['OFP_C', 3]]]])
def test_trailing_comma(self):
src = """\
enum foo {
OFP_A = 1,
OFP_B = 2,
OFP_C = 3,
};
"""
ast = parser.parse(src)
self.assertEquals(ast, [['enum', 'foo', [['OFP_A', 1], ['OFP_B', 2], ['OFP_C', 3]]]])
class TestMetadata(unittest.TestCase):
def test_version(self):
src = """\
#version 1
"""
ast = parser.parse(src)
self.assertEquals(ast, [['metadata', 'version', '1']])
class TestToplevel(unittest.TestCase):
def test_multiple_structs(self):
src = """\
struct foo { };
struct bar { };
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['struct', 'foo', []], ['struct', 'bar', []]])
def test_comments(self):
src = """\
// comment 1
struct foo { //comment 2
// comment 3
uint32_t a; //comment 5
// comment 6
};
// comment 4
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['struct', 'foo', [['data', 'uint32_t', 'a']]]])
def test_mixed(self):
src = """\
#version 1
struct foo { };
#version 2
struct bar { };
"""
ast = parser.parse(src)
self.assertEquals(ast,
[['metadata', 'version', '1'],
['struct', 'foo', []],
['metadata', 'version', '2'],
['struct', 'bar', []]])
class TestErrors(unittest.TestCase):
def syntax_error(self, src, regex):
with self.assertRaisesRegexp(pyparsing.ParseSyntaxException, regex):
parser.parse(src)
def test_missing_struct_syntax(self):
self.syntax_error('struct { uint32_t bar; };',
'Expected identifier \(at char 7\)')
self.syntax_error('struct foo uint32_t bar; };',
'Expected "{" \(at char 11\)')
self.syntax_error('struct foo { uint32_t bar; ;',
'Expected "}" \(at char 27\)')
self.syntax_error('struct foo { uint32_t bar; }',
'Expected ";" \(at char 28\)')
def test_invalid_type_name(self):
self.syntax_error('struct foo { list<of_action_t> bar; }',
'Expected "\(" \(at char 17\)')
self.syntax_error('struct foo { uint32_t[10 bar; }',
'Expected "\]" \(at char 24\)')
def test_invalid_member_syntax(self):
self.syntax_error('struct foo { bar; }',
'Expected identifier \(at char 16\)')
self.syntax_error('struct foo { uint32_t bar baz; }',
'Expected ";" \(at char 26\)')
if __name__ == '__main__':
unittest.main()