mojombo-erlectricity 0.2.1 → 1.0.0

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.
Files changed (49) hide show
  1. data/History.txt +14 -1
  2. data/LICENSE +20 -0
  3. data/README.md +130 -0
  4. data/Rakefile +63 -60
  5. data/VERSION.yml +4 -0
  6. data/examples/echo/README.md +12 -0
  7. data/examples/echo/echo.erl +13 -0
  8. data/examples/echo/echo.rb +11 -0
  9. data/examples/gruff/gruff.erl +17 -18
  10. data/examples/gruff/gruff_provider.rb +12 -19
  11. data/examples/gruff/{gruff_run.erl → gruff_run.sh} +5 -3
  12. data/examples/gruff/{stat_run.erl → stat_run.sh} +5 -3
  13. data/examples/gruff/stat_writer.erl +6 -6
  14. data/examples/simple/README.md +5 -0
  15. data/examples/simple/rerl.rb +111 -0
  16. data/examples/simple/rerl.sh +37 -0
  17. data/examples/tinderl/README.md +14 -0
  18. data/examples/tinderl/tinderl.erl +19 -21
  19. data/examples/tinderl/tinderl.rb +11 -10
  20. data/ext/decoder.c +67 -60
  21. data/lib/erlectricity/condition.rb +35 -20
  22. data/lib/erlectricity/conditions/boolean.rb +11 -0
  23. data/lib/erlectricity/conditions/hash.rb +9 -10
  24. data/lib/erlectricity/conditions/static.rb +31 -10
  25. data/lib/erlectricity/conditions/type.rb +14 -14
  26. data/lib/erlectricity/constants.rb +3 -4
  27. data/lib/erlectricity/decoder.rb +205 -199
  28. data/lib/erlectricity/encoder.rb +49 -35
  29. data/lib/erlectricity/errors/decode_error.rb +1 -1
  30. data/lib/erlectricity/errors/encode_error.rb +1 -1
  31. data/lib/erlectricity/errors/erlectricity_error.rb +1 -1
  32. data/lib/erlectricity/matcher.rb +15 -32
  33. data/lib/erlectricity/port.rb +11 -11
  34. data/lib/erlectricity/receiver.rb +54 -64
  35. data/lib/erlectricity/types/list.rb +3 -1
  36. data/lib/erlectricity.rb +4 -7
  37. data/test/condition_spec.rb +8 -9
  38. data/test/decode_spec.rb +27 -28
  39. data/test/encode_spec.rb +31 -24
  40. data/test/matcher_spec.rb +24 -12
  41. data/test/port_spec.rb +3 -4
  42. data/test/receiver_spec.rb +18 -20
  43. data/test/test_helper.rb +9 -5
  44. metadata +36 -29
  45. data/CONTRIBUTORS +0 -2
  46. data/Manifest.txt +0 -45
  47. data/README.txt +0 -43
  48. data/setup.rb +0 -1585
  49. data/test/test_erlectricity.rb +0 -2
@@ -3,44 +3,44 @@ module Erlectricity
3
3
  attr_reader :input, :output
4
4
  attr_reader :skipped
5
5
  attr_reader :queue
6
-
6
+
7
7
  def initialize(input=STDIN, output=STDOUT)
8
8
  @input = input
9
9
  @output = output
10
-
10
+
11
11
  input.sync = true
12
12
  output.sync = true
13
-
13
+
14
14
  @encoder = Erlectricity::Encoder.new(nil)
15
15
  @skipped = []
16
16
  @queue = []
17
17
  end
18
-
18
+
19
19
  def receive
20
20
  queue.empty? ? read_from_input : queue.shift
21
21
  end
22
-
22
+
23
23
  def send(term)
24
24
  @encoder.out = StringIO.new('', 'w')
25
25
  @encoder.write_any(term)
26
26
  data = @encoder.out.string
27
27
  output.write([data.length].pack("N"))
28
- output.write data
28
+ output.write(data)
29
29
  end
30
-
30
+
31
31
  def restore_skipped
32
32
  @queue = self.skipped + self.queue
33
33
  end
34
-
34
+
35
35
  private
36
+
36
37
  def read_from_input
37
38
  raw = input.read(4)
38
39
  return nil unless raw
