Beispiel #1
0
  def testGetIdentifierStart(self):

    tokens = testutil.TokenizeSource("""
start1 . // comment
    prototype. /* another comment */
    end1

['edge'][case].prototype.
    end2 = function() {}
""")

    def _GetTokenStartingWith(token_starts_with):
      for t in tokens:
        if t.string.startswith(token_starts_with):
          return t

    self.assertEquals(
        'start1',
        tokenutil.GetIdentifierStart(_GetTokenStartingWith('end1')).string)

    self.assertEquals(
        'start1',
        tokenutil.GetIdentifierStart(_GetTokenStartingWith('start1')).string)

    self.assertEquals(
        None,
        tokenutil.GetIdentifierStart(_GetTokenStartingWith('end2')))
    def testGetMissingProvides_privatefunction(self):
        """Tests that unprovided private functions don't cause a missing provide."""
        input_lines = ['package.Foo_ = function() {};']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(0, len(namespaces_info.GetMissingProvides()))
    def testGetMissingRequires_required(self):
        """Tests that required namespaces don't cause a missing require."""
        input_lines = ['goog.require(\'package.Foo\');', 'package.Foo();']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(0, len(namespaces_info.GetMissingProvides()))
    def testIsExtraRequire_notClosurized(self):
        """Tests that requires of non-closurized namespaces are not extra."""
        input_lines = ['goog.require(\'notclosurized.Foo\');']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertFalse(namespaces_info.IsExtraRequire(token),
                         'Should not be extra since it is not closurized.')
    def testGetMissingProvides_unprovided(self):
        """Tests that unprovided functions cause a missing provide."""
        input_lines = ['package.Foo = function() {};']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(1, len(namespaces_info.GetMissingProvides()))
        self.assertTrue('package.Foo' in namespaces_info.GetMissingProvides())
    def testIsExtraProvide_notCreated(self):
        """Tests that provides for non-created namespaces are extra."""
        input_lines = ['goog.provide(\'package.Foo\');']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertTrue(namespaces_info.IsExtraProvide(token),
                        'Should be extra since it is not created.')
    def testGetMissingRequires_unrequired(self):
        """Tests that unrequired namespaces cause a missing require."""
        input_lines = ['package.Foo();']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(1, len(namespaces_info.GetMissingRequires()))
        self.assertTrue('package.Foo' in namespaces_info.GetMissingRequires())
    def testIsExtraRequire_notUsed(self):
        """Tests that requires for unused namespaces are extra."""
        input_lines = ['goog.require(\'package.Foo\');']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertTrue(namespaces_info.IsExtraRequire(token),
                        'Should be extra since it is not used.')
    def testIsExtraRequire_defaults(self):
        """Tests that there are no warnings about extra requires for test utils"""
        input_lines = ['goog.require(\'goog.testing.jsunit\');']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['goog'], [])

        self.assertFalse(namespaces_info.IsExtraRequire(token),
                         'Should not be extra since it is for testing.')
    def testGetWholeIdentifierString(self):
        """Tests that created identifiers satisfy usage of the identifier."""
        input_lines = ['package.Foo.', '    veryLong.', '    identifier;']

        token = testutil.TokenizeSource(input_lines)

        self.assertEquals('package.Foo.veryLong.identifier',
                          tokenutil.GetIdentifierForToken(token))

        self.assertEquals(None, tokenutil.GetIdentifierForToken(token.next))
    def testGetMissingRequires_createdIdentifier(self):
        """Tests that created identifiers satisfy usage of the identifier."""
        input_lines = [
            'package.Foo.methodName = function();', 'package.Foo.methodName();'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(0, len(namespaces_info.GetMissingRequires()))
    def testGetMissingRequires_provided(self):
        """Tests that provided namespaces satisfy identifiers on that namespace."""
        input_lines = [
            'goog.provide(\'package.Foo\');', 'package.Foo.methodName();'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(0, len(namespaces_info.GetMissingRequires()))
    def testIsFirstProvide(self):
        """Tests operation of the isFirstProvide method."""
        input_lines = [
            'goog.provide(\'package.Foo\');', 'package.Foo.methodName();'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertTrue(namespaces_info.IsFirstProvide(token))
    def testGetMissingProvides_providedIdentifier(self):
        """Tests that provided identifiers don't cause a missing provide."""
        input_lines = [
            'goog.provide(\'package.Foo.methodName\');',
            'package.Foo.methodName = function() {};'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(0, len(namespaces_info.GetMissingProvides()))
    def testGetWholeIdentifierString(self):
        """Tests that created identifiers satisfy usage of the identifier."""
        input_lines = ['package.Foo.', '    veryLong.', '    identifier;']
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = closurizednamespacesinfo.ClosurizedNamespacesInfo([],
                                                                            [])

        self.assertEquals('package.Foo.veryLong.identifier',
                          namespaces_info._GetWholeIdentifierString(token))
        self.assertEquals(
            None, namespaces_info._GetWholeIdentifierString(token.next))
    def testIsExtraProvide_created(self):
        """Tests that provides for created namespaces are not extra."""
        input_lines = [
            'goog.provide(\'package.Foo\');', 'package.Foo = function() {};'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertFalse(namespaces_info.IsExtraProvide(token),
                         'Should not be extra since it is created.')
    def testGetMissingRequires_requiredParentClass(self):
        """Tests that requiring a parent class of an object is sufficient to prevent
    a missing require on that object."""
        input_lines = [
            'goog.require(\'package.Foo\');', 'package.Foo.methodName();',
            'package.Foo.methodName(package.Foo.ObjectName);'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(0, len(namespaces_info.GetMissingRequires()))
    def testIsExtraRequire_used(self):
        """Tests that requires for used namespaces are not extra."""
        input_lines = [
            'goog.require(\'package.Foo\');',
            'var x = package.Foo.methodName();'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertFalse(namespaces_info.IsExtraRequire(token),
                         'Should not be extra since it is used.')
    def testIsExtraRequire_methodNotOnClass(self):
        """Tests that requiring a method not on a class is OK."""
        input_lines = [
            'goog.require(\'package.subpackage.method\');',
            'var x = package.subpackage.method()',
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertFalse(namespaces_info.IsExtraRequire(token),
                         'Methods can be required except on classes.')
    def testIsExtraRequire_constantOnClass(self):
        """Tests that requiring a constant on a class is extra."""
        input_lines = [
            'goog.require(\'package.Foo.CONSTANT\');',
            'var x = package.Foo.CONSTANT',
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertTrue(namespaces_info.IsExtraRequire(token),
                        'The class, not the constant, should be required.')
    def testIsExtraRequire_objectOnClass(self):
        """Tests that requiring an object on a class is extra."""
        input_lines = [
            'goog.require(\'package.Foo.Enum\');',
            'var x = package.Foo.Enum.VALUE1;',
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertTrue(
            namespaces_info.IsExtraRequire(token),
            'The whole class, not the object, should be required.')
    def testGetMissingProvides_providedParentIdentifier(self):
        """Tests that provided identifiers on a class don't cause a missing provide
    on objects attached to that class."""
        input_lines = [
            'goog.provide(\'package.foo.ClassName\');',
            'package.foo.ClassName.methodName = function() {};',
            'package.foo.ClassName.ObjectName = 1;',
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(0, len(namespaces_info.GetMissingProvides()))
    def testIsExtraRequire_usedIdentifier(self):
        """Tests that requires for used methods on classes are extra."""
        input_lines = [
            'goog.require(\'package.Foo.methodName\');',
            'var x = package.Foo.methodName();'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertTrue(
            namespaces_info.IsExtraRequire(token),
            'Should require the package, not the method specifically.')
    def testGetMissingRequires_objectOnClass(self):
        """Tests that we should require a class, not the object on the class."""
        input_lines = [
            'goog.require(\'package.Foo.Enum\');',
            'var x = package.Foo.Enum.VALUE1;',
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        self.assertEquals(
            1, len(namespaces_info.GetMissingRequires()),
            'The whole class, not the object, should be required.')
    def testIsExtraProvide_duplicate(self):
        """Tests that providing a namespace twice makes the second one extra."""
        input_lines = [
            'goog.provide(\'package.Foo\');', 'goog.provide(\'package.Foo\');',
            'package.Foo = function() {};'
        ]
        token = testutil.TokenizeSource(input_lines)
        namespaces_info = self._GetInitializedNamespacesInfo(
            token, ['package'], [])

        # Advance to the second goog.provide token.
        token = tokenutil.Search(token.next, TokenType.IDENTIFIER)

        self.assertTrue(namespaces_info.IsExtraProvide(token),
                        'Should be extra since it is already provided.')
Beispiel #26
0
  def testGetPreviousCodeToken(self):

    tokens = testutil.TokenizeSource("""
start1. // comment
    /* another comment */
    end1
""")

    def _GetTokenStartingWith(token_starts_with):
      for t in tokens:
        if t.string.startswith(token_starts_with):
          return t

    self.assertEquals(
        None,
        tokenutil.GetPreviousCodeToken(_GetTokenStartingWith('start1')))

    self.assertEquals(
        'start1.',
        tokenutil.GetPreviousCodeToken(_GetTokenStartingWith('end1')).string)
 def _GetRequireTokens(self, namespace):
     """Returns a list of tokens for a goog.require of the given namespace."""
     line_text = 'goog.require(\'' + namespace + '\');\n'
     return testutil.TokenizeSource([line_text])
    def _GetStartTokenAndNamespacesInfoForScript(self, script,
                                                 closurized_namespaces):

        token = testutil.TokenizeSource(script)
        return token, self._GetInitializedNamespacesInfo(
            token, closurized_namespaces, [])
Beispiel #29
0
  def testGetIdentifierForToken(self):

    tokens = testutil.TokenizeSource("""
start1.abc.def.prototype.
  onContinuedLine

(start2.abc.def
  .hij.klm
  .nop)

start3.abc.def
   .hij = function() {};

// An absurd multi-liner.
start4.abc.def.
   hij.
   klm = function() {};

start5 . aaa . bbb . ccc
  shouldntBePartOfThePreviousSymbol

start6.abc.def ghi.shouldntBePartOfThePreviousSymbol

var start7 = 42;

function start8() {

}

start9.abc. // why is there a comment here?
  def /* another comment */
  shouldntBePart

start10.abc // why is there a comment here?
  .def /* another comment */
  shouldntBePart

start11.abc. middle1.shouldNotBeIdentifier
""")

    def _GetTokenStartingWith(token_starts_with):
      for t in tokens:
        if t.string.startswith(token_starts_with):
          return t

    self.assertEquals(
        'start1.abc.def.prototype.onContinuedLine',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start1')))

    self.assertEquals(
        'start2.abc.def.hij.klm.nop',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start2')))

    self.assertEquals(
        'start3.abc.def.hij',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start3')))

    self.assertEquals(
        'start4.abc.def.hij.klm',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start4')))

    self.assertEquals(
        'start5.aaa.bbb.ccc',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start5')))

    self.assertEquals(
        'start6.abc.def',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start6')))

    self.assertEquals(
        'start7',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start7')))

    self.assertEquals(
        'start8',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start8')))

    self.assertEquals(
        'start9.abc.def',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start9')))

    self.assertEquals(
        'start10.abc.def',
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('start10')))

    self.assertIsNone(
        tokenutil.GetIdentifierForToken(_GetTokenStartingWith('middle1')))