blob: 7e51b8ef58bfe5a7bc0edcd5f2d4e34a04882f0a [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001# 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 Murdochb8a8cc12014-11-26 15:28:44 +000028
Ben Murdoch8b112d22011-06-08 16:22:53 +010029import os
Ben Murdoch257744e2011-11-30 15:57:28 +000030import re
Ben Murdoch8b112d22011-06-08 16:22:53 +010031
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032from testrunner.local import testsuite
33from testrunner.local import utils
34from testrunner.objects import testcase
Ben Murdoch8b112d22011-06-08 16:22:53 +010035
36
Emily Bernierd0a1eb72015-03-24 16:35:39 -040037FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)")
38INVALID_FLAGS = ["--enable-slow-asserts"]
39
40
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041class PreparserTestSuite(testsuite.TestSuite):
42 def __init__(self, name, root):
43 super(PreparserTestSuite, self).__init__(name, root)
Ben Murdoch8b112d22011-06-08 16:22:53 +010044
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 def shell(self):
46 return "d8"
Ben Murdoch8b112d22011-06-08 16:22:53 +010047
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 def _GetExpectations(self):
49 expects_file = os.path.join(self.root, "preparser.expectation")
50 expectations_map = {}
51 if not os.path.exists(expects_file): return expectations_map
52 rule_regex = re.compile("^([\w\-]+)(?::([\w\-]+))?(?::(\d+),(\d+))?$")
53 for line in utils.ReadLinesFrom(expects_file):
54 rule_match = rule_regex.match(line)
55 if not rule_match: continue
56 expects = []
57 if (rule_match.group(2)):
58 expects += [rule_match.group(2)]
59 if (rule_match.group(3)):
60 expects += [rule_match.group(3), rule_match.group(4)]
61 expectations_map[rule_match.group(1)] = " ".join(expects)
62 return expectations_map
Ben Murdoch8b112d22011-06-08 16:22:53 +010063
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 def _ParsePythonTestTemplates(self, result, filename):
65 pathname = os.path.join(self.root, filename + ".pyt")
Emily Bernierd0a1eb72015-03-24 16:35:39 -040066 def Test(name, source, expectation, extra_flags=[]):
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 source = source.replace("\n", " ")
68 testname = os.path.join(filename, name)
69 flags = ["-e", source]
70 if expectation:
71 flags += ["--throws"]
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072 flags += extra_flags
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 test = testcase.TestCase(self, testname, flags=flags)
Ben Murdoch257744e2011-11-30 15:57:28 +000074 result.append(test)
75 def Template(name, source):
76 def MkTest(replacement, expectation):
77 testname = name
78 testsource = source
79 for key in replacement.keys():
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 testname = testname.replace("$" + key, replacement[key]);
81 testsource = testsource.replace("$" + key, replacement[key]);
Ben Murdoch257744e2011-11-30 15:57:28 +000082 Test(testname, testsource, expectation)
83 return MkTest
Ben Murdoch589d6972011-11-30 16:04:58 +000084 execfile(pathname, {"Test": Test, "Template": Template})
Ben Murdoch257744e2011-11-30 15:57:28 +000085
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 def ListTests(self, context):
87 expectations = self._GetExpectations()
Ben Murdoch257744e2011-11-30 15:57:28 +000088 result = []
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089
90 # Find all .js files in this directory.
Ben Murdoch8b112d22011-06-08 16:22:53 +010091 filenames = [f[:-3] for f in os.listdir(self.root) if f.endswith(".js")]
92 filenames.sort()
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 for f in filenames:
94 throws = expectations.get(f, None)
95 flags = [f + ".js"]
96 if throws:
97 flags += ["--throws"]
98 test = testcase.TestCase(self, f, flags=flags)
99 result.append(test)
100
101 # Find all .pyt files in this directory.
Ben Murdoch257744e2011-11-30 15:57:28 +0000102 filenames = [f[:-4] for f in os.listdir(self.root) if f.endswith(".pyt")]
103 filenames.sort()
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 for f in filenames:
105 self._ParsePythonTestTemplates(result, f)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100106 return result
107
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 def GetFlagsForTestCase(self, testcase, context):
109 first = testcase.flags[0]
110 if first != "-e":
111 testcase.flags[0] = os.path.join(self.root, first)
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400112 source = self.GetSourceForTest(testcase)
113 result = []
114 flags_match = re.findall(FLAGS_PATTERN, source)
115 for match in flags_match:
116 result += match.strip().split()
117 result += context.mode_flags
118 result = [x for x in result if x not in INVALID_FLAGS]
119 result.append(os.path.join(self.root, testcase.path + ".js"))
120 return testcase.flags + result
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 return testcase.flags
Ben Murdoch8b112d22011-06-08 16:22:53 +0100122
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 def GetSourceForTest(self, testcase):
124 if testcase.flags[0] == "-e":
125 return testcase.flags[1]
126 with open(testcase.flags[0]) as f:
127 return f.read()
128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 def _VariantGeneratorFactory(self):
130 return testsuite.StandardVariantGenerator
Ben Murdoch257744e2011-11-30 15:57:28 +0000131
Ben Murdoch8b112d22011-06-08 16:22:53 +0100132
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133def GetSuite(name, root):
134 return PreparserTestSuite(name, root)