schmurfy-bert 1.2.3

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
data/lib/bert/terms.rb ADDED
@@ -0,0 +1,13 @@
1
+ module BERT
2
+ module Terms
3
+ class Pid
4
+ attr_reader :node, :node_id, :serial, :creation
5
+ def initialize(node,nid,serial,created)
6
+ @node = node
7
+ @node_id = nid
8
+ @serial = serial
9
+ @creation = created
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/bert/types.rb ADDED
@@ -0,0 +1,22 @@
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
+ PID = 103
10
+ SMALL_TUPLE = 104
11
+ LARGE_TUPLE = 105
12
+ NIL = 106
13
+ STRING = 107
14
+ LIST = 108
15
+ BIN = 109
16
+ FUN = 117
17
+ NEW_FUN = 112
18
+ MAGIC = 131
19
+ MAX_INT = (1 << 27) -1
20
+ MIN_INT = -(1 << 27)
21
+ end
22
+ end
data/test/bert_test.rb ADDED
@@ -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,107 @@
1
+ #encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class EncoderTest < Test::Unit::TestCase
5
+ context "BERT Encoder complex type converter" do
6
+ should "convert nil" do
7
+ assert_equal [:bert, :nil], BERT::Encoder.convert(nil)
8
+ end
9
+
10
+ should 'preserve original encoding type' do
11
+ string = "fred"
12
+ before = string.encoding
13
+ BERT::Encode.encode(string)
14
+ after = string.encoding
15
+ assert_equal before, after
16
+ end
17
+
18
+ should 'convert deal with a pound' do
19
+ before = '£'
20
+ after = "<<131,109,0,0,0,2,194,163>>"
21
+ assert_equal after, BERT.ebin(BERT::Encode.encode(before))
22
+ end
23
+
24
+ should "convert nested nil" do
25
+ before = [nil, [nil]]
26
+ after = [[:bert, :nil], [[:bert, :nil]]]
27
+ assert_equal after, BERT::Encoder.convert(before)
28
+ end
29
+
30
+ should "convert hashes" do
31
+ before = {:foo => 'bar'}
32
+ after = [:bert, :dict, [[:foo, 'bar']]]
33
+ assert_equal after, BERT::Encoder.convert(before)
34
+ end
35
+
36
+ should "convert nested hashes" do
37
+ before = {:foo => {:baz => 'bar'}}
38
+ after = [:bert, :dict, [[:foo, [:bert, :dict, [[:baz, "bar"]]]]]]
39
+ assert_equal after, BERT::Encoder.convert(before)
40
+ end
41
+
42
+ should "convert hash to tuple with array of tuples" do
43
+ arr = BERT::Encoder.convert({:foo => 'bar'})
44
+ assert arr.is_a?(Array)
45
+ assert arr[2].is_a?(Array)
46
+ assert arr[2][0].is_a?(Array)
47
+ end
48
+
49
+ should "convert tuple to array" do
50
+ arr = BERT::Encoder.convert(t[:foo, 2])
51
+ assert arr.is_a?(Array)
52
+ end
53
+
54
+ should "convert array to erl list" do
55
+ list = BERT::Encoder.convert([1, 2])
56
+ assert list.is_a?(Array)
57
+ end
58
+
59
+ should "convert an array in a tuple" do
60
+ arrtup = BERT::Encoder.convert(t[:foo, [1, 2]])
61
+ assert arrtup.is_a?(Array)
62
+ assert arrtup[1].is_a?(Array)
63
+ end
64
+
65
+ should "convert true" do
66
+ before = true
67
+ after = [:bert, :true]
68
+ assert_equal after, BERT::Encoder.convert(before)
69
+ end
70
+
71
+ should "convert false" do
72
+ before = false
73
+ after = [:bert, :false]
74
+ assert_equal after, BERT::Encoder.convert(before)
75
+ end
76
+
77
+ should "convert times" do
78
+ before = Time.at(1254976067)
79
+ after = [:bert, :time, 1254, 976067, 0]
80
+ assert_equal after, BERT::Encoder.convert(before)
81
+ end
82
+
83
+ should "convert regexen" do
84
+ before = /^c(a)t$/ix
85
+ after = [:bert, :regex, '^c(a)t$', [:caseless, :extended]]
86
+ assert_equal after, BERT::Encoder.convert(before)
87
+ end
88
+
89
+ should "properly convert types" do
90
+ ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], nil, true, false, :true, :false]
91
+ cruby = BERT::Encoder.convert(ruby)
92
+ assert cruby.instance_of?(BERT::Tuple)
93
+ assert cruby[0].instance_of?(Symbol)
94
+ assert cruby[1].instance_of?(BERT::Tuple)
95
+ end
96
+
97
+ should "handle bignums" do
98
+ bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
99
+ assert_equal bert, BERT::Encoder.encode(10_000_000_000_000_000_000)
100
+ end
101
+
102
+ should "leave other stuff alone" do
103
+ before = [1, 2.0, [:foo, 'bar']]
104
+ assert_equal before, BERT::Encoder.convert(before)
105
+ end
106
+ end
107
+ 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,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schmurfy-bert
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 3
10
+ version: 1.2.3
11
+ platform: ruby
12
+ authors:
13
+ - Tom Preston-Werner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-07 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: BERT Serializiation for Ruby
36
+ email: tom@mojombo.com
37
+ executables: []
38
+
39
+ extensions:
40
+ - ext/bert/c/extconf.rb
41
+ - ext/bert/c/extconf.rb
42
+ extra_rdoc_files:
43
+ - LICENSE
44
+ - README.md
45
+ files:
46
+ - .document
47
+ - .gitignore
48
+ - History.txt
49
+ - LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - VERSION
53
+ - bench/bench.rb
54
+ - bench/decode_bench.rb
55
+ - bench/encode_bench.rb
56
+ - bench/results.txt
57
+ - bert.gemspec
58
+ - ext/bert/c/extconf.rb
59
+ - lib/bert.rb
60
+ - lib/bert/bert.rb
61
+ - lib/bert/decode.rb
62
+ - lib/bert/decoder.rb
63
+ - lib/bert/encode.rb
64
+ - lib/bert/encoder.rb
65
+ - lib/bert/terms.rb
66
+ - lib/bert/types.rb
67
+ - test/bert_test.rb
68
+ - test/decoder_test.rb
69
+ - test/encoder_test.rb
70
+ - test/test_helper.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/mojombo/bert
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --charset=UTF-8
78
+ require_paths:
79
+ - lib
80
+ - ext
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.6.1
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: BERT Serializiation for Ruby
106
+ test_files:
107
+ - test/bert_test.rb
108
+ - test/decoder_test.rb
109
+ - test/encoder_test.rb
110
+ - test/test_helper.rb