1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
6 from tvcm import fake_fs
7 from tvcm import module
8 from tvcm import js_module
9 from tvcm import project as project_module
10 from tvcm import resource as resource_module
11 from tvcm import resource_loader
12 from tvcm import strip_js_comments
14 class SmokeTest(unittest.TestCase):
16 with fake_fs.FakeFS({"/src/test.js": "// blahblahblah\n\n'use strict';\n\ntvcm.require('dependency1');"}):
17 project = project_module.Project(['/src/'], include_tvcm_paths=False)
18 loader = resource_loader.ResourceLoader(project)
19 resource = resource_module.Resource('/src/', '/src/test.js')
20 my_module = js_module.JSModule(loader, 'test', resource)
22 class ValidateStrictModeTests(unittest.TestCase):
23 """Test case for ValidateUsesStrictMode."""
25 def test_ValidateUsesStrictMode_returns_true(self):
26 text = "// blahblahblah\n\n'use strict';\n\ntvcm.require('dependency1');"
27 stripped_text = strip_js_comments.strip_js_comments(text)
28 self.assertIsNone(js_module.ValidateUsesStrictMode('module', stripped_text))
30 def test_ValidateUsesStrictModeOneLiner(self):
31 text = "'use strict'; tvcm.require('dependency1');"
32 stripped_text = strip_js_comments.strip_js_comments(text)
33 self.assertIsNone(js_module.ValidateUsesStrictMode('module', stripped_text))
35 def test_ValidateUsesStrictMode_catches_missing_strict_mode(self):
36 text = "// blahblahblah\n\ntvcm.require('dependency1');"
37 stripped_text = strip_js_comments.strip_js_comments(text)
39 lambda: js_module.ValidateUsesStrictMode('module', stripped_text))
41 class ValidateTestSuiteDefinition(unittest.TestCase):
42 def test_basic_success(self):
44 tvcm.unittest.testSuite('foo.bar_test', function() {
47 js_module.ValidateTestSuiteDefinition('foo.bar_test', text)
50 def test_wrong_name(self):
52 tvcm.unittest.testSuite('foo.bar', function() {
56 lambda: js_module.ValidateTestSuiteDefinition('foo.bar_test', text))
58 def test_no_suite_failure(self):
62 lambda: js_module.ValidateTestSuiteDefinition('foo.bar_test', text))
64 def test_multiple_suites_failure(self):
66 tvcm.unittest.testSuite('foo.bar_test', function() {
68 tvcm.unittest.testSuite('foo.bar_test', function() {
72 lambda: js_module.ValidateTestSuiteDefinition('foo.bar_test', text))
74 class ParseDefinitionTests(unittest.TestCase):
75 """Test case for js_module.Parse."""
77 def test_Parse_populates_resource_name_lists(self):
78 # Dependencies to resources can be specified in a my_module "definition",
79 # and lists of resource names for the my_module are populated correctly.
83 "tvcm.require('dependency1');\n"
84 "tvcm.require('dependency2');\n"
85 "tvcm.requireStylesheet('myStylesheet');\n"
86 "tvcm.requireTemplate('myTemplate');\n")
87 stripped_text = strip_js_comments.strip_js_comments(text)
88 deps = js_module.Parse('module_name', stripped_text)
89 self.assertEquals(['myStylesheet'], deps.style_sheet_names)
90 self.assertEquals(['myTemplate'], deps.html_template_names)
91 self.assertEquals(['dependency1', 'dependency2'],
92 deps.dependent_module_names)
94 def test_Parse_missing_semicolons(self):
95 # Semicolons can be omitted after tvcm.require statements.
99 "tvcm.require('dependency1')\n"
100 "tvcm.require('dependency2');\n"
101 "tvcm.requireStylesheet('myStylesheet')\n")
102 # Gross hack. We should separate parsing from the module object.
103 stripped_text = strip_js_comments.strip_js_comments(text)
104 deps = js_module.Parse('module_name', stripped_text)
105 self.assertEquals(['myStylesheet'], deps.style_sheet_names)
106 self.assertEquals(['dependency1', 'dependency2'],
107 deps.dependent_module_names)
109 def test_Parse_with_deps_and_stylesheet_swapped(self):
110 # The dependencies can be specified in different orders.
114 "tvcm.require('dependency1');\n"
115 "tvcm.requireStylesheet('myStylesheet');\n"
116 "tvcm.require('dependency2');\n")
117 # Gross hack. We should separate parsing from the module object.
118 stripped_text = strip_js_comments.strip_js_comments(text)
119 deps = js_module.Parse('module_name', stripped_text)
120 self.assertEquals(['myStylesheet'], deps.style_sheet_names)
121 self.assertEquals(['dependency1', 'dependency2'],
122 deps.dependent_module_names)
124 def test_Parse_empty_definition(self):
125 # If there are no tvcm.require statements, the lists of resource names
126 # for the module are all empty.
127 text = "// blahblahblah\n'use strict';"
128 # Gross hack. We should separate parsing from the module object.
129 stripped_text = strip_js_comments.strip_js_comments(text)
130 deps = js_module.Parse('module_name', stripped_text)
131 self.assertEquals([], deps.style_sheet_names)
132 self.assertEquals([], deps.dependent_module_names)
134 def test_Parse_with_commented_out_dependency(self):
135 # Commented-out tvcm.require statements don't count.
139 "tvcm.require('dependency1');\n"
140 "//tvcm.require('dependency2');\n")
141 # Gross hack. We should separate parsing from the module object.
142 stripped_text = strip_js_comments.strip_js_comments(text)
143 deps = js_module.Parse('module_name', stripped_text)
144 self.assertEquals([], deps.style_sheet_names)
145 self.assertEquals(['dependency1'], deps.dependent_module_names)
147 def test_Parse_with_multiline_comment_before(self):
148 # There can be long comments before the tvcm.require lines.
150 "// Copyright (c) 2012 The Chromium Authors. All rights reserved.\n"
151 "// Use of this source code is governed by a BSD-style license that"
153 "// found in the LICENSE file.\n\n"
156 " * @fileoverview TimelineView visualizes TRACE_EVENT events using\n"
157 " * the tracing.TimelineTrackView component and adds in selection\n"
158 " * summary and control buttons.\n"
160 "tvcm.requireStylesheet('timeline_view')\n"
161 "tvcm.require('timeline_track_view');\n"
162 "tvcm.require('timeline_analysis');\n"
163 "tvcm.require('overlay');\n"
164 "tvcm.require('trace_event_importer');\n"
165 "tvcm.require('linux_perf_importer');\n"
166 "tvcm.exportsTo('tracing', function() {\n")
167 # Gross hack. We should separate parsing from the module object.
168 stripped_text = strip_js_comments.strip_js_comments(text)
169 deps = js_module.Parse('module_name', stripped_text)
170 self.assertEquals(['timeline_view'], deps.style_sheet_names)
171 self.assertEquals(['timeline_track_view',
174 'trace_event_importer',
175 'linux_perf_importer'], deps.dependent_module_names)
177 def test_Parse_with_definition_in_comments(self):
178 # Statements inside multi-line comments are ignored.
182 " * All subclasses should depend on linux_perfParser, e.g.\n"
184 " * tvcm.require('linux_perfParser');\n"
185 " * tvcm.exportTo('tracing', function() { });\n"
189 "tvcm.require('dependency1');\n"
190 "tvcm.require('dependency2');\n")
191 # Gross hack. We should separate parsing from the module object.
192 stripped_text = strip_js_comments.strip_js_comments(text)
193 deps = js_module.Parse('module_name', stripped_text)
194 self.assertEquals([], deps.style_sheet_names)
195 self.assertEquals(['dependency1', 'dependency2'],
196 deps.dependent_module_names)
198 def test_Parse_dependency_with_slashes_throws_error(self):
199 # An error should be thrown if a slash is found in a resource name.
200 text = "tvcm.require('foo/dependency1')"
201 # Gross hack. We should separate parsing from the module object.
202 self.assertRaises(module.DepsException,
203 lambda: js_module.Parse('module_name', text))
205 def test_Parse_dependency_with_dots_is_okay(self):
206 # Module names can contain dots.
207 text = "tvcm.require('foo.dependency1')"
208 # Gross hack. We should separate parsing from the module object.
209 stripped_text = strip_js_comments.strip_js_comments(text)
210 deps = js_module.Parse('module_name', stripped_text)
211 self.assertEquals([], deps.style_sheet_names)
212 self.assertEquals(['foo.dependency1'],
213 deps.dependent_module_names)
216 class IsJSModuleTests(unittest.TestCase):
217 """Test case for ValidateUsesStrictMode."""
218 def testPositive(self):
219 js = """'use strict';
221 tvcm.requireRawScript('gl-matrix/src/gl-matrix/vec4.js');
223 tvcm.exportTo('tvcm', function() {
224 var tmp_vec2 = vec2.create();"""
225 self.assertTrue(js_module.IsJSModule(js))
227 def testPositive(self):
228 js = """'use strict';
230 tvcm.require('tvcm.bbox2');
232 tvcm.unittest.testSuite('tvcm.bbox2_test', function() {
233 test('addVec2', function() {"""
234 self.assertTrue(js_module.IsJSModule(js))
236 def testNegative(self):
242 self.assertFalse(js_module.IsJSModule(js))