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
29import os
30import re
31
32from testrunner.local import testsuite
33from testrunner.local import utils
34from testrunner.objects import testcase
35
36
37FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)")
38INVALID_FLAGS = ["--enable-slow-asserts"]
39
40
41class PreparserTestSuite(testsuite.TestSuite):
42  def __init__(self, name, root):
43    super(PreparserTestSuite, self).__init__(name, root)
44
45  def shell(self):
46    return "d8"
47
48  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
63
64  def _ParsePythonTestTemplates(self, result, filename):
65    pathname = os.path.join(self.root, filename + ".pyt")
66    def Test(name, source, expectation, extra_flags=[]):
67      source = source.replace("\n", " ")
68      testname = os.path.join(filename, name)
69      flags = ["-e", source]
70      if expectation:
71        flags += ["--throws"]
72      flags += extra_flags
73      test = testcase.TestCase(self, testname, flags=flags)
74      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():
80          testname = testname.replace("$" + key, replacement[key]);
81          testsource = testsource.replace("$" + key, replacement[key]);
82        Test(testname, testsource, expectation)
83      return MkTest
84    execfile(pathname, {"Test": Test, "Template": Template})
85
86  def ListTests(self, context):
87    expectations = self._GetExpectations()
88    result = []
89
90    # Find all .js files in this directory.
91    filenames = [f[:-3] for f in os.listdir(self.root) if f.endswith(".js")]
92    filenames.sort()
93    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.
102    filenames = [f[:-4] for f in os.listdir(self.root) if f.endswith(".pyt")]
103    filenames.sort()
104    for f in filenames:
105      self._ParsePythonTestTemplates(result, f)
106    return result
107
108  def GetFlagsForTestCase(self, testcase, context):
109    first = testcase.flags[0]
110    if first != "-e":
111      testcase.flags[0] = os.path.join(self.root, first)
112      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
121    return testcase.flags
122
123  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
129  def _VariantGeneratorFactory(self):
130    return testsuite.StandardVariantGenerator
131
132
133def GetSuite(name, root):
134  return PreparserTestSuite(name, root)
135