39
-
40
+
40
41
  packet_length = raw.unpack('N').first
41
42
  data = input.read(packet_length)
42
- result = Erlectricity::Decoder.read_any_from(data)
43
- result
43
+ Erlectricity::Decoder.decode(data)
44
44
  end
45
45
  end
46
46
  end
@@ -1,79 +1,69 @@
1
1
  module Erlectricity
2
- class Receiver
3
-
4
- attr_accessor :port
5
- attr_accessor :parent
6
- attr_accessor :matchers
7
-
8
- RECEIVE_LOOP = Object.new
9
- NO_MATCH = Object.new
10
-
11
- def initialize(port, parent=nil, &block)
12
- @port = port
13
- @parent = parent
14
- @matchers = []
15
- block.call self if block
16
- end
17
-
18
- def process(arg)
19
- matcher = @matchers.find{|r| r.matches? arg}
2
+ class Receiver
3
+ attr_accessor :port
4
+ attr_accessor :parent
5
+ attr_accessor :matchers
6
+
7
+ RECEIVE_LOOP = Object.new
8
+ NO_MATCH = Object.new
20
9
 
21
- if(matcher)
22
- port.restore_skipped
23
- matcher.run arg
24
- else
25
- NO_MATCH
10
+ def initialize(port, parent = nil, &block)
11
+ @port = port
12
+ @parent = parent
13
+ @matchers = []
14
+ block.call(self) if block
26
15
  end
27
- end
28
-
29
- def when(*args, &block)
30
- args = args.map do |a|
31
- case a
32
- when Condition then a
33
- when Class then TypeCondition.new(a)
34
- else StaticCondition.new(a)
16
+
17
+ def process(arg)
18
+ matcher = @matchers.find { |r| r.matches?(arg) }
19
+
20
+ if matcher
21
+ port.restore_skipped
22
+ matcher.run(arg)
23
+ else
24
+ NO_MATCH
35
25
  end
36
26
  end
37
-
38
- args = args.first if args.length == 1
39
- @matchers << Matcher.new(self, args, block)
40
- end
41
-
42
- def run
43
-
44
- loop do
45
- msg = port.receive
46
- return if msg.nil?
47
-
48
- case result = process(msg)
49
- when RECEIVE_LOOP then next
50
- when NO_MATCH
51
- port.skipped << msg
52
- next
53
- else
54
- break result
27
+
28
+ def when(arg, &block)
29
+ condition = Condition.for(arg)
30
+ @matchers << Matcher.new(self, condition, block)
31
+ end
32
+
33
+ def run
34
+ loop do
35
+ msg = port.receive
36
+ return if msg.nil?
37
+
38
+ case result = process(msg)
39
+ when RECEIVE_LOOP then next
40
+ when NO_MATCH
41
+ port.skipped << msg
42
+ next
43
+ else
44
+ break result
45
+ end
55
46
  end
56
47
  end
57
- end
58
-
59
- def receive(&block)
60
- Receiver.new(port, self, &block).run
61
- end
62
48
 
63
- def receive_loop
64
- RECEIVE_LOOP
65
- end
66
-
67
- def send!(*term)
68
- term = term.first if term.length == 1
69
- port.send(term)
49
+ def receive(&block)
50
+ Receiver.new(port, self, &block).run
51
+ end
52
+
53
+ def receive_loop
54
+ RECEIVE_LOOP
55
+ end
56
+
57
+ def send!(term)
58
+ port.send(term)
59
+ end
70
60
  end
71
61
  end
72
- end
73
-
74
62
 
75
63
  module Kernel
76
- def receive(input=STDIN, output=STDOUT, &block)
64
+ def receive(input = nil, output = nil, &block)
65
+ input ||= IO.new(3)
66
+ output ||= IO.new(4)
77
67
  Erlectricity::Receiver.new(Erlectricity::Port.new(input, output), nil, &block).run
78
68
  end
79
69
  end
@@ -1 +1,3 @@
1
- class Erlectricity::List < Array ; end
1
+ class Erlectricity::List < Array
2
+
3
+ end
data/lib/erlectricity.rb CHANGED
@@ -1,19 +1,18 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext])
2
2
 
3
+ require 'stringio'
3
4
 
4
5
  require 'erlectricity/constants'
5
-
6
6
  require 'erlectricity/types/new_reference'
