Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / third_party / closure_compiler / processor_test.py
blob942d472ba7f3d09d9d39aede1aceafc343b4c652
1 #!/usr/bin/env python
2 # Copyright 2014 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.
6 """Test resources processing, i.e. <if> and <include> tag handling."""
8 import unittest
9 from processor import FileCache, Processor, LineNumber
12 class ProcessorTest(unittest.TestCase):
13 """Test <include> tag processing logic."""
15 def __init__(self, *args, **kwargs):
16 unittest.TestCase.__init__(self, *args, **kwargs)
17 self.maxDiff = None
19 def setUp(self):
20 FileCache._cache["/debug.js"] = """
21 // Copyright 2002 Older Chromium Author dudes.
22 function debug(msg) { if (window.DEBUG) alert(msg); }
23 """.strip()
25 FileCache._cache["/global.js"] = """
26 // Copyright 2014 Old Chromium Author dudes.
27 <include src="/debug.js">
28 var global = 'type checking!';
29 """.strip()
31 FileCache._cache["/checked.js"] = """
32 // Copyright 2028 Future Chromium Author dudes.
33 /**
34 * @fileoverview Coolest app ever.
35 * @author Douglas Crockford (douglas@crockford.com)
37 <include src="/global.js">
38 debug(global);
39 // Here continues checked.js, a swell file.
40 """.strip()
42 self._processor = Processor("/checked.js")
44 def testInline(self):
45 self.assertMultiLineEqual("""
46 // Copyright 2028 Future Chromium Author dudes.
47 /**
48 * @fileoverview Coolest app ever.
49 * @author Douglas Crockford (douglas@crockford.com)
51 // Copyright 2014 Old Chromium Author dudes.
52 // Copyright 2002 Older Chromium Author dudes.
53 function debug(msg) { if (window.DEBUG) alert(msg); }
54 var global = 'type checking!';
55 debug(global);
56 // Here continues checked.js, a swell file.
57 """.strip(), self._processor.contents)
59 def assertLineNumber(self, abs_line, expected_line):
60 actual_line = self._processor.get_file_from_line(abs_line)
61 self.assertEqual(expected_line.file, actual_line.file)
62 self.assertEqual(expected_line.line_number, actual_line.line_number)
64 def testGetFileFromLine(self):
65 """Verify that inlined files retain their original line info."""
66 self.assertLineNumber(1, LineNumber("/checked.js", 1))
67 self.assertLineNumber(5, LineNumber("/checked.js", 5))
68 self.assertLineNumber(6, LineNumber("/global.js", 1))
69 self.assertLineNumber(7, LineNumber("/debug.js", 1))
70 self.assertLineNumber(8, LineNumber("/debug.js", 2))
71 self.assertLineNumber(9, LineNumber("/global.js", 3))
72 self.assertLineNumber(10, LineNumber("/checked.js", 7))
73 self.assertLineNumber(11, LineNumber("/checked.js", 8))
75 def testIncludedFiles(self):
76 """Verify that files are tracked correctly as they're inlined."""
77 self.assertEquals(set(["/global.js", "/debug.js"]),
78 self._processor.included_files)
81 class IfStrippingTest(unittest.TestCase):
82 """Test that the contents of XML <if> blocks are stripped."""
84 def __init__(self, *args, **kwargs):
85 unittest.TestCase.__init__(self, *args, **kwargs)
86 self.maxDiff = None
88 def setUp(self):
89 FileCache._cache["/century.js"] = """
90 function getCurrentCentury() {
91 <if expr="netscape_os">
92 alert("Oh wow!");
93 return "XX";
94 </if>
95 return "XXI";
97 """.strip()
99 self.processor_ = Processor("/century.js")
101 def testIfStripping(self):
102 self.assertMultiLineEqual("""
103 function getCurrentCentury() {
105 alert("Oh wow!");
106 return "XX";
108 return "XXI";
110 """.strip(), self.processor_.contents)
113 if __name__ == '__main__':
114 unittest.main()