File: value_decoder_spec.rb

package info (click to toggle)
ruby-amq-protocol 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: ruby: 5,225; python: 248; makefile: 4
file content (86 lines) | stat: -rw-r--r-- 2,418 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
require 'time'
require "amq/protocol/table_value_decoder"

module AMQ
  module Protocol
    RSpec.describe TableValueDecoder do

      it "is capable of decoding basic arrays TableValueEncoder encodes" do
        input1 = [1, 2, 3]

        value, _offset = described_class.decode_array(TableValueEncoder.encode(input1), 1)
        expect(value.size).to eq(3)
        expect(value.first).to eq(1)
        expect(value).to eq(input1)



        input2 = ["one", 2, "three"]

        value, _offset = described_class.decode_array(TableValueEncoder.encode(input2), 1)
        expect(value.size).to eq(3)
        expect(value.first).to eq("one")
        expect(value).to eq(input2)



        input3 = ["one", 2, "three", 4.0, 5000000.0]

        value, _offset = described_class.decode_array(TableValueEncoder.encode(input3), 1)
        expect(value.size).to eq(5)
        expect(value.last).to eq(5000000.0)
        expect(value).to eq(input3)
      end



      it "is capable of decoding arrays TableValueEncoder encodes" do
        input1 = [{ "one" => 2 }, 3]
        data1  = TableValueEncoder.encode(input1)

        # puts(TableValueEncoder.encode({ "one" => 2 }).inspect)
        # puts(TableValueEncoder.encode(input1).inspect)


        value, _offset = described_class.decode_array(data1, 1)
        expect(value.size).to eq(2)
        expect(value.first).to eq(Hash["one" => 2])
        expect(value).to eq(input1)



        input2 = ["one", 2, { "three" => { "four" => 5.0 } }]

        value, _offset = described_class.decode_array(TableValueEncoder.encode(input2), 1)
        expect(value.size).to eq(3)
        expect(value.last["three"]["four"]).to eq(5.0)
        expect(value).to eq(input2)
      end

      it "is capable of decoding 32 bit float values" do
        input = Float32Bit.new(10.0)
        data  = TableValueEncoder.encode(input)

        value = described_class.decode_32bit_float(data, 1)[0]
        expect(value).to eq(10.0)
      end

      context "8bit/byte decoding" do
        let(:examples) {
          {
              0x00 => "\x00",
              0x01 => "\x01",
              0x10 => "\x10",
              255   => "\xFF" # not -1
          }
        }

        it "is capable of decoding byte values" do
          examples.each do |key, value|
            expect(described_class.decode_byte(value, 0).first).to eq(key)
          end
        end
      end
    end
  end
end