blob: 39b62c396de4c304d5b4c33b60463a45f45d86ab [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
30from os.path import join, dirname, exists
31import 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")
101 source = open(pathname).read();
102 def Test(name, source, expectation):
103 throws = None
104 if (expectation is not None):
105 throws = [expectation]
106 test = PreparserTestCase(self.root,
107 current_path + [filename, name],
108 executable,
109 mode, throws, self.context,
110 source.replace("\n", " "))
111 result.append(test)
112 def Template(name, source):
113 def MkTest(replacement, expectation):
114 testname = name
115 testsource = source
116 for key in replacement.keys():
117 testname = testname.replace("$"+key, replacement[key]);
118 testsource = testsource.replace("$"+key, replacement[key]);
119 Test(testname, testsource, expectation)
120 return MkTest
121 eval(compile(source, pathname, "exec"),
122 {"Test": Test, "Template": Template}, {})
123
Ben Murdoch8b112d22011-06-08 16:22:53 +0100124 def ListTests(self, current_path, path, mode, variant_flags):
125 executable = join('obj', 'preparser', mode, 'preparser')
126 if utils.IsWindows():
127 executable += '.exe'
128 executable = join(self.context.buildspace, executable)
Ben Murdoch257744e2011-11-30 15:57:28 +0000129 expectations = self.GetExpectations()
130 result = []
Ben Murdoch8b112d22011-06-08 16:22:53 +0100131 # Find all .js files in tests/preparser directory.
132 filenames = [f[:-3] for f in os.listdir(self.root) if f.endswith(".js")]
133 filenames.sort()
Ben Murdoch8b112d22011-06-08 16:22:53 +0100134 for file in filenames:
Ben Murdoch257744e2011-11-30 15:57:28 +0000135 throws = None;
136 if (file in expectations):
137 throws = expectations[file]
Ben Murdoch8b112d22011-06-08 16:22:53 +0100138 result.append(PreparserTestCase(self.root,
139 current_path + [file], executable,
Ben Murdoch257744e2011-11-30 15:57:28 +0000140 mode, throws, self.context, None))
141 # Find all .pyt files in test/preparser directory.
142 filenames = [f[:-4] for f in os.listdir(self.root) if f.endswith(".pyt")]
143 filenames.sort()
144 for file in filenames:
145 # Each file as a python source file to be executed in a specially
146 # perparsed environment (defining the Template and Test functions)
147 self.ParsePythonTestTemplates(result, file,
148 executable, current_path, mode)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100149 return result
150
151 def GetTestStatus(self, sections, defs):
152 status_file = join(self.root, 'preparser.status')
153 if exists(status_file):
154 test.ReadConfigurationInto(status_file, sections, defs)
155
Ben Murdoch257744e2011-11-30 15:57:28 +0000156 def VariantFlags(self):
157 return [[]];
158
Ben Murdoch8b112d22011-06-08 16:22:53 +0100159
160def GetConfiguration(context, root):
161 return PreparserTestConfiguration(context, root)