File: ext_value_spec.rb

package info (click to toggle)
ruby-msgpack 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 972 kB
  • sloc: ruby: 4,789; ansic: 4,309; java: 1,809; makefile: 4
file content (99 lines) | stat: -rw-r--r-- 2,803 bytes parent folder | download | duplicates (6)
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
# encoding: ascii-8bit
require 'spec/spec_helper'

describe MessagePack::ExtensionValue do
  subject do
    described_class.new(1, "data")
  end

  describe '#type/#type=' do
    it 'returns value set by #initialize' do
      subject.type.should == 1
    end

    it 'assigns a value' do
      subject.type = 2
      subject.type.should == 2
    end
  end

  describe '#payload/#payload=' do
    it 'returns value set by #initialize' do
      subject.payload.should == "data"
    end

    it 'assigns a value' do
      subject.payload = "a"
      subject.payload.should == "a"
    end
  end

  describe '#==/#eql?/#hash' do
    it 'returns equivalent if the content is same' do
      ext1 = MessagePack::ExtensionValue.new(1, "data")
      ext2 = MessagePack::ExtensionValue.new(1, "data")
      (ext1 == ext2).should be true
      ext1.eql?(ext2).should be true
      (ext1.hash == ext2.hash).should be true
    end

    it 'returns not equivalent if type is not same' do
      ext1 = MessagePack::ExtensionValue.new(1, "data")
      ext2 = MessagePack::ExtensionValue.new(2, "data")
      (ext1 == ext2).should be false
      ext1.eql?(ext2).should be false
      (ext1.hash == ext2.hash).should be false
    end

    it 'returns not equivalent if payload is not same' do
      ext1 = MessagePack::ExtensionValue.new(1, "data")
      ext2 = MessagePack::ExtensionValue.new(1, "value")
      (ext1 == ext2).should be false
      ext1.eql?(ext2).should be false
      (ext1.hash == ext2.hash).should be false
    end
  end

  describe '#to_msgpack' do
    it 'serializes very short payload' do
      ext = MessagePack::ExtensionValue.new(1, "a"*2).to_msgpack
      ext.should == "\xd5\x01" + "a"*2
    end

    it 'serializes short payload' do
      ext = MessagePack::ExtensionValue.new(1, "a"*18).to_msgpack
      ext.should == "\xc7\x12\x01" + "a"*18
    end

    it 'serializes long payload' do
      ext = MessagePack::ExtensionValue.new(1, "a"*65540).to_msgpack
      ext.should == "\xc9\x00\x01\x00\x04\x01" + "a"*65540
    end

    it 'with a packer serializes to a packer' do
      ext = MessagePack::ExtensionValue.new(1, "aa")
      packer = MessagePack::Packer.new
      ext.to_msgpack(packer)
      packer.buffer.to_s.should == "\xd5\x01aa"
    end

    [-129, -65540, -(2**40), 128, 65540, 2**40].each do |type|
      context "with invalid type (#{type})" do
        it 'raises RangeError' do
          lambda { MessagePack::ExtensionValue.new(type, "a").to_msgpack }.should raise_error(RangeError)
        end
      end
    end
  end

  describe '#dup' do
    it 'duplicates' do
      ext1 = MessagePack::ExtensionValue.new(1, "data")
      ext2 = ext1.dup
      ext2.type = 2
      ext2.payload = "data2"
      ext1.type.should == 1
      ext1.payload.should == "data"
    end
  end
end