|
6 | 6 | class VerExTest(unittest.TestCase): |
7 | 7 | ''' |
8 | 8 | Tests for verbal_expressions.py |
9 | | - |
10 | | - The "range" method test is lacking, because I couldn't really understand its use. |
11 | | - I could use some help. :) |
12 | 9 | ''' |
| 10 | + |
13 | 11 | def setUp(self): |
14 | 12 | self.v = VerEx() |
15 | | - |
| 13 | + |
16 | 14 | def tearDown(self): |
17 | 15 | self.v = None |
18 | 16 | self.exp = None |
19 | | - |
20 | | - def test__str__(self): |
| 17 | + |
| 18 | + def test_should_render_verex_as_string(self): |
21 | 19 | self.assertEquals(str(self.v.add('^$')), '^$') |
22 | | - |
23 | | - def test_start_of_line(self): |
| 20 | + |
| 21 | + def test_should_match_characters_in_range(self): |
| 22 | + self.exp = self.v.start_of_line().range('a', 'c').regex() |
| 23 | + for character in ['a', 'b', 'c']: |
| 24 | + self.assertRegexpMatches(character, self.exp) |
| 25 | + |
| 26 | + def test_should_not_match_characters_outside_of_range(self): |
| 27 | + self.exp = self.v.start_of_line().range('a', 'c').regex() |
| 28 | + self.assertNotRegexpMatches('d', self.exp) |
| 29 | + |
| 30 | + def test_should_match_characters_in_extended_range(self): |
| 31 | + self.exp = self.v.start_of_line().range('a', 'b', 'X', 'Z').regex() |
| 32 | + for character in ['a', 'b']: |
| 33 | + self.assertRegexpMatches(character, self.exp) |
| 34 | + for character in ['X', 'Y', 'Z']: |
| 35 | + self.assertRegexpMatches(character, self.exp) |
| 36 | + |
| 37 | + def test_should_not_match_characters_outside_of_extended_range(self): |
| 38 | + self.exp = self.v.start_of_line().range('a', 'b', 'X', 'Z').regex() |
| 39 | + self.assertNotRegexpMatches('c', self.exp) |
| 40 | + self.assertNotRegexpMatches('W', self.exp) |
| 41 | + |
| 42 | + |
| 43 | + def test_should_match_start_of_line(self): |
24 | 44 | self.exp = self.v.start_of_line().regex() |
25 | 45 | self.assertRegexpMatches('text ', self.exp, 'Not started :(') |
26 | | - |
27 | | - def test_end_of_line(self): |
| 46 | + |
| 47 | + def test_should_match_end_of_line(self): |
28 | 48 | self.exp = self.v.start_of_line().end_of_line().regex() |
29 | 49 | self.assertRegexpMatches('', self.exp, 'It\'s not the end!') |
30 | | - |
31 | | - def test_anything(self): |
| 50 | + |
| 51 | + def test_should_match_anything(self): |
32 | 52 | self.exp = self.v.start_of_line().anything().end_of_line().regex() |
33 | 53 | self.assertRegexpMatches('!@#$%¨&*()__+{}', self.exp, 'Not so anything...') |
34 | | - |
35 | | - def test_anything_but(self): |
| 54 | + |
| 55 | + def test_should_match_anything_but_specified_element_when_element_is_not_found(self): |
36 | 56 | self.exp = self.v.start_of_line().anything_but('X').end_of_line().regex() |
37 | 57 | self.assertRegexpMatches('Y Files', self.exp, 'Found the X!') |
38 | | - |
39 | | - def test_anything_but_false(self): |
| 58 | + |
| 59 | + def test_should_not_match_anything_but_specified_element_when_specified_element_is_found(self): |
40 | 60 | self.exp = self.v.start_of_line().anything_but('X').end_of_line().regex() |
41 | | - self.assertFalse(re.match(self.exp, 'VerEX'), 'Didn\'t found the X :(') |
| 61 | + self.assertNotRegexpMatches('VerEX', self.exp, 'Didn\'t found the X :(') |
42 | 62 |
|
43 | | - def test_find_true(self): |
| 63 | + def test_should_find_element(self): |
44 | 64 | self.exp = self.v.start_of_line().find('Wally').end_of_line().regex() |
45 | 65 | self.assertRegexpMatches('Wally', self.exp, '404! Wally not Found!') |
46 | 66 |
|
47 | | - def test_find_false(self): |
| 67 | + def test_should_not_find_missing_element(self): |
48 | 68 | self.exp = self.v.start_of_line().find('Wally').end_of_line().regex() |
49 | | - self.assertFalse(re.match(self.exp, 'Wall-e'), 'DAFUQ is Wall-e?') |
| 69 | + self.assertNotRegexpMatches('Wall-e', self.exp, 'DAFUQ is Wall-e?') |
50 | 70 |
|
51 | | - def test_maybe(self): |
| 71 | + def test_should_match_when_maybe_element_is_present(self): |
52 | 72 | self.exp = self.v.start_of_line().find('Python2.').maybe('7').end_of_line().regex() |
53 | 73 | self.assertRegexpMatches('Python2.7', self.exp, 'Version doesn\'t match!') |
54 | | - |
55 | | - def test_any_true(self): |
| 74 | + |
| 75 | + def test_should_match_when_maybe_element_is_missing(self): |
| 76 | + self.exp = self.v.start_of_line().find('Python2.').maybe('7').end_of_line().regex() |
| 77 | + self.assertRegexpMatches('Python2.', self.exp, 'Version doesn\'t match!') |
| 78 | + |
| 79 | + def test_should_match_on_any_when_element_is_found(self): |
56 | 80 | self.exp = self.v.start_of_line().any('Q').anything().end_of_line().regex() |
57 | 81 | self.assertRegexpMatches('Query', self.exp, 'No match found!') |
58 | | - |
59 | | - def test_any_false(self): |
| 82 | + |
| 83 | + def test_should_not_match_on_any_when_element_is_not_found(self): |
60 | 84 | self.exp = self.v.start_of_line().any('Q').anything().end_of_line().regex() |
61 | | - self.assertFalse(re.match(self.exp, 'W'), 'I\'ve found it!') |
| 85 | + self.assertNotRegexpMatches('W', self.exp, 'I\'ve found it!') |
62 | 86 |
|
63 | | - def test_line_break_true_n(self): |
| 87 | + def test_should_match_when_line_break_present(self): |
64 | 88 | self.exp = self.v.start_of_line().anything().line_break().anything().end_of_line().regex() |
65 | 89 | self.assertRegexpMatches('Marco \n Polo', self.exp, 'Give me a break!!') |
66 | | - |
67 | | - def test_line_break_true_rn(self): |
| 90 | + |
| 91 | + def test_should_match_when_line_break_and_carriage_return_present(self): |
68 | 92 | self.exp = self.v.start_of_line().anything().line_break().anything().end_of_line().regex() |
69 | 93 | self.assertRegexpMatches('Marco \r\n Polo', self.exp, 'Give me a break!!') |
70 | | - |
71 | | - def test_line_break_false(self): |
| 94 | + |
| 95 | + def test_should_not_match_when_line_break_is_missing(self): |
72 | 96 | self.exp = self.v.start_of_line().anything().line_break().anything().end_of_line().regex() |
73 | | - self.assertFalse(re.match(self.exp, 'Marco Polo'), 'There\'s a break here!') |
74 | | - |
75 | | - def test_tab_true(self): |
| 97 | + self.assertNotRegexpMatches('Marco Polo', self.exp, 'There\'s a break here!') |
| 98 | + |
| 99 | + def test_should_match_when_tab_present(self): |
76 | 100 | self.exp = self.v.start_of_line().anything().tab().end_of_line().regex() |
77 | 101 | self.assertRegexpMatches('One tab only ', self.exp, 'No tab here!') |
78 | | - |
79 | | - def test_tab_false(self): |
| 102 | + |
| 103 | + def test_should_not_match_when_tab_is_missing(self): |
80 | 104 | self.exp = self.v.start_of_line().anything().tab().end_of_line().regex() |
81 | 105 | self.assertFalse(re.match(self.exp, 'No tab here'), 'There\'s a tab here!') |
82 | | - |
83 | | - def test_word_true(self): |
| 106 | + |
| 107 | + def test_should_match_when_word_present(self): |
84 | 108 | self.exp = self.v.start_of_line().anything().word().end_of_line().regex() |
85 | 109 | self.assertRegexpMatches('Oneword', self.exp, 'Not just a word!') |
86 | | - |
87 | | - def test_word_false(self): |
| 110 | + |
| 111 | + def test_not_match_when_two_words_are_present_instead_of_one(self): |
88 | 112 | self.exp = self.v.start_of_line().anything().tab().end_of_line().regex() |
89 | 113 | self.assertFalse(re.match(self.exp, 'Two words'), 'I\'ve found two of them') |
90 | | - |
91 | | - def test_or_true(self): |
| 114 | + |
| 115 | + def test_should_match_when_or_condition_fulfilled(self): |
92 | 116 | self.exp = self.v.start_of_line().anything().find('G').OR().find('h').end_of_line().regex() |
93 | 117 | self.assertRegexpMatches('Github', self.exp, 'Octocat not found') |
94 | | - |
95 | | - def test_or_false(self): |
| 118 | + |
| 119 | + def test_should_not_match_when_or_condition_not_fulfilled(self): |
96 | 120 | self.exp = self.v.start_of_line().anything().find('G').OR().find('h').end_of_line().regex() |
97 | 121 | self.assertFalse(re.match(self.exp, 'Bitbucket'), 'Bucket not found') |
98 | | - |
99 | | - def test_any_case(self): |
| 122 | + |
| 123 | + def test_should_match_on_upper_case_when_lower_case_is_given_and_any_case_is_true(self): |
100 | 124 | self.exp = self.v.start_of_line().find('THOR').end_of_line().with_any_case(True).regex() |
101 | 125 | self.assertRegexpMatches('thor', self.exp, 'Upper case Thor, please!') |
102 | | - |
103 | | - def test_multi_line(self): |
| 126 | + |
| 127 | + def test_should_match_multiple_lines(self): |
104 | 128 | self.exp = self.v.start_of_line().anything().find('Pong').anything().end_of_line().search_one_line(True).regex() |
105 | 129 | self.assertRegexpMatches('Ping \n Pong \n Ping', self.exp, 'Pong didn\'t answer') |
106 | | - |
107 | | - def test_email(self): |
| 130 | + |
| 131 | + def test_should_match_email_address(self): |
108 | 132 | self.exp = self.v.start_of_line().word().then('@').word().then('.').word().end_of_line().regex() |
109 | 133 | self.assertRegexpMatches('mail@mail.com', self.exp, 'Not a valid email') |
110 | | - |
111 | | - def test_url(self): |
| 134 | + |
| 135 | + def test_should_match_url(self): |
112 | 136 | self.exp = self.v.start_of_line().then('http').maybe('s').then('://').maybe('www.').word().then('.').word().maybe('/').end_of_line().regex() |
113 | 137 | self.assertRegexpMatches('https://www.google.com/', self.exp, 'Not a valid email') |
0 commit comments