blob: 88c06a31adcfe6a4737b8e28218c4bf1acda9975 [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
28import test
29import os
Ben Murdoch69a99ed2011-11-30 16:03:39 +000030from os.path import join, dirname, exists, isfile
Ben Murdoch8b112d22011-06-08 16:22:53 +010031import platform
32import utils
Ben Murdoch257744e2011-11-30 15:57:28 +000033import re
Ben Murdoch8b112d22011-06-08 16:22:53 +010034
35class PreparserTestCase(test.TestCase):
36
Ben Murdoch257744e2011-11-30 15:57:28 +000037 def __init__(self, root, path, executable, mode, throws, context, source):
Ben Murdoch8b112d22011-06-08 16:22:53 +010038 super(PreparserTestCase, self).__init__(context, path, mode)
39 self.executable = executable
40 self.root = root
Ben Murdoch257744e2011-11-30 15:57:28 +000041 self.throws = throws
42 self.source = source
Ben Murdoch8b112d22011-06-08 16:22:53 +010043
44 def GetLabel(self):
45 return "%s %s %s" % (self.mode, self.path[-2], self.path[-1])
46
47 def GetName(self):
48 return self.path[-1]
49
Ben Murdoch257744e2011-11-30 15:57:28 +000050 def HasSource(self):
51 return self.source is not None
52
53 def GetSource():
54 return self.source
55
Ben Murdoch8b112d22011-06-08 16:22:53 +010056 def BuildCommand(self, path):
Ben Murdoch257744e2011-11-30 15:57:28 +000057 if (self.source is not None):
58 result = [self.executable, "-e", self.source]
59 else:
60 testfile = join(self.root, self.GetName()) + ".js"
61 result = [self.executable, testfile]
62 if (self.throws):
63 result += ['throws'] + self.throws
Ben Murdoch8b112d22011-06-08 16:22:53 +010064 return result
65
66 def GetCommand(self):
67 return self.BuildCommand(self.path)
68
69 def Run(self):
70 return test.TestCase.Run(self)
71
72
73class PreparserTestConfiguration(test.TestConfiguration):
74
75 def __init__(self, context, root):
76 super(PreparserTestConfiguration, self).__init__(context, root)
77
78 def GetBuildRequirements(self):
79 return ['preparser']
80
Ben Murdoch257744e2011-11-30 15:57:28 +000081 def GetExpectations(self):
82 expects_file = join(self.root, 'preparser.expectation')
83 map = {}
84 if exists(expects_file):
85 rule_regex = re.compile("^([\w\-]+)(?::([\w\-]+))?(?::(\d+),(\d+))?$")
86 for line in utils.ReadLinesFrom(expects_file):
87 if (line[0] == '#'): continue
88 rule_match = rule_regex.match(line)
89 if rule_match:
90 expects = []
91 if (rule_match.group(2)):
92 expects = expects + [rule_match.group(2)]
93 if (rule_match.group(3)):
94 expects = expects + [rule_match.group(3), rule_match.group(4)]
95 map[rule_match.group(1)] = expects
96 return map;
97
98 def ParsePythonTestTemplates(self, result, filename,
99 executable, current_path, mode):
100 pathname = join(self.root, filename + ".pyt")
Ben Murdoch257744e2011-11-30 15:57:28 +0000101 def Test(name, source, expectation):
102 throws = None
103 if (expectation is not None):
104 throws = [expectation]
105 test = PreparserTestCase(self.root,
106 current_path + [filename, name],
107 executable,
108 mode, throws, self.context,
109 source.replace("\n", " "))
110 result.append(test)
111 def Template(name, source):
112 def MkTest(replacement, expectation):
113 testname = name
114 testsource = source
115 for key in replacement.keys():
116 testname = testname.replace("$"+key, replacement[key]);
117 testsource = testsource.replace("$"+key, replacement[key]);
118 Test(testname, testsource, expectation)
119 return MkTest
Ben Murdoch589d6972011-11-30 16:04:58 +0000120 execfile(pathname, {"Test": Test, "Template": Template})
Ben Murdoch257744e2011-11-30 15:57:28 +0000121
Ben Murdoch8b112d22011-06-08 16:22:53 +0100122 def ListTests(self, current_path, path, mode, variant_flags):
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000123 executable = 'preparser'
Ben Murdoch8b112d22011-06-08 16:22:53 +0100124 if utils.IsWindows():
125 executable += '.exe'
126 executable = join(self.context.buildspace, executable)
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000127 if not isfile(executable):
128 executable = join('obj', 'preparser', mode, 'preparser')
129 if utils.IsWindows():
130 executable += '.exe'
131 executable = join(self.context.buildspace, executable)
Ben Murdoch257744e2011-11-30 15:57:28 +0000132 expectations = self.GetExpectations()
133 result = []
Ben Murdoch8b112d22011-06-08 16:22:53 +0100134 # Find all .js files in tests/preparser directory.
135 filenames = [f[:-3] for f in os.listdir(self.root) if f.endswith(".js")]
136 filenames.sort()
Ben Murdoch8b112d22011-06-08 16:22:53 +0100137 for file in filenames:
Ben Murdoch257744e2011-11-30 15:57:28 +0000138 throws = None;
139 if (file in expectations):
140 throws = expectations[file]
Ben Murdoch8b112d22011-06-08 16:22:53 +0100141 result.append(PreparserTestCase(self.root,
142 current_path + [file], executable,
Ben Murdoch257744e2011-11-30 15:57:28 +0000143 mode, throws, self.context, None))
144 # Find all .pyt files in test/preparser directory.
145 filenames = [f[:-4] for f in os.listdir(self.root) if f.endswith(".pyt")]
146 filenames.sort()
147 for file in filenames:
148 # Each file as a python source file to be executed in a specially
Ben Murdoch589d6972011-11-30 16:04:58 +0000149 # created environment (defining the Template and Test functions)
Ben Murdoch257744e2011-11-30 15:57:28 +0000150 self.ParsePythonTestTemplates(result, file,
151 executable, current_path, mode)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100152 return result
153
154 def GetTestStatus(self, sections, defs):
155 status_file = join(self.root, 'preparser.status')
156 if exists(status_file):
157 test.ReadConfigurationInto(status_file, sections, defs)
158
Ben Murdoch257744e2011-11-30 15:57:28 +0000159 def VariantFlags(self):
160 return [[]];
161
Ben Murdoch8b112d22011-06-08 16:22:53 +0100162
163def GetConfiguration(context, root):
164 return PreparserTestConfiguration(context, root)