Skip to content

Commit

Permalink
use new style time format, assume new style dict format, roundtrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Oct 11, 2009
1 parent 702e43c commit c53da43
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
6 changes: 4 additions & 2 deletions History.txt
@@ -1,6 +1,8 @@
=
* Bug fixes
* Properly decode empty dicts
* Minor changes
* Use {time, MegaSecs, Secs, Microsecs} for time serialization
* Tests
* Add roundtrip tests

= 0.1.0 / 2009-10-08
* Birthday!
10 changes: 3 additions & 7 deletions lib/bert/decoder.rb
Expand Up @@ -25,17 +25,13 @@ def self.convert(item)
when Array
case item.first
when :dict
if item[1]
item[1].inject({}) do |acc, x|
acc[convert(x[0])] = convert(x[1]); acc
end
else
{}
item[1].inject({}) do |acc, x|
acc[convert(x[0])] = convert(x[1]); acc
end
when :bool
item[1]
when :time
Time.at(item[1].to_i, item[2].to_i)
Time.at(item[1] * 1_000_000 + item[2], item[3])
when :regex
options = 0
options |= Regexp::EXTENDED if item[2] =~ /x/
Expand Down
2 changes: 1 addition & 1 deletion lib/bert/encoder.rb
Expand Up @@ -29,7 +29,7 @@ def self.convert(item)
when TrueClass, FalseClass
[:bool, item.to_s.to_sym]
when Time
[:time, item.to_i, item.usec]
[:time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec]
when Regexp
options = ''
options += 'i' if item.options & Regexp::IGNORECASE > 0
Expand Down
39 changes: 36 additions & 3 deletions test/bert_test.rb
Expand Up @@ -4,9 +4,9 @@ class BertTest < Test::Unit::TestCase
context "BERT" do
setup do
time = Time.at(1254976067)
@ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], time,nil, true, false, :true, :false]
@bert = "\203h\td\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\003d\000\004timen\004\000Cj\315Ja\000h\002d\000\003nild\000\003nilh\002d\000\004boold\000\004trueh\002d\000\004boold\000\005falsed\000\004trued\000\005false"
@ebin = "<<131,104,9,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,3,100,0,4,116,105,109,101,110,4,0,67,106,205,74,97,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>>"
@ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], time, nil, true, false, :true, :false]
@bert = "\203h\td\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\004d\000\004timeb\000\000\004\346b\000\016\344\303a\000h\002d\000\003nild\000\003nilh\002d\000\004boold\000\004trueh\002d\000\004boold\000\005falsed\000\004trued\000\005false"
@ebin = "<<131,104,9,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,4,100,0,4,116,105,109,101,98,0,0,4,230,98,0,14,228,195,97,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>>"
end

should "encode" do
Expand All @@ -21,6 +21,39 @@ class BertTest < Test::Unit::TestCase
assert_equal @ebin, BERT.ebin(@bert)
end

should "do roundtrips" do
dd = []
dd << 1
dd << 1.0
dd << :a
dd << t[]
dd << t[:a]
dd << t[:a, :b]
dd << t[t[:a, 1], t[:b, 2]]
dd << []
dd << [:a]
dd << [:a, 1]
dd << [[:a, 1], [:b, 2]]
dd << "a"

dd << nil
dd << true
dd << false
dd << {}
dd << {:a => 1}
dd << {:a => 1, :b => 2}
dd << Time.now
dd << /^c(a)t$/i

dd << :true
dd << :false
dd << :nil

dd.each do |d|
assert_equal d, BERT.decode(BERT.encode(d))
end
end

# should "let me inspect it" do
# puts
# p @ruby
Expand Down
4 changes: 2 additions & 2 deletions test/decoder_test.rb
Expand Up @@ -21,7 +21,7 @@ class DecoderTest < Test::Unit::TestCase
end

should "convert empty hashes" do
before = [:dict]
before = [:dict, []]
after = {}
assert_equal after, BERT::Decoder.convert(before)
end
Expand All @@ -45,7 +45,7 @@ class DecoderTest < Test::Unit::TestCase
end

should "convert times" do
before = [:time, 1254976067, 0]
before = [:time, 1254, 976067, 0]
after = Time.at(1254976067)
assert_equal after, BERT::Decoder.convert(before)
end
Expand Down
2 changes: 1 addition & 1 deletion test/encoder_test.rb
Expand Up @@ -61,7 +61,7 @@ class EncoderTest < Test::Unit::TestCase

should "convert times" do
before = Time.at(1254976067)
after = [:time, 1254976067, 0]
after = [:time, 1254, 976067, 0]
assert_equal after, BERT::Encoder.convert(before)
end

Expand Down

0 comments on commit c53da43

Please sign in to comment.