def test_strip_comments(self): self.assertEquals( 'A ', strip_js_comments.strip_js_comments('A // foo')) self.assertEquals( 'A bar', strip_js_comments.strip_js_comments('A // foo\nbar')) self.assertEquals( 'A b', strip_js_comments.strip_js_comments('A /* foo */ b')) self.assertEquals( 'A b', strip_js_comments.strip_js_comments('A /* foo\n */ b'))
def test_Parse_with_multiline_comment_before(self): # There can be long comments before the tvcm.require lines. text = ( "// Copyright (c) 2012 The Chromium Authors. All rights reserved.\n" "// Use of this source code is governed by a BSD-style license that" " can be\n" "// found in the LICENSE file.\n\n" "'use strict';\n\n" "/**\n" " * @fileoverview TimelineView visualizes TRACE_EVENT events using\n" " * the tracing.TimelineTrackView component and adds in selection\n" " * summary and control buttons.\n" " */\n" "tvcm.requireStylesheet('timeline_view')\n" "tvcm.require('timeline_track_view');\n" "tvcm.require('timeline_analysis');\n" "tvcm.require('overlay');\n" "tvcm.require('trace_event_importer');\n" "tvcm.require('linux_perf_importer');\n" "tvcm.exportsTo('tracing', function() {\n") # Gross hack. We should separate parsing from the module object. stripped_text = strip_js_comments.strip_js_comments(text) deps = js_module.Parse('module_name', stripped_text) self.assertEquals(['timeline_view'], deps.style_sheet_names) self.assertEquals(['timeline_track_view', 'timeline_analysis', 'overlay', 'trace_event_importer', 'linux_perf_importer'], deps.dependent_module_names)
def test_Parse_dependency_with_dots_is_okay(self): # Module names can contain dots. text = "tvcm.require('foo.dependency1')" # Gross hack. We should separate parsing from the module object. stripped_text = strip_js_comments.strip_js_comments(text) deps = js_module.Parse('module_name', stripped_text) self.assertEquals([], deps.style_sheet_names) self.assertEquals(['foo.dependency1'], deps.dependent_module_names)
def test_Parse_empty_definition(self): # If there are no tvcm.require statements, the lists of resource names # for the module are all empty. text = "// blahblahblah\n'use strict';" # Gross hack. We should separate parsing from the module object. stripped_text = strip_js_comments.strip_js_comments(text) deps = js_module.Parse('module_name', stripped_text) self.assertEquals([], deps.style_sheet_names) self.assertEquals([], deps.dependent_module_names)
def Parse(self): stripped_text = strip_js_comments.strip_js_comments(self.contents) if self.name != 'tvcm': if not IsJSModule(stripped_text): raise module.DepsException('%s is not a JS Module' % self.name) ValidateUsesStrictMode(self.name, stripped_text) if self.name.endswith('_test'): ValidateTestSuiteDefinition(self.name, stripped_text) self.dependency_metadata = Parse(self.name, stripped_text)
def test_Parse_with_commented_out_dependency(self): # Commented-out tvcm.require statements don't count. text = ( "// blahblahblah\n" "'use strict';\n" "tvcm.require('dependency1');\n" "//tvcm.require('dependency2');\n") # Gross hack. We should separate parsing from the module object. stripped_text = strip_js_comments.strip_js_comments(text) deps = js_module.Parse('module_name', stripped_text) self.assertEquals([], deps.style_sheet_names) self.assertEquals(['dependency1'], deps.dependent_module_names)
def test_Parse_missing_semicolons(self): # Semicolons can be omitted after tvcm.require statements. text = ( "// blahblahblah\n" "'use strict';\n" "tvcm.require('dependency1')\n" "tvcm.require('dependency2');\n" "tvcm.requireStylesheet('myStylesheet')\n") # Gross hack. We should separate parsing from the module object. stripped_text = strip_js_comments.strip_js_comments(text) deps = js_module.Parse('module_name', stripped_text) self.assertEquals(['myStylesheet'], deps.style_sheet_names) self.assertEquals(['dependency1', 'dependency2'], deps.dependent_module_names)
def test_Parse_with_deps_and_stylesheet_swapped(self): # The dependencies can be specified in different orders. text = ( "// blahblahblah\n" "'use strict';\n" "tvcm.require('dependency1');\n" "tvcm.requireStylesheet('myStylesheet');\n" "tvcm.require('dependency2');\n") # Gross hack. We should separate parsing from the module object. stripped_text = strip_js_comments.strip_js_comments(text) deps = js_module.Parse('module_name', stripped_text) self.assertEquals(['myStylesheet'], deps.style_sheet_names) self.assertEquals(['dependency1', 'dependency2'], deps.dependent_module_names)
def test_Parse_populates_resource_name_lists(self): # Dependencies to resources can be specified in a my_module "definition", # and lists of resource names for the my_module are populated correctly. text = ( "// blahblahblah\n" "'use strict';\n" "tvcm.require('dependency1');\n" "tvcm.require('dependency2');\n" "tvcm.requireStylesheet('myStylesheet');\n" "tvcm.requireTemplate('myTemplate');\n") stripped_text = strip_js_comments.strip_js_comments(text) deps = js_module.Parse('module_name', stripped_text) self.assertEquals(['myStylesheet'], deps.style_sheet_names) self.assertEquals(['myTemplate'], deps.html_template_names) self.assertEquals(['dependency1', 'dependency2'], deps.dependent_module_names)
def IsJSModule(text, text_is_stripped=True): if text_is_stripped: stripped_text = text else: stripped_text = strip_js_comments.strip_js_comments(text) if re.search("""tvcm\s*\.\s*exportTo""", stripped_text, re.DOTALL): return True if re.search("""tvcm\s*\.\s*require""", stripped_text, re.DOTALL): return True if re.search("""tvcm\s*\.\s*unittest\s*\.\s*testSuite\((["'])(.+?)\\1""", stripped_text, re.DOTALL): return True return False
def test_Parse_with_definition_in_comments(self): # Statements inside multi-line comments are ignored. text = ( "// SomeComment\n" "/*\n" " * All subclasses should depend on linux_perfParser, e.g.\n" " *\n" " * tvcm.require('linux_perfParser');\n" " * tvcm.exportTo('tracing', function() { });\n" " *\n" " */\n" "'use strict';\n" "tvcm.require('dependency1');\n" "tvcm.require('dependency2');\n") # Gross hack. We should separate parsing from the module object. stripped_text = strip_js_comments.strip_js_comments(text) deps = js_module.Parse('module_name', stripped_text) self.assertEquals([], deps.style_sheet_names) self.assertEquals(['dependency1', 'dependency2'], deps.dependent_module_names)
def test_ValidateUsesStrictMode_catches_missing_strict_mode(self): text = "// blahblahblah\n\ntvcm.require('dependency1');" stripped_text = strip_js_comments.strip_js_comments(text) self.assertRaises( lambda: js_module.ValidateUsesStrictMode('module', stripped_text))
def test_ValidateUsesStrictModeOneLiner(self): text = "'use strict'; tvcm.require('dependency1');" stripped_text = strip_js_comments.strip_js_comments(text) self.assertIsNone(js_module.ValidateUsesStrictMode('module', stripped_text))
def test_ValidateUsesStrictMode_returns_true(self): text = "// blahblahblah\n\n'use strict';\n\ntvcm.require('dependency1');" stripped_text = strip_js_comments.strip_js_comments(text) self.assertIsNone(js_module.ValidateUsesStrictMode('module', stripped_text))