Show More
@@ -1,232 +1,232 b'' | |||||
1 | from __future__ import absolute_import |
|
1 | from __future__ import absolute_import | |
2 |
|
2 | |||
3 | import unittest |
|
3 | import unittest | |
4 | from mercurial import error, mdiff |
|
4 | from mercurial import error, mdiff | |
5 |
|
5 | |||
6 | # for readability, line numbers are 0-origin |
|
6 | # for readability, line numbers are 0-origin | |
7 | text1 = ''' |
|
7 | text1 = b''' | |
8 | 00 at OLD |
|
8 | 00 at OLD | |
9 | 01 at OLD |
|
9 | 01 at OLD | |
10 | 02 at OLD |
|
10 | 02 at OLD | |
11 | 02 at NEW, 03 at OLD |
|
11 | 02 at NEW, 03 at OLD | |
12 | 03 at NEW, 04 at OLD |
|
12 | 03 at NEW, 04 at OLD | |
13 | 04 at NEW, 05 at OLD |
|
13 | 04 at NEW, 05 at OLD | |
14 | 05 at NEW, 06 at OLD |
|
14 | 05 at NEW, 06 at OLD | |
15 | 07 at OLD |
|
15 | 07 at OLD | |
16 | 08 at OLD |
|
16 | 08 at OLD | |
17 | 09 at OLD |
|
17 | 09 at OLD | |
18 | 10 at OLD |
|
18 | 10 at OLD | |
19 | 11 at OLD |
|
19 | 11 at OLD | |
20 | '''[1:] # strip initial LF |
|
20 | '''[1:] # strip initial LF | |
21 |
|
21 | |||
22 | text2 = ''' |
|
22 | text2 = b''' | |
23 | 00 at NEW |
|
23 | 00 at NEW | |
24 | 01 at NEW |
|
24 | 01 at NEW | |
25 | 02 at NEW, 03 at OLD |
|
25 | 02 at NEW, 03 at OLD | |
26 | 03 at NEW, 04 at OLD |
|
26 | 03 at NEW, 04 at OLD | |
27 | 04 at NEW, 05 at OLD |
|
27 | 04 at NEW, 05 at OLD | |
28 | 05 at NEW, 06 at OLD |
|
28 | 05 at NEW, 06 at OLD | |
29 | 06 at NEW |
|
29 | 06 at NEW | |
30 | 07 at NEW |
|
30 | 07 at NEW | |
31 | 08 at NEW |
|
31 | 08 at NEW | |
32 | 09 at NEW |
|
32 | 09 at NEW | |
33 | 10 at NEW |
|
33 | 10 at NEW | |
34 | 11 at NEW |
|
34 | 11 at NEW | |
35 | '''[1:] # strip initial LF |
|
35 | '''[1:] # strip initial LF | |
36 |
|
36 | |||
37 | def filteredblocks(blocks, rangeb): |
|
37 | def filteredblocks(blocks, rangeb): | |
38 | """return `rangea` extracted from `blocks` coming from |
|
38 | """return `rangea` extracted from `blocks` coming from | |
39 | `mdiff.blocksinrange` along with the mask of blocks within rangeb. |
|
39 | `mdiff.blocksinrange` along with the mask of blocks within rangeb. | |
40 | """ |
|
40 | """ | |
41 | filtered, rangea = mdiff.blocksinrange(blocks, rangeb) |
|
41 | filtered, rangea = mdiff.blocksinrange(blocks, rangeb) | |
42 | skipped = [b not in filtered for b in blocks] |
|
42 | skipped = [b not in filtered for b in blocks] | |
43 | return rangea, skipped |
|
43 | return rangea, skipped | |
44 |
|
44 | |||
45 | class blocksinrangetests(unittest.TestCase): |
|
45 | class blocksinrangetests(unittest.TestCase): | |
46 |
|
46 | |||
47 | def setUp(self): |
|
47 | def setUp(self): | |
48 | self.blocks = list(mdiff.allblocks(text1, text2)) |
|
48 | self.blocks = list(mdiff.allblocks(text1, text2)) | |
49 | assert self.blocks == [ |
|
49 | assert self.blocks == [ | |
50 | ([0, 3, 0, 2], '!'), |
|
50 | ([0, 3, 0, 2], b'!'), | |
51 | ((3, 7, 2, 6), '='), |
|
51 | ((3, 7, 2, 6), b'='), | |
52 | ([7, 12, 6, 12], '!'), |
|
52 | ([7, 12, 6, 12], b'!'), | |
53 | ((12, 12, 12, 12), '='), |
|
53 | ((12, 12, 12, 12), b'='), | |
54 | ], self.blocks |
|
54 | ], self.blocks | |
55 |
|
55 | |||
56 | def testWithinEqual(self): |
|
56 | def testWithinEqual(self): | |
57 | """linerange within an "=" block""" |
|
57 | """linerange within an "=" block""" | |
58 | # IDX 0 1 |
|
58 | # IDX 0 1 | |
59 | # 012345678901 |
|
59 | # 012345678901 | |
60 | # SRC NNOOOONNNNNN (New/Old) |
|
60 | # SRC NNOOOONNNNNN (New/Old) | |
61 | # ^^ |
|
61 | # ^^ | |
62 | linerange2 = (3, 5) |
|
62 | linerange2 = (3, 5) | |
63 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
63 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
64 | self.assertEqual(linerange1, (4, 6)) |
|
64 | self.assertEqual(linerange1, (4, 6)) | |
65 | self.assertEqual(skipped, [True, False, True, True]) |
|
65 | self.assertEqual(skipped, [True, False, True, True]) | |
66 |
|
66 | |||
67 | def testWithinEqualStrictly(self): |
|
67 | def testWithinEqualStrictly(self): | |
68 | """linerange matching exactly an "=" block""" |
|
68 | """linerange matching exactly an "=" block""" | |
69 | # IDX 0 1 |
|
69 | # IDX 0 1 | |
70 | # 012345678901 |
|
70 | # 012345678901 | |
71 | # SRC NNOOOONNNNNN (New/Old) |
|
71 | # SRC NNOOOONNNNNN (New/Old) | |
72 | # ^^^^ |
|
72 | # ^^^^ | |
73 | linerange2 = (2, 6) |
|
73 | linerange2 = (2, 6) | |
74 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
74 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
75 | self.assertEqual(linerange1, (3, 7)) |
|
75 | self.assertEqual(linerange1, (3, 7)) | |
76 | self.assertEqual(skipped, [True, False, True, True]) |
|
76 | self.assertEqual(skipped, [True, False, True, True]) | |
77 |
|
77 | |||
78 | def testWithinEqualLowerbound(self): |
|
78 | def testWithinEqualLowerbound(self): | |
79 | """linerange at beginning of an "=" block""" |
|
79 | """linerange at beginning of an "=" block""" | |
80 | # IDX 0 1 |
|
80 | # IDX 0 1 | |
81 | # 012345678901 |
|
81 | # 012345678901 | |
82 | # SRC NNOOOONNNNNN (New/Old) |
|
82 | # SRC NNOOOONNNNNN (New/Old) | |
83 | # ^^ |
|
83 | # ^^ | |
84 | linerange2 = (2, 4) |
|
84 | linerange2 = (2, 4) | |
85 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
85 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
86 | self.assertEqual(linerange1, (3, 5)) |
|
86 | self.assertEqual(linerange1, (3, 5)) | |
87 | self.assertEqual(skipped, [True, False, True, True]) |
|
87 | self.assertEqual(skipped, [True, False, True, True]) | |
88 |
|
88 | |||
89 | def testWithinEqualLowerboundOneline(self): |
|
89 | def testWithinEqualLowerboundOneline(self): | |
90 | """oneline-linerange at beginning of an "=" block""" |
|
90 | """oneline-linerange at beginning of an "=" block""" | |
91 | # IDX 0 1 |
|
91 | # IDX 0 1 | |
92 | # 012345678901 |
|
92 | # 012345678901 | |
93 | # SRC NNOOOONNNNNN (New/Old) |
|
93 | # SRC NNOOOONNNNNN (New/Old) | |
94 | # ^ |
|
94 | # ^ | |
95 | linerange2 = (2, 3) |
|
95 | linerange2 = (2, 3) | |
96 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
96 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
97 | self.assertEqual(linerange1, (3, 4)) |
|
97 | self.assertEqual(linerange1, (3, 4)) | |
98 | self.assertEqual(skipped, [True, False, True, True]) |
|
98 | self.assertEqual(skipped, [True, False, True, True]) | |
99 |
|
99 | |||
100 | def testWithinEqualUpperbound(self): |
|
100 | def testWithinEqualUpperbound(self): | |
101 | """linerange at end of an "=" block""" |
|
101 | """linerange at end of an "=" block""" | |
102 | # IDX 0 1 |
|
102 | # IDX 0 1 | |
103 | # 012345678901 |
|
103 | # 012345678901 | |
104 | # SRC NNOOOONNNNNN (New/Old) |
|
104 | # SRC NNOOOONNNNNN (New/Old) | |
105 | # ^^^ |
|
105 | # ^^^ | |
106 | linerange2 = (3, 6) |
|
106 | linerange2 = (3, 6) | |
107 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
107 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
108 | self.assertEqual(linerange1, (4, 7)) |
|
108 | self.assertEqual(linerange1, (4, 7)) | |
109 | self.assertEqual(skipped, [True, False, True, True]) |
|
109 | self.assertEqual(skipped, [True, False, True, True]) | |
110 |
|
110 | |||
111 | def testWithinEqualUpperboundOneLine(self): |
|
111 | def testWithinEqualUpperboundOneLine(self): | |
112 | """oneline-linerange at end of an "=" block""" |
|
112 | """oneline-linerange at end of an "=" block""" | |
113 | # IDX 0 1 |
|
113 | # IDX 0 1 | |
114 | # 012345678901 |
|
114 | # 012345678901 | |
115 | # SRC NNOOOONNNNNN (New/Old) |
|
115 | # SRC NNOOOONNNNNN (New/Old) | |
116 | # ^ |
|
116 | # ^ | |
117 | linerange2 = (5, 6) |
|
117 | linerange2 = (5, 6) | |
118 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
118 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
119 | self.assertEqual(linerange1, (6, 7)) |
|
119 | self.assertEqual(linerange1, (6, 7)) | |
120 | self.assertEqual(skipped, [True, False, True, True]) |
|
120 | self.assertEqual(skipped, [True, False, True, True]) | |
121 |
|
121 | |||
122 | def testWithinFirstBlockNeq(self): |
|
122 | def testWithinFirstBlockNeq(self): | |
123 | """linerange within the first "!" block""" |
|
123 | """linerange within the first "!" block""" | |
124 | # IDX 0 1 |
|
124 | # IDX 0 1 | |
125 | # 012345678901 |
|
125 | # 012345678901 | |
126 | # SRC NNOOOONNNNNN (New/Old) |
|
126 | # SRC NNOOOONNNNNN (New/Old) | |
127 | # ^ |
|
127 | # ^ | |
128 | # | (empty) |
|
128 | # | (empty) | |
129 | # ^ |
|
129 | # ^ | |
130 | # ^^ |
|
130 | # ^^ | |
131 | for linerange2 in [ |
|
131 | for linerange2 in [ | |
132 | (0, 1), |
|
132 | (0, 1), | |
133 | (1, 1), |
|
133 | (1, 1), | |
134 | (1, 2), |
|
134 | (1, 2), | |
135 | (0, 2), |
|
135 | (0, 2), | |
136 | ]: |
|
136 | ]: | |
137 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
137 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
138 | self.assertEqual(linerange1, (0, 3)) |
|
138 | self.assertEqual(linerange1, (0, 3)) | |
139 | self.assertEqual(skipped, [False, True, True, True]) |
|
139 | self.assertEqual(skipped, [False, True, True, True]) | |
140 |
|
140 | |||
141 | def testWithinLastBlockNeq(self): |
|
141 | def testWithinLastBlockNeq(self): | |
142 | """linerange within the last "!" block""" |
|
142 | """linerange within the last "!" block""" | |
143 | # IDX 0 1 |
|
143 | # IDX 0 1 | |
144 | # 012345678901 |
|
144 | # 012345678901 | |
145 | # SRC NNOOOONNNNNN (New/Old) |
|
145 | # SRC NNOOOONNNNNN (New/Old) | |
146 | # ^ |
|
146 | # ^ | |
147 | # ^ |
|
147 | # ^ | |
148 | # | (empty) |
|
148 | # | (empty) | |
149 | # ^^^^^^ |
|
149 | # ^^^^^^ | |
150 | # ^ |
|
150 | # ^ | |
151 | for linerange2 in [ |
|
151 | for linerange2 in [ | |
152 | (6, 7), |
|
152 | (6, 7), | |
153 | (7, 8), |
|
153 | (7, 8), | |
154 | (7, 7), |
|
154 | (7, 7), | |
155 | (6, 12), |
|
155 | (6, 12), | |
156 | (11, 12), |
|
156 | (11, 12), | |
157 | ]: |
|
157 | ]: | |
158 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
158 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
159 | self.assertEqual(linerange1, (7, 12)) |
|
159 | self.assertEqual(linerange1, (7, 12)) | |
160 | self.assertEqual(skipped, [True, True, False, True]) |
|
160 | self.assertEqual(skipped, [True, True, False, True]) | |
161 |
|
161 | |||
162 | def testAccrossTwoBlocks(self): |
|
162 | def testAccrossTwoBlocks(self): | |
163 | """linerange accross two blocks""" |
|
163 | """linerange accross two blocks""" | |
164 | # IDX 0 1 |
|
164 | # IDX 0 1 | |
165 | # 012345678901 |
|
165 | # 012345678901 | |
166 | # SRC NNOOOONNNNNN (New/Old) |
|
166 | # SRC NNOOOONNNNNN (New/Old) | |
167 | # ^^^^ |
|
167 | # ^^^^ | |
168 | linerange2 = (1, 5) |
|
168 | linerange2 = (1, 5) | |
169 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
169 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
170 | self.assertEqual(linerange1, (0, 6)) |
|
170 | self.assertEqual(linerange1, (0, 6)) | |
171 | self.assertEqual(skipped, [False, False, True, True]) |
|
171 | self.assertEqual(skipped, [False, False, True, True]) | |
172 |
|
172 | |||
173 | def testCrossingSeveralBlocks(self): |
|
173 | def testCrossingSeveralBlocks(self): | |
174 | """linerange accross three blocks""" |
|
174 | """linerange accross three blocks""" | |
175 | # IDX 0 1 |
|
175 | # IDX 0 1 | |
176 | # 012345678901 |
|
176 | # 012345678901 | |
177 | # SRC NNOOOONNNNNN (New/Old) |
|
177 | # SRC NNOOOONNNNNN (New/Old) | |
178 | # ^^^^^^^ |
|
178 | # ^^^^^^^ | |
179 | linerange2 = (1, 8) |
|
179 | linerange2 = (1, 8) | |
180 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
180 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
181 | self.assertEqual(linerange1, (0, 12)) |
|
181 | self.assertEqual(linerange1, (0, 12)) | |
182 | self.assertEqual(skipped, [False, False, False, True]) |
|
182 | self.assertEqual(skipped, [False, False, False, True]) | |
183 |
|
183 | |||
184 | def testStartInEqBlock(self): |
|
184 | def testStartInEqBlock(self): | |
185 | """linerange starting in an "=" block""" |
|
185 | """linerange starting in an "=" block""" | |
186 | # IDX 0 1 |
|
186 | # IDX 0 1 | |
187 | # 012345678901 |
|
187 | # 012345678901 | |
188 | # SRC NNOOOONNNNNN (New/Old) |
|
188 | # SRC NNOOOONNNNNN (New/Old) | |
189 | # ^^^^ |
|
189 | # ^^^^ | |
190 | # ^^^^^^^ |
|
190 | # ^^^^^^^ | |
191 | for linerange2, expectedlinerange1 in [ |
|
191 | for linerange2, expectedlinerange1 in [ | |
192 | ((5, 9), (6, 12)), |
|
192 | ((5, 9), (6, 12)), | |
193 | ((4, 11), (5, 12)), |
|
193 | ((4, 11), (5, 12)), | |
194 | ]: |
|
194 | ]: | |
195 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
195 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
196 | self.assertEqual(linerange1, expectedlinerange1) |
|
196 | self.assertEqual(linerange1, expectedlinerange1) | |
197 | self.assertEqual(skipped, [True, False, False, True]) |
|
197 | self.assertEqual(skipped, [True, False, False, True]) | |
198 |
|
198 | |||
199 | def testEndInEqBlock(self): |
|
199 | def testEndInEqBlock(self): | |
200 | """linerange ending in an "=" block""" |
|
200 | """linerange ending in an "=" block""" | |
201 | # IDX 0 1 |
|
201 | # IDX 0 1 | |
202 | # 012345678901 |
|
202 | # 012345678901 | |
203 | # SRC NNOOOONNNNNN (New/Old) |
|
203 | # SRC NNOOOONNNNNN (New/Old) | |
204 | # ^^ |
|
204 | # ^^ | |
205 | # ^^^^^ |
|
205 | # ^^^^^ | |
206 | for linerange2, expectedlinerange1 in [ |
|
206 | for linerange2, expectedlinerange1 in [ | |
207 | ((1, 3), (0, 4)), |
|
207 | ((1, 3), (0, 4)), | |
208 | ((0, 4), (0, 5)), |
|
208 | ((0, 4), (0, 5)), | |
209 | ]: |
|
209 | ]: | |
210 | linerange1, skipped = filteredblocks(self.blocks, linerange2) |
|
210 | linerange1, skipped = filteredblocks(self.blocks, linerange2) | |
211 | self.assertEqual(linerange1, expectedlinerange1) |
|
211 | self.assertEqual(linerange1, expectedlinerange1) | |
212 | self.assertEqual(skipped, [False, False, True, True]) |
|
212 | self.assertEqual(skipped, [False, False, True, True]) | |
213 |
|
213 | |||
214 | def testOutOfRange(self): |
|
214 | def testOutOfRange(self): | |
215 | """linerange exceeding file size""" |
|
215 | """linerange exceeding file size""" | |
216 | exctype = error.Abort |
|
216 | exctype = error.Abort | |
217 | for linerange2 in [ |
|
217 | for linerange2 in [ | |
218 | (0, 34), |
|
218 | (0, 34), | |
219 | (15, 12), |
|
219 | (15, 12), | |
220 | ]: |
|
220 | ]: | |
221 | # Could be `with self.assertRaises(error.Abort)` but python2.6 |
|
221 | # Could be `with self.assertRaises(error.Abort)` but python2.6 | |
222 | # does not have assertRaises context manager. |
|
222 | # does not have assertRaises context manager. | |
223 | try: |
|
223 | try: | |
224 | mdiff.blocksinrange(self.blocks, linerange2) |
|
224 | mdiff.blocksinrange(self.blocks, linerange2) | |
225 | except exctype as exc: |
|
225 | except exctype as exc: | |
226 | self.assertTrue('line range exceeds file size' in str(exc)) |
|
226 | self.assertTrue('line range exceeds file size' in str(exc)) | |
227 | else: |
|
227 | else: | |
228 | self.fail('%s not raised' % exctype.__name__) |
|
228 | self.fail('%s not raised' % exctype.__name__) | |
229 |
|
229 | |||
230 | if __name__ == '__main__': |
|
230 | if __name__ == '__main__': | |
231 | import silenttestrunner |
|
231 | import silenttestrunner | |
232 | silenttestrunner.main(__name__) |
|
232 | silenttestrunner.main(__name__) |
General Comments 0
You need to be logged in to leave comments.
Login now