Home | History | Annotate | Download | only in preparser
      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
     30 from os.path import join, dirname, exists, isfile
     31 import platform
     32 import utils
     33 import re
     34 
     35 class PreparserTestCase(test.TestCase):
     36 
     37   def __init__(self, root, path, executable, mode, throws, context, source):
     38     super(PreparserTestCase, self).__init__(context, path, mode)
     39     self.executable = executable
     40     self.root = root
     41     self.throws = throws
     42     self.source = source
     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 
     50   def HasSource(self):
     51     return self.source is not None
     52 
     53   def GetSource():
     54     return self.source
     55 
     56   def BuildCommand(self, path):
     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
     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 
     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")
    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
    120     execfile(pathname, {"Test": Test, "Template": Template})
    121 
    122   def ListTests(self, current_path, path, mode, variant_flags):
    123     executable = 'preparser'
    124     if utils.IsWindows():
    125       executable += '.exe'
    126     executable = join(self.context.buildspace, executable)
    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)
    132     expectations = self.GetExpectations()
    133     result = []
    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()
    137     for file in filenames:
    138       throws = None;
    139       if (file in expectations):
    140         throws = expectations[file]
    141       result.append(PreparserTestCase(self.root,
    142                                       current_path + [file], executable,
    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
    149       # created environment (defining the Template and Test functions)
    150       self.ParsePythonTestTemplates(result, file,
    151                                     executable, current_path, mode)
    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 
    159   def VariantFlags(self):
    160     return [[]];
    161 
    162 
    163 def GetConfiguration(context, root):
    164   return PreparserTestConfiguration(context, root)
    165