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 | |
| 28 | import test |
| 29 | import os |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 30 | from os.path import join, dirname, exists, isfile |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 31 | import platform |
| 32 | import utils |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 33 | import re |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 34 | |
| 35 | class PreparserTestCase(test.TestCase): |
| 36 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 37 | def __init__(self, root, path, executable, mode, throws, context, source): |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 38 | super(PreparserTestCase, self).__init__(context, path, mode) |
| 39 | self.executable = executable |
| 40 | self.root = root |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 41 | self.throws = throws |
| 42 | self.source = source |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 43 | |
| 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 50 | def HasSource(self): |
| 51 | return self.source is not None |
| 52 | |
| 53 | def GetSource(): |
| 54 | return self.source |
| 55 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 56 | def BuildCommand(self, path): |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 57 | 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 Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 64 | 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 | |
| 73 | class 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 81 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 101 | 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 Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 120 | execfile(pathname, {"Test": Test, "Template": Template}) |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 121 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 122 | def ListTests(self, current_path, path, mode, variant_flags): |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 123 | executable = 'preparser' |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 124 | if utils.IsWindows(): |
| 125 | executable += '.exe' |
| 126 | executable = join(self.context.buildspace, executable) |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 127 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 132 | expectations = self.GetExpectations() |
| 133 | result = [] |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 134 | # 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 Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 137 | for file in filenames: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 138 | throws = None; |
| 139 | if (file in expectations): |
| 140 | throws = expectations[file] |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 141 | result.append(PreparserTestCase(self.root, |
| 142 | current_path + [file], executable, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 143 | 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 Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 149 | # created environment (defining the Template and Test functions) |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 150 | self.ParsePythonTestTemplates(result, file, |
| 151 | executable, current_path, mode) |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 152 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 159 | def VariantFlags(self): |
| 160 | return [[]]; |
| 161 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 162 | |
| 163 | def GetConfiguration(context, root): |
| 164 | return PreparserTestConfiguration(context, root) |