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
37class PreparserTestSuite(testsuite.TestSuite):
38  def __init__(self, name, root):
39    super(PreparserTestSuite, self).__init__(name, root)
40
41  def shell(self):
42    return "d8"
43
44  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
59
60  def _ParsePythonTestTemplates(self, result, filename):
61    pathname = os.path.join(self.root, filename + ".pyt")
62    def Test(name, source, expectation):
63      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)
69      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():
75          testname = testname.replace("$" + key, replacement[key]);
76          testsource = testsource.replace("$" + key, replacement[key]);
77        Test(testname, testsource, expectation)
78      return MkTest
79    execfile(pathname, {"Test": Test, "Template": Template})
80
81  def ListTests(self, context):
82    expectations = self._GetExpectations()
83    result = []
84
85    # Find all .js files in this directory.
86    filenames = [f[:-3] for f in os.listdir(self.root) if f.endswith(".js")]
87    filenames.sort()
88    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.
97    filenames = [f[:-4] for f in os.listdir(self.root) if f.endswith(".pyt")]
98    filenames.sort()
99    for f in filenames:
100      self._ParsePythonTestTemplates(result, f)
101    return result
102
103  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
108
109  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):
116    return [[]];
117
118
119def GetSuite(name, root):
120  return PreparserTestSuite(name, root)
121