7
7
  require 'erlectricity/types/pid'
8
8
  require 'erlectricity/types/function'
9
9
  require 'erlectricity/types/list'
10
10
 
11
-
12
11
  begin
13
- #try to load the decoder C extension
12
+ # try to load the decoder C extension
14
13
  require 'decoder'
15
14
  rescue LoadError
16
- #load the pure ruby instead
15
+ # fall back on the pure ruby version
17
16
  require 'erlectricity/decoder'
18
17
  end
19
18
 
@@ -21,14 +20,12 @@ require 'erlectricity/encoder'
21
20
 
22
21
  require 'erlectricity/port'
23
22
  require 'erlectricity/matcher'
24
-
25
23
  require 'erlectricity/condition'
24
+ require 'erlectricity/conditions/boolean'
26
25
  require 'erlectricity/conditions/hash'
27
26
  require 'erlectricity/conditions/static'
28
27
  require 'erlectricity/conditions/type'
29
-
30
28
  require 'erlectricity/receiver'
31
-
32
29
  require 'erlectricity/errors/erlectricity_error'
33
30
  require 'erlectricity/errors/decode_error'
34
31
  require 'erlectricity/errors/encode_error'
@@ -6,14 +6,14 @@ context "Erlectricity::StaticConditions" do
6
6
  Erlectricity::StaticCondition.new([:foo]).satisfies?([:foo]).should == true
7
7
  Erlectricity::StaticCondition.new(3).satisfies?(3).should == true
8
8
  end
9
-
9
+
10
10
  specify "should not satisfy on different values" do
11
11
  Erlectricity::StaticCondition.new(:foo).satisfies?("foo").should == false
12
12
  Erlectricity::StaticCondition.new([:foo]).satisfies?(:foo).should == false
13
13
  Erlectricity::StaticCondition.new(Object.new).satisfies?(Object.new).should == false
14
14
  Erlectricity::StaticCondition.new(3).satisfies?(3.0).should == false
15
15
  end
16
-
16
+
17
17
  specify "should not produce any bindings" do
18
18
  s = Erlectricity::StaticCondition.new(:foo)
19
19
  s.binding_for(:foo).should == nil
@@ -29,25 +29,24 @@ context "Erlectricity::TypeConditions" do
29
29
  Erlectricity::TypeCondition.new(Array).satisfies?([]).should == true
30
30
  Erlectricity::TypeCondition.new(Fixnum).satisfies?(3).should == true
31
31
  end
32
-
32
+
33
33
  specify "should be satisfied when the arg is of a descendent class" do
34
34
  Erlectricity::TypeCondition.new(Object).satisfies?(:foo).should == true
35
35
  Erlectricity::TypeCondition.new(Object).satisfies?("foo").should == true
36
36
  Erlectricity::TypeCondition.new(Object).satisfies?(3).should == true
37
37
  end
38
-
38
+
39
39
  specify "should not be satisfied when the arg is of a different class" do
40
40
  Erlectricity::TypeCondition.new(String).satisfies?(:foo).should == false
41
41
  Erlectricity::TypeCondition.new(Symbol).satisfies?("foo").should == false
42
42
  Erlectricity::TypeCondition.new(Fixnum).satisfies?(3.0).should == false
43
43
  end
44
-
44
+
45
45
  specify "should bind the arg with no transormations" do
46
46
  s = Erlectricity::TypeCondition.new(Symbol)
47
47
  s.binding_for(:foo).should == :foo
48
48
  s.binding_for(:bar).should == :bar
49
49
  end
50
-
51
50
  end
52
51
 
53
52
  context "Erlectricity::HashConditions" do
@@ -55,17 +54,17 @@ context "Erlectricity::HashConditions" do
55
54
  Erlectricity::HashCondition.new.satisfies?([[:foo, 3], [:bar, Object.new]]).should == true
56
55
  Erlectricity::HashCondition.new.satisfies?([[:foo, 3]]).should == true
57
56
  end
58
-
57
+
59
58
  specify "should satisfy on empty arrays" do
60
59
  Erlectricity::HashCondition.new.satisfies?([]).should == true
61
60
  end
62
-
61
+
63
62
  specify "should nat satisfy other args" do
