xdr 0.0.3 → 0.0.4

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: 338a81fa5cb0c2cc1f5db4449c4b7794a6e96083
4
- data.tar.gz: dee4b7e4edc80cebe761a8ea2588936abb559d56
3
+ metadata.gz: bc2054a14c8b40897a81b9758fe8f7d48fa5be07
4
+ data.tar.gz: 3bddeb7f3628cb88feae1800467e060080b8b942
5
5
  SHA512:
6
- metadata.gz: c9ffd4fcccdcb2cd41a4992cad1900add89645e54c164d4a5879374625e0c3e77701bc8cf94cfb8bef6e1d12a1ec155d7dce4708b4e8df074b0cc3554062ad50
7
- data.tar.gz: c82033a24062fcbfd46ac04b9fc7dd0a650bcd5e734bcc42f04c1ac2f2af6c54270687b6f33bdc7ad86f50f038ce56ccc84f105b63cf1d198b600cbe260cdb4e
6
+ metadata.gz: b22b70f5094869e0f09d9819b63746eab3416416eb4a7c2128043888e587dc4e7a5e7a7452e9df925d302d9f771883ca72b76615c9588059c15d9fec793ff59d
7
+ data.tar.gz: 6d8bdbafec1e37e2a6a9ade6d139ebd94cf297277e9ff25b676560d6f1d85f70fd8e7e71123d01170e5fcc09a285253a9ce56c303b1fb4f2d84f08e52082c329
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ .DS_Store
@@ -27,10 +27,3 @@ r.get # => nil
27
27
 
28
28
  r.set(:nonsense)
29
29
  r.get # => nil
30
-
31
-
32
- # TODO
33
- case r
34
- when ResultType::OK ; puts "i'm ok!"
35
- when ResultType::ERROR ; puts "broken!"
36
- end
@@ -10,7 +10,7 @@ module XDR::DSL::Union
10
10
  def switch(switch, arm=nil)
11
11
  raise ArgumentError, "`switch_on` not defined yet" if self.switch_type.nil?
12
12
 
13
- switch = normalize_switch(switch)
13
+ switch = normalize_switch_declaration(switch)
14
14
  self.switches = self.switches.merge(switch => arm)
15
15
  end
16
16
 
@@ -22,11 +22,18 @@ module XDR::DSL::Union
22
22
  end
23
23
 
24
24
  private
25
- def normalize_switch(switch)
26
- case switch
27
- when self.switch_type ; switch
28
- when :default ; switch
29
- else ; self.switch_type.from_name(switch)
25
+ def normalize_switch_declaration(switch)
26
+ case
27
+ when switch.is_a?(self.switch_type)
28
+ switch
29
+ when self.switch_type.valid?(switch)
30
+ switch
31
+ when switch == :default
32
+ switch
33
+ when self.switch_type.respond_to?(:from_name)
34
+ self.switch_type.from_name(switch)
35
+ else
36
+ raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{self.switch_type}"
30
37
  end
31
38
  end
32
- end
39
+ end
@@ -21,7 +21,11 @@ class XDR::Union
21
21
  attribute_method_suffix '!'
22
22
 
23
23
  def self.arm_for_switch(switch)
24
- raise XDR::InvalidSwitchError unless switch.is_a?(switch_type)
24
+ begin
25
+ switch = normalize_switch switch
26
+ rescue ArgumentError => e
27
+ raise XDR::InvalidSwitchError, e.message
28
+ end
25
29
 
26
30
  result = switches.fetch(switch, :switch_not_found)
27
31
  result = switches.fetch(:default, :switch_not_found) if result == :switch_not_found
@@ -33,6 +37,19 @@ class XDR::Union
33
37
  result
34
38
  end
35
39
 
40
+ def self.normalize_switch(switch)
41
+ case
42
+ when switch.is_a?(self.switch_type)
43
+ switch
44
+ when self.switch_type.valid?(switch)
45
+ switch
46
+ when self.switch_type.respond_to?(:from_name)
47
+ self.switch_type.from_name(switch)
48
+ else
49
+ raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{self.switch_type}"
50
+ end
51
+ end
52
+
36
53
  def self.read(io)
37
54
  switch = switch_type.read(io)
38
55
  arm = arm_for_switch(switch)
@@ -51,11 +68,11 @@ class XDR::Union
51
68
  val.is_a?(self)
52
69
  end
53
70
 
54
- def initialize(switch=nil, value=:void)
71
+ def initialize(switch=:__unset__, value=:void)
55
72
  @switch = nil
56
73
  @arm = nil
57
74
  @value = nil
58
- set(switch, value) if switch
75
+ set(switch, value) unless switch == :__unset__
59
76
  end
60
77
 
61
78
  def to_xdr
@@ -63,7 +80,7 @@ class XDR::Union
63
80
  end
64
81
 
65
82
  def set(switch, value=:void)
66
- @switch = switch.is_a?(switch_type) ? switch : switch_type.from_name(switch)
83
+ @switch = self.class.normalize_switch switch
67
84
  @arm = self.class.arm_for_switch @switch
68
85
 
69
86
  raise XDR::InvalidValueError unless valid_for_arm_type(value, @arm)
@@ -1,3 +1,3 @@
1
1
  module XDR
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -19,4 +19,51 @@ describe XDR::DSL::Union, "#switch" do
19
19
  member :error, 1
20
20
  seal
21
21
  end
22
- end
22
+ end
23
+
24
+ describe XDR::DSL::Union, "#switch_on" do
25
+ klass = nil
26
+
27
+ it "allows int types" do
28
+ expect do
29
+ klass = Class.new(XDR::Union) do
30
+ switch_on XDR::Int, :type
31
+ switch 0
32
+ switch 1
33
+ end
34
+ end.to_not raise_error
35
+
36
+ expect{ klass.new(0) }.to_not raise_error
37
+ expect{ klass.new(1) }.to_not raise_error
38
+ expect{ klass.new(2) }.to raise_error(XDR::InvalidSwitchError)
39
+ end
40
+
41
+ it "allows unsigned int types" do
42
+ expect do
43
+ klass = Class.new(XDR::Union) do
44
+ switch_on XDR::UnsignedInt, :type
45
+ switch 0
46
+ switch 1
47
+ end
48
+ end.to_not raise_error
49
+
50
+ expect{ klass.new(0) }.to_not raise_error
51
+ expect{ klass.new(1) }.to_not raise_error
52
+ expect{ klass.new(2) }.to raise_error(XDR::InvalidSwitchError)
53
+ expect{ klass.new(-1) }.to raise_error(XDR::InvalidSwitchError)
54
+ end
55
+
56
+ it "allows bool types", :focus do
57
+ klass = nil
58
+
59
+ expect do
60
+ klass = Class.new(XDR::Union) do
61
+ switch_on XDR::Bool, :type
62
+ switch true
63
+ end
64
+ end.to_not raise_error
65
+
66
+ expect{ klass.new(true) }.to_not raise_error
67
+ expect{ klass.new(false) }.to raise_error(XDR::InvalidSwitchError)
68
+ end
69
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xdr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Fleckenstein
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-11 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport