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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ccf2a504724afc56fa86576ce25a3acf31ed62db
4
- data.tar.gz: ba444fb4a80331bf4f0e6360c6163febcfaaa358
3
+ metadata.gz: e06a8f8d192ef2bfd7ef1b08ac24a51673c70d59
4
+ data.tar.gz: 96188fdf46de1a6d1d010c50da43ad1806c183a4
5
5
  SHA512:
6
- metadata.gz: f0c61d1f31d7cf262efd8eaabbf25ad4039caa964b85c6c14ac0118b9dda3c5847143e73a3ce65a1a7b544e274a92e388b406c18143c8fc13f64c3a52e61593f
7
- data.tar.gz: 6a79eb757a4b12c3288c2b40f4b849a1df59288ea93e3b03526a8bad62262603c73a535cdd9624bcaee3edfed8600eb80c59707e40cfac75e2d70065f15b10d0
6
+ metadata.gz: a47bdd20de56a8417d0800f3a0c206c6f4d34ca0f26413403a23166a42a4d739c5d09054a53f20dee3d662944ced8ba47c90010a805fa657715085a9c57f8455
7
+ data.tar.gz: d063f5752aa2297dd706c10690f01a122eebaf1fe97e238a4ef4b7d22ac7febaf18d047036b2cb7cbb4b889f9ce0935e5969c6b1b0306c83a5441c78754b6fce
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nstrct (0.1.2)
4
+ nstrct (0.1.3)
5
5
  digest-crc
6
6
 
7
7
  GEM
@@ -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.to_i ? 1 : 0].pack('C')
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
- data += [@value.size].pack('C')
128
- @value.each do |val|
129
- data = pack_value(@datatype, val, data)
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')
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Nstrct
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nstrct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joël Gähwiler