File: utils_spec.rb

package info (click to toggle)
vim-command-t 5.0.2-5-g7147ba9-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 744 kB
  • sloc: ruby: 3,394; ansic: 1,177; makefile: 27; sh: 26; xml: 11
file content (415 lines) | stat: -rw-r--r-- 13,041 bytes parent folder | download | duplicates (4)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# Copyright 2014-present Greg Hurrell. All rights reserved.
# Licensed under the terms of the BSD 2-clause license.

require 'spec_helper'

describe CommandT::Watchman::Utils do
  def binary(str)
    if str.respond_to?(:force_encoding) # Ruby >= 1.9
      str.force_encoding('ASCII-8BIT')
    else
      str
    end
  end

  def little_endian?
    byte = [0xff00].pack('s')[0]
    if byte.is_a?(Fixnum) # ie. Ruby 1.8
      byte.zero?
    elsif byte.is_a?(String) # ie. Ruby >= 1.9
      byte == "\x00"
    else
      raise 'unable to determine endianness'
    end
  end

  def roundtrip(value)
    described_class.load(described_class.dump(value))
  end

  it 'roundtrips arrays' do
    value = [1, 2, ['three', false]]
    expect(roundtrip(value)).to eq(value)
  end

  it 'roundtrips hashes' do
    value = {
      'foo' => 1,
      'bar' => {
        'baz' => 'bing',
      }
    }
    expect(roundtrip(value)).to eq(value)
  end

  it 'roundtrips strings' do
    expect(roundtrip('')).to eq('')
    expect(roundtrip('/foo/bar/baz')).to eq('/foo/bar/baz')
  end

  it 'roundtrips uint8_t integers' do
    expect(roundtrip(0)).to eq(0)
    expect(roundtrip(1)).to eq(1)
    expect(roundtrip(0xff)).to eq(0xff)
  end

  it 'roundtrips uint16_t integers' do
    expect(roundtrip(0x1234)).to eq(0x1234)
  end

  it 'roundtrips uint32_t integers' do
    expect(roundtrip(0x12345678)).to eq(0x12345678)
  end

  it 'roundtrips uint64_t integers' do
    expect(roundtrip(0x12345678abcdef00)).to eq(0x12345678abcdef00)
  end

  it 'roundtrips floats' do
    expect(roundtrip(1234.5678)).to eq(1234.5678)
  end

  it 'roundtrips `true` booleans' do
    expect(roundtrip(true)).to be true
  end

  it 'roundtrips `false` booleans' do
    expect(roundtrip(false)).to be false
  end

  it 'roundtrips nil' do
    expect(roundtrip(nil)).to be nil
  end

  describe '.load' do
    it 'rejects undersized input' do
      expect { described_class.load('') }.
        to raise_error(ArgumentError, /undersized/i)
    end

    it 'rejects input without a binary marker' do
      expect { described_class.load('gibberish') }.
        to raise_error(ArgumentError, /missing/i)
    end

    it 'rejects a missing payload header' do
      # binary protocol marker, but nothing else
      input = binary("\x00\x01")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /undersized/i)
    end

    it 'rejects empty payloads' do
      # uint8_t size marker of zero
      input = binary("\x00\x01\x03\x00")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /empty/i)
    end

    it 'rejects unrecognized payload markers' do
      # 0x10 is not a valid integer marker
      input = binary("\x00\x01\x10\x00")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /bad integer/i)
    end

    it 'rejects undersized payload markers' do
      # int16_t marker, but only storage for int8_t
      input = binary("\x00\x01\x04\x00")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /overrun\b.+\bint16_t/i)
    end

    it 'loads array values' do
      input = binary(
        "\x00\x01\x03\x16\x00\x03\x05\x03\x01\x02\x03" \
        "\x06foobar\x08\x09\x00\x03\x02\x03\x0a\x0a"
      )
      expect(described_class.load(input)).
        to eq([1, 'foobar', true, false, [10, nil]])
    end

    it 'handles empty arrays' do
      input = binary("\x00\x01\x03\x03\x00\x03\x00")
      expect(described_class.load(input)).to eq([])
    end

    it 'rejects arrays with incomplete headers' do
      input = binary("\x00\x01\x03\x02\x00\x03")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /incomplete array header/i)
    end

    it 'rejects arrays with incomplete entries' do
      input = binary("\x00\x01\x03\x05\x00\x03\x10\x0a\x0a")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /unexpected end/i)
    end

    it 'loads hash values' do
      input = binary(
        "\x00\x01\x03\x1a\x01\x03\x02\x02\x03\x03foo\x0a" \
        "\x02\x03\x03bar\x01\x03\x01\x02\x03\x03baz\x08"
      )
      expected = {
        'foo' => nil,
        'bar' => {
          'baz' => true,
        }
      }
      expect(described_class.load(input)).to eq(expected)
    end

    it 'handles empty hashes' do
      input = binary("\x00\x01\x03\x03\x01\x03\x00")
      expect(described_class.load(input)).to eq({})
    end

    it 'rejects hashes with incomplete headers' do
      input = binary("\x00\x01\x03\x02\x01\x03")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /incomplete hash header/i)
    end

    it 'rejects hashes with invalid keys' do
      # keys must be strings; this one uses uses a number instead
      input = binary("\x00\x01\x03\x05\x01\x03\x01\x03\x00")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /not a number/i)
    end

    it 'rejects hashes with missing keys' do
      input = binary("\x00\x01\x03\x03\x01\x03\x01")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /unexpected end/i)
    end

    it 'rejects hashes with missing values' do
      input = binary("\x00\x01\x03\x09\x01\x03\x01\x02\x03\x03foo")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /unexpected end/i)
    end

    it 'loads string values' do
      input = binary("\x00\x01\x03\x06\x02\x03\x03foo")
      expect(described_class.load(input)).to eq('foo')
    end

    it 'handles empty strings' do
      input = binary("\x00\x01\x03\x03\x02\x03\x00")
      expect(described_class.load(input)).to eq('')
    end

    if String.new.respond_to?(:encoding) # ie. Ruby >= 1.9
      it 'loads string values as ASCII-8BIT encoded strings' do
        input = binary("\x00\x01\x03\x06\x02\x03\x03foo")
        expect(described_class.load(input).encoding.to_s).to eq('ASCII-8BIT')
      end
    end

    it 'rejects string values with incomplete headers' do
      input = binary("\x00\x01\x03\x01\x02")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /invalid string header/i)
    end

    it 'rejects string values with invalid headers' do
      # expect a number indicating the string length, get a boolean instead
      input = binary("\x00\x01\x03\x05\x02\x08foo")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /bad integer/i)
    end

    it 'rejects string values with insufficient storage' do
      # expect 3 bytes, get 2 instead
      input = binary("\x00\x01\x03\x05\x02\x03\x03fo")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /insufficient string storage/i)
    end

    it 'loads uint8_t values' do
      input = binary("\x00\x01\x03\x02\x03\x12")
      expect(described_class.load(input)).to eq(0x12)
    end

    it 'loads uint16_t values' do
      if little_endian?
        input = binary("\x00\x01\x03\x03\x04\x34\x12")
      else
        input = binary("\x00\x01\x03\x03\x04\x12\x34")
      end

      expect(described_class.load(input)).to eq(0x1234)
    end

    it 'loads uint32_t values' do
      if little_endian?
        input = binary("\x00\x01\x03\x05\x05\x78\x56\x34\x12")
      else
        input = binary("\x00\x01\x03\x05\x05\x12\x34\x56\x78")
      end

      expect(described_class.load(input)).to eq(0x12345678)
    end

    it 'loads int uint64_t values' do
      if little_endian?
        input = binary("\x00\x01\x03\x09\x06\xef\xcd\xab\x90\x78\x56\x34\x12")
      else
        input = binary("\x00\x01\x03\x09\x06\x12\x34\x56\x78\x90\xab\xcd\xef")
      end
      expect(described_class.load(input)).to eq(0x1234567890abcdef)
    end

    it 'rejects int markers with missing values' do
      # expect an integer, but hit the end of the buffer
      input = binary("\x00\x01\x03\x01\x05")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /insufficient int storage/i)
    end

    it 'rejects double markers with insufficient storage' do
      # double with 7 bytes of storage instead of the required 8 bytes
      input = binary("\x00\x01\x03\x08\x07\x00\x00\x00\x00\x00\x00\x00")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /insufficient double storage/i)
    end

    it 'loads boolean `true` values' do
      input = binary("\x00\x01\x03\x01\x08")
      expect(described_class.load(input)).to be true
    end

    it 'loads boolean `false` values' do
      input = binary("\x00\x01\x03\x01\x09")
      expect(described_class.load(input)).to be false
    end

    it 'loads nil' do
      input = binary("\x00\x01\x03\x01\x0a")
      expect(described_class.load(input)).to be nil
    end

    it 'loads templates' do
      # this example includes a "skip" marker
      input = binary(
        "\x00\x01\x03\x28\x0b\x00\x03\x02\x02\x03\x04name" \
        "\x02\x03\x03age\x03\x03\x02\x03\x04fred\x03" \
        "\x14\x02\x03\x04pete\x03\x1e\x0c\x03\x19"
      )
      expected = [
        { 'name' => 'fred', 'age' => 20 },
        { 'name' => 'pete', 'age' => 30 },
        { 'age' => 25  },
      ]
      expect(described_class.load(input)).to eq(expected)
    end

    it 'handles empty templates' do
      input = binary(
        "\x00\x01\x03\x12\x0b\x00\x03\x02\x02" \
        "\x03\x03foo\x02\x03\x03bar\x03\x00"
      )
      expect(described_class.load(input)).to eq([])
    end

    it 'rejects templates without a header array' do
      input = binary("\x00\x01\x03\x01\x0b")
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /unexpected end/i)
    end

    it 'rejects templates without a row items array' do
      input = binary(
        "\x00\x01\x03\x10\x0b\x00\x03\x02\x02" \
        "\x03\x03foo\x02\x03\x03bar"
      )
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /insufficient/i)
    end

    it 'rejects templates with non-string header items' do
      input = binary(
        "\x00\x01\x03\x0e\x0b\x00\x03\x02\x02" \
        "\x03\x03foo\x03\x03\x03\x00"
      )
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /not a number/)
    end

    it 'rejects templates with a header item array count mismatch' do
      input = binary(
        "\x00\x01\x03\x0a\x0b\x00\x03\x02\x02" \
        "\x03\x03foo"
      )
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /unexpected end/)
    end

    it 'rejects templates with a row item count mismatch' do
      input = binary(
        "\x00\x01\x03\x25\x0b\x00\x03\x02\x02\x03\x04name" \
        "\x02\x03\x03age\x03\x03\x02\x03\x04fred\x03" \
        "\x14\x02\x03\x04pete\x03\x1e"
      )
      expect { described_class.load(input) }.
        to raise_error(ArgumentError, /unexpected end/)
    end
  end

  describe '.dump' do
    let(:query) do
      # this is the typical kind of query that Command-T will actually issue
      ['query', '/some/path', {
        'expression' => ['type', 'f'],
        'fields'     => ['name'],
      }]
    end

    it 'serializes' do
      expect { described_class.dump(query) }.to_not raise_error
    end

    if String.new.respond_to?(:encoding) # ie. Ruby >= 1.9
      it 'serializes to an ASCII-8BIT string' do
        expect(described_class.dump(query).encoding.to_s).to eq('ASCII-8BIT')
      end
    end

    it 'generates a correct serialization' do
      # in Ruby 1.8, hashes aren't ordered, so two serializations are possible
      if little_endian?
        expected = [
          binary(
            "\x00\x01\x06\x49\x00\x00\x00\x00\x00\x00\x00\x00\x03\x03\x02\x03" \
            "\x05query\x02\x03\x0a/some/path\x01\x03\x02\x02\x03\x0a" \
            "expression\x00\x03\x02\x02\x03\x04type\x02\x03\x01f\x02\x03\x06" \
            "fields\x00\x03\x01\x02\x03\x04name"
          ),
          binary(
            "\x00\x01\x06\x49\x00\x00\x00\x00\x00\x00\x00\x00\x03\x03\x02\x03" \
            "\x05query\x02\x03\x0a/some/path\x01\x03\x02\x02\x03\x06fields" \
            "\x00\x03\x01\x02\x03\x04name\x02\x03\x0aexpression\x00\x03\x02" \
            "\x02\x03\x04type\x02\x03\x01f"
          )
        ]
      else
        expected = [
          binary(
            "\x00\x01\x06\x00\x00\x00\x00\x00\x00\x00\x49\x00\x03\x03\x02\x03" \
            "\x05query\x02\x03\x0a/some/path\x01\x03\x02\x02\x03\x0a" \
            "expression\x00\x03\x02\x02\x03\x04type\x02\x03\x01f\x02\x03\x06" \
            "fields\x00\x03\x01\x02\x03\x04name"
          ),
          binary(
            "\x00\x01\x06\x00\x00\x00\x00\x00\x00\x00\x49\x00\x03\x03\x02\x03" \
            "\x05query\x02\x03\x0a/some/path\x01\x03\x02\x02\x03\x06fields" \
            "\x00\x03\x01\x02\x03\x04name\x02\x03\x0aexpression\x00\x03\x02" \
            "\x02\x03\x04type\x02\x03\x01f"
          )
        ]
      end
      expect(expected).to include(described_class.dump(query))
    end
  end
end