Mercurial > vim
comparison src/testdir/test_blob.vim @ 15454:1d2b5c016f17 v8.1.0735
patch 8.1.0735: cannot handle binary data
commit https://github.com/vim/vim/commit/6e5ea8d2a995b32bbc5972edc4f827b959f2702f
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Jan 12 22:47:31 2019 +0100
patch 8.1.0735: cannot handle binary data
Problem: Cannot handle binary data.
Solution: Add the Blob type. (Yasuhiro Matsumoto, closes https://github.com/vim/vim/issues/3638)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 12 Jan 2019 23:00:06 +0100 |
parents | |
children | f01eb1aed348 |
comparison
equal
deleted
inserted
replaced
15453:cdee6e827112 | 15454:1d2b5c016f17 |
---|---|
1 " Tests for the Blob types | |
2 | |
3 func TearDown() | |
4 " Run garbage collection after every test | |
5 call test_garbagecollect_now() | |
6 endfunc | |
7 | |
8 " Tests for Blob type | |
9 | |
10 " Blob creation from constant | |
11 func Test_blob_create() | |
12 let b = 0zDEADBEEF | |
13 call assert_equal(v:t_blob, type(b)) | |
14 call assert_equal(4, len(b)) | |
15 call assert_equal(0xDE, b[0]) | |
16 call assert_equal(0xAD, b[1]) | |
17 call assert_equal(0xBE, b[2]) | |
18 call assert_equal(0xEF, b[3]) | |
19 call assert_fails('let x = b[4]') | |
20 | |
21 call assert_equal(0xDE, get(b, 0)) | |
22 call assert_equal(0xEF, get(b, 3)) | |
23 call assert_fails('let x = get(b, 4)') | |
24 endfunc | |
25 | |
26 " assignment to a blob | |
27 func Test_blob_assign() | |
28 let b = 0zDEADBEEF | |
29 let b2 = b[1:2] | |
30 call assert_equal(0zADBE, b2) | |
31 | |
32 let bcopy = b[:] | |
33 call assert_equal(b, bcopy) | |
34 call assert_false(b is bcopy) | |
35 endfunc | |
36 | |
37 func Test_blob_to_string() | |
38 let b = 0zDEADBEEF | |
39 call assert_equal('[0xDE,0xAD,0xBE,0xEF]', string(b)) | |
40 call remove(b, 0, 3) | |
41 call assert_equal('[]', string(b)) | |
42 endfunc | |
43 | |
44 func Test_blob_compare() | |
45 let b1 = 0z0011 | |
46 let b2 = 0z1100 | |
47 call assert_false(b1 == b2) | |
48 call assert_true(b1 != b2) | |
49 call assert_true(b1 == 0z0011) | |
50 | |
51 call assert_false(b1 is b2) | |
52 let b2 = b1 | |
53 call assert_true(b1 is b2) | |
54 | |
55 call assert_fails('let x = b1 > b2') | |
56 call assert_fails('let x = b1 < b2') | |
57 call assert_fails('let x = b1 - b2') | |
58 call assert_fails('let x = b1 / b2') | |
59 call assert_fails('let x = b1 * b2') | |
60 endfunc | |
61 | |
62 " test for range assign | |
63 func Test_blob_range_assign() | |
64 let b = 0z00 | |
65 let b[1] = 0x11 | |
66 let b[2] = 0x22 | |
67 call assert_equal(0z001122, b) | |
68 call assert_fails('let b[4] = 0x33') | |
69 endfunc | |
70 | |
71 func Test_blob_for_loop() | |
72 let blob = 0z00010203 | |
73 let i = 0 | |
74 for byte in blob | |
75 call assert_equal(i, byte) | |
76 let i += 1 | |
77 endfor | |
78 | |
79 let blob = 0z00 | |
80 call remove(blob, 0) | |
81 call assert_equal(0, len(blob)) | |
82 for byte in blob | |
83 call assert_error('loop over empty blob') | |
84 endfor | |
85 endfunc | |
86 | |
87 func Test_blob_concatenate() | |
88 let b = 0z0011 | |
89 let b += 0z2233 | |
90 call assert_equal(0z00112233, b) | |
91 | |
92 call assert_fails('let b += "a"') | |
93 call assert_fails('let b += 88') | |
94 | |
95 let b = 0zDEAD + 0zBEEF | |
96 call assert_equal(0zDEADBEEF, b) | |
97 endfunc | |
98 | |
99 " Test removing items in blob | |
100 func Test_blob_func_remove() | |
101 " Test removing 1 element | |
102 let b = 0zDEADBEEF | |
103 call assert_equal(0xDE, remove(b, 0)) | |
104 call assert_equal(0zADBEEF, b) | |
105 | |
106 let b = 0zDEADBEEF | |
107 call assert_equal(0xEF, remove(b, -1)) | |
108 call assert_equal(0zDEADBE, b) | |
109 | |
110 let b = 0zDEADBEEF | |
111 call assert_equal(0xAD, remove(b, 1)) | |
112 call assert_equal(0zDEBEEF, b) | |
113 | |
114 " Test removing range of element(s) | |
115 let b = 0zDEADBEEF | |
116 call assert_equal(0zBE, remove(b, 2, 2)) | |
117 call assert_equal(0zDEADEF, b) | |
118 | |
119 let b = 0zDEADBEEF | |
120 call assert_equal(0zADBE, remove(b, 1, 2)) | |
121 call assert_equal(0zDEEF, b) | |
122 | |
123 " Test invalid cases | |
124 let b = 0zDEADBEEF | |
125 call assert_fails("call remove(b, 5)", 'E979:') | |
126 call assert_fails("call remove(b, 1, 5)", 'E979:') | |
127 call assert_fails("call remove(b, 3, 2)", 'E979:') | |
128 call assert_fails("call remove(1, 0)", 'E712:') | |
129 call assert_fails("call remove(b, b)", 'E974:') | |
130 endfunc | |
131 | |
132 func Test_blob_read_write() | |
133 let b = 0zDEADBEEF | |
134 call writefile(b, 'Xblob') | |
135 let br = readfile('Xblob', 'B') | |
136 call assert_equal(b, br) | |
137 call delete('Xblob') | |
138 endfunc | |
139 | |
140 " filter() item in blob | |
141 func Test_blob_filter() | |
142 let b = 0zDEADBEEF | |
143 call filter(b, 'v:val != 0xEF') | |
144 call assert_equal(0zDEADBE, b) | |
145 endfunc | |
146 | |
147 " map() item in blob | |
148 func Test_blob_map() | |
149 let b = 0zDEADBEEF | |
150 call map(b, 'v:val + 1') | |
151 call assert_equal(0zDFAEBFF0, b) | |
152 endfunc | |
153 | |
154 func Test_blob_index() | |
155 call assert_equal(2, index(0zDEADBEEF, 0xBE)) | |
156 call assert_equal(-1, index(0zDEADBEEF, 0)) | |
157 endfunc | |
158 | |
159 func Test_blob_insert() | |
160 let b = 0zDEADBEEF | |
161 call insert(b, 0x33) | |
162 call assert_equal(0z33DEADBEEF, b) | |
163 | |
164 let b = 0zDEADBEEF | |
165 call insert(b, 0x33, 2) | |
166 call assert_equal(0zDEAD33BEEF, b) | |
167 endfunc | |
168 | |
169 func Test_blob_reverse() | |
170 call assert_equal(0zEFBEADDE, reverse(0zDEADBEEF)) | |
171 call assert_equal(0zBEADDE, reverse(0zDEADBE)) | |
172 call assert_equal(0zADDE, reverse(0zDEAD)) | |
173 call assert_equal(0zDE, reverse(0zDE)) | |
174 endfunc | |
175 | |
176 func Test_blob_json_encode() | |
177 call assert_equal('[222,173,190,239]', json_encode(0zDEADBEEF)) | |
178 call assert_equal('[]', json_encode(0z)) | |
179 endfunc |