##// END OF EJS Templates
tests: remove use of string in CBOR test...
Gregory Szorc -
r39447:fcc6bd11 default
parent child Browse files
Show More
@@ -1,197 +1,194 b''
1 from __future__ import absolute_import, print_function
1 from __future__ import absolute_import, print_function
2
2
3 import unittest
3 import unittest
4
4
5 from mercurial import (
5 from mercurial import (
6 util,
6 util,
7 wireprotoframing as framing,
7 wireprotoframing as framing,
8 )
8 )
9
9
10 ffs = framing.makeframefromhumanstring
10 ffs = framing.makeframefromhumanstring
11
11
12 class FrameHumanStringTests(unittest.TestCase):
12 class FrameHumanStringTests(unittest.TestCase):
13 def testbasic(self):
13 def testbasic(self):
14 self.assertEqual(ffs(b'1 1 0 1 0 '),
14 self.assertEqual(ffs(b'1 1 0 1 0 '),
15 b'\x00\x00\x00\x01\x00\x01\x00\x10')
15 b'\x00\x00\x00\x01\x00\x01\x00\x10')
16
16
17 self.assertEqual(ffs(b'2 4 0 1 0 '),
17 self.assertEqual(ffs(b'2 4 0 1 0 '),
18 b'\x00\x00\x00\x02\x00\x04\x00\x10')
18 b'\x00\x00\x00\x02\x00\x04\x00\x10')
19
19
20 self.assertEqual(ffs(b'2 4 0 1 0 foo'),
20 self.assertEqual(ffs(b'2 4 0 1 0 foo'),
21 b'\x03\x00\x00\x02\x00\x04\x00\x10foo')
21 b'\x03\x00\x00\x02\x00\x04\x00\x10foo')
22
22
23 def testcborint(self):
23 def testcborint(self):
24 self.assertEqual(ffs(b'1 1 0 1 0 cbor:15'),
24 self.assertEqual(ffs(b'1 1 0 1 0 cbor:15'),
25 b'\x01\x00\x00\x01\x00\x01\x00\x10\x0f')
25 b'\x01\x00\x00\x01\x00\x01\x00\x10\x0f')
26
26
27 self.assertEqual(ffs(b'1 1 0 1 0 cbor:42'),
27 self.assertEqual(ffs(b'1 1 0 1 0 cbor:42'),
28 b'\x02\x00\x00\x01\x00\x01\x00\x10\x18*')
28 b'\x02\x00\x00\x01\x00\x01\x00\x10\x18*')
29
29
30 self.assertEqual(ffs(b'1 1 0 1 0 cbor:1048576'),
30 self.assertEqual(ffs(b'1 1 0 1 0 cbor:1048576'),
31 b'\x05\x00\x00\x01\x00\x01\x00\x10\x1a'
31 b'\x05\x00\x00\x01\x00\x01\x00\x10\x1a'
32 b'\x00\x10\x00\x00')
32 b'\x00\x10\x00\x00')
33
33
34 self.assertEqual(ffs(b'1 1 0 1 0 cbor:0'),
34 self.assertEqual(ffs(b'1 1 0 1 0 cbor:0'),
35 b'\x01\x00\x00\x01\x00\x01\x00\x10\x00')
35 b'\x01\x00\x00\x01\x00\x01\x00\x10\x00')
36
36
37 self.assertEqual(ffs(b'1 1 0 1 0 cbor:-1'),
37 self.assertEqual(ffs(b'1 1 0 1 0 cbor:-1'),
38 b'\x01\x00\x00\x01\x00\x01\x00\x10 ')
38 b'\x01\x00\x00\x01\x00\x01\x00\x10 ')
39
39
40 self.assertEqual(ffs(b'1 1 0 1 0 cbor:-342542'),
40 self.assertEqual(ffs(b'1 1 0 1 0 cbor:-342542'),
41 b'\x05\x00\x00\x01\x00\x01\x00\x10:\x00\x05:\r')
41 b'\x05\x00\x00\x01\x00\x01\x00\x10:\x00\x05:\r')
42
42
43 def testcborstrings(self):
43 def testcborstrings(self):
44 self.assertEqual(ffs(b"1 1 0 1 0 cbor:b'foo'"),
44 self.assertEqual(ffs(b"1 1 0 1 0 cbor:b'foo'"),
45 b'\x04\x00\x00\x01\x00\x01\x00\x10Cfoo')
45 b'\x04\x00\x00\x01\x00\x01\x00\x10Cfoo')
46
46
47 self.assertEqual(ffs(b"1 1 0 1 0 cbor:u'foo'"),
48 b'\x04\x00\x00\x01\x00\x01\x00\x10cfoo')
49
50 def testcborlists(self):
47 def testcborlists(self):
51 self.assertEqual(ffs(b"1 1 0 1 0 cbor:[None, True, False, 42, b'foo']"),
48 self.assertEqual(ffs(b"1 1 0 1 0 cbor:[None, True, False, 42, b'foo']"),
52 b'\n\x00\x00\x01\x00\x01\x00\x10\x85\xf6\xf5\xf4'
49 b'\n\x00\x00\x01\x00\x01\x00\x10\x85\xf6\xf5\xf4'
53 b'\x18*Cfoo')
50 b'\x18*Cfoo')
54
51
55 def testcbordicts(self):
52 def testcbordicts(self):
56 self.assertEqual(ffs(b"1 1 0 1 0 "
53 self.assertEqual(ffs(b"1 1 0 1 0 "
57 b"cbor:{b'foo': b'val1', b'bar': b'val2'}"),
54 b"cbor:{b'foo': b'val1', b'bar': b'val2'}"),
58 b'\x13\x00\x00\x01\x00\x01\x00\x10\xa2'
55 b'\x13\x00\x00\x01\x00\x01\x00\x10\xa2'
59 b'CbarDval2CfooDval1')
56 b'CbarDval2CfooDval1')
60
57
61 class FrameTests(unittest.TestCase):
58 class FrameTests(unittest.TestCase):
62 def testdataexactframesize(self):
59 def testdataexactframesize(self):
63 data = util.bytesio(b'x' * framing.DEFAULT_MAX_FRAME_SIZE)
60 data = util.bytesio(b'x' * framing.DEFAULT_MAX_FRAME_SIZE)
64
61
65 stream = framing.stream(1)
62 stream = framing.stream(1)
66 frames = list(framing.createcommandframes(stream, 1, b'command',
63 frames = list(framing.createcommandframes(stream, 1, b'command',
67 {}, data))
64 {}, data))
68 self.assertEqual(frames, [
65 self.assertEqual(frames, [
69 ffs(b'1 1 stream-begin command-request new|have-data '
66 ffs(b'1 1 stream-begin command-request new|have-data '
70 b"cbor:{b'name': b'command'}"),
67 b"cbor:{b'name': b'command'}"),
71 ffs(b'1 1 0 command-data continuation %s' % data.getvalue()),
68 ffs(b'1 1 0 command-data continuation %s' % data.getvalue()),
72 ffs(b'1 1 0 command-data eos ')
69 ffs(b'1 1 0 command-data eos ')
73 ])
70 ])
74
71
75 def testdatamultipleframes(self):
72 def testdatamultipleframes(self):
76 data = util.bytesio(b'x' * (framing.DEFAULT_MAX_FRAME_SIZE + 1))
73 data = util.bytesio(b'x' * (framing.DEFAULT_MAX_FRAME_SIZE + 1))
77
74
78 stream = framing.stream(1)
75 stream = framing.stream(1)
79 frames = list(framing.createcommandframes(stream, 1, b'command', {},
76 frames = list(framing.createcommandframes(stream, 1, b'command', {},
80 data))
77 data))
81 self.assertEqual(frames, [
78 self.assertEqual(frames, [
82 ffs(b'1 1 stream-begin command-request new|have-data '
79 ffs(b'1 1 stream-begin command-request new|have-data '
83 b"cbor:{b'name': b'command'}"),
80 b"cbor:{b'name': b'command'}"),
84 ffs(b'1 1 0 command-data continuation %s' % (
81 ffs(b'1 1 0 command-data continuation %s' % (
85 b'x' * framing.DEFAULT_MAX_FRAME_SIZE)),
82 b'x' * framing.DEFAULT_MAX_FRAME_SIZE)),
86 ffs(b'1 1 0 command-data eos x'),
83 ffs(b'1 1 0 command-data eos x'),
87 ])
84 ])
88
85
89 def testargsanddata(self):
86 def testargsanddata(self):
90 data = util.bytesio(b'x' * 100)
87 data = util.bytesio(b'x' * 100)
91
88
92 stream = framing.stream(1)
89 stream = framing.stream(1)
93 frames = list(framing.createcommandframes(stream, 1, b'command', {
90 frames = list(framing.createcommandframes(stream, 1, b'command', {
94 b'key1': b'key1value',
91 b'key1': b'key1value',
95 b'key2': b'key2value',
92 b'key2': b'key2value',
96 b'key3': b'key3value',
93 b'key3': b'key3value',
97 }, data))
94 }, data))
98
95
99 self.assertEqual(frames, [
96 self.assertEqual(frames, [
100 ffs(b'1 1 stream-begin command-request new|have-data '
97 ffs(b'1 1 stream-begin command-request new|have-data '
101 b"cbor:{b'name': b'command', b'args': {b'key1': b'key1value', "
98 b"cbor:{b'name': b'command', b'args': {b'key1': b'key1value', "
102 b"b'key2': b'key2value', b'key3': b'key3value'}}"),
99 b"b'key2': b'key2value', b'key3': b'key3value'}}"),
103 ffs(b'1 1 0 command-data eos %s' % data.getvalue()),
100 ffs(b'1 1 0 command-data eos %s' % data.getvalue()),
104 ])
101 ])
105
102
106 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
103 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
107 # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
104 # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
108 # the regex version.
105 # the regex version.
109 assertRaisesRegex = (# camelcase-required
106 assertRaisesRegex = (# camelcase-required
110 unittest.TestCase.assertRaisesRegexp)
107 unittest.TestCase.assertRaisesRegexp)
111
108
112 def testtextoutputformattingstringtype(self):
109 def testtextoutputformattingstringtype(self):
113 """Formatting string must be bytes."""
110 """Formatting string must be bytes."""
114 with self.assertRaisesRegex(ValueError, 'must use bytes formatting '):
111 with self.assertRaisesRegex(ValueError, 'must use bytes formatting '):
115 list(framing.createtextoutputframe(None, 1, [
112 list(framing.createtextoutputframe(None, 1, [
116 (b'foo'.decode('ascii'), [], [])]))
113 (b'foo'.decode('ascii'), [], [])]))
117
114
118 def testtextoutputargumentbytes(self):
115 def testtextoutputargumentbytes(self):
119 with self.assertRaisesRegex(ValueError, 'must use bytes for argument'):
116 with self.assertRaisesRegex(ValueError, 'must use bytes for argument'):
120 list(framing.createtextoutputframe(None, 1, [
117 list(framing.createtextoutputframe(None, 1, [
121 (b'foo', [b'foo'.decode('ascii')], [])]))
118 (b'foo', [b'foo'.decode('ascii')], [])]))
122
119
123 def testtextoutputlabelbytes(self):
120 def testtextoutputlabelbytes(self):
124 with self.assertRaisesRegex(ValueError, 'must use bytes for labels'):
121 with self.assertRaisesRegex(ValueError, 'must use bytes for labels'):
125 list(framing.createtextoutputframe(None, 1, [
122 list(framing.createtextoutputframe(None, 1, [
126 (b'foo', [], [b'foo'.decode('ascii')])]))
123 (b'foo', [], [b'foo'.decode('ascii')])]))
127
124
128 def testtextoutput1simpleatom(self):
125 def testtextoutput1simpleatom(self):
129 stream = framing.stream(1)
126 stream = framing.stream(1)
130 val = list(framing.createtextoutputframe(stream, 1, [
127 val = list(framing.createtextoutputframe(stream, 1, [
131 (b'foo', [], [])]))
128 (b'foo', [], [])]))
132
129
133 self.assertEqual(val, [
130 self.assertEqual(val, [
134 ffs(b'1 1 stream-begin text-output 0 '
131 ffs(b'1 1 stream-begin text-output 0 '
135 b"cbor:[{b'msg': b'foo'}]"),
132 b"cbor:[{b'msg': b'foo'}]"),
136 ])
133 ])
137
134
138 def testtextoutput2simpleatoms(self):
135 def testtextoutput2simpleatoms(self):
139 stream = framing.stream(1)
136 stream = framing.stream(1)
140 val = list(framing.createtextoutputframe(stream, 1, [
137 val = list(framing.createtextoutputframe(stream, 1, [
141 (b'foo', [], []),
138 (b'foo', [], []),
142 (b'bar', [], []),
139 (b'bar', [], []),
143 ]))
140 ]))
144
141
145 self.assertEqual(val, [
142 self.assertEqual(val, [
146 ffs(b'1 1 stream-begin text-output 0 '
143 ffs(b'1 1 stream-begin text-output 0 '
147 b"cbor:[{b'msg': b'foo'}, {b'msg': b'bar'}]")
144 b"cbor:[{b'msg': b'foo'}, {b'msg': b'bar'}]")
148 ])
145 ])
149
146
150 def testtextoutput1arg(self):
147 def testtextoutput1arg(self):
151 stream = framing.stream(1)
148 stream = framing.stream(1)
152 val = list(framing.createtextoutputframe(stream, 1, [
149 val = list(framing.createtextoutputframe(stream, 1, [
153 (b'foo %s', [b'val1'], []),
150 (b'foo %s', [b'val1'], []),
154 ]))
151 ]))
155
152
156 self.assertEqual(val, [
153 self.assertEqual(val, [
157 ffs(b'1 1 stream-begin text-output 0 '
154 ffs(b'1 1 stream-begin text-output 0 '
158 b"cbor:[{b'msg': b'foo %s', b'args': [b'val1']}]")
155 b"cbor:[{b'msg': b'foo %s', b'args': [b'val1']}]")
159 ])
156 ])
160
157
161 def testtextoutput2arg(self):
158 def testtextoutput2arg(self):
162 stream = framing.stream(1)
159 stream = framing.stream(1)
163 val = list(framing.createtextoutputframe(stream, 1, [
160 val = list(framing.createtextoutputframe(stream, 1, [
164 (b'foo %s %s', [b'val', b'value'], []),
161 (b'foo %s %s', [b'val', b'value'], []),
165 ]))
162 ]))
166
163
167 self.assertEqual(val, [
164 self.assertEqual(val, [
168 ffs(b'1 1 stream-begin text-output 0 '
165 ffs(b'1 1 stream-begin text-output 0 '
169 b"cbor:[{b'msg': b'foo %s %s', b'args': [b'val', b'value']}]")
166 b"cbor:[{b'msg': b'foo %s %s', b'args': [b'val', b'value']}]")
170 ])
167 ])
171
168
172 def testtextoutput1label(self):
169 def testtextoutput1label(self):
173 stream = framing.stream(1)
170 stream = framing.stream(1)
174 val = list(framing.createtextoutputframe(stream, 1, [
171 val = list(framing.createtextoutputframe(stream, 1, [
175 (b'foo', [], [b'label']),
172 (b'foo', [], [b'label']),
176 ]))
173 ]))
177
174
178 self.assertEqual(val, [
175 self.assertEqual(val, [
179 ffs(b'1 1 stream-begin text-output 0 '
176 ffs(b'1 1 stream-begin text-output 0 '
180 b"cbor:[{b'msg': b'foo', b'labels': [b'label']}]")
177 b"cbor:[{b'msg': b'foo', b'labels': [b'label']}]")
181 ])
178 ])
182
179
183 def testargandlabel(self):
180 def testargandlabel(self):
184 stream = framing.stream(1)
181 stream = framing.stream(1)
185 val = list(framing.createtextoutputframe(stream, 1, [
182 val = list(framing.createtextoutputframe(stream, 1, [
186 (b'foo %s', [b'arg'], [b'label']),
183 (b'foo %s', [b'arg'], [b'label']),
187 ]))
184 ]))
188
185
189 self.assertEqual(val, [
186 self.assertEqual(val, [
190 ffs(b'1 1 stream-begin text-output 0 '
187 ffs(b'1 1 stream-begin text-output 0 '
191 b"cbor:[{b'msg': b'foo %s', b'args': [b'arg'], "
188 b"cbor:[{b'msg': b'foo %s', b'args': [b'arg'], "
192 b"b'labels': [b'label']}]")
189 b"b'labels': [b'label']}]")
193 ])
190 ])
194
191
195 if __name__ == '__main__':
192 if __name__ == '__main__':
196 import silenttestrunner
193 import silenttestrunner
197 silenttestrunner.main(__name__)
194 silenttestrunner.main(__name__)
General Comments 0
You need to be logged in to leave comments. Login now