def fixRequiresTest_withTestOnly(self, position): """Regression-tests sorting even with a goog.setTestOnly statement. Args: position: The position in the list where to insert the goog.setTestOnly statement. Will be used to test all possible combinations for this test. """ input_lines = [ 'goog.provide(\'package.subpackage.Whatever\');', '', 'goog.require(\'package.subpackage.ClassB\');', 'goog.require(\'package.subpackage.ClassA\');' ] expected_lines = [ 'goog.provide(\'package.subpackage.Whatever\');', '', 'goog.require(\'package.subpackage.ClassA\');', 'goog.require(\'package.subpackage.ClassB\');' ] input_lines.insert(position, 'goog.setTestOnly();') expected_lines.insert(position, 'goog.setTestOnly();') token = testutil.TokenizeSourceAndRunEcmaPass(input_lines) sorter = requireprovidesorter.RequireProvideSorter() sorter.FixRequires(token) self.assertEquals(expected_lines, self._GetLines(token))
def testMultipleGoogScopeCalls(self): start_token = testutil.TokenizeSourceAndRunEcmaPass( _TEST_MULTIPLE_SCOPE_SCRIPT) error_accumulator = erroraccumulator.ErrorAccumulator() alias_pass = aliaspass.AliasPass(set(['goog', 'myproject']), error_handler=error_accumulator) alias_pass.Process(start_token) alias_errors = error_accumulator.GetErrors() self.assertEquals(3, len(alias_errors)) error = alias_errors[0] self.assertEquals(errors.INVALID_USE_OF_GOOG_SCOPE, error.code) self.assertEquals(7, error.token.line_number) error = alias_errors[1] self.assertEquals(errors.EXTRA_GOOG_SCOPE_USAGE, error.code) self.assertEquals(7, error.token.line_number) error = alias_errors[2] self.assertEquals(errors.EXTRA_GOOG_SCOPE_USAGE, error.code) self.assertEquals(11, error.token.line_number)
def testAliasedIdentifiers(self): start_token = testutil.TokenizeSourceAndRunEcmaPass(_TEST_ALIAS_SCRIPT) alias_pass = aliaspass.AliasPass(set(['goog', 'myproject'])) alias_pass.Process(start_token) alias_token = _GetTokenByLineAndString(start_token, 'Event', 4) self.assertTrue(alias_token.metadata.is_alias_definition) my_class_token = _GetTokenByLineAndString(start_token, 'myClass', 8) self.assertIsNone(my_class_token.metadata.aliased_symbol) component_token = _GetTokenByLineAndString(start_token, 'Component', 16) self.assertEquals('goog.ui.Component', component_token.metadata.aliased_symbol) event_token = _GetTokenByLineAndString(start_token, 'Event.Something', 16) self.assertEquals('goog.events.Event.Something', event_token.metadata.aliased_symbol) non_closurized_token = _GetTokenByLineAndString( start_token, 'NonClosurizedClass', 17) self.assertIsNone(non_closurized_token.metadata.aliased_symbol) long_start_token = _GetTokenByLineAndString(start_token, 'Event.', 20) self.assertEquals('goog.events.Event.MultilineIdentifier.someMethod', long_start_token.metadata.aliased_symbol)
def testDeleteTokens(self): start_token = testutil.TokenizeSourceAndRunEcmaPass(_TEST_SCRIPT) fourth_token = start_token.next.next.next self.error_fixer.HandleFile('test_file', start_token) self.error_fixer._DeleteTokens(start_token, 3) self.assertEqual(fourth_token, self.error_fixer._file_token)
def testInvalidGoogScopeCall(self): start_token = testutil.TokenizeSourceAndRunEcmaPass(_TEST_SCOPE_SCRIPT) error_accumulator = erroraccumulator.ErrorAccumulator() alias_pass = aliaspass.AliasPass(error_handler=error_accumulator) alias_pass.Process(start_token) alias_errors = error_accumulator.GetErrors() self.assertEquals(1, len(alias_errors)) alias_error = alias_errors[0] self.assertEquals(errors.INVALID_USE_OF_GOOG_SCOPE, alias_error.code) self.assertEquals('goog.scope', alias_error.token.string)
def testFixRequires_removeBlankLines(self): """Tests that blank lines are omitted in sorted goog.require statements.""" input_lines = [ 'goog.provide(\'package.subpackage.Whatever\');', '', 'goog.require(\'package.subpackage.ClassB\');', '', 'goog.require(\'package.subpackage.ClassA\');' ] expected_lines = [ 'goog.provide(\'package.subpackage.Whatever\');', '', 'goog.require(\'package.subpackage.ClassA\');', 'goog.require(\'package.subpackage.ClassB\');' ] token = testutil.TokenizeSourceAndRunEcmaPass(input_lines) sorter = requireprovidesorter.RequireProvideSorter() sorter.FixRequires(token) self.assertEquals(expected_lines, self._GetLines(token))
def testGetFixedRequireString(self): """Tests that fixed string constains proper comments also.""" input_lines = [ 'goog.require(\'package.xyz\');', '/** This is needed for scope. **/', 'goog.require(\'package.abcd\');' ] expected_lines = [ '/** This is needed for scope. **/', 'goog.require(\'package.abcd\');', 'goog.require(\'package.xyz\');' ] token = testutil.TokenizeSourceAndRunEcmaPass(input_lines) sorter = requireprovidesorter.RequireProvideSorter() fixed_require_string = sorter.GetFixedRequireString(token) self.assertEquals(expected_lines, fixed_require_string.splitlines())
def assertAlias(self, expected_match, script): start_token = testutil.TokenizeSourceAndRunEcmaPass(script) statement = _FindFirstContextOfType( start_token, ecmametadatapass.EcmaContext.STATEMENT) match = scopeutil.MatchAlias(statement) self.assertEquals(expected_match, match)
def setUp(self): self.start_token = testutil.TokenizeSourceAndRunEcmaPass(_TEST_SCRIPT)
def _GetBlocks(source): start_token = testutil.TokenizeSourceAndRunEcmaPass(source) for context in _FindContexts(start_token): if context.type is ecmametadatapass.EcmaContext.BLOCK: yield context