OILS / core / util_test.py View on Github | oilshell.org

64 lines, 30 significant
1#!/usr/bin/env python2
2# Copyright 2016 Andy Chu. All rights reserved.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8"""util_test.py: Tests for util.py."""
9
10import unittest
11import sys
12
13from core import util # module under test
14
15# guard some tests that fail on Darwin
16IS_DARWIN = sys.platform == 'darwin'
17
18
19class UtilTest(unittest.TestCase):
20
21 def testDebugFile(self):
22 n = util.NullDebugFile()
23 n.write('foo')
24
25 def testRegexSearch(self):
26 cases = [
27 ('([a-z]+)([0-9]+)', 'foo123', ['foo123', 'foo', '123']),
28 (r'.*\.py', 'foo.py', ['foo.py']),
29 (r'.*\.py', 'abcd', None),
30 # The match is unanchored
31 (r'bc', 'abcd', ['bc']),
32 # The match is unanchored
33 (r'.c', 'abcd', ['bc']),
34 # Empty matches empty
35 None if IS_DARWIN else (r'', '', ['']),
36 (r'^$', '', ['']),
37 (r'^.$', '', None),
38 (r'(a*)(b*)', '', ['', '', '']),
39 (r'(a*)(b*)', 'aa', ['aa', 'aa', '']),
40 (r'(a*)(b*)', 'bb', ['bb', '', 'bb']),
41 (r'(a*)(b*)', 'aabb', ['aabb', 'aa', 'bb']),
42 (r'(a*(z)?)|(b*)', 'aaz', ['aaz', 'aaz', 'z', '']),
43 (r'(a*(z)?)|(b*)', 'bb', ['bb', '', '', 'bb']),
44 ]
45
46 # TODO:
47 #
48 # return a single list of length 2*(1 + nsub)
49 # 2 is for start and end, +1 is for 0
50 #
51 # indices = regex_search(...)
52 # indices[2*group] is start
53 # indices[2*group+1] is end
54 # group is from 0 ... n
55
56 for pat, s, expected in filter(None, cases):
57 #print('CASE %s' % pat)
58 actual = util.RegexSearch(pat, s)
59 #print('actual %r' % actual)
60 self.assertEqual(expected, actual)
61
62
63if __name__ == '__main__':
64 unittest.main()