64
63
  Erlectricity::HashCondition.new.satisfies?(:foo).should == false
65
64
  Erlectricity::HashCondition.new.satisfies?("foo").should == false
66
65
  Erlectricity::HashCondition.new.satisfies?(3.0).should == false
67
66
  end
68
-
67
+
69
68
  specify "should bind to a Hash" do
70
69
  s = Erlectricity::HashCondition.new()
71
70
  s.binding_for([[:foo, 3], [:bar, [3,4,5]]]).should == {:foo => 3, :bar => [3,4,5] }
data/test/decode_spec.rb CHANGED
@@ -3,127 +3,126 @@ require File.dirname(__FILE__) + '/test_helper.rb'
3
3
  context "When unpacking from a binary stream" do
4
4
  setup do
5
5
  end
6
-
6
+
7
7
  specify "an erlang atom should decode to a ruby symbol" do
8
8
  get("haha").should == :haha
9
9
  end
10
-
10
+
11
11
  specify "an erlang number encoded as a small_int (< 255) should decode to a fixnum" do
12
12
  get("0").should == 0
13
13
  get("255").should == 255
14
14
  end
15
-
15
+
16
16
  specify "an erlang number encoded as a int (signed 27-bit number) should decode to a fixnum" do
17
17
  get("256").should == 256
18
18
  get("#{(1 << 27) -1}").should == (1 << 27) -1
19
19
  get("-1").should == -1
20
20
  get("#{-(1 << 27)}").should == -(1 << 27)
21
21
  end
22
-
23
-
22
+
24
23
  specify "an erlang number encoded as a small bignum (1 byte length) should decode to fixnum if it can" do
25
24
  get("#{(1 << 27)}").should == (1 << 27)
26
25
  get("#{-(1 << 27) - 1}").should == -(1 << 27) - 1
27
26
  get("#{(1 << word_length) - 1}").should == (1 << word_length) - 1
28
27
  get("#{-(1 << word_length)}").should == -(1 << word_length)
29
28
  end
30
-
29
+
31
30
  specify "an erlang number encoded as a small bignum (1 byte length) should decode to bignum if it can't be a fixnum" do
32
31
  get("#{(1 << word_length)}").should == (1 << word_length)
33
32
  get("#{-(1 << word_length) - 1}").should == -(1 << word_length) - 1
34
33
  get("#{(1 << (255 * 8)) - 1}").should == (1 << (255 * 8)) - 1
35
34
  get("#{-((1 << (255 * 8)) - 1)}").should == -((1 << (255 * 8)) - 1)
36
35
  end
37
-
38
-
36
+
39
37
  specify "an erlang number encoded as a big bignum (4 byte length) should decode to bignum" do
40
38
  get("#{(1 << (255 * 8)) }").should == (1 << (255 * 8))
41
39
  get("#{-(1 << (255 * 8))}").should == -(1 << (255 * 8))
42
40
  get("#{(1 << (512 * 8)) }").should == (1 << (512 * 8))
43
41
  get("#{-(1 << (512 * 8))}").should == -(1 << (512 * 8))
44
42
  end
45
-
43
+
46
44
  specify "an erlang float should decode to a Float" do
47
45
  get("#{1.0}").should == 1.0
48
46
  get("#{-1.0}").should == -1.0
49
47
  get("#{123.456}").should == 123.456
50
48
  get("#{123.456789012345}").should == 123.456789012345
51
49
  end
52
-
53
-
50
+
54
51
  specify "an erlang reference should decode to a Reference object" do
55
52
  ref = get("make_ref()")
56
53
  ref.should.be.instance_of Erlectricity::NewReference
57
54
  ref.node.should.be.instance_of Symbol
58
55
  end
59
-
56
+
60
57
  specify "an erlang pid should decode to a Pid object" do
61
58
  pid = get("spawn(fun() -> 3 end)")
62
59
  pid.should.be.instance_of Erlectricity::Pid
63
60
  pid.node.should.be.instance_of Symbol
64
61
  end
65
-
66
-
62
+
67
63
  specify "an erlang tuple encoded as a small tuple (1-byte length) should decode to an array" do
68
64
  ref = get("{3}")
69
65
  ref.length.should == 1
70
66
  ref.first.should == 3
71
-
67
+
72
68
  ref = get("{3, a, make_ref()}")
