OILS / core / util_test.py View on Github | oils.pub

63 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
9import unittest
10import sys
11
12from core import util # module under test
13
14# guard some tests that fail on Darwin
15IS_DARWIN = sys.platform == 'darwin'
16
17
18class UtilTest(unittest.TestCase):
19
20 def testDebugFile(self):
21 n = util.NullDebugFile()
22 n.write('foo')
23
24 def testRegexSearch(self):
25 cases = [
26 ('([a-z]+)([0-9]+)', 'foo123', ['foo123', 'foo', '123']),
27 (r'.*\.py', 'foo.py', ['foo.py']),
28 (r'.*\.py', 'abcd', None),
29 # The match is unanchored
30 (r'bc', 'abcd', ['bc']),
31 # The match is unanchored
32 (r'.c', 'abcd', ['bc']),
33 # Empty matches empty
34 None if IS_DARWIN else (r'', '', ['']),
35 (r'^$', '', ['']),
36 (r'^.$', '', None),
37 (r'(a*)(b*)', '', ['', '', '']),
38 (r'(a*)(b*)', 'aa', ['aa', 'aa', '']),
39 (r'(a*)(b*)', 'bb', ['bb', '', 'bb']),
40 (r'(a*)(b*)', 'aabb', ['aabb', 'aa', 'bb']),
41 (r'(a*(z)?)|(b*)', 'aaz', ['aaz', 'aaz', 'z', '']),
42 (r'(a*(z)?)|(b*)', 'bb', ['bb', '', '', 'bb']),
43 ]
44
45 # TODO:
46 #
47 # return a single list of length 2*(1 + nsub)
48 # 2 is for start and end, +1 is for 0
49 #
50 # indices = regex_search(...)
51 # indices[2*group] is start
52 # indices[2*group+1] is end
53 # group is from 0 ... n
54
55 for pat, s, expected in filter(None, cases):
56 #print('CASE %s' % pat)
57 actual = util.RegexSearch(pat, s)
58 #print('actual %r' % actual)
59 self.assertEqual(expected, actual)
60
61
62if __name__ == '__main__':
63 unittest.main()