Skip to content
Browse files

switch to explicitly declaring tuples instead of lists

  • Loading branch information...
1 parent 3d28846 commit a13f8746e60952dacc8158ad3d45d56937c9b8c5 @mojombo committed Oct 8, 2009
Showing with 75 additions and 12 deletions.
  1. +4 −3 README.md
  2. +10 −2 lib/bert.rb
  3. +3 −1 lib/bert/decoder.rb
  4. +4 −2 lib/bert/encoder.rb
  5. +14 −4 test/bert_test.rb
  6. +31 −0 test/encoder_test.rb
  7. +9 −0 test/test_helper.rb
View
7 README.md
@@ -22,11 +22,12 @@ Instances of the following Ruby classes will be automatically converted to the p
* Time
* Regexp
-Most of these conversions work as expected with one important caveat. Arrays will be converted into tuples. To have an array converted as a list, simply prepend it with `l` like so:
+To designate tuples, simply prefix an Array literal with a `t` or use the BERT::Tuple class:
- [:foo, l[1, 2, 3]]
+ t[:foo, [1, 2, 3]]
+ BERT::Tuple.new([:foo, [1, 2, 3]])
-This will be converted as (in Erlang syntax):
+Both of these will be converted to (in Erlang syntax):
{foo, [1, 2, 3]}
View
12 lib/bert.rb
@@ -20,6 +20,14 @@ def self.ebin(str)
end
end
-def l
- Erl::List
+module BERT
+ class Tuple < Array
+ def inspect
+ "t#{super}"
+ end
+ end
+end
+
+def t
+ BERT::Tuple
end
View
4 lib/bert/decoder.rb
@@ -20,6 +20,8 @@ def self.convert(item)
item.to_s.to_sym
when [:nil, :nil]
nil
+ when Erl::List
+ item.map { |x| convert(x) }
when Array
case item.first
when :dict
@@ -37,7 +39,7 @@ def self.convert(item)
options |= Regexp::MULTILINE if item[2] =~ /m/
Regexp.new(item[1], options)
else
- item.map { |x| convert(x) }
+ Tuple.new(item.map { |x| convert(x) })
end
else
item
View
6 lib/bert/encoder.rb
@@ -17,11 +17,13 @@ def self.encode(ruby)
def self.convert(item)
case item
when Hash
- pairs = l[]
+ pairs = Erl::List[]
item.each_pair { |k, v| pairs << [convert(k), convert(v)] }
[:dict, pairs]
- when Array
+ when Tuple
item.map { |x| convert(x) }
+ when Array
+ Erl::List.new(item.map { |x| convert(x) })
when nil
[:nil, :nil]
when TrueClass, FalseClass
View
18 test/bert_test.rb
@@ -3,10 +3,10 @@
class BertTest < Test::Unit::TestCase
context "BERT" do
setup do
- t = Time.at(1254976067)
- @ruby = [:user, {:name => 'TPW'}, l[/cat/i, 9.9], nil, true, false, :true, :false]
- @bert = "\203h\bd\000\004userh\002d\000\004dictl\000\000\000\001h\002d\000\004namem\000\000\000\003TPWjh\002h\003d\000\005regexm\000\000\000\003catm\000\000\000\001ic9.900000000000000e+00\000\000\000\000\000\000\000\000\000\000h\002d\000\003nild\000\003nilh\002d\000\004boold\000\004trueh\002d\000\004boold\000\005falsed\000\004trued\000\005false"
- @ebin = "<<131,104,8,100,0,4,117,115,101,114,104,2,100,0,4,100,105,99,116,108,0,0,0,1,104,2,100,0,4,110,97,109,101,109,0,0,0,3,84,80,87,106,104,2,104,3,100,0,5,114,101,103,101,120,109,0,0,0,3,99,97,116,109,0,0,0,1,105,99,57,46,57,48,48,48,48,48,48,48,48,48,48,48,48,48,48,101,43,48,48,0,0,0,0,0,0,0,0,0,0,104,2,100,0,3,110,105,108,100,0,3,110,105,108,104,2,100,0,4,98,111,111,108,100,0,4,116,114,117,101,104,2,100,0,4,98,111,111,108,100,0,5,102,97,108,115,101,100,0,4,116,114,117,101,100,0,5,102,97,108,115,101>>"
+ time = Time.at(1254976067)
+ @ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], nil, true, false, :true, :false]
+ @bert = "\203h\bd\000\004userh\002d\000\004dictl\000\000\000\001h\002d\000\004namem\000\000\000\003TPWjl\000\000\000\002h\003d\000\005regexm\000\000\000\003catm\000\000\000\001ic9.900000000000000e+00\000\000\000\000\000\000\000\000\000\000jh\002d\000\003nild\000\003nilh\002d\000\004boold\000\004trueh\002d\000\004boold\000\005falsed\000\004trued\000\005false"
+ @ebin = "<<131,104,8,100,0,4,117,115,101,114,104,2,100,0,4,100,105,99,116,108,0,0,0,1,104,2,100,0,4,110,97,109,101,109,0,0,0,3,84,80,87,106,108,0,0,0,2,104,3,100,0,5,114,101,103,101,120,109,0,0,0,3,99,97,116,109,0,0,0,1,105,99,57,46,57,48,48,48,48,48,48,48,48,48,48,48,48,48,48,101,43,48,48,0,0,0,0,0,0,0,0,0,0,106,104,2,100,0,3,110,105,108,100,0,3,110,105,108,104,2,100,0,4,98,111,111,108,100,0,4,116,114,117,101,104,2,100,0,4,98,111,111,108,100,0,5,102,97,108,115,101,100,0,4,116,114,117,101,100,0,5,102,97,108,115,101>>"
end
should "encode" do
@@ -20,5 +20,15 @@ class BertTest < Test::Unit::TestCase
should "ebin" do
assert_equal @ebin, BERT.ebin(@bert)
end
+
+ # should "let me inspect it" do
+ # puts
+ # p @ruby
+ # ruby2 = BERT.decode(@bert)
+ # p ruby2
+ # bert2 = BERT.encode(ruby2)
+ # ruby3 = BERT.decode(bert2)
+ # p ruby3
+ # end
end
end
View
31 test/encoder_test.rb
@@ -24,6 +24,29 @@ class EncoderTest < Test::Unit::TestCase
assert_equal after, BERT::Encoder.convert(before)
end
+ should "convert hash to tuple with array of tuples" do
+ arr = BERT::Encoder.convert({:foo => 'bar'})
+ assert arr.is_a?(Array)
+ assert arr[1].is_a?(Erl::List)
+ assert arr[1][0].is_a?(Array)
+ end
+
+ should "convert tuple to array" do
+ arr = BERT::Encoder.convert(t[:foo, 2])
+ assert arr.is_a?(Array)
+ end
+
+ should "convert array to erl list" do
+ list = BERT::Encoder.convert([1, 2])
+ assert list.is_a?(Erl::List)
+ end
+
+ should "convert an array in a tuple" do
+ arrtup = BERT::Encoder.convert(t[:foo, [1, 2]])
+ assert arrtup.is_a?(Array)
+ assert arrtup[1].is_a?(Erl::List)
+ end
+
should "convert true" do
before = true
after = [:bool, :true]
@@ -48,6 +71,14 @@ class EncoderTest < Test::Unit::TestCase
assert_equal after, BERT::Encoder.convert(before)
end
+ should "properly convert types" do
+ ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], nil, true, false, :true, :false]
+ cruby = BERT::Encoder.convert(ruby)
+ assert cruby.instance_of?(Array)
+ assert cruby[0].instance_of?(Symbol)
+ assert cruby[1].instance_of?(Array)
+ end
+
should "leave other stuff alone" do
before = [1, 2.0, [:foo, 'bar']]
assert_equal before, BERT::Encoder.convert(before)
View
9 test/test_helper.rb
@@ -8,3 +8,12 @@
class Test::Unit::TestCase
end
+
+# So I can easily see which arrays are Erl::List
+module Erl
+ class List
+ def inspect
+ "l#{super}"
+ end
+ end
+end

0 comments on commit a13f874

Please sign in to comment.
Something went wrong with that request. Please try again.