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."""
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
)
20 FileCache
._cache
["/debug.js"] = """
21 // Copyright 2002 Older Chromium Author dudes.
22 function debug(msg) { if (window.DEBUG) alert(msg); }
25 FileCache
._cache
["/global.js"] = """
26 // Copyright 2014 Old Chromium Author dudes.
27 <include src="/debug.js">
28 var global = 'type checking!';
31 FileCache
._cache
["/checked.js"] = """
32 // Copyright 2028 Future Chromium Author dudes.
34 * @fileoverview Coolest app ever.
35 * @author Douglas Crockford (douglas@crockford.com)
37 <include src="/global.js">
39 // Here continues checked.js, a swell file.
42 FileCache
._cache
["/double-debug.js"] = """
43 <include src="/debug.js">
44 <include src="/debug.js">
47 self
._processor
= Processor("/checked.js")
50 self
.assertMultiLineEqual("""
51 // Copyright 2028 Future Chromium Author dudes.
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!';
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
)
99 FileCache
._cache
["/century.js"] = """
100 function getCurrentCentury() {
101 <if expr="netscape_os">
109 self
.processor_
= Processor("/century.js")
111 def testIfStripping(self
):
112 self
.assertMultiLineEqual("""
113 function getCurrentCentury() {
120 """.strip(), self
.processor_
.contents
)
123 if __name__
== '__main__':