| OLD | NEW |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. | 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without | 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are | 3 # modification, are permitted provided that the following conditions are |
| 4 # met: | 4 # met: |
| 5 # | 5 # |
| 6 # * Redistributions of source code must retain the above copyright | 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above | 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following | 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided | 10 # disclaimer in the documentation and/or other materials provided |
| 11 # with the distribution. | 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its | 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived | 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. | 14 # from this software without specific prior written permission. |
| 15 # | 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 import unittest | 28 import unittest |
| 29 from transition_keys import TransitionKey | 29 from transition_keys import TransitionKey, KeyEncoding |
| 30 from regex_parser import RegexParser | 30 from regex_parser import RegexParser |
| 31 | 31 |
| 32 class TransitionKeyTestCase(unittest.TestCase): | 32 class TransitionKeyTestCase(unittest.TestCase): |
| 33 | 33 |
| 34 __encoding = KeyEncoding.get('latin1') |
| 35 |
| 34 __equal_pairs = [ | 36 __equal_pairs = [ |
| 35 (TransitionKey.epsilon(), TransitionKey.epsilon()), | 37 (TransitionKey.epsilon(), TransitionKey.epsilon()), |
| 36 (TransitionKey.any(), TransitionKey.any()), | 38 (TransitionKey.any(__encoding), TransitionKey.any(__encoding)), |
| 37 (TransitionKey.single_char('a'), TransitionKey.single_char('a')), | 39 (TransitionKey.single_char(__encoding, 'a'), |
| 40 TransitionKey.single_char(__encoding, 'a')), |
| 38 ] | 41 ] |
| 39 | 42 |
| 40 def test_eq(self): | 43 def test_eq(self): |
| 41 for (left, right) in self.__equal_pairs: | 44 for (left, right) in self.__equal_pairs: |
| 42 self.assertEqual(left, right) | 45 self.assertEqual(left, right) |
| 43 | 46 |
| 44 def test_hash(self): | 47 def test_hash(self): |
| 45 for (left, right) in self.__equal_pairs: | 48 for (left, right) in self.__equal_pairs: |
| 46 self.assertEqual(hash(left), hash(right)) | 49 self.assertEqual(hash(left), hash(right)) |
| 47 | 50 |
| 48 def test_classes(self): | 51 def test_classes(self): |
| 49 # class regex, should match, should not match | 52 # class regex, should match, should not match |
| 50 data = [ | 53 data = [ |
| 51 ("1-2", "12", "ab"), | 54 ("1-2", "12", "ab"), |
| 52 ("a-zA-Z", "abyzABYZ" , "123"), | 55 ("a-zA-Z", "abyzABYZ" , "123"), |
| 53 ("a-zA-Z0g" , "abyzABYZ0" , "123"), | 56 ("a-zA-Z0g" , "abyzABYZ0" , "123"), |
| 54 ("a-z:whitespace::letter:" , "abc" , "123"), | 57 ("a-z:whitespace::letter:" , "abc" , "123"), |
| 55 ] | 58 ] |
| 56 classes = {} | 59 classes = {} |
| 60 encoding = self.__encoding |
| 57 for (string, match, no_match) in data: | 61 for (string, match, no_match) in data: |
| 58 for invert in [False, True]: | 62 for invert in [False, True]: |
| 59 if invert: | 63 if invert: |
| 60 regex = "[^%s]" % string | 64 regex = "[^%s]" % string |
| 61 token = "NOT_CLASS" | 65 token = "NOT_CLASS" |
| 62 else: | 66 else: |
| 63 regex = "[%s]" % string | 67 regex = "[%s]" % string |
| 64 token = "CLASS" | 68 token = "CLASS" |
| 65 graph = RegexParser.parse(regex) | 69 graph = RegexParser.parse(regex) |
| 66 assert graph[0] == token | 70 assert graph[0] == token |
| 67 key = TransitionKey.character_class(graph, classes) | 71 key = TransitionKey.character_class(encoding, graph, classes) |
| 68 for c in match: | 72 for c in match: |
| 69 self.assertEqual(invert, not key.matches_char(c)) | 73 self.assertEqual(invert, not key.matches_char(c)) |
| 70 for c in no_match: | 74 for c in no_match: |
| 71 self.assertEqual(invert, key.matches_char(c)) | 75 self.assertEqual(invert, key.matches_char(c)) |
| 72 | 76 |
| 73 def test_self_defined_classes(self): | 77 def test_self_defined_classes(self): |
| 78 encoding = self.__encoding |
| 74 graph = RegexParser.parse("[a-z]") | 79 graph = RegexParser.parse("[a-z]") |
| 75 classes = { | 80 classes = { |
| 76 'self_defined' : TransitionKey.character_class(graph, {})} | 81 'self_defined' : TransitionKey.character_class(encoding, graph, {})} |
| 77 graph = RegexParser.parse("[^:self_defined:]") | 82 graph = RegexParser.parse("[^:self_defined:]") |
| 78 key = TransitionKey.character_class(graph, classes) | 83 key = TransitionKey.character_class(encoding, graph, classes) |
| 79 self.assertTrue(key.matches_char('A')) | 84 self.assertTrue(key.matches_char('A')) |
| 80 | 85 |
| 81 | |
| 82 def test_disjoint_keys(self): | 86 def test_disjoint_keys(self): |
| 83 key1 = TransitionKey([(1, 10)]) | 87 encoding = self.__encoding |
| 84 key2 = TransitionKey([(5, 15)]) | 88 key1 = TransitionKey(encoding, [(1, 10)]) |
| 85 disjoint_set = TransitionKey.disjoint_keys(set([key1, key2])) | 89 key2 = TransitionKey(encoding, [(5, 15)]) |
| 86 self.assertTrue(TransitionKey([(1, 4)]) in disjoint_set) | 90 disjoint_set = TransitionKey.disjoint_keys(encoding, set([key1, key2])) |
| 87 self.assertTrue(TransitionKey([(5, 10)]) in disjoint_set) | 91 self.assertTrue(TransitionKey(encoding, [(1, 4)]) in disjoint_set) |
| 88 self.assertTrue(TransitionKey([(11, 15)]) in disjoint_set) | 92 self.assertTrue(TransitionKey(encoding, [(5, 10)]) in disjoint_set) |
| 93 self.assertTrue(TransitionKey(encoding, [(11, 15)]) in disjoint_set) |
| OLD | NEW |