73
69
  ref.length.should == 3
74
70
  ref[0].should == 3
75
71
  ref[1].should == :a
76
72
  ref[2].class.should == Erlectricity::NewReference
77
-
73
+
78
74
  tuple_meat = (['3'] * 255).join(', ')
79
75
  ref = get("{#{tuple_meat}}")
80
76
  ref.length.should == 255
81
77
  ref.each{|r| r.should == 3}
82
78
  end
83
-
84
-
79
+
85
80
  specify "an erlang tuple encoded as a large tuple (4-byte length) should decode to an array" do
86
81
  tuple_meat = (['3'] * 256).join(', ')
87
82
  ref = get("{#{tuple_meat}}")
88
83
  ref.length.should == 256
89
84
  ref.each{|r| r.should == 3}
90
-
85
+
91
86
  tuple_meat = (['3'] * 512).join(', ')
92
87
  ref = get("{#{tuple_meat}}")
93
88
  ref.length.should == 512
94
89
  ref.each{|r| r.should == 3}
95
90
  end
96
-
97
-
91
+
98
92
  specify "an empty erlang list encoded as a nil should decode to an array" do
99
93
  get("[]").should == []
100
94
  end
101
-
95
+
102
96
  specify "an erlang list encoded as a string should decode to an array of bytes (less than ideal, but consistent)" do
103
97
  get("\"asdasd\"").should == "asdasd".split('').map{|c| c[0]}
104
98
  get("\"#{'a' * 65534}\"").should == ['a'[0]] * 65534
105
99
  end
106
-
100
+
107
101
  specify "an erlang list encoded as a list should decode to a array" do
108
102
  get("[3,4,256]").should == [3,4,256]
109
103
  get("\"#{'a' * 65535 }\"").should == [97] * 65535
110
104
  get("[3,4, foo, {3,4,5,bar}, 256]").should == [3,4, :foo, [3,4,5,:bar], 256]
111
105
  end
112
-
113
-
106
+
114
107
  specify "an erlang binary should decode to a string" do
115
108
  get("<< 3,4,255 >>").should == "\003\004\377"
116
109
  get("<< \"whatup\" >>").should == "whatup"
117
110
  end
118
-
111
+
112
+ specify "erlang atomic booleans should decode to ruby booleans" do
113
+ get("true").should == true
114
+ get("false").should == false
115
+ get("falsereio").should == :falsereio
116
+ end
117
+
119
118
  specify "a good thing should be awesome" do
120
119
  get(%Q-[{options,{struct,[{test,<<"I'm chargin' mah lazer">>}]}},{passage,<<"Why doesn't this work?">>}]-).should ==
121
120
  [[:options, [:struct, [[:test, "I'm chargin' mah lazer"]]]], [:passage, "Why doesn't this work?"]]
122
121
  end
123
-
122
+
124
123
  def get(str)
125
124
  x = "term_to_binary(#{str.gsub(/"/, '\\\"')})"
126
125
  bin = run_erl(x)
127
- Erlectricity::Decoder.read_any_from(bin)
126
+ Erlectricity::Decoder.decode(bin)
128
127
  end
129
128
  end
data/test/encode_spec.rb CHANGED
@@ -5,19 +5,26 @@ context "When packing to a binary stream" do
5
5
  @out = StringIO.new('', 'w')
6
6
  @encoder = Erlectricity::Encoder.new(@out)
7
7
  end
8
+
8
9
  specify "A symbol should be encoded to an erlang atom" do
9
10
  get{@encoder.write_symbol :haha}.should == get_erl("haha")
10
11
  write_any(:haha).should == get_erl_with_magic("haha")
11
12
  end
12
-
13
+
14
+ specify "A boolean should be encoded to an erlang atom" do
15
+ get{@encoder.write_boolean true}.should == get_erl("true")
16
+ get{@encoder.write_boolean false}.should == get_erl("false")
17
+ write_any(true).should == get_erl_with_magic("true")
18
+ write_any(false).should == get_erl_with_magic("false")
19
+ end
20
+
13
21
  specify "A number should be encoded as an erlang number would be" do
14
-
15
22
  #SMALL_INTS
16
23
  get{@encoder.write_fixnum 0}.should == get_erl("0")
