Debian Codegraph @

tests/py2win_doctests_mercurial_extension_utils.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # mercurial extension utils: Windows doctests
4 #
5 # Copyright (c) 2015 Marcin Kasperski <[email protected]>
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # 3. The name of the author may not be used to endorse or promote products
17 # derived from this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #
30 # See README.rst for more details.
31
32 r'''
33 This module exists solely to give some examples (and doctest)
34 of mercurial_extension_utils behaviour on Windows. Structure
35 mimics that of mercurial_extension_utils.
36
37
38
39 >>> normalize_path("~/src")
40 'C:/Users/lordvader/src'
41 >>> normalize_path("/some/where")
42 'c:/some/where'
43 >>> normalize_path("/some/where/")
44 'c:/some/where'
45 >>> normalize_path("../../../some/where")
46 'c:/Users/lordvader/some/where'
47 >>> normalize_path(r'C:\Users\Joe\source files')
48 'C:/Users/Joe/source files'
49
50
51
52 >>> belongs_to_tree("/tmp/sub/dir", "/tmp")
53 'c:/tmp'
54 >>> belongs_to_tree("/tmp", "/tmp")
55 'c:/tmp'
56 >>> belongs_to_tree("/tmp/sub", "/tmp/sub/dir/../..")
57 'c:/tmp'
58
59 >>> belongs_to_tree("/usr/sub", "/tmp")
60
61 >>> home_work_src = os.path.join(os.environ["HOME"], "work", "src")
62 >>> belongs_to_tree(home_work_src, "~/work")
63 'C:/Users/lordvader/work'
64 >>> belongs_to_tree("/home/lordvader/devel/webapps" if os.name != 'nt' else "c:/users/lordvader/devel/webapps",
65 ... "~lordvader/devel")
66 'C:/Users/lordvader/devel'
67
68
69
70 >>> belongs_to_tree_group("/tmp/sub/dir", ["/bin", "/tmp"])
71 'c:/tmp'
72 >>> belongs_to_tree_group("/tmp", ["/tmp"])
73 'c:/tmp'
74 >>> belongs_to_tree_group("/tmp/sub/dir", ["/bin", "~/src"])
75
76 >>> belongs_to_tree_group("/tmp/sub/dir", ["/tmp", "/bin", "/tmp", "/tmp/sub"])
77 'c:/tmp/sub'
78
79 >>> belongs_to_tree_group("C:/Users/lordvader/src/apps", ["~/src", "C:/Users/lordvader"])
80 'C:/Users/lordvader/src'
81
82
83
84 >>> pat = DirectoryPattern('~/src/{suffix}')
85 >>> pat.is_valid()
86 True
87 >>> pat.search("/opt/repos/abcd")
88 >>> pat.search("~/src/repos/in/tree")
89 {'suffix': 'repos/in/tree'}
90 >>> pat.search("c:/users/lordvader/src/repos/here")
91 {'suffix': 'repos/here'}
92 >>> pat.search("C:/Users/lordvader/src/repos/here")
93 {'suffix': 'repos/here'}
94 >>> pat.search("/home/lordvader/src")
95
96 >>> pat = DirectoryPattern('~lordvader/devel/(item)')
97 >>> pat.search("/opt/repos/abcd")
98 >>> pat.search("~/devel/libxuza")
99 {'item': 'libxuza'}
100 >>> pat.search("~/devel/libs/libxuza")
101 >>> pat.search("C:/Users/lordvader/devel/webapp")
102 {'item': 'webapp'}
103 >>> pat.search("/users/lordvader/devel/webapp")
104 {'item': 'webapp'}
105 >>> pat.search("/home/lordvader/devel")
106
107 >>> pat = DirectoryPattern('/opt/repos/(group)/{suffix}')
108 >>> pat.search("/opt/repos/abcd")
109 >>> pat.search("/opt/repos/libs/abcd")
110 {'group': 'libs', 'suffix': 'abcd'}
111 >>> pat.search("/opt/repos/apps/mini/webby")
112 {'group': 'apps', 'suffix': 'mini/webby'}
113
114 >>> pat = DirectoryPattern('/opt/repos/(group/{suffix}')
115 >>> pat.is_valid()
116 False
117 >>> pat.search('/opt/repos/some/where')
118
119
120
121 >>> tf = TextFiller('{some}/text/to/{fill}')
122 >>> tf.fill(some='prefix', fill='suffix')
123 'prefix/text/to/suffix'
124 >>> tf.fill(some='/ab/c/d', fill='x')
125 '/ab/c/d/text/to/x'
126
127 >>> tf = TextFiller('{some}/text/to/{some}')
128 >>> tf.is_valid()
129 True
130 >>> tf.fill(some='val')
131 'val/text/to/val'
132 >>> tf.fill(some='ab/c/d', fill='x')
133 'ab/c/d/text/to/ab/c/d'
134
135 >>> tf = TextFiller('{prefix:_=___}/goto/{suffix:/=-}')
136 >>> tf.fill(prefix='some_prefix', suffix='some/long/suffix')
137 'some___prefix/goto/some-long-suffix'
138
139 >>> tf = TextFiller('{prefix:/home/=}/docs/{suffix:.txt=.html}')
140 >>> tf.fill(prefix='/home/joe', suffix='some/document.txt')
141 'joe/docs/some/document.html'
142
143 >>> tf = TextFiller(r'/goto/{item:/=-:\=_}/')
144 >>> tf.fill(item='this/is/slashy')
145 '/goto/this-is-slashy/'
146 >>> tf.fill(item=r'this\is\back')
147 '/goto/this_is_back/'
148 >>> tf.fill(item=r'this/is\mixed')
149 '/goto/this-is_mixed/'
150
151 >>> tf = TextFiller(r'http://go.to/{item:/=-}, G:{item:/=\}, name: {item}')
152 >>> print tf.fill(item='so/me/thing')
153 http://go.to/so-me-thing, G:so\me\thing, name: so/me/thing
154
155 >>> tf = TextFiller('{some}/text/to/{fill}')
156 >>> tf.fill(some='prefix', badfill='suffix')
157
158 >>> tf = TextFiller('{some/text/to/{fill}')
159 >>> tf.is_valid()
160 False
161 >>> tf.fill(some='prefix', fill='suffix')
162
163 >>> tf = TextFiller('{some}/text/to/{fill:}')
164 >>> tf.is_valid()
165 False
166 >>> tf.fill(some='prefix', fill='suffix')
167
168
169
170 >>> import mercurial.ui; ui = mercurial.ui.ui()
171 >>> setconfig_dict(ui, "sect1", {'a': 7, 'bbb': 'xxx', 'c': '-'})
172 >>> setconfig_dict(ui, "sect2", {'v': 'vvv'})
173 >>> ui.config("sect1", 'a')
174 7
175 >>> ui.config("sect2", 'v')
176 'vvv'
177
178
179 >>> import mercurial.ui; ui = mercurial.ui.ui()
180 >>> setconfig_dict(ui, "foo", {
181 ... "pfx-some-sfx": "ala, ma kota",
182 ... "some.nonitem": "bela nie",
183 ... "x": "yes",
184 ... "pfx-other-sfx": 4})
185 >>> setconfig_dict(ui, "notfoo", {
186 ... "pfx-some-sfx": "bad",
187 ... "pfx-also-sfx": "too",
188 ... })
189 >>>
190 >>> for name, value in rgxp_config_items(
191 ... ui, "foo", re.compile(r'^pfx-(\w+)-sfx$')):
192 ... print name, value
193 some ala, ma kota
194 other 4
195
196
197 >>> import mercurial.ui; ui = mercurial.ui.ui()
198 >>> setconfig_dict(ui, "foo", {
199 ... "pfx-some-sfx": "ala, ma kota",
200 ... "some.nonitem": "bela nie",
201 ... "x": "yes",
202 ... "pfx-other-sfx": "sth"})
203 >>> setconfig_dict(ui, "notfoo", {
204 ... "pfx-some-sfx": "bad",
205 ... "pfx-also-sfx": "too",
206 ... })
207 >>>
208 >>> for name, value in rgxp_configlist_items(
209 ... ui, "foo", re.compile(r'^pfx-(\w+)-sfx$')):
210 ... print name, value
211 some ['ala', 'ma', 'kota']
212 other ['sth']
213
214
215 >>> import mercurial.ui; ui = mercurial.ui.ui()
216 >>> setconfig_dict(ui, "foo", {
217 ... "pfx-some-sfx": "true",
218 ... "some.nonitem": "bela nie",
219 ... "x": "yes",
220 ... "pfx-other-sfx": "false"})
221 >>> setconfig_dict(ui, "notfoo", {
222 ... "pfx-some-sfx": "1",
223 ... "pfx-also-sfx": "0",
224 ... })
225 >>>
226 >>> for name, value in rgxp_configbool_items(
227 ... ui, "foo", re.compile(r'^pfx-(\w+)-sfx$')):
228 ... print name, value
229 some True
230 other False
231
232
233 >>> import mercurial.ui; ui = mercurial.ui.ui()
234 >>> setconfig_dict(ui, "foo", {
235 ... "some.item": "ala, ma kota",
236 ... "some.nonitem": "bela nie",
237 ... "x": "yes",
238 ... "other.item": 4})
239 >>> setconfig_dict(ui, "notfoo", {
240 ... "some.item": "bad",
241 ... "also.item": "too",
242 ... })
243 >>>
244 >>> for name, value in suffix_config_items(
245 ... ui, "foo", 'item'):
246 ... print name, value
247 some ala, ma kota
248 other 4
249
250
251 >>> import mercurial.ui; ui = mercurial.ui.ui()
252 >>> setconfig_dict(ui, "foo", {
253 ... "some.item": "ala, ma kota",
254 ... "some.nonitem": "bela nie",
255 ... "x": "yes",
256 ... "other.item": "kazimira"})
257 >>> setconfig_dict(ui, "notfoo", {
258 ... "some.item": "bad",
259 ... "also.item": "too",
260 ... })
261 >>>
262 >>> for name, value in suffix_configlist_items(
263 ... ui, "foo", "item"):
264 ... print name, value
265 some ['ala', 'ma', 'kota']
266 other ['kazimira']
267
268
269 >>> import mercurial.ui; ui = mercurial.ui.ui()
270 >>> setconfig_dict(ui, "foo", {
271 ... "true.item": "true",
272 ... "false.item": "false",
273 ... "one.item": "1",
274 ... "zero.item": "0",
275 ... "yes.item": "yes",
276 ... "no.item": "no",
277 ... "some.nonitem": "1",
278 ... "x": "yes"})
279 >>> setconfig_dict(ui, "notfoo", {
280 ... "some.item": "0",
281 ... "also.item": "too",
282 ... })
283 >>>
284 >>> for name, value in suffix_configbool_items(
285 ... ui, "foo", "item"):
286 ... print name, str(value)
287 zero False
288 yes True
289 one True
290 true True
291 no False
292 false False
293 >>>
294 >>> ui.setconfig("foo", "text.item", "something")
295 >>> for name, value in suffix_configbool_items(
296 ... ui, "foo", "item"):
297 ... print name, str(value)
298 Traceback (most recent call last):
299 File "/usr/lib/python2.7/dist-packages/mercurial/ui.py", line 237, in configbool
300 % (section, name, v))
301 ConfigError: foo.text.item is not a boolean ('something')
302
303
304 >>> class SomeClass(object):
305 ... def meth(self, arg):
306 ... return "Original: " + arg
307 >>>
308 >>> @monkeypatch_method(SomeClass)
309 ... def meth(self, arg):
310 ... return "Patched: " + meth.orig(self, arg)
311 >>>
312 >>> obj = SomeClass()
313 >>> print obj.meth("some param")
314 Patched: Original: some param
315
316
317 >>> class SomeClass(object):
318 ... def meth(self, arg):
319 ... return "Original: " + arg
320 >>>
321 >>> @monkeypatch_method(SomeClass, "meth")
322 ... def another_meth(self, arg):
323 ... return "Patched: " + another_meth.orig(self, arg)
324 >>>
325 >>> obj = SomeClass()
326 >>> print obj.meth("some param")
327 Patched: Original: some param
328
329
330 >>> import random
331 >>> @monkeypatch_function(random)
332 ... def seed(x=None):
333 ... print "Forcing random to seed with 0 instead of", x
334 ... return seed.orig(0)
335 >>>
336 >>> random.seed()
337 Forcing random to seed with 0 instead of None
338 >>> random.randint(0, 10)
339 9
340
341 >>> import random
342 >>> @monkeypatch_function(random, 'choice')
343 ... def choice_first(sequence):
344 ... return sequence[0]
345 >>> for x in range(0, 4): print random.choice("abcdefgh")
346 a
347 a
348 a
349 a
350
351 '''