Debian Codegraph @

tests/test-byteify-strings.t

raw
1 $ byteify_strings () {
2 > "$PYTHON" "$TESTDIR/../contrib/byteify-strings.py" "$@"
3 > }
4
5 Test version
6
7 $ byteify_strings --version
8 Byteify strings * (glob)
9
10 Test in-place
11
12 $ cat > testfile.py <<EOF
13 > obj['test'] = b"1234"
14 > mydict.iteritems()
15 > EOF
16 $ byteify_strings testfile.py -i
17 $ cat testfile.py
18 obj[b'test'] = b"1234"
19 mydict.iteritems()
20
21 Test with dictiter
22
23 $ cat > testfile.py <<EOF
24 > obj['test'] = b"1234"
25 > mydict.iteritems()
26 > EOF
27 $ byteify_strings testfile.py --dictiter
28 obj[b'test'] = b"1234"
29 mydict.items()
30
31 Test kwargs-like objects
32
33 $ cat > testfile.py <<EOF
34 > kwargs['test'] = "123"
35 > kwargs[test['testing']]
36 > kwargs[test[[['testing']]]]
37 > kwargs[kwargs['testing']]
38 > kwargs.get('test')
39 > kwargs.pop('test')
40 > kwargs.get('test', 'testing')
41 > kwargs.pop('test', 'testing')
42 > kwargs.setdefault('test', 'testing')
43 >
44 > opts['test'] = "123"
45 > opts[test['testing']]
46 > opts[test[[['testing']]]]
47 > opts[opts['testing']]
48 > opts.get('test')
49 > opts.pop('test')
50 > opts.get('test', 'testing')
51 > opts.pop('test', 'testing')
52 > opts.setdefault('test', 'testing')
53 >
54 > commitopts['test'] = "123"
55 > commitopts[test['testing']]
56 > commitopts[test[[['testing']]]]
57 > commitopts[commitopts['testing']]
58 > commitopts.get('test')
59 > commitopts.pop('test')
60 > commitopts.get('test', 'testing')
61 > commitopts.pop('test', 'testing')
62 > commitopts.setdefault('test', 'testing')
63 > EOF
64 $ byteify_strings testfile.py --treat-as-kwargs kwargs opts commitopts
65 kwargs['test'] = b"123"
66 kwargs[test[b'testing']]
67 kwargs[test[[[b'testing']]]]
68 kwargs[kwargs['testing']]
69 kwargs.get('test')
70 kwargs.pop('test')
71 kwargs.get('test', b'testing')
72 kwargs.pop('test', b'testing')
73 kwargs.setdefault('test', b'testing')
74
75 opts['test'] = b"123"
76 opts[test[b'testing']]
77 opts[test[[[b'testing']]]]
78 opts[opts['testing']]
79 opts.get('test')
80 opts.pop('test')
81 opts.get('test', b'testing')
82 opts.pop('test', b'testing')
83 opts.setdefault('test', b'testing')
84
85 commitopts['test'] = b"123"
86 commitopts[test[b'testing']]
87 commitopts[test[[[b'testing']]]]
88 commitopts[commitopts['testing']]
89 commitopts.get('test')
90 commitopts.pop('test')
91 commitopts.get('test', b'testing')
92 commitopts.pop('test', b'testing')
93 commitopts.setdefault('test', b'testing')
94
95 Test attr*() as methods
96
97 $ cat > testfile.py <<EOF
98 > setattr(o, 'a', 1)
99 > util.setattr(o, 'ae', 1)
100 > util.getattr(o, 'alksjdf', 'default')
101 > util.addattr(o, 'asdf')
102 > util.hasattr(o, 'lksjdf', 'default')
103 > util.safehasattr(o, 'lksjdf', 'default')
104 > @eh.wrapfunction(func, 'lksjdf')
105 > def f():
106 > pass
107 > @eh.wrapclass(klass, 'lksjdf')
108 > def f():
109 > pass
110 > EOF
111
112 Test without attr*() as methods
113
114 $ cat > testfile.py <<EOF
115 > setattr(o, 'a', 1)
116 > util.setattr(o, 'ae', 1)
117 > util.getattr(o, 'alksjdf', 'default')
118 > util.addattr(o, 'asdf')
119 > util.hasattr(o, 'lksjdf', 'default')
120 > util.safehasattr(o, 'lksjdf', 'default')
121 > @eh.wrapfunction(func, 'lksjdf')
122 > def f():
123 > pass
124 > @eh.wrapclass(klass, 'lksjdf')
125 > def f():
126 > pass
127 > EOF
128 $ byteify_strings testfile.py
129 setattr(o, 'a', 1)
130 util.setattr(o, 'ae', 1)
131 util.getattr(o, 'alksjdf', b'default')
132 util.addattr(o, 'asdf')
133 util.hasattr(o, 'lksjdf', b'default')
134 util.safehasattr(o, 'lksjdf', b'default')
135 @eh.wrapfunction(func, 'lksjdf')
136 def f():
137 pass
138 @eh.wrapclass(klass, 'lksjdf')
139 def f():
140 pass
141
142 Test ignore comments
143
144 $ cat > testfile.py <<EOF
145 > # py3-transform: off
146 > "none"
147 > "of"
148 > 'these'
149 > s = """should"""
150 > d = '''be'''
151 > # py3-transform: on
152 > "this should"
153 > 'and this also'
154 >
155 > # no-py3-transform
156 > l = "this should be ignored"
157 > l2 = "this shouldn't"
158 >
159 > EOF
160 $ byteify_strings testfile.py
161 # py3-transform: off
162 "none"
163 "of"
164 'these'
165 s = """should"""
166 d = '''be'''
167 # py3-transform: on
168 b"this should"
169 b'and this also'
170
171 # no-py3-transform
172 l = "this should be ignored"
173 l2 = b"this shouldn't"
174
175 Test triple-quoted strings
176
177 $ cat > testfile.py <<EOF
178 > """This is ignored
179 > """
180 >
181 > line = """
182 > This should not be
183 > """
184 > line = '''
185 > Neither should this
186 > '''
187 > EOF
188 $ byteify_strings testfile.py
189 """This is ignored
190 """
191
192 line = b"""
193 This should not be
194 """
195 line = b'''
196 Neither should this
197 '''
198
199 Test prefixed strings
200
201 $ cat > testfile.py <<EOF
202 > obj['test'] = b"1234"
203 > obj[r'test'] = u"1234"
204 > EOF
205 $ byteify_strings testfile.py
206 obj[b'test'] = b"1234"
207 obj[r'test'] = u"1234"
208
209 Test multi-line alignment
210
211 $ cat > testfile.py <<'EOF'
212 > def foo():
213 > error.Abort(_("foo"
214 > "bar"
215 > "%s")
216 > % parameter)
217 > {
218 > 'test': dict,
219 > 'test2': dict,
220 > }
221 > [
222 > "thing",
223 > "thing2"
224 > ]
225 > (
226 > "tuple",
227 > "tuple2",
228 > )
229 > {"thing",
230 > }
231 > EOF
232 $ byteify_strings testfile.py
233 def foo():
234 error.Abort(_(b"foo"
235 b"bar"
236 b"%s")
237 % parameter)
238 {
239 b'test': dict,
240 b'test2': dict,
241 }
242 [
243 b"thing",
244 b"thing2"
245 ]
246 (
247 b"tuple",
248 b"tuple2",
249 )
250 {b"thing",
251 }