Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame^] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | import re |
| 9 | from keyword_substitution import keyword_substitute, log_print, Verbosity |
| 10 | |
| 11 | tests = [] |
| 12 | VERBOSITY = Verbosity.warning |
| 13 | log_print(Verbosity.debug, "Setting verbosity to", VERBOSITY, verbosity=VERBOSITY) |
| 14 | tests.append({ |
| 15 | "description": "Test no substitution", |
| 16 | "template": "quickbrownfox", |
| 17 | "db": {"a": "b", "c": "d"}, |
| 18 | "expected": "quickbrownfox\n"}) |
| 19 | tests.append({ |
| 20 | "description": "Test simple substitution", |
| 21 | "template": "@@a@@", |
| 22 | "db": {"a": "b", "c": "d"}, |
| 23 | "expected": "b\n"}) |
| 24 | tests.append({ |
| 25 | "template": "a123", |
| 26 | "db": [{"c": "d", "a": {"a1": "lollipop", "a2": "PPAP"}}, |
| 27 | {"c": "x", "a": {"a1": "candyshop", "a2": "GitS: SAC"}}], |
| 28 | "expected": "a123\na123\n"}) |
| 29 | tests.append({ |
| 30 | "description": "Test two independent base symbols with two substitutions", |
| 31 | "template": "@@a@@ @@c@@", |
| 32 | "db": [{"a": "b", "c": "d"}, {"a": "x", "c": "x"}], |
| 33 | "expected": "b d\n" + |
| 34 | "x x\n"}) |
| 35 | tests.append({ |
| 36 | "template": "@@a@@@ @b@@c@@", |
| 37 | "db": [{"a": "b", "c": "d"}, {"a": "x", "c": "x"}], |
| 38 | "expected": "b@ @bd\n" + |
| 39 | "x@ @bx\n"}) |
| 40 | tests.append({ |
| 41 | "template": "@@a.a1@@", |
| 42 | "db": [{"a": {"a1": "lollipop", "a2": "PPAP"}, "c": "d"}], |
| 43 | "expected": "lollipop\n"}) |
| 44 | tests.append({ |
| 45 | "template": "@@a.a1@@ @@a.a2@@", |
| 46 | "db": [{"a": {"a1": "lollipop", "a2": "PPAP"}, "c": "d"}, {"a": "x", "c": "x"}], |
| 47 | "expected": "lollipop PPAP\n" + |
| 48 | "x x\n"}) |
| 49 | tests.append({ |
| 50 | "template": "@@a.a1@@ @@a.a2@@", |
| 51 | "db": [{"a": {"a1": "lollipop", "a2": "PPAP"}, "c": "d"}, |
| 52 | {"a": {"a1": "candyshop", "a2": "GitS: SAC"}, "c": "x"}], |
| 53 | "expected": "lollipop PPAP\n" + |
| 54 | "candyshop GitS: SAC\n"}) |
| 55 | tests.append({ |
| 56 | "template": "@@a.a1@@ @@a.a2@@ @@a@@", |
| 57 | "db": [{"a": {"a1": "lollipop", "a2": "PPAP"}, "c": "d"}, {"a": {"a1": "candyshop", "a2": "GitS: SAC"}, "c": "x"}], |
| 58 | "expected": "lollipop PPAP @@@@\n" + |
| 59 | "candyshop GitS: SAC @@@@\n"}) |
| 60 | tests.append({ |
| 61 | "description": "List in dict", |
| 62 | "template": "@@a.name@@", |
| 63 | "db": [{"a": [{"name": "inst1"}, {"name": "inst2"}]}], |
| 64 | "expected": "inst1\n" + |
| 65 | "inst2\n"}) |
| 66 | tests.append({ |
| 67 | "description": "List in dict in list", |
| 68 | "template": "@@name@@ subinstance: @@subinstlist.name@@", |
| 69 | "db": [{"name": "inst1", "subinstlist": [{"name": "subinst1"}, {"name": "subinst2"}]}, |
| 70 | {"name": "inst2", "subinstlist": [{"name": "subinst3"}, {"name": "subinst4"}]}], |
| 71 | "expected": "inst1 subinstance: subinst1\n" + |
| 72 | "inst1 subinstance: subinst2\n" + |
| 73 | "inst2 subinstance: subinst3\n" + |
| 74 | "inst2 subinstance: subinst4\n"}) |
| 75 | tests.append({ |
| 76 | "description": "Chain forks: multiple diverging chains", |
| 77 | "template": "@@name@@ subinstance: @@subinstlist.name@@ @@subinst2.name@@", |
| 78 | "db": {"name": "inst1", "subinstlist": [{"name": "subinst1"}, {"name": "subinst2"}], |
| 79 | "subinst2": [{"name": "xxx"}, {"name": "yyy"}]}, |
| 80 | "expected": "inst1 subinstance: subinst1 xxx\n" + |
| 81 | "inst1 subinstance: subinst1 yyy\n" + |
| 82 | "inst1 subinstance: subinst2 xxx\n" + |
| 83 | "inst1 subinstance: subinst2 yyy\n"}) |
| 84 | tests.append({ |
| 85 | "description": "Chain forks: multiple diverging chains", |
| 86 | "template": "@@name@@: @@subinstlist.name@@ @@subinstlist2.name@@ @@subinstlist3.name@@", |
| 87 | "db": {"name": "inst1", "subinstlist1": [{"name": "subinst1"}, {"name": "subinst2"}], |
| 88 | "subinstlist2": [{"name": "xxx"}, {"name": "yyy"}], |
| 89 | "subinstlist3": [{"name": "aaa"}, {"name": "bbb"}]}, |
| 90 | "expected": "inst1: @@subinstlist.name@@ xxx aaa\n" + |
| 91 | "inst1: @@subinstlist.name@@ xxx bbb\n" + |
| 92 | "inst1: @@subinstlist.name@@ yyy aaa\n" + |
| 93 | "inst1: @@subinstlist.name@@ yyy bbb\n"}) |
| 94 | tests.append({ |
| 95 | "description": "Chain forks: multiple diverging chains, array of root instances", |
| 96 | "template": "@@name@@ subinstance: @@subinstlist.name@@ @@subinst2.name@@", |
| 97 | "db": [{"name": "inst1", "subinstlist": [{"name": "subinst1"}, {"name": "subinst2"}], |
| 98 | "subinst2": [{"name": "xxx"}, {"name": "yyy"}]}, |
| 99 | {"name": "inst2", "subinstlist": [{"name": "subinst3"}, {"name": "subinst4"}], |
| 100 | "subinst2": [{"name": "inst2subinstlist2subinst1"}, {"name": "inst2subinstlist2subinst2"}]}], |
| 101 | "expected": "inst1 subinstance: subinst1 xxx\n" + |
| 102 | "inst1 subinstance: subinst1 yyy\n" + |
| 103 | "inst1 subinstance: subinst2 xxx\n" + |
| 104 | "inst1 subinstance: subinst2 yyy\n" + |
| 105 | "inst2 subinstance: subinst3 inst2subinstlist2subinst1\n" + |
| 106 | "inst2 subinstance: subinst3 inst2subinstlist2subinst2\n" + |
| 107 | "inst2 subinstance: subinst4 inst2subinstlist2subinst1\n" + |
| 108 | "inst2 subinstance: subinst4 inst2subinstlist2subinst2\n"}) |
| 109 | tests.append({ |
| 110 | "description": "Chain forks: multiple diverging chains with missing subinstance", |
| 111 | "template": "@@name@@ subinstance: @@subinstlist1.name@@ @@subinstlist2.name@@", |
| 112 | "db": [{"name": "inst1", "subinstlist1": [{"name": "subinst1"}, {"name": "subinst2"}], |
| 113 | "subinstlist2": [{"name": "xxx"}, {"name": "yyy"}]}, |
| 114 | {"name": "inst2", "subinstlist1": [{"name": "subinst3"}, {"name": "subinst4"}]}], |
| 115 | "expected": "inst1 subinstance: subinst1 xxx\n" |
| 116 | "inst1 subinstance: subinst1 yyy\n" |
| 117 | "inst1 subinstance: subinst2 xxx\n" |
| 118 | "inst1 subinstance: subinst2 yyy\n" |
| 119 | "inst2 subinstance: subinst3 @@subinstlist2.name@@\n" |
| 120 | "inst2 subinstance: subinst4 @@subinstlist2.name@@\n"}) |
| 121 | |
| 122 | def testsuite(): |
| 123 | for tcidx, test in enumerate(tests): |
| 124 | print |
| 125 | log_print(Verbosity.debug, "template:", test["template"]) |
| 126 | log_print(Verbosity.debug, "db:", test["db"]) |
| 127 | outlist = keyword_substitute(test["db"], test["template"], "report") |
| 128 | log_print(Verbosity.debug, outlist) |
| 129 | outstring = "" |
| 130 | for outline in outlist: |
| 131 | outstring += ''.join(outline) + "\n" |
| 132 | log_print(Verbosity.info, "Got:") |
| 133 | log_print(Verbosity.info, outstring) |
| 134 | if outstring == test["expected"]: |
| 135 | print "Test", tcidx, "PASSED" |
| 136 | test["result"] = "PASSED" |
| 137 | else: |
| 138 | print "Test", tcidx, "FAILED, expected:" |
| 139 | print test["expected"] |
| 140 | test["result"] = "FAILED" |
| 141 | |
| 142 | print |
| 143 | print "Test summary:" |
| 144 | for idx, test in enumerate(tests): |
| 145 | print "Test", idx, test["result"] |
| 146 | |
| 147 | def main(): |
| 148 | testsuite() |
| 149 | |
| 150 | if __name__ == "__main__": |
| 151 | main() |