Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / third_party / closure_compiler / processor_test.py
blobf5517cca73ee22039cf8212862ab9fc0421dce60
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 FileCache._cache["/double-debug.js"] = """
43 <include src="/debug.js">
44 <include src="/debug.js">
45 """.strip()
47 self._processor = Processor("/checked.js")
49 def testInline(self):
50 self.assertMultiLineEqual("""
51 // Copyright 2028 Future Chromium Author dudes.
52 /**
53 * @fileoverview Coolest app ever.
54 * @author Douglas Crockford (douglas@crockford.com)
56 // Copyright 2014 Old Chromium Author dudes.
57 // Copyright 2002 Older Chromium Author dudes.
58 function debug(msg) { if (window.DEBUG) alert(msg); }
59 var global = 'type checking!';
60 debug(global);
61 // Here continues checked.js, a swell file.
62 """.strip(), self._processor.contents)
64 def assertLineNumber(self, abs_line, expected_line):
65 actual_line = self._processor.get_file_from_line(abs_line)
66 self.assertEqual(expected_line.file, actual_line.file)
67 self.assertEqual(expected_line.line_number, actual_line.line_number)
69 def testGetFileFromLine(self):
70 """Verify that inlined files retain their original line info."""
71 self.assertLineNumber(1, LineNumber("/checked.js", 1))
72 self.assertLineNumber(5, LineNumber("/checked.js", 5))
73 self.assertLineNumber(6, LineNumber("/global.js", 1))
74 self.assertLineNumber(7, LineNumber("/debug.js", 1))
75 self.assertLineNumber(8, LineNumber("/debug.js", 2))
76 self.assertLineNumber(9, LineNumber("/global.js", 3))
77 self.assertLineNumber(10, LineNumber("/checked.js", 7))
78 self.assertLineNumber(11, LineNumber("/checked.js", 8))
80 def testIncludedFiles(self):
81 """Verify that files are tracked correctly as they're inlined."""
82 self.assertEquals(set(["/global.js", "/debug.js"]),
83 self._processor.included_files)
85 def testDoubleIncludedSkipped(self):
86 """Verify that doubly included files are skipped."""
87 processor = Processor("/double-debug.js")
88 self.assertEquals(set(["/debug.js"]), processor.included_files)
89 self.assertEquals(FileCache.read("/debug.js") + "\n", processor.contents)
91 class IfStrippingTest(unittest.TestCase):
92 """Test that the contents of XML <if> blocks are stripped."""
94 def __init__(self, *args, **kwargs):
95 unittest.TestCase.__init__(self, *args, **kwargs)
96 self.maxDiff = None
98 def setUp(self):
99 FileCache._cache["/century.js"] = """
100 function getCurrentCentury() {
101 <if expr="netscape_os">
102 alert("Oh wow!");
103 return "XX";
104 </if>
105 return "XXI";
107 """.strip()
109 self.processor_ = Processor("/century.js")
111 def testIfStripping(self):
112 self.assertMultiLineEqual("""
113 function getCurrentCentury() {
115 alert("Oh wow!");
116 return "XX";
118 return "XXI";
120 """.strip(), self.processor_.contents)
123 if __name__ == '__main__':
124 unittest.main()