blob: 850c0a45898591dfc4017fd8bf6defbc0b61f12f [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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037class PreparserTestSuite(testsuite.TestSuite):
38 def __init__(self, name, root):
39 super(PreparserTestSuite, self).__init__(name, root)
Ben Murdoch8b112d22011-06-08 16:22:53 +010040
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 def shell(self):
42 return "d8"
Ben Murdoch8b112d22011-06-08 16:22:53 +010043
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 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 Murdoch8b112d22011-06-08 16:22:53 +010059
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 def _ParsePythonTestTemplates(self, result, filename):
61 pathname = os.path.join(self.root, filename + ".pyt")
Ben Murdoch257744e2011-11-30 15:57:28 +000062 def Test(name, source, expectation):
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 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 Murdoch257744e2011-11-30 15:57:28 +000069 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 Murdochb8a8cc12014-11-26 15:28:44 +000075 testname = testname.replace("$" + key, replacement[key]);
76 testsource = testsource.replace("$" + key, replacement[key]);
Ben Murdoch257744e2011-11-30 15:57:28 +000077 Test(testname, testsource, expectation)
78 return MkTest
Ben Murdoch589d6972011-11-30 16:04:58 +000079 execfile(pathname, {"Test": Test, "Template": Template})
Ben Murdoch257744e2011-11-30 15:57:28 +000080
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 def ListTests(self, context):
82 expectations = self._GetExpectations()
Ben Murdoch257744e2011-11-30 15:57:28 +000083 result = []
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084
85 # Find all .js files in this directory.
Ben Murdoch8b112d22011-06-08 16:22:53 +010086 filenames = [f[:-3] for f in os.listdir(self.root) if f.endswith(".js")]
87 filenames.sort()
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 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 Murdoch257744e2011-11-30 15:57:28 +000097 filenames = [f[:-4] for f in os.listdir(self.root) if f.endswith(".pyt")]
98 filenames.sort()
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 for f in filenames:
100 self._ParsePythonTestTemplates(result, f)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100101 return result
102
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 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 Murdoch8b112d22011-06-08 16:22:53 +0100108
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 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 Murdoch257744e2011-11-30 15:57:28 +0000116 return [[]];
117
Ben Murdoch8b112d22011-06-08 16:22:53 +0100118
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119def GetSuite(name, root):
120 return PreparserTestSuite(name, root)