Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | # Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | # Redistribution and use in source and binary forms, with or without |
| 3 | # modification, are permitted provided that the following conditions are |
| 4 | # met: |
| 5 | # |
| 6 | # * Redistributions of source code must retain the above copyright |
| 7 | # notice, this list of conditions and the following disclaimer. |
| 8 | # * Redistributions in binary form must reproduce the above |
| 9 | # copyright notice, this list of conditions and the following |
| 10 | # disclaimer in the documentation and/or other materials provided |
| 11 | # with the distribution. |
| 12 | # * Neither the name of Google Inc. nor the names of its |
| 13 | # contributors may be used to endorse or promote products derived |
| 14 | # from this software without specific prior written permission. |
| 15 | # |
| 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 28 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 29 | import os |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 30 | import re |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 32 | from testrunner.local import testsuite |
| 33 | from testrunner.local import utils |
| 34 | from testrunner.objects import testcase |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 35 | |
| 36 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 37 | class PreparserTestSuite(testsuite.TestSuite): |
| 38 | def __init__(self, name, root): |
| 39 | super(PreparserTestSuite, self).__init__(name, root) |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 40 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 41 | def shell(self): |
| 42 | return "d8" |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 43 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 44 | def _GetExpectations(self): |
| 45 | expects_file = os.path.join(self.root, "preparser.expectation") |
| 46 | expectations_map = {} |
| 47 | if not os.path.exists(expects_file): return expectations_map |
| 48 | rule_regex = re.compile("^([\w\-]+)(?::([\w\-]+))?(?::(\d+),(\d+))?$") |
| 49 | for line in utils.ReadLinesFrom(expects_file): |
| 50 | rule_match = rule_regex.match(line) |
| 51 | if not rule_match: continue |
| 52 | expects = [] |
| 53 | if (rule_match.group(2)): |
| 54 | expects += [rule_match.group(2)] |
| 55 | if (rule_match.group(3)): |
| 56 | expects += [rule_match.group(3), rule_match.group(4)] |
| 57 | expectations_map[rule_match.group(1)] = " ".join(expects) |
| 58 | return expectations_map |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 59 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 60 | def _ParsePythonTestTemplates(self, result, filename): |
| 61 | pathname = os.path.join(self.root, filename + ".pyt") |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 62 | def Test(name, source, expectation): |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 63 | source = source.replace("\n", " ") |
| 64 | testname = os.path.join(filename, name) |
| 65 | flags = ["-e", source] |
| 66 | if expectation: |
| 67 | flags += ["--throws"] |
| 68 | test = testcase.TestCase(self, testname, flags=flags) |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 69 | result.append(test) |
| 70 | def Template(name, source): |
| 71 | def MkTest(replacement, expectation): |
| 72 | testname = name |
| 73 | testsource = source |
| 74 | for key in replacement.keys(): |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 75 | testname = testname.replace("$" + key, replacement[key]); |
| 76 | testsource = testsource.replace("$" + key, replacement[key]); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 77 | Test(testname, testsource, expectation) |
| 78 | return MkTest |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 79 | execfile(pathname, {"Test": Test, "Template": Template}) |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 80 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 81 | def ListTests(self, context): |
| 82 | expectations = self._GetExpectations() |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 83 | result = [] |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 84 | |
| 85 | # Find all .js files in this directory. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 86 | filenames = [f[:-3] for f in os.listdir(self.root) if f.endswith(".js")] |
| 87 | filenames.sort() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 88 | for f in filenames: |
| 89 | throws = expectations.get(f, None) |
| 90 | flags = [f + ".js"] |
| 91 | if throws: |
| 92 | flags += ["--throws"] |
| 93 | test = testcase.TestCase(self, f, flags=flags) |
| 94 | result.append(test) |
| 95 | |
| 96 | # Find all .pyt files in this directory. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 97 | filenames = [f[:-4] for f in os.listdir(self.root) if f.endswith(".pyt")] |
| 98 | filenames.sort() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 99 | for f in filenames: |
| 100 | self._ParsePythonTestTemplates(result, f) |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 101 | return result |
| 102 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 103 | def GetFlagsForTestCase(self, testcase, context): |
| 104 | first = testcase.flags[0] |
| 105 | if first != "-e": |
| 106 | testcase.flags[0] = os.path.join(self.root, first) |
| 107 | return testcase.flags |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 108 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 109 | def GetSourceForTest(self, testcase): |
| 110 | if testcase.flags[0] == "-e": |
| 111 | return testcase.flags[1] |
| 112 | with open(testcase.flags[0]) as f: |
| 113 | return f.read() |
| 114 | |
| 115 | def VariantFlags(self, testcase, default_flags): |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 116 | return [[]]; |
| 117 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 118 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 119 | def GetSuite(name, root): |
| 120 | return PreparserTestSuite(name, root) |