2 # Copyright 2011 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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 __author__
= 'nnaze@google.com (Nathan Naze)'
19 import unittest
as googletest
20 from closure_linter
.common
import tokens
23 def _CreateDummyToken():
24 return tokens
.Token('foo', None, 1, 1)
27 def _CreateDummyTokens(count
):
29 for _
in xrange(count
):
30 dummy_tokens
.append(_CreateDummyToken())
34 def _SetTokensAsNeighbors(neighbor_tokens
):
35 for i
in xrange(len(neighbor_tokens
)):
40 neighbor_tokens
[i
].previous
= neighbor_tokens
[prev_index
]
42 if next_index
< len(neighbor_tokens
):
43 neighbor_tokens
[i
].next
= neighbor_tokens
[next_index
]
46 class TokensTest(googletest
.TestCase
):
48 def testIsFirstInLine(self
):
50 # First token in file (has no previous).
51 self
.assertTrue(_CreateDummyToken().IsFirstInLine())
53 a
, b
= _CreateDummyTokens(2)
54 _SetTokensAsNeighbors([a
, b
])
60 self
.assertFalse(b
.IsFirstInLine())
62 # Tokens on different lines
64 self
.assertTrue(b
.IsFirstInLine())
66 def testIsLastInLine(self
):
67 # Last token in file (has no next).
68 self
.assertTrue(_CreateDummyToken().IsLastInLine())
70 a
, b
= _CreateDummyTokens(2)
71 _SetTokensAsNeighbors([a
, b
])
76 self
.assertFalse(a
.IsLastInLine())
79 self
.assertTrue(a
.IsLastInLine())
82 a
= tokens
.Token('foo', 'fakeType1', 1, 1)
83 self
.assertTrue(a
.IsType('fakeType1'))
84 self
.assertFalse(a
.IsType('fakeType2'))
86 def testIsAnyType(self
):
87 a
= tokens
.Token('foo', 'fakeType1', 1, 1)
88 self
.assertTrue(a
.IsAnyType(['fakeType1', 'fakeType2']))
89 self
.assertFalse(a
.IsAnyType(['fakeType3', 'fakeType4']))
92 a
= tokens
.Token('foo', 'fakeType1', 1, 1)
93 self
.assertEquals('<Token: fakeType1, "foo", None, 1, None>', str(a
))
96 dummy_tokens
= _CreateDummyTokens(5)
97 _SetTokensAsNeighbors(dummy_tokens
)
98 a
, b
, c
, d
, e
= dummy_tokens
101 self
.assertListEqual([a
, b
, c
, d
, e
], list(i
))
103 def testReverseIter(self
):
104 dummy_tokens
= _CreateDummyTokens(5)
105 _SetTokensAsNeighbors(dummy_tokens
)
106 a
, b
, c
, d
, e
= dummy_tokens
109 self
.assertListEqual([e
, d
, c
, b
, a
], list(ri
))
112 if __name__
== '__main__':