Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9, 1.9.1, and 1.9.2

tests: move with_encoding() decorator to tests module

Changeset 8e525e1b6b45

Parent b64e8ded9334

by Yuya Nishihara

Changes to 2 files · Browse files at 8e525e1b6b45 Showing diff from parent b64e8ded9334 Diff from another changeset...

Change 1 of 2 Show Entire File tests/​__init__.py Stacked
 
1
 
2
 
3
4
5
 
29
30
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
 
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@@ -1,5 +1,7 @@
 import os, tempfile, shutil +from nose.tools import *  from mercurial import ui, commands, error +from tortoisehg.util import hglib  from tortoisehg.hgqt import thgrepo    FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixtures') @@ -29,3 +31,19 @@
  return thgrepo.repository(ui.ui(), path)   except error.RepoError:   return create_fixture_repo(name) + + +def with_encoding(encoding, fallbackencoding=None): + """Change locale encoding temporarily""" + orig_encoding = hglib._encoding + orig_fallbackencoding = hglib._fallbackencoding + + def setenc(): + hglib._encoding = encoding + hglib._fallbackencoding = fallbackencoding or encoding + + def restoreenc(): + hglib._encoding = orig_encoding + hglib._fallbackencoding = orig_fallbackencoding + + return with_setup(setenc, restoreenc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
1
2
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 """Test for encoding helper functions of tortoisehg.util.hglib"""  from nose.tools import *  from tortoisehg.util import hglib - -def with_encoding(encoding, fallbackencoding=None): - """Change locale encoding temporarily""" - orig_encoding = hglib._encoding - orig_fallbackencoding = hglib._fallbackencoding - - def setenc(): - hglib._encoding = encoding - hglib._fallbackencoding = fallbackencoding or encoding - - def restoreenc(): - hglib._encoding = orig_encoding - hglib._fallbackencoding = orig_fallbackencoding - - return with_setup(setenc, restoreenc) +from tests import with_encoding    JAPANESE_KANA_I = u'\u30a4' # Japanese katakana "i"    @with_encoding('utf-8')  def test_none():   """None shouldn't be touched"""   for e in ('fromunicode', 'fromutf', 'tounicode', 'toutf'):   f = getattr(hglib, e)   assert_equals(None, f(None))      @with_encoding('utf-8')  def test_fromunicode():   assert_equals(JAPANESE_KANA_I.encode('utf-8'),   hglib.fromunicode(JAPANESE_KANA_I))    @with_encoding('utf-8')  def test_fromunicode_unicodableobj():   """fromunicode() accepts unicode-able obj like QString"""   class Unicodable(object):   def __unicode__(self):   return JAPANESE_KANA_I     assert_equals(JAPANESE_KANA_I.encode('utf-8'),   hglib.fromunicode(Unicodable()))    @with_encoding('ascii', 'utf-8')  def test_fromunicode_fallback():   assert_equals(JAPANESE_KANA_I.encode('utf-8'),   hglib.fromunicode(JAPANESE_KANA_I))    @with_encoding('ascii')  def test_fromunicode_replace():   assert_equals('?', hglib.fromunicode(JAPANESE_KANA_I,   errors='replace'))    @with_encoding('ascii')  def test_fromunicode_strict():   assert_raises(UnicodeEncodeError,   lambda: hglib.fromunicode(JAPANESE_KANA_I))      @with_encoding('euc-jp')  def test_fromutf():   assert_equals(JAPANESE_KANA_I.encode('euc-jp'),   hglib.fromutf(JAPANESE_KANA_I.encode('utf-8')))    @with_encoding('ascii', 'euc-jp')  def test_fromutf_fallback():   assert_equals(JAPANESE_KANA_I.encode('euc-jp'),   hglib.fromutf(JAPANESE_KANA_I.encode('utf-8')))    @with_encoding('ascii')  def test_fromutf_replace():   assert_equals('?', hglib.fromutf(JAPANESE_KANA_I.encode('utf-8')))      @with_encoding('euc-jp')  def test_tounicode():   assert_equals(JAPANESE_KANA_I,   hglib.tounicode(JAPANESE_KANA_I.encode('euc-jp')))    @with_encoding('ascii', 'euc-jp')  def test_tounicode_fallback():   assert_equals(JAPANESE_KANA_I,   hglib.tounicode(JAPANESE_KANA_I.encode('euc-jp')))      @with_encoding('euc-jp')  def test_toutf():   assert_equals(JAPANESE_KANA_I.encode('utf-8'),   hglib.toutf(JAPANESE_KANA_I.encode('euc-jp')))    @with_encoding('ascii', 'euc-jp')  def test_toutf_fallback():   assert_equals(JAPANESE_KANA_I.encode('utf-8'),   hglib.toutf(JAPANESE_KANA_I.encode('euc-jp')))