Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: third_party/closure_linter/closure_linter/aliaspass_test.py

Issue 2328693002: Updated linter with upstream release (2.3.19) (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright 2012 The Closure Linter Authors. All Rights Reserved. 3 # Copyright 2012 The Closure Linter Authors. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License. 5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at 6 # You may obtain a copy of the License at
7 # 7 #
8 # http://www.apache.org/licenses/LICENSE-2.0 8 # http://www.apache.org/licenses/LICENSE-2.0
9 # 9 #
10 # Unless required by applicable law or agreed to in writing, software 10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS-IS" BASIS, 11 # distributed under the License is distributed on an "AS-IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and 13 # See the License for the specific language governing permissions and
14 # limitations under the License. 14 # limitations under the License.
15 15
16 """Unit tests for the aliaspass module.""" 16 """Unit tests for the aliaspass module."""
17 17
18 # Allow non-Google copyright 18 # Allow non-Google copyright
19 # pylint: disable=g-bad-file-header 19 # pylint: disable=g-bad-file-header
20 20
21 __author__ = ('nnaze@google.com (Nathan Naze)') 21 __author__ = ('nnaze@google.com (Nathan Naze)')
22 22
23 import unittest as googletest 23 import unittest as googletest
24 24
25 from closure_linter import aliaspass 25 from closure_linter import aliaspass
26 from closure_linter import errors 26 from closure_linter import errors
27 from closure_linter import javascriptstatetracker
27 from closure_linter import testutil 28 from closure_linter import testutil
28 from closure_linter.common import erroraccumulator 29 from closure_linter.common import erroraccumulator
29 30
30 31
31 def _GetTokenByLineAndString(start_token, string, line_number): 32 def _GetTokenByLineAndString(start_token, string, line_number):
32 for token in start_token: 33 for token in start_token:
33 if token.line_number == line_number and token.string == string: 34 if token.line_number == line_number and token.string == string:
34 return token 35 return token
35 36
36 37
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 component_token.metadata.aliased_symbol) 69 component_token.metadata.aliased_symbol)
69 70
70 event_token = _GetTokenByLineAndString(start_token, 'Event.Something', 17) 71 event_token = _GetTokenByLineAndString(start_token, 'Event.Something', 17)
71 self.assertEquals('goog.events.Event.Something', 72 self.assertEquals('goog.events.Event.Something',
72 event_token.metadata.aliased_symbol) 73 event_token.metadata.aliased_symbol)
73 74
74 non_closurized_token = _GetTokenByLineAndString( 75 non_closurized_token = _GetTokenByLineAndString(
75 start_token, 'NonClosurizedClass', 18) 76 start_token, 'NonClosurizedClass', 18)
76 self.assertIsNone(non_closurized_token.metadata.aliased_symbol) 77 self.assertIsNone(non_closurized_token.metadata.aliased_symbol)
77 78
78 long_start_token = _GetTokenByLineAndString(start_token, 'Event.', 21) 79 long_start_token = _GetTokenByLineAndString(start_token, 'Event', 24)
79 self.assertEquals('goog.events.Event.MultilineIdentifier.someMethod', 80 self.assertEquals('goog.events.Event.MultilineIdentifier.someMethod',
80 long_start_token.metadata.aliased_symbol) 81 long_start_token.metadata.aliased_symbol)
81 82
83 def testAliasedDoctypes(self):
84 """Tests that aliases are correctly expanded within type annotations."""
85 start_token = testutil.TokenizeSourceAndRunEcmaPass(_TEST_ALIAS_SCRIPT)
86 tracker = javascriptstatetracker.JavaScriptStateTracker()
87 tracker.DocFlagPass(start_token, error_handler=None)
88
89 alias_pass = aliaspass.AliasPass(set(['goog', 'myproject']))
90 alias_pass.Process(start_token)
91
92 flag_token = _GetTokenByLineAndString(start_token, '@type', 22)
93 self.assertEquals(
94 'goog.events.Event.<goog.ui.Component,Array<myproject.foo.MyClass>>',
95 repr(flag_token.attached_object.jstype))
96
97 def testModuleAlias(self):
98 start_token = testutil.TokenizeSourceAndRunEcmaPass("""
99 goog.module('goog.test');
100 var Alias = goog.require('goog.Alias');
101 Alias.use();
102 """)
103 alias_pass = aliaspass.AliasPass(set(['goog']))
104 alias_pass.Process(start_token)
105 alias_token = _GetTokenByLineAndString(start_token, 'Alias', 3)
106 self.assertTrue(alias_token.metadata.is_alias_definition)
107
82 def testMultipleGoogScopeCalls(self): 108 def testMultipleGoogScopeCalls(self):
83 start_token = testutil.TokenizeSourceAndRunEcmaPass( 109 start_token = testutil.TokenizeSourceAndRunEcmaPass(
84 _TEST_MULTIPLE_SCOPE_SCRIPT) 110 _TEST_MULTIPLE_SCOPE_SCRIPT)
85 111
86 error_accumulator = erroraccumulator.ErrorAccumulator() 112 error_accumulator = erroraccumulator.ErrorAccumulator()
87 113
88 alias_pass = aliaspass.AliasPass( 114 alias_pass = aliaspass.AliasPass(
89 set(['goog', 'myproject']), 115 set(['goog', 'myproject']),
90 error_handler=error_accumulator) 116 error_handler=error_accumulator)
91 alias_pass.Process(start_token) 117 alias_pass.Process(start_token)
(...skipping 27 matching lines...) Expand all
119 145
120 var Component = goog.ui.Component; // scope alias 146 var Component = goog.ui.Component; // scope alias
121 var MyClass = myproject.foo.MyClass; // scope alias 147 var MyClass = myproject.foo.MyClass; // scope alias
122 148
123 // Scope alias of non-Closurized namespace. 149 // Scope alias of non-Closurized namespace.
124 var NonClosurizedClass = aaa.bbb.NonClosurizedClass; 150 var NonClosurizedClass = aaa.bbb.NonClosurizedClass;
125 151
126 var component = new Component(Event.Something); 152 var component = new Component(Event.Something);
127 var nonClosurized = NonClosurizedClass(); 153 var nonClosurized = NonClosurizedClass();
128 154
129 // A created namespace with a really long identifier. 155 /**
156 * A created namespace with a really long identifier.
157 * @type {events.Event.<Component,Array<MyClass>}
158 */
130 Event. 159 Event.
131 MultilineIdentifier. 160 MultilineIdentifier.
132 someMethod = function() {}; 161 someMethod = function() {};
133 }); 162 });
134 """ 163 """
135 164
136 _TEST_SCOPE_SCRIPT = """ 165 _TEST_SCOPE_SCRIPT = """
137 function foo () { 166 function foo () {
138 // This goog.scope call is invalid. 167 // This goog.scope call is invalid.
139 goog.scope(function() { 168 goog.scope(function() {
(...skipping 13 matching lines...) Expand all
153 182
154 // This goog.scope invalid. There can be only one. 183 // This goog.scope invalid. There can be only one.
155 goog.scope(function() { 184 goog.scope(function() {
156 185
157 }); 186 });
158 """ 187 """
159 188
160 189
161 if __name__ == '__main__': 190 if __name__ == '__main__':
162 googletest.main() 191 googletest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698