nstrct 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/nstrct/argument.rb +15 -4
- data/lib/nstrct/instruction.rb +1 -1
- data/lib/nstrct/version.rb +1 -1
- data/spec/protocol_spec.rb +19 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e06a8f8d192ef2bfd7ef1b08ac24a51673c70d59
|
4
|
+
data.tar.gz: 96188fdf46de1a6d1d010c50da43ad1806c183a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a47bdd20de56a8417d0800f3a0c206c6f4d34ca0f26413403a23166a42a4d739c5d09054a53f20dee3d662944ced8ba47c90010a805fa657715085a9c57f8455
|
7
|
+
data.tar.gz: d063f5752aa2297dd706c10690f01a122eebaf1fe97e238a4ef4b7d22ac7febaf18d047036b2cb7cbb4b889f9ce0935e5969c6b1b0306c83a5441c78754b6fce
|
data/Gemfile.lock
CHANGED
data/lib/nstrct/argument.rb
CHANGED
@@ -86,7 +86,7 @@ module Nstrct
|
|
86
86
|
def pack_value datatype, value, data
|
87
87
|
case datatype
|
88
88
|
when :boolean
|
89
|
-
data += [value
|
89
|
+
data += [to_boolean(value) ? 1 : 0].pack('C')
|
90
90
|
when :int8
|
91
91
|
data += [value.to_i].pack('c')
|
92
92
|
when :int16
|
@@ -118,15 +118,26 @@ module Nstrct
|
|
118
118
|
data
|
119
119
|
end
|
120
120
|
|
121
|
+
def to_boolean(value)
|
122
|
+
return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
123
|
+
return Integer(value) >= 1
|
124
|
+
rescue
|
125
|
+
return value == 'true'
|
126
|
+
end
|
127
|
+
|
121
128
|
# Pack a single argument in a buffer
|
122
129
|
def pack
|
123
130
|
data = ''
|
124
131
|
if @array
|
125
132
|
data += [DATATYPES[:array]].pack('C')
|
126
133
|
data += [DATATYPES[@datatype]].pack('C')
|
127
|
-
|
128
|
-
|
129
|
-
|
134
|
+
if @value.is_a?(Array)
|
135
|
+
data += [@value.size].pack('C')
|
136
|
+
@value.each do |val|
|
137
|
+
data = pack_value(@datatype, val, data)
|
138
|
+
end
|
139
|
+
else
|
140
|
+
data += [0].pack('C')
|
130
141
|
end
|
131
142
|
else
|
132
143
|
data += [DATATYPES[datatype]].pack('C')
|
data/lib/nstrct/instruction.rb
CHANGED
@@ -44,7 +44,7 @@ module Nstrct
|
|
44
44
|
|
45
45
|
# get all elements in arrays
|
46
46
|
def array_elements
|
47
|
-
@arguments.inject(0){ |sum, a| sum + (a.array ? a.value.size : 0) }
|
47
|
+
@arguments.inject(0){ |sum, a| sum + (a.array ? a.value.is_a?(Array) ? a.value.size : 0 : 0) }
|
48
48
|
end
|
49
49
|
|
50
50
|
# Pack a single instruction in a buffer
|
data/lib/nstrct/version.rb
CHANGED
data/spec/protocol_spec.rb
CHANGED
@@ -1,5 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
describe Nstrct::Argument do
|
4
|
+
|
5
|
+
it 'should coerce string booleans' do
|
6
|
+
Nstrct::Argument.new(:boolean, 'true', false).pack.should == "\x01\x01"
|
7
|
+
Nstrct::Argument.new(:boolean, 'false', false).pack.should == "\x01\x00"
|
8
|
+
Nstrct::Argument.new(:boolean, true, false).pack.should == "\x01\x01"
|
9
|
+
Nstrct::Argument.new(:boolean, false, false).pack.should == "\x01\x00"
|
10
|
+
Nstrct::Argument.new(:boolean, 1, false).pack.should == "\x01\x01"
|
11
|
+
Nstrct::Argument.new(:boolean, 0, false).pack.should == "\x01\x00"
|
12
|
+
Nstrct::Argument.new(:boolean, 2, false).pack.should == "\x01\x01"
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should treat wrong arrays' do
|
16
|
+
Nstrct::Argument.new(:boolean, 'no-array-value', true).pack.should == " \x01\x00"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
3
21
|
describe Nstrct::Instruction do
|
4
22
|
|
5
23
|
it 'should pack and unpack an empty instruction' do
|
@@ -9,7 +27,7 @@ describe Nstrct::Instruction do
|
|
9
27
|
end
|
10
28
|
|
11
29
|
it 'should pack and unpack an instructionw with arguments' do
|
12
|
-
instruction1 = Nstrct::Instruction.build(232, [:float32, 1.0 ], [:boolean, true], [:int8, 2], [:float32, 1.0], [[:uint16], [54, 23, 1973]])
|
30
|
+
instruction1 = Nstrct::Instruction.build(232, [:float32, 1.0 ], [[:boolean], []], [:boolean, true], [:int8, 2], [:float32, 1.0], [[:uint16], [54, 23, 1973]])
|
13
31
|
instruction2 = Nstrct::Instruction.parse(instruction1.pack)
|
14
32
|
instruction1.inspect.should == instruction2.inspect
|
15
33
|
end
|