bert 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/History.txt +9 -0
- data/LICENSE +20 -0
- data/README.md +74 -0
- data/Rakefile +61 -0
- data/VERSION +1 -0
- data/bert.gemspec +63 -0
- data/lib/bert.rb +33 -0
- data/lib/bert/decoder.rb +63 -0
- data/lib/bert/encoder.rb +46 -0
- data/test/bert_test.rb +67 -0
- data/test/decoder_test.rb +64 -0
- data/test/encoder_test.rb +87 -0
- data/test/test_helper.rb +19 -0
- metadata +92 -0
data/.document
ADDED
data/History.txt
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Tom Preston-Werner
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
BERT
|
2
|
+
====
|
3
|
+
|
4
|
+
BERT is a BERT (Binary ERlang Term) serialization library for Ruby. It can
|
5
|
+
encode Ruby objects into BERT format and decode BERT binaries into Ruby
|
6
|
+
objects.
|
7
|
+
|
8
|
+
See the BERT specification at [bert-rpc.org](http://bert-rpc.org).
|
9
|
+
|
10
|
+
Instances of the following Ruby classes will be automatically converted to the proper simple BERT type:
|
11
|
+
|
12
|
+
* Fixnum
|
13
|
+
* Float
|
14
|
+
* Symbol
|
15
|
+
* Array
|
16
|
+
* String
|
17
|
+
|
18
|
+
Instances of the following Ruby classes will be automatically converted to the proper complex BERT type:
|
19
|
+
|
20
|
+
* NilClass
|
21
|
+
* TrueClass
|
22
|
+
* FalseClass
|
23
|
+
* Hash
|
24
|
+
* Time
|
25
|
+
* Regexp
|
26
|
+
|
27
|
+
To designate tuples, simply prefix an Array literal with a `t` or use the BERT::Tuple class:
|
28
|
+
|
29
|
+
t[:foo, [1, 2, 3]]
|
30
|
+
BERT::Tuple.new([:foo, [1, 2, 3]])
|
31
|
+
|
32
|
+
Both of these will be converted to (in Erlang syntax):
|
33
|
+
|
34
|
+
{foo, [1, 2, 3]}
|
35
|
+
|
36
|
+
|
37
|
+
Installation
|
38
|
+
------------
|
39
|
+
|
40
|
+
gem install bert -s http://gemcutter.org
|
41
|
+
|
42
|
+
|
43
|
+
Usage
|
44
|
+
-----
|
45
|
+
|
46
|
+
require 'bert'
|
47
|
+
|
48
|
+
bert = BERT.encode(t[:user, {:name => 'TPW', :nick => 'mojombo'}])
|
49
|
+
# => "\203h\002d\000\004userh\002d\000\004dictl\000\000\000\002h\002d
|
50
|
+
\000\004namem\000\000\000\003TPWh\002d\000\004nickm\000\000\000
|
51
|
+
\amojomboj"
|
52
|
+
|
53
|
+
BERT.decode(bert)
|
54
|
+
# => t[:user, {:name=>"TPW", :nick=>"mojombo"}]
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
Note on Patches/Pull Requests
|
59
|
+
-----------------------------
|
60
|
+
|
61
|
+
* Fork the project.
|
62
|
+
* Make your feature addition or bug fix.
|
63
|
+
* Add tests for it. This is important so I don't break it in a
|
64
|
+
future version unintentionally.
|
65
|
+
* Commit, do not mess with rakefile, version, or history.
|
66
|
+
(if you want to have your own version, that is fine but
|
67
|
+
bump version in a commit by itself I can ignore when I pull)
|
68
|
+
* Send me a pull request. Bonus points for topic branches.
|
69
|
+
|
70
|
+
|
71
|
+
Copyright
|
72
|
+
---------
|
73
|
+
|
74
|
+
Copyright (c) 2009 Tom Preston-Werner. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "bert"
|
8
|
+
gem.summary = %Q{BERT Serializiation for Ruby}
|
9
|
+
gem.description = %Q{BERT Serializiation for Ruby}
|
10
|
+
gem.email = "tom@mojombo.com"
|
11
|
+
gem.homepage = "http://github.com/mojombo/bert"
|
12
|
+
gem.authors = ["Tom Preston-Werner"]
|
13
|
+
gem.add_dependency('erlectricity', '>= 1.1.0')
|
14
|
+
gem.add_development_dependency("thoughtbot-shoulda")
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
if File.exist?('VERSION')
|
48
|
+
version = File.read('VERSION')
|
49
|
+
else
|
50
|
+
version = ""
|
51
|
+
end
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "bert #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
58
|
+
|
59
|
+
task :console do
|
60
|
+
exec('irb -I lib -rbert')
|
61
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/bert.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{bert}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Tom Preston-Werner"]
|
12
|
+
s.date = %q{2009-10-15}
|
13
|
+
s.description = %q{BERT Serializiation for Ruby}
|
14
|
+
s.email = %q{tom@mojombo.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"History.txt",
|
23
|
+
"LICENSE",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"bert.gemspec",
|
28
|
+
"lib/bert.rb",
|
29
|
+
"lib/bert/decoder.rb",
|
30
|
+
"lib/bert/encoder.rb",
|
31
|
+
"test/bert_test.rb",
|
32
|
+
"test/decoder_test.rb",
|
33
|
+
"test/encoder_test.rb",
|
34
|
+
"test/test_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/mojombo/bert}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{BERT Serializiation for Ruby}
|
41
|
+
s.test_files = [
|
42
|
+
"test/bert_test.rb",
|
43
|
+
"test/decoder_test.rb",
|
44
|
+
"test/encoder_test.rb",
|
45
|
+
"test/test_helper.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_runtime_dependency(%q<erlectricity>, [">= 1.1.0"])
|
54
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<erlectricity>, [">= 1.1.0"])
|
57
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<erlectricity>, [">= 1.1.0"])
|
61
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
62
|
+
end
|
63
|
+
end
|
data/lib/bert.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'erlectricity'
|
3
|
+
|
4
|
+
require 'bert/encoder'
|
5
|
+
require 'bert/decoder'
|
6
|
+
|
7
|
+
module BERT
|
8
|
+
def self.encode(ruby)
|
9
|
+
Encoder.encode(ruby)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.decode(bert)
|
13
|
+
Decoder.decode(bert)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.ebin(str)
|
17
|
+
bytes = []
|
18
|
+
str.each_byte { |b| bytes << b.to_s }
|
19
|
+
"<<" + bytes.join(',') + ">>"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module BERT
|
24
|
+
class Tuple < Array
|
25
|
+
def inspect
|
26
|
+
"t#{super}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def t
|
32
|
+
BERT::Tuple
|
33
|
+
end
|
data/lib/bert/decoder.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module BERT
|
2
|
+
class Decoder
|
3
|
+
# Decode a BERT into a Ruby object.
|
4
|
+
# +bert+ is the BERT String
|
5
|
+
#
|
6
|
+
# Returns a Ruby object
|
7
|
+
def self.decode(bert)
|
8
|
+
simple_ruby = Erlectricity::Decoder.decode(bert)
|
9
|
+
convert(simple_ruby)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Convert Erlectricity representation of BERT complex types into
|
13
|
+
# corresponding Ruby types.
|
14
|
+
# +item+ is the Ruby object to convert
|
15
|
+
#
|
16
|
+
# Returns the converted Ruby object
|
17
|
+
def self.convert(item)
|
18
|
+
case item
|
19
|
+
when TrueClass, FalseClass
|
20
|
+
item.to_s.to_sym
|
21
|
+
when Erl::List
|
22
|
+
item.map { |x| convert(x) }
|
23
|
+
when Array
|
24
|
+
if item[0] == :bert
|
25
|
+
convert_bert(item)
|
26
|
+
else
|
27
|
+
Tuple.new(item.map { |x| convert(x) })
|
28
|
+
end
|
29
|
+
else
|
30
|
+
item
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Convert complex types.
|
35
|
+
# +item+ is the complex type array
|
36
|
+
#
|
37
|
+
# Returns the converted Ruby object
|
38
|
+
def self.convert_bert(item)
|
39
|
+
case item[1]
|
40
|
+
when :nil
|
41
|
+
nil
|
42
|
+
when :dict
|
43
|
+
item[2].inject({}) do |acc, x|
|
44
|
+
acc[convert(x[0])] = convert(x[1]); acc
|
45
|
+
end
|
46
|
+
when TrueClass
|
47
|
+
true
|
48
|
+
when FalseClass
|
49
|
+
false
|
50
|
+
when :time
|
51
|
+
Time.at(item[2] * 1_000_000 + item[3], item[4])
|
52
|
+
when :regex
|
53
|
+
options = 0
|
54
|
+
options |= Regexp::EXTENDED if item[3].include?(:extended)
|
55
|
+
options |= Regexp::IGNORECASE if item[3].include?(:caseless)
|
56
|
+
options |= Regexp::MULTILINE if item[3].include?(:multiline)
|
57
|
+
Regexp.new(item[2], options)
|
58
|
+
else
|
59
|
+
nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/bert/encoder.rb
ADDED
@@ -0,0 +1,46 @@
|
|
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
|
+
Erlectricity::Encoder.encode(complex_ruby)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Convert Ruby types into corresponding Erlectricity representation
|
13
|
+
# of BERT complex types.
|
14
|
+
# +item+ is the Ruby object to convert
|
15
|
+
#
|
16
|
+
# Returns the converted Ruby object
|
17
|
+
def self.convert(item)
|
18
|
+
case item
|
19
|
+
when Hash
|
20
|
+
pairs = Erl::List[]
|
21
|
+
item.each_pair { |k, v| pairs << [convert(k), convert(v)] }
|
22
|
+
[:bert, :dict, pairs]
|
23
|
+
when Tuple
|
24
|
+
item.map { |x| convert(x) }
|
25
|
+
when Array
|
26
|
+
Erl::List.new(item.map { |x| convert(x) })
|
27
|
+
when nil
|
28
|
+
[:bert, :nil]
|
29
|
+
when TrueClass
|
30
|
+
[:bert, :true]
|
31
|
+
when FalseClass
|
32
|
+
[:bert, :false]
|
33
|
+
when Time
|
34
|
+
[:bert, :time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec]
|
35
|
+
when Regexp
|
36
|
+
options = Erl::List[]
|
37
|
+
options << :caseless if item.options & Regexp::IGNORECASE > 0
|
38
|
+
options << :extended if item.options & Regexp::EXTENDED > 0
|
39
|
+
options << :multiline if item.options & Regexp::MULTILINE > 0
|
40
|
+
[:bert, :regex, item.source, options]
|
41
|
+
else
|
42
|
+
item
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/test/bert_test.rb
ADDED
@@ -0,0 +1,67 @@
|
|
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 << :true
|
49
|
+
dd << :false
|
50
|
+
dd << :nil
|
51
|
+
|
52
|
+
dd.each do |d|
|
53
|
+
assert_equal d, BERT.decode(BERT.encode(d))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# should "let me inspect it" do
|
58
|
+
# puts
|
59
|
+
# p @ruby
|
60
|
+
# ruby2 = BERT.decode(@bert)
|
61
|
+
# p ruby2
|
62
|
+
# bert2 = BERT.encode(ruby2)
|
63
|
+
# ruby3 = BERT.decode(bert2)
|
64
|
+
# p ruby3
|
65
|
+
# end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DecoderTest < Test::Unit::TestCase
|
4
|
+
context "BERT Decoder complex type converter" do
|
5
|
+
should "convert nil" do
|
6
|
+
before = [:bert, :nil]
|
7
|
+
after = nil
|
8
|
+
assert_equal after, BERT::Decoder.convert(before)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "convert nested nil" do
|
12
|
+
before = [[:bert, :nil], [[:bert, :nil]]]
|
13
|
+
after = [nil, [nil]]
|
14
|
+
assert_equal after, BERT::Decoder.convert(before)
|
15
|
+
end
|
16
|
+
|
17
|
+
should "convert hashes" do
|
18
|
+
before = [:bert, :dict, [[:foo, 'bar']]]
|
19
|
+
after = {:foo => 'bar'}
|
20
|
+
assert_equal after, BERT::Decoder.convert(before)
|
21
|
+
end
|
22
|
+
|
23
|
+
should "convert empty hashes" do
|
24
|
+
before = [:bert, :dict, []]
|
25
|
+
after = {}
|
26
|
+
assert_equal after, BERT::Decoder.convert(before)
|
27
|
+
end
|
28
|
+
|
29
|
+
should "convert nested hashes" do
|
30
|
+
before = [:bert, :dict, [[:foo, [:bert, :dict, [[:baz, 'bar']]]]]]
|
31
|
+
after = {:foo => {:baz => 'bar'}}
|
32
|
+
assert_equal after, BERT::Decoder.convert(before)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "convert true" do
|
36
|
+
before = [:bert, true]
|
37
|
+
after = true
|
38
|
+
assert_equal after, BERT::Decoder.convert(before)
|
39
|
+
end
|
40
|
+
|
41
|
+
should "convert false" do
|
42
|
+
before = [:bert, false]
|
43
|
+
after = false
|
44
|
+
assert_equal after, BERT::Decoder.convert(before)
|
45
|
+
end
|
46
|
+
|
47
|
+
should "convert times" do
|
48
|
+
before = [:bert, :time, 1254, 976067, 0]
|
49
|
+
after = Time.at(1254976067)
|
50
|
+
assert_equal after, BERT::Decoder.convert(before)
|
51
|
+
end
|
52
|
+
|
53
|
+
should "convert regexen" do
|
54
|
+
before = [:bert, :regex, '^c(a)t$', [:caseless, :extended]]
|
55
|
+
after = /^c(a)t$/ix
|
56
|
+
assert_equal after, BERT::Decoder.convert(before)
|
57
|
+
end
|
58
|
+
|
59
|
+
should "leave other stuff alone" do
|
60
|
+
before = [1, 2.0, [:foo, 'bar']]
|
61
|
+
assert_equal before, BERT::Decoder.convert(before)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,87 @@
|
|
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?(Erl::List)
|
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?(Erl::List)
|
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?(Erl::List)
|
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?(Array)
|
78
|
+
assert cruby[0].instance_of?(Symbol)
|
79
|
+
assert cruby[1].instance_of?(Array)
|
80
|
+
end
|
81
|
+
|
82
|
+
should "leave other stuff alone" do
|
83
|
+
before = [1, 2.0, [:foo, 'bar']]
|
84
|
+
assert_equal before, BERT::Encoder.convert(before)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
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.dirname(__FILE__))
|
7
|
+
require 'bert'
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
end
|
11
|
+
|
12
|
+
# So I can easily see which arrays are Erl::List
|
13
|
+
module Erl
|
14
|
+
class List
|
15
|
+
def inspect
|
16
|
+
"l#{super}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Preston-Werner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: erlectricity
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.0
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: thoughtbot-shoulda
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: BERT Serializiation for Ruby
|
36
|
+
email: tom@mojombo.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.md
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- History.txt
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- VERSION
|
52
|
+
- bert.gemspec
|
53
|
+
- lib/bert.rb
|
54
|
+
- lib/bert/decoder.rb
|
55
|
+
- lib/bert/encoder.rb
|
56
|
+
- test/bert_test.rb
|
57
|
+
- test/decoder_test.rb
|
58
|
+
- test/encoder_test.rb
|
59
|
+
- test/test_helper.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/mojombo/bert
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --charset=UTF-8
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.5
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: BERT Serializiation for Ruby
|
88
|
+
test_files:
|
89
|
+
- test/bert_test.rb
|
90
|
+
- test/decoder_test.rb
|
91
|
+
- test/encoder_test.rb
|
92
|
+
- test/test_helper.rb
|