1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright 2017, Data61 6# Commonwealth Scientific and Industrial Research Organisation (CSIRO) 7# ABN 41 687 119 230. 8# 9# This software may be distributed and modified according to the terms of 10# the BSD 2-Clause license. Note that NO WARRANTY is provided. 11# See "LICENSE_BSD2.txt" for details. 12# 13# @TAG(DATA61_BSD) 14# 15 16from __future__ import absolute_import, division, print_function, \ 17 unicode_literals 18 19import os, six, sys, unittest 20 21ME = os.path.abspath(__file__) 22 23# Make CAmkES importable 24sys.path.append(os.path.join(os.path.dirname(ME), '../../..')) 25 26from camkes.ast import Assembly, Component, Instance 27from camkes.internal.tests.utils import CAmkESTest 28from camkes.parser.stage0 import CPP, Reader 29from camkes.parser.stage1 import Parse1 30from camkes.parser.stage2 import Parse2 31from camkes.parser.stage3 import Parse3 32from camkes.parser.stage4 import Parse4 33from camkes.parser.stage5 import Parse5 34from camkes.parser.stage6 import Parse6 35 36class TestStage6(CAmkESTest): 37 def setUp(self): 38 super(TestStage6, self).setUp() 39 r = Reader() 40 s1 = Parse1(r) 41 s2 = Parse2(s1) 42 s3 = Parse3(s2, debug=True) 43 s4 = Parse4(s3) 44 s5 = Parse5(s4) 45 self.parser = Parse6(s5) 46 47 def test_basic(self): 48 ast, _ = self.parser.parse_string(''' 49 component A {} 50 assembly { 51 composition { 52 component A a; 53 component A b; 54 } 55 } 56 ''') 57 58 self.assertLen(ast.items, 2) 59 A, assembly = ast.items 60 61 self.assertIsInstance(A, Component) 62 self.assertIsInstance(assembly, Assembly) 63 64 def test_assembly_combining_basic(self): 65 ast, _ = self.parser.parse_string(''' 66 component A {} 67 assembly ass { 68 composition { 69 component A a; 70 } 71 } 72 assembly ass2 { 73 composition { 74 component A b; 75 } 76 } 77 ''') 78 79 self.assertLen(ast.items, 2) 80 A, ass = ast.items 81 82 self.assertIsInstance(A, Component) 83 self.assertIsInstance(ass, Assembly) 84 self.assertEqual(ass.name, 'ass') 85 86 self.assertLen(ass.composition.instances, 2) 87 a, b = ass.composition.instances 88 89 self.assertIsInstance(a, Instance) 90 self.assertEqual(a.name, 'a') 91 92 self.assertIsInstance(b, Instance) 93 self.assertEqual(b.name, 'b') 94 95 def test_assembly_combining_with_groups(self): 96 ast, _ = self.parser.parse_string(''' 97 component A {} 98 assembly ass { 99 composition { 100 group { 101 component A a; 102 } 103 } 104 } 105 assembly ass2 { 106 composition { 107 group foo { 108 component A b; 109 } 110 } 111 } 112 ''') 113 114 self.assertLen(ast.items, 2) 115 A, ass = ast.items 116 117 self.assertIsInstance(A, Component) 118 self.assertIsInstance(ass, Assembly) 119 self.assertEqual(ass.name, 'ass') 120 121 self.assertLen(ass.composition.instances, 2) 122 a, b = ass.composition.instances 123 124 self.assertIsInstance(a, Instance) 125 self.assertEqual(a.name, 'a') 126 six.assertRegex(self, a.address_space, r'^unamed_group_.*$') 127 128 self.assertIsInstance(b, Instance) 129 self.assertEqual(b.name, 'b') 130 self.assertEqual(b.address_space, 'foo') 131 132 def test_assembly_line_numbering(self): 133 ast, _ = self.parser.parse_string(''' 134 135 assembly A { 136 composition {} 137 } 138 139 assembly B { 140 composition {} 141 } 142 ''') 143 144 self.assertLen(ast.items, 1) 145 A = ast.items[0] 146 147 self.assertIsInstance(A, Assembly) 148 self.assertEqual(A.lineno, 3) 149 150if __name__ == '__main__': 151 unittest.main() 152