17
24
  get{@encoder.write_fixnum 255}.should == get_erl("255")
18
25
  write_any(0).should == get_erl_with_magic("0")
19
26
  write_any(255).should == get_erl_with_magic("255")
20
-
27
+
21
28
  #INTS
22
29
  get{@encoder.write_fixnum 256}.should == get_erl("256")
23
30
  get{@encoder.write_fixnum((1 << 27) - 1)}.should == get_erl("#{(1 << 27) - 1}")
@@ -27,105 +34,105 @@ context "When packing to a binary stream" do
27
34
  write_any((1 << 27) - 1).should == get_erl_with_magic("#{(1 << 27) - 1}")
28
35
  write_any(-1).should == get_erl_with_magic("-1")
29
36
  write_any(-(1 << 27)).should == get_erl_with_magic("#{-(1 << 27)}")
30
-
37
+
31
38
  # #SMALL_BIGNUMS
32
39
  # get{@encoder.write_fixnum((1 << word_length))}.should == get_erl("#{(1 << word_length)}")
33
40
  # get{@encoder.write_fixnum(-(1 << word_length) - 1)}.should == get_erl("#{-(1 << word_length) - 1}")
34
41
  # get{@encoder.write_fixnum((1 << (255 * 8)) - 1)}.should == get_erl("#{(1 << (255 * 8)) - 1}")
35
42
  # get{@encoder.write_fixnum(-((1 << (255 * 8)) - 1))}.should == get_erl("#{-((1 << (255 * 8)) - 1)}")
36
- #
43
+ #
37
44
  # write_any((1 << word_length)).should == get_erl_with_magic("#{(1 << word_length)}")
38
45
  # write_any(-(1 << word_length) - 1).should == get_erl_with_magic("#{-(1 << word_length) - 1}")
39
46
  # write_any((1 << (255 * 8)) - 1).should == get_erl_with_magic("#{(1 << (255 * 8)) - 1}")
40
47
  # write_any(-((1 << (255 * 8)) - 1)).should == get_erl_with_magic("#{-((1 << (255 * 8)) - 1)}")
41
- #
48
+ #
42
49
  # #LARG_BIGNUMS
43
50
  # get{@encoder.write_fixnum((1 << (255 * 8)))}.should == get_erl("#{(1 << (255 * 8))}")
44
51
  # get{@encoder.write_fixnum(-(1 << (255 * 8))}.should == get_erl("#{-(1 << (255 * 8)}")
45
52
  # get{@encoder.write_fixnum((1 << (512 * 8))}.should == get_erl("#{(1 << (512 * 8))}")
46
53
  # get{@encoder.write_fixnum(-((1 << (512 * 8)) - 1))}.should == get_erl("#{-((1 << (512 * 8)) - 1)}")
47
- #
54
+ #
48
55
  # write_any((1 << (255 * 8))).should == get_erl_with_magic("#{(1 << (255 * 8))}")
49
56
  # write_any(-(1 << (255 * 8)).should == get_erl_with_magic("#{-(1 << (255 * 8)}")
50
57
  # write_any((1 << (512 * 8))).should == get_erl_with_magic("#{(1 << (512 * 8))}")
51
58
  # write_any(-((1 << (512 * 8)) - 1)).should == get_erl_with_magic("#{-((1 << (512 * 8)) - 1)}")
52
59
  end
53
-
60
+
54
61
  # specify "A float (that is within the truncated precision of ruby compared to erlang) should encode as erlang does" do
55
62
  # get{@encoder.write_float 1.0}.should == get_erl("1.0")
56
63
  # get{@encoder.write_float -1.0}.should == get_erl("-1.0")
57
64
  # get{@encoder.write_float 123.456}.should == get_erl("123.456")
58
65
  # get{@encoder.write_float 123.456789012345}.should == get_erl("123.456789012345")
59
66
  # end
60
-
67
+
61
68
  specify "An Erlectiricity::NewReference should encode back to its original form" do
62
69
  ref_bin = run_erl("term_to_binary(make_ref())")
63
- ruby_ref = Erlectricity::Decoder.read_any_from(ref_bin)
64
-
70
+ ruby_ref = Erlectricity::Decoder.decode(ref_bin)
71
+
65
72
  get{@encoder.write_new_reference(ruby_ref)}.should == ref_bin[1..-1]
66
73
  write_any(ruby_ref).should == ref_bin
67
74
  end
68
-
75
+
69
76
  specify "An Erlectiricity::Pid should encode back to its original form" do
70
77
  pid_bin = run_erl("term_to_binary(spawn(fun() -> 3 end))")
71
- ruby_pid = Erlectricity::Decoder.read_any_from(pid_bin)
72
-
78
+ ruby_pid = Erlectricity::Decoder.decode(pid_bin)
79
+
73
80
  get{@encoder.write_pid(ruby_pid)}.should == pid_bin[1..-1]
74
81
  write_any(ruby_pid).should == pid_bin
75
82
  end
76
-
83
+
77
84
  specify "An array written with write_tuple should encode as erlang would a tuple" do
78
85
  get{@encoder.write_tuple [1,2,3]}.should == get_erl("{1,2,3}")
79
86
  get{@encoder.write_tuple [3] * 255}.should == get_erl("{#{([3] * 255).join(',')}}")
80
87
  get{@encoder.write_tuple [3] * 256}.should == get_erl("{#{([3] * 256).join(',')}}")
81
88
  get{@encoder.write_tuple [3] * 512}.should == get_erl("{#{([3] * 512).join(',')}}")
82
89
  end
83
-
90
+
84
91
  specify "An array should by default be written as a tuple" do
85
92
  write_any([1,2,3]).should == get_erl_with_magic("{1,2,3}")
86
93
  write_any([3] * 255).should == get_erl_with_magic("{#{([3] * 255).join(',')}}")
87
94
  write_any([3] * 256).should == get_erl_with_magic("{#{([3] * 256).join(',')}}")
88
95
  write_any([3] * 512).should == get_erl_with_magic("{#{([3] * 512).join(',')}}")
89
96
  end
90
-
97
+
91
98
  specify "An Erlectricity::List should by default be written as a list" do
92
99
  write_any(Erl::List.new([1,2,300])).should == get_erl_with_magic("[1,2,300]")
93
100
  write_any(Erl::List.new([300] * 255)).should == get_erl_with_magic("[#{([300] * 255).join(',')}]")
94
101
  write_any(Erl::List.new([300] * 256)).should == get_erl_with_magic("[#{([300] * 256).join(',')}]")
95
102
  write_any(Erl::List.new([300] * 512)).should == get_erl_with_magic("[#{([300] * 512).join(',')}]")
96
103
  end
97
-
104
+
98
105
  specify "An array written with write_list should encode as erlang would a list" do
99
106
  get{@encoder.write_list [1,2,300]}.should == get_erl("[1,2,300]")
100
107
  get{@encoder.write_list [300] * 255}.should == get_erl("[#{([300] * 255).join(',')}]")
101
108
  get{@encoder.write_list [300] * 256}.should == get_erl("[#{([300] * 256).join(',')}]")
102
109
  get{@encoder.write_list [300] * 512}.should == get_erl("[#{([300] * 512).join(',')}]")
103
110
  end
104
-
111
+
105
112
  specify "a string should be encoded as a erlang binary would be" do
106
113
  get{@encoder.write_binary "hey who"}.should == get_erl("<< \"hey who\" >>")
107
114
  get{@encoder.write_binary ""}.should == get_erl("<< \"\" >>")
108
-
115
+
109
116
  write_any("hey who").should == get_erl_with_magic("<< \"hey who\" >>")
110
117
  write_any("").should == get_erl_with_magic("<< \"\" >>")
111
118
  end
112
-
119
+
113
120
  def get
114
121
  @encoder.out = StringIO.new('', 'w')
115
122
  yield
116
123
  @encoder.out.string
117
124
  end
118
-
125
+
119
126
  def write_any(term)
120
127
  @encoder.out = StringIO.new('', 'w')
121
128
  @encoder.write_any term
122
129
  @encoder.out.string
123
130
  end
124
-
131
+
125
132
  def get_erl(str)
126
133
  get_erl_with_magic(str)[1..-1] #[1..-1] to chop off the magic number
127
134
  end
128
-
135
+
129
136
  def get_erl_with_magic(str)
130
137
  run_erl("term_to_binary(#{str.gsub(/"/, '\\\"')})")
131
138
  end