schleyfox-bert 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ module BERT
2
+ class Encoder
3
+ # Encode a Ruby object into a BERT.
4
+ # +ruby+ is the Ruby object
5
+ #
6
+ # Returns a BERT
7
+ def self.encode(ruby)
8
+ complex_ruby = convert(ruby)
9
+ Encode.encode(complex_ruby)
10
+ end
11
+
12
+ # Convert complex Ruby form in simple Ruby form.
13
+ # +item+ is the Ruby object to convert
14
+ #
15
+ # Returns the converted Ruby object
16
+ def self.convert(item)
17
+ case item
18
+ when Hash
19
+ pairs = []
20
+ item.each_pair { |k, v| pairs << t[convert(k), convert(v)] }
21
+ t[:bert, :dict, pairs]
22
+ when Tuple
23
+ Tuple.new(item.map { |x| convert(x) })
24
+ when Array
25
+ item.map { |x| convert(x) }
26
+ when nil
27
+ t[:bert, :nil]
28
+ when TrueClass
29
+ t[:bert, :true]
30
+ when FalseClass
31
+ t[:bert, :false]
32
+ when Time
33
+ t[:bert, :time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec]
34
+ when Regexp
35
+ options = []
36
+ options << :caseless if item.options & Regexp::IGNORECASE > 0
37
+ options << :extended if item.options & Regexp::EXTENDED > 0
38
+ options << :multiline if item.options & Regexp::MULTILINE > 0
39
+ t[:bert, :regex, item.source, options]
40
+ else
41
+ item
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ module BERT
2
+ module Types
3
+ SMALL_INT = 97
4
+ INT = 98
5
+ SMALL_BIGNUM = 110
6
+ LARGE_BIGNUM = 111
7
+ FLOAT = 99
8
+ ATOM = 100
9
+ SMALL_TUPLE = 104
10
+ LARGE_TUPLE = 105
11
+ NIL = 106
12
+ STRING = 107
13
+ LIST = 108
14
+ BIN = 109
15
+ FUN = 117
16
+ NEW_FUN = 112
17
+ MAGIC = 131
18
+ MAX_INT = (1 << 27) -1
19
+ MIN_INT = -(1 << 27)
20
+ end
21
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class BertTest < Test::Unit::TestCase
4
+ context "BERT" do
5
+ setup do
6
+ time = Time.at(1254976067)
7
+ @ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], time, nil, true, false, :true, :false]
8
+ @bert = "\203h\td\000\004userh\003d\000\004bertd\000\004dictl\000\000\000\001h\002d\000\004namem\000\000\000\003TPWjl\000\000\000\002h\004d\000\004bertd\000\005regexm\000\000\000\003catl\000\000\000\001d\000\bcaselessjc9.900000000000000e+00\000\000\000\000\000\000\000\000\000\000jh\005d\000\004bertd\000\004timeb\000\000\004\346b\000\016\344\303a\000h\002d\000\004bertd\000\003nilh\002d\000\004bertd\000\004trueh\002d\000\004bertd\000\005falsed\000\004trued\000\005false"
9
+ @ebin = "<<131,104,9,100,0,4,117,115,101,114,104,3,100,0,4,98,101,114,116,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,4,100,0,4,98,101,114,116,100,0,5,114,101,103,101,120,109,0,0,0,3,99,97,116,108,0,0,0,1,100,0,8,99,97,115,101,108,101,115,115,106,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,5,100,0,4,98,101,114,116,100,0,4,116,105,109,101,98,0,0,4,230,98,0,14,228,195,97,0,104,2,100,0,4,98,101,114,116,100,0,3,110,105,108,104,2,100,0,4,98,101,114,116,100,0,4,116,114,117,101,104,2,100,0,4,98,101,114,116,100,0,5,102,97,108,115,101,100,0,4,116,114,117,101,100,0,5,102,97,108,115,101>>"
10
+ end
11
+
12
+ should "encode" do
13
+ assert_equal @bert, BERT.encode(@ruby)
14
+ end
15
+
16
+ should "decode" do
17
+ assert_equal @ruby, BERT.decode(@bert)
18
+ end
19
+
20
+ should "ebin" do
21
+ assert_equal @ebin, BERT.ebin(@bert)
22
+ end
23
+
24
+ should "do roundtrips" do
25
+ dd = []
26
+ dd << 1
27
+ dd << 1.0
28
+ dd << :a
29
+ dd << t[]
30
+ dd << t[:a]
31
+ dd << t[:a, :b]
32
+ dd << t[t[:a, 1], t[:b, 2]]
33
+ dd << []
34
+ dd << [:a]
35
+ dd << [:a, 1]
36
+ dd << [[:a, 1], [:b, 2]]
37
+ dd << "a"
38
+
39
+ dd << nil
40
+ dd << true
41
+ dd << false
42
+ dd << {}
43
+ dd << {:a => 1}
44
+ dd << {:a => 1, :b => 2}
45
+ dd << Time.now
46
+ dd << /^c(a)t$/i
47
+
48
+ dd << 256**256 - 1
49
+
50
+ dd << :true
51
+ dd << :false
52
+ dd << :nil
53
+
54
+ dd.each do |d|
55
+ assert_equal d, BERT.decode(BERT.encode(d))
56
+ end
57
+ end
58
+
59
+ # should "let me inspect it" do
60
+ # puts
61
+ # p @ruby
62
+ # ruby2 = BERT.decode(@bert)
63
+ # p ruby2
64
+ # bert2 = BERT.encode(ruby2)
65
+ # ruby3 = BERT.decode(bert2)
66
+ # p ruby3
67
+ # end
68
+ end
69
+ end
@@ -0,0 +1,91 @@
1
+ require 'test_helper'
2
+
3
+ class DecoderTest < Test::Unit::TestCase
4
+ BERT_NIL = [131,104,2,100,0,4,98,101,114,116,100,0,3,110,105,108].pack('c*')
5
+ BERT_TRUE = [131,104,2,100,0,4,98,101,114,116,100,0,4,116,114,117,101].pack('c*')
6
+ BERT_FALSE = [131,104,2,100,0,4,98,101,114,116,100,0,5,102,97,108,115,101].pack('c*')
7
+
8
+ context "BERT Decoder complex type converter" do
9
+ should "convert nil" do
10
+ assert_equal nil, BERT::Decoder.decode(BERT_NIL)
11
+ end
12
+
13
+ should "convert nested nil" do
14
+ bert = [131,108,0,0,0,2,104,2,100,0,4,98,101,114,116,100,0,3,110,105,
15
+ 108,108,0,0,0,1,104,2,100,0,4,98,101,114,116,100,0,3,110,105,
16
+ 108,106,106].pack('c*')
17
+ assert_equal [nil, [nil]], BERT::Decoder.decode(bert)
18
+ end
19
+
20
+ should "convert hashes" do
21
+ bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,108,
22
+ 0,0,0,1,104,2,100,0,3,102,111,111,109,0,0,0,3,98,97,114,
23
+ 106].pack('c*')
24
+ after = {:foo => 'bar'}
25
+ assert_equal after, BERT::Decoder.decode(bert)
26
+ end
27
+
28
+ should "convert empty hashes" do
29
+ bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,
30
+ 106].pack('c*')
31
+ after = {}
32
+ assert_equal after, BERT::Decoder.decode(bert)
33
+ end
34
+
35
+ should "convert nested hashes" do
36
+ bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,108,0,
37
+ 0,0,1,104,2,100,0,3,102,111,111,104,3,100,0,4,98,101,114,116,
38
+ 100,0,4,100,105,99,116,108,0,0,0,1,104,2,100,0,3,98,97,122,109,
39
+ 0,0,0,3,98,97,114,106,106].pack('c*')
40
+ after = {:foo => {:baz => 'bar'}}
41
+ assert_equal after, BERT::Decoder.decode(bert)
42
+ end
43
+
44
+ should "convert true" do
45
+ assert_equal true, BERT::Decoder.decode(BERT_TRUE)
46
+ end
47
+
48
+ should "convert false" do
49
+ assert_equal false, BERT::Decoder.decode(BERT_FALSE)
50
+ end
51
+
52
+ should "convert times" do
53
+ bert = [131,104,5,100,0,4,98,101,114,116,100,0,4,116,105,109,101,98,0,
54
+ 0,4,230,98,0,14,228,195,97,0].pack('c*')
55
+ after = Time.at(1254976067)
56
+ assert_equal after, BERT::Decoder.decode(bert)
57
+ end
58
+
59
+ should "convert regexen" do
60
+ bert = [131,104,4,100,0,4,98,101,114,116,100,0,5,114,101,103,101,120,
61
+ 109,0,0,0,7,94,99,40,97,41,116,36,108,0,0,0,2,100,0,8,99,97,
62
+ 115,101,108,101,115,115,100,0,8,101,120,116,101,110,100,101,
63
+ 100,106].pack('c*')
64
+ after = /^c(a)t$/ix
65
+ assert_equal after, BERT::Decoder.decode(bert)
66
+ end
67
+
68
+ should "leave other stuff alone" do
69
+ bert = [131,108,0,0,0,3,97,1,99,50,46,48,48,48,48,48,48,48,48,48,48,48,
70
+ 48,48,48,48,48,48,48,48,48,101,43,48,48,0,0,0,0,0,108,0,0,0,2,
71
+ 100,0,3,102,111,111,109,0,0,0,3,98,97,114,106,106].pack('c*')
72
+ after = [1, 2.0, [:foo, 'bar']]
73
+ assert_equal after, BERT::Decoder.decode(bert)
74
+ end
75
+
76
+ should "handle bignums" do
77
+ bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
78
+ assert_equal 10_000_000_000_000_000_000, BERT::Decoder.decode(bert)
79
+ end
80
+
81
+ should "handle bytelists" do
82
+ bert = [131,104,3,100,0,3,102,111,111,107,0,2,97,97,100,0,3,98,97,114].pack('c*')
83
+ assert_equal t[:foo, [97, 97], :bar], BERT::Decoder.decode(bert)
84
+ end
85
+
86
+ should "handle massive binaries" do
87
+ bert = [131,109,0,128,0,0].pack('c*') + ('a' * (8 * 1024 * 1024))
88
+ assert_equal (8 * 1024 * 1024), BERT::Decoder.decode(bert).size
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+
3
+ class EncoderTest < Test::Unit::TestCase
4
+ context "BERT Encoder complex type converter" do
5
+ should "convert nil" do
6
+ assert_equal [:bert, :nil], BERT::Encoder.convert(nil)
7
+ end
8
+
9
+ should "convert nested nil" do
10
+ before = [nil, [nil]]
11
+ after = [[:bert, :nil], [[:bert, :nil]]]
12
+ assert_equal after, BERT::Encoder.convert(before)
13
+ end
14
+
15
+ should "convert hashes" do
16
+ before = {:foo => 'bar'}
17
+ after = [:bert, :dict, [[:foo, 'bar']]]
18
+ assert_equal after, BERT::Encoder.convert(before)
19
+ end
20
+
21
+ should "convert nested hashes" do
22
+ before = {:foo => {:baz => 'bar'}}
23
+ after = [:bert, :dict, [[:foo, [:bert, :dict, [[:baz, "bar"]]]]]]
24
+ assert_equal after, BERT::Encoder.convert(before)
25
+ end
26
+
27
+ should "convert hash to tuple with array of tuples" do
28
+ arr = BERT::Encoder.convert({:foo => 'bar'})
29
+ assert arr.is_a?(Array)
30
+ assert arr[2].is_a?(Array)
31
+ assert arr[2][0].is_a?(Array)
32
+ end
33
+
34
+ should "convert tuple to array" do
35
+ arr = BERT::Encoder.convert(t[:foo, 2])
36
+ assert arr.is_a?(Array)
37
+ end
38
+
39
+ should "convert array to erl list" do
40
+ list = BERT::Encoder.convert([1, 2])
41
+ assert list.is_a?(Array)
42
+ end
43
+
44
+ should "convert an array in a tuple" do
45
+ arrtup = BERT::Encoder.convert(t[:foo, [1, 2]])
46
+ assert arrtup.is_a?(Array)
47
+ assert arrtup[1].is_a?(Array)
48
+ end
49
+
50
+ should "convert true" do
51
+ before = true
52
+ after = [:bert, :true]
53
+ assert_equal after, BERT::Encoder.convert(before)
54
+ end
55
+
56
+ should "convert false" do
57
+ before = false
58
+ after = [:bert, :false]
59
+ assert_equal after, BERT::Encoder.convert(before)
60
+ end
61
+
62
+ should "convert times" do
63
+ before = Time.at(1254976067)
64
+ after = [:bert, :time, 1254, 976067, 0]
65
+ assert_equal after, BERT::Encoder.convert(before)
66
+ end
67
+
68
+ should "convert regexen" do
69
+ before = /^c(a)t$/ix
70
+ after = [:bert, :regex, '^c(a)t$', [:caseless, :extended]]
71
+ assert_equal after, BERT::Encoder.convert(before)
72
+ end
73
+
74
+ should "properly convert types" do
75
+ ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], nil, true, false, :true, :false]
76
+ cruby = BERT::Encoder.convert(ruby)
77
+ assert cruby.instance_of?(BERT::Tuple)
78
+ assert cruby[0].instance_of?(Symbol)
79
+ assert cruby[1].instance_of?(BERT::Tuple)
80
+ end
81
+
82
+ should "handle bignums" do
83
+ bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
84
+ assert_equal bert, BERT::Encoder.encode(10_000_000_000_000_000_000)
85
+ end
86
+
87
+ should "leave other stuff alone" do
88
+ before = [1, 2.0, [:foo, 'bar']]
89
+ assert_equal before, BERT::Encoder.convert(before)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'ext', 'bert', 'c'))
7
+
8
+ load 'bert.rb'
9
+
10
+ puts "Using #{BERT::Decode.impl} implementation."
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schleyfox-bert
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 2
9
+ version: 1.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Tom Preston-Werner
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-02 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: BERT Serializiation for Ruby (1.9 Update)
33
+ email: tom@mojombo.com
34
+ executables: []
35
+
36
+ extensions:
37
+ - ext/bert/c/extconf.rb
38
+ - ext/bert/c/extconf.rb
39
+ extra_rdoc_files:
40
+ - LICENSE
41
+ - README.md
42
+ files:
43
+ - .document
44
+ - .gitignore
45
+ - History.txt
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - VERSION
50
+ - bench/bench.rb
51
+ - bench/decode_bench.rb
52
+ - bench/encode_bench.rb
53
+ - bench/results.txt
54
+ - bert.gemspec
55
+ - ext/bert/c/decode.c
56
+ - ext/bert/c/extconf.rb
57
+ - lib/bert.rb
58
+ - lib/bert/bert.rb
59
+ - lib/bert/decode.rb
60
+ - lib/bert/decoder.rb
61
+ - lib/bert/encode.rb
62
+ - lib/bert/encoder.rb
63
+ - lib/bert/types.rb
64
+ - test/bert_test.rb
65
+ - test/decoder_test.rb
66
+ - test/encoder_test.rb
67
+ - test/test_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/mojombo/bert
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ - ext
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.6
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: BERT Serializiation for Ruby
99
+ test_files:
100
+ - test/bert_test.rb
101
+ - test/decoder_test.rb
102
+ - test/encoder_test.rb
103
+ - test/test_helper.rb