1#!/usr/bin/env python 2# Copyright (c) 2012 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import os 7import sys 8import unittest 9 10from compiler import GenerateSchema 11 12# If --rebase is passed to this test, this is set to True, indicating the test 13# output should be re-generated for each test (rather than running the tests 14# themselves). 15REBASE_MODE = False 16 17# The directory containing the input and expected output files corresponding 18# to each test name. 19TESTS_DIR = 'dart_test' 20 21class DartTest(unittest.TestCase): 22 23 def _RunTest(self, test_filename): 24 '''Given the name of a test, runs compiler.py on the file: 25 TESTS_DIR/test_filename.idl 26 and compares it to the output in the file: 27 TESTS_DIR/test_filename.dart 28 ''' 29 file_rel = os.path.join(TESTS_DIR, test_filename) 30 31 output_dir = None 32 if REBASE_MODE: 33 output_dir = TESTS_DIR 34 output_code = GenerateSchema('dart', ['%s.idl' % file_rel], TESTS_DIR, 35 output_dir, '', None, None, []) 36 37 if not REBASE_MODE: 38 with open('%s.dart' % file_rel) as f: 39 expected_output = f.read() 40 # Remove the first line of the output code (as it contains the filename). 41 # Also remove all blank lines, ignoring them from the comparison. 42 # Compare with lists instead of strings for clearer diffs (especially with 43 # whitespace) when a test fails. 44 self.assertEqual([l for l in expected_output.split('\n') if l], 45 [l for l in output_code.split('\n')[1:] if l]) 46 47 def setUp(self): 48 # Increase the maximum diff amount to see the full diff on a failed test. 49 self.maxDiff = 2000 50 51 def testComments(self): 52 self._RunTest('comments') 53 54 def testDictionaries(self): 55 self._RunTest('dictionaries') 56 57 def testEmptyNamespace(self): 58 self._RunTest('empty_namespace') 59 60 def testEmptyType(self): 61 self._RunTest('empty_type') 62 63 def testEvents(self): 64 self._RunTest('events') 65 66 def testBasicFunction(self): 67 self._RunTest('functions') 68 69 def testOpratableType(self): 70 self._RunTest('operatable_type') 71 72 def testTags(self): 73 self._RunTest('tags') 74 75 76if __name__ == '__main__': 77 if '--rebase' in sys.argv: 78 print "Running in rebase mode." 79 REBASE_MODE = True 80 sys.argv.remove('--rebase') 81 unittest.main() 82