| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # | |
| 3 # Copyright 2012 The Closure Linter Authors. All Rights Reserved. | |
| 4 # Licensed under the Apache License, Version 2.0 (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 | |
| 7 # | |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 # | |
| 10 # Unless required by applicable law or agreed to in writing, software | |
| 11 # distributed under the License is distributed on an "AS-IS" BASIS, | |
| 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 # See the License for the specific language governing permissions and | |
| 14 # limitations under the License. | |
| 15 | |
| 16 """Unit tests for the statetracker module.""" | |
| 17 | |
| 18 # Allow non-Google copyright | |
| 19 # pylint: disable=g-bad-file-header | |
| 20 | |
| 21 __author__ = ('nnaze@google.com (Nathan Naze)') | |
| 22 | |
| 23 | |
| 24 | |
| 25 import unittest as googletest | |
| 26 | |
| 27 from closure_linter import javascripttokens | |
| 28 from closure_linter import statetracker | |
| 29 from closure_linter import testutil | |
| 30 | |
| 31 | |
| 32 class _FakeDocFlag(object): | |
| 33 | |
| 34 def __repr__(self): | |
| 35 return '@%s %s' % (self.flag_type, self.name) | |
| 36 | |
| 37 | |
| 38 class IdentifierTest(googletest.TestCase): | |
| 39 | |
| 40 def testJustIdentifier(self): | |
| 41 a = javascripttokens.JavaScriptToken( | |
| 42 'abc', javascripttokens.JavaScriptTokenType.IDENTIFIER, 'abc', 1) | |
| 43 | |
| 44 st = statetracker.StateTracker() | |
| 45 st.HandleToken(a, None) | |
| 46 | |
| 47 | |
| 48 class DocCommentTest(googletest.TestCase): | |
| 49 | |
| 50 @staticmethod | |
| 51 def _MakeDocFlagFake(flag_type, name=None): | |
| 52 flag = _FakeDocFlag() | |
| 53 flag.flag_type = flag_type | |
| 54 flag.name = name | |
| 55 return flag | |
| 56 | |
| 57 def testDocFlags(self): | |
| 58 comment = statetracker.DocComment(None) | |
| 59 | |
| 60 a = self._MakeDocFlagFake('param', 'foo') | |
| 61 comment.AddFlag(a) | |
| 62 | |
| 63 b = self._MakeDocFlagFake('param', '') | |
| 64 comment.AddFlag(b) | |
| 65 | |
| 66 c = self._MakeDocFlagFake('param', 'bar') | |
| 67 comment.AddFlag(c) | |
| 68 | |
| 69 self.assertEquals( | |
| 70 ['foo', 'bar'], | |
| 71 comment.ordered_params) | |
| 72 | |
| 73 self.assertEquals( | |
| 74 [a, b, c], | |
| 75 comment.GetDocFlags()) | |
| 76 | |
| 77 def testInvalidate(self): | |
| 78 comment = statetracker.DocComment(None) | |
| 79 | |
| 80 self.assertFalse(comment.invalidated) | |
| 81 self.assertFalse(comment.IsInvalidated()) | |
| 82 | |
| 83 comment.Invalidate() | |
| 84 | |
| 85 self.assertTrue(comment.invalidated) | |
| 86 self.assertTrue(comment.IsInvalidated()) | |
| 87 | |
| 88 def testSuppressionOnly(self): | |
| 89 comment = statetracker.DocComment(None) | |
| 90 | |
| 91 self.assertFalse(comment.SuppressionOnly()) | |
| 92 comment.AddFlag(self._MakeDocFlagFake('suppress')) | |
| 93 self.assertTrue(comment.SuppressionOnly()) | |
| 94 comment.AddFlag(self._MakeDocFlagFake('foo')) | |
| 95 self.assertFalse(comment.SuppressionOnly()) | |
| 96 | |
| 97 def testRepr(self): | |
| 98 comment = statetracker.DocComment(None) | |
| 99 comment.AddFlag(self._MakeDocFlagFake('param', 'foo')) | |
| 100 comment.AddFlag(self._MakeDocFlagFake('param', 'bar')) | |
| 101 | |
| 102 self.assertEquals( | |
| 103 '<DocComment: [\'foo\', \'bar\'], [@param foo, @param bar]>', | |
| 104 repr(comment)) | |
| 105 | |
| 106 def testDocFlagParam(self): | |
| 107 comment = self._ParseComment(""" | |
| 108 /** | |
| 109 * @param {string} [name] Name of customer. | |
| 110 */""") | |
| 111 flag = comment.GetFlag('param') | |
| 112 self.assertEquals('string', flag.type) | |
| 113 self.assertEquals('string', flag.jstype.ToString()) | |
| 114 self.assertEquals('[name]', flag.name) | |
| 115 | |
| 116 def _ParseComment(self, script): | |
| 117 """Parse a script that contains one comment and return it.""" | |
| 118 _, comments = testutil.ParseFunctionsAndComments(script) | |
| 119 self.assertEquals(1, len(comments)) | |
| 120 return comments[0] | |
| 121 | |
| 122 if __name__ == '__main__': | |
| 123 googletest.main() | |
| OLD | NEW |