rubyfox-sfsobject 0.2.0-java

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm jruby@rubyfox-sfsobject --create
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubyfox-sfsobject.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,80 @@
1
+ = Rubyfox::SFSObject {<img src="https://secure.travis-ci.org/neopoly/rubyfox-sfsobject.png?branch=master" alt="Build Status" />}[https://travis-ci.org/neopoly/rubyfox-sfsobject]
2
+
3
+ Converts between SmartFox's SFSObjects and Ruby Hashes.
4
+
5
+ Gem[https://rubygems.org/gems/rubyfox-sfsobject] |
6
+ Source[https://github.com/neopoly/rubyfox-sfsobject] |
7
+ Documentation[http://rubydoc.info/github/neopoly/rubyfox-sfsobject/master/file/README.rdoc]
8
+
9
+ == Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'rubyfox-sfsobject'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rubyfox-sfsobject
22
+
23
+ == Usage
24
+
25
+ === Bulk mode
26
+
27
+ require 'rubyfox/sfsobject/bulk'
28
+ sfs_object = Rubyfox::SFSObject::Bulk.to_sfs({ :hello => "world" })
29
+ # => SFSObject ready to use in SmartFox
30
+ hash = Rubyfox::SFSObject::Bulk.to_hash(sfs_object)
31
+ # => { :hello => "world" }
32
+
33
+
34
+ === Schema mode
35
+
36
+ require 'rubyfox/sfsobject/schema'
37
+ schema = { :hello => String }
38
+ sfs_object = Rubyfox::SFSObject::Schema.to_sfs(schema, { :hello => "world" })
39
+ # => SFSObject ready to use in SmartFox
40
+ hash = Rubyfox::SFSObject::Schema.to_hash(schema, sfs_object)
41
+ # => { :hello => "world" }
42
+
43
+ === Core extension
44
+
45
+ You can extend Hash and SFSObject with method shortcuts:
46
+
47
+ require 'rubyfox/sfsobject/core_ext'
48
+ sfs_object = { :hello => "world" }.to_sfs
49
+ # => SFSObject
50
+ sfs_object.to_hash
51
+ # { :hello => "world" }
52
+
53
+ ==== JSON
54
+
55
+ require 'rubyfox/sfsobject/core_ext'
56
+ json = sfs_object.to_json
57
+ Rubyfox::SFSObject.from_json(json)
58
+
59
+ ==== Hash-like access
60
+
61
+ require 'rubyfox/sfsobject/core_ext'
62
+ sfs_object[:string] = "value"
63
+ sfs_object[:string] # => "value"
64
+
65
+
66
+ == Caveats
67
+
68
+ *Note* that all hash keys will be converted to symbols.
69
+
70
+ == TODO
71
+
72
+ * More docs, please!
73
+
74
+ == Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ # RDoc
13
+ require 'rdoc/task'
14
+ RDoc::Task.new do |rdoc|
15
+ rdoc.title = "Rubyfox SFSObject"
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.main = 'README.rdoc'
18
+ rdoc.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
19
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubyfox/sfsobject/bulk'
2
+
3
+ module Rubyfox
4
+ module SFSObject
5
+ module Accessor
6
+ def [](key)
7
+ Bulk.unwrap_value!(self, key)
8
+ end
9
+
10
+ def []=(key, value)
11
+ Bulk.wrap_value!(self, key, value)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,109 @@
1
+ require 'rubyfox/sfsobject/java'
2
+
3
+ module Rubyfox
4
+ module SFSObject
5
+ module Bulk
6
+ extend self
7
+
8
+ TO_SFS = {
9
+ NilClass => proc { |o, k, _| o.putNull(k) },
10
+ String => :putUtfString,
11
+ TrueClass => :putBool,
12
+ FalseClass => :putBool,
13
+ Fixnum => :putInt,
14
+ Float => :putDouble,
15
+ Hash => proc { |o, k, v| o.putSFSObject(k, to_sfs(v)) },
16
+ [String] => :putUtfStringArray,
17
+ [TrueClass] => :putBoolArray,
18
+ [FalseClass] => :putBoolArray,
19
+ [Fixnum] => proc do |o, k, v|
20
+ collection = Java::ArrayList.new
21
+ v.each { |e| collection.add(e.to_java(:int)) }
22
+ o.putIntArray(k, collection)
23
+ end,
24
+ [Float] => :putDoubleArray,
25
+ [Hash] => proc do |o, k, v|
26
+ ary = Java::SFSArray.new
27
+ v.each { |e| ary.addSFSObject(to_sfs(e)) }
28
+ o.putSFSArray(k, ary)
29
+ end
30
+ }
31
+
32
+ TO_HASH = {
33
+ "NULL" => proc { |k, v| nil },
34
+ "UTF_STRING" => :getUtfString,
35
+ "BOOL" => :getBool,
36
+ "INT" => :getInt,
37
+ "DOUBLE" => :getDouble,
38
+ "UTF_STRING_ARRAY" => :getUtfStringArray,
39
+ "BOOL_ARRAY" => :getBoolArray,
40
+ #"INT_ARRAY" => :getIntArray,
41
+ "INT_ARRAY" => proc { |k, v| v.object.to_a },
42
+ "LONG_ARRAY" => :getLongArray,
43
+ "DOUBLE_ARRAY" => :getDoubleArray,
44
+ "SFS_OBJECT" => proc { |k, v| to_hash(v.object) },
45
+ "SFS_ARRAY" => proc do |k, v|
46
+ v.object.iterator.map { |e| to_hash(e.object) }
47
+ end
48
+ }
49
+
50
+ # hash -> object
51
+ def to_sfs(hash={}, schema=nil)
52
+ object = Java::SFSObject.new_instance
53
+ hash.each do |key, value|
54
+ wrap_value!(object, key, value)
55
+ end
56
+ object
57
+ end
58
+
59
+ def wrap_value!(object, key, value)
60
+ if wrapper_method = _wrapper(value)
61
+ case wrapper_method
62
+ when Symbol
63
+ object.send(wrapper_method, key, value)
64
+ else
65
+ wrapper_method.call(object, key, value)
66
+ end
67
+ else
68
+ raise ArgumentError, "wrapper for #{key}=#{value} (#{value.class}) not found"
69
+ end
70
+ end
71
+
72
+ def _wrapper(value)
73
+ case value
74
+ when Array
75
+ TO_SFS[[value.first.class]]
76
+ else
77
+ TO_SFS[value.class]
78
+ end
79
+ end
80
+
81
+ # object -> hash
82
+ def to_hash(object)
83
+ hash = {}
84
+ object.keys.each do |key|
85
+ hash[key.to_sym] = unwrap_value!(object, key)
86
+ end
87
+ hash
88
+ end
89
+
90
+ def unwrap_value!(object, key)
91
+ value = object.get(key)
92
+ if wrapper_method = _unwrapper(value)
93
+ case wrapper_method
94
+ when Symbol
95
+ object.send(wrapper_method, key)
96
+ else
97
+ wrapper_method.call(key, value)
98
+ end
99
+ else
100
+ raise ArgumentError, "unwrapper for #{key}=#{value.object.inspect} (#{value.type_id}) not found"
101
+ end
102
+ end
103
+
104
+ def _unwrapper(value)
105
+ TO_HASH[value.type_id.to_s]
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,35 @@
1
+ # Extends core object with some conversion methods.
2
+
3
+ require 'rubyfox/sfsobject/bulk'
4
+ require 'rubyfox/sfsobject/schema'
5
+ require 'rubyfox/sfsobject/accessor'
6
+
7
+ class Hash
8
+ def to_sfs(schema=:none)
9
+ if schema == :none
10
+ Rubyfox::SFSObject::Bulk.to_sfs(self)
11
+ else
12
+ Rubyfox::SFSObject::Schema.to_sfs(schema, self)
13
+ end
14
+ end
15
+ end
16
+
17
+ class Rubyfox::SFSObject::Java::SFSObject
18
+ include Rubyfox::SFSObject::Accessor
19
+
20
+ def to_hash(schema=:none)
21
+ if schema == :none
22
+ Rubyfox::SFSObject::Bulk.to_hash(self)
23
+ else
24
+ Rubyfox::SFSObject::Schema.to_hash(schema, self)
25
+ end
26
+ end
27
+
28
+ def self.from_json(data)
29
+ if data == "{}"
30
+ new
31
+ else
32
+ new_from_json_data(data)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ require 'java'
2
+
3
+ module Rubyfox
4
+ module SFSObject
5
+ module Java
6
+ SFSObject = com.smartfoxserver.v2.entities.data.SFSObject
7
+ SFSArray = com.smartfoxserver.v2.entities.data.SFSArray
8
+ ArrayList = java.util.ArrayList
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,110 @@
1
+ module Boolean
2
+ end
3
+
4
+ class TrueClass
5
+ include Boolean
6
+ end
7
+
8
+ class FalseClass
9
+ include Boolean
10
+ end
11
+
12
+ require 'rubyfox/sfsobject/java'
13
+
14
+ module Rubyfox
15
+ module SFSObject
16
+ class Schema
17
+ TO_SFS = {
18
+ String => proc { |o, s, k, v| o.put_utf_string(k, v) },
19
+ Boolean => proc { |o, s, k, v| o.put_bool(k, v) },
20
+ Fixnum => proc { |o, s, k, v| o.put_int(k, v) },
21
+ Float => proc { |o, s, k, v| o.put_double(k, v) },
22
+ Hash => proc { |o, s, k, v| o.put_sfs_object(k, to_sfs(s, v)) },
23
+ [String] => proc { |o, s, k, v| o.put_utf_string_array(k, v) },
24
+ [Boolean] => proc { |o, s, k, v| o.put_bool_array(k, v) },
25
+ [Fixnum] => proc do |o, s, k, v|
26
+ collection = Java::ArrayList.new(v.size)
27
+ v.each { |e| collection.add(e.to_java(:int)) }
28
+ o.put_int_array(k, collection)
29
+ end,
30
+ [Float] => proc { |o, s, k, v| o.put_double_array(k, v) },
31
+ [Hash] => proc { |o, s, k, v|
32
+ ary = Java::SFSArray.new
33
+ schema = s[0]
34
+ v.each { |e| ary.add_sfs_object(to_sfs(schema, e)) }
35
+ o.put_sfs_array(k, ary)
36
+ }
37
+ }
38
+
39
+ TO_HASH = {
40
+ String => proc { |o, s, k| o.get_utf_string(k) },
41
+ Boolean => proc { |o, s, k| o.get_bool(k) },
42
+ Fixnum => proc { |o, s, k| o.get_int(k) },
43
+ Float => proc { |o, s, k| o.get_double(k) },
44
+ Hash => proc { |o, s, k| to_hash(s, o.get_sfs_object(k)) },
45
+ [String] => proc { |o, s, k| o.get_utf_string_array(k).to_a },
46
+ [Boolean] => proc { |o, s, k| o.get_bool_array(k).to_a },
47
+ [Fixnum] => proc { |o, s, k| o.get_int_array(k).to_a },
48
+ [Float] => proc { |o, s, k| o.get_double_array(k).to_a },
49
+ [Hash] => proc { |o, s, k|
50
+ sfs_ary = o.get_sfs_array(k)
51
+ ary = []
52
+ schema = s[0]
53
+ sfs_ary.size.times do |i|
54
+ ary << to_hash(schema, sfs_ary.get_sfs_object(i))
55
+ end
56
+ ary
57
+ }
58
+ }
59
+
60
+ def initialize(schema={})
61
+ @schema = schema
62
+ end
63
+
64
+ def to_sfs(hash)
65
+ self.class.to_sfs(@schema, hash)
66
+ end
67
+
68
+ def self.to_sfs(schemas, hash)
69
+ object = Java::SFSObject.new_instance
70
+ schemas.each do |key, schema|
71
+ next unless hash && hash.key?(key)
72
+ value = hash[key]
73
+ if method = wrap_method(TO_SFS, schema)
74
+ method.call(object, schema, key, value)
75
+ else
76
+ raise ArgumentError, "wrapper for #{key}=#{value} (#{schema}) not found"
77
+ end
78
+ end
79
+ object
80
+ end
81
+
82
+ def to_hash(object)
83
+ self.class.to_hash(@schema, object)
84
+ end
85
+
86
+ def self.to_hash(schemas, object)
87
+ hash = {}
88
+ schemas.each do |key, schema|
89
+ next unless object.contains_key(key.to_s)
90
+ if method = wrap_method(TO_HASH, schema)
91
+ hash[key] = method.call(object, schema, key)
92
+ else
93
+ raise ArgumentError, "unwrapper for #{key}=#{value} (#{schema}) not found"
94
+ end
95
+ end
96
+ hash
97
+ end
98
+
99
+ def self.wrap_method(map, schema)
100
+ if Hash === schema
101
+ map[Hash]
102
+ elsif Array === schema && Hash === schema.first
103
+ map[[Hash]]
104
+ else
105
+ map[schema]
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,5 @@
1
+ module Rubyfox
2
+ module SFSObject
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require 'pathname'
2
+
3
+ module Rubyfox
4
+ module SFSObject
5
+ def self.boot!(sf_dir)
6
+ unless $LOAD_PATH.include?(sf_dir)
7
+ path = Pathname.new(sf_dir).join("*.jar")
8
+ jars = Dir[path].to_a
9
+ unless jars.empty?
10
+ jars.each { |jar| require jar }
11
+ else
12
+ raise LoadError, "No jars found in #{path}"
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.new(*args)
18
+ Java::SFSObject.new(*args)
19
+ end
20
+
21
+ def self.from_json(data)
22
+ Java::SFSObject.new_from_json_data(data)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubyfox/sfsobject/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rubyfox-sfsobject"
8
+ gem.version = Rubyfox::SFSObject::VERSION
9
+ gem.platform = Gem::Platform::JAVA
10
+ gem.authors = ["Peter Suschlik"]
11
+ gem.email = ["ps@neopoly.de"]
12
+ gem.description = %q{Map SFSObject into Ruby Hash}
13
+ gem.summary = %q{}
14
+ gem.homepage = "https://github.com/neopoly/rubyfox-sfsobject"
15
+
16
+ gem.files = `git ls-files`.split($/).reject { |file| file =~ %r{test/vendor} }
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'rdoc'
23
+ gem.add_development_dependency 'minitest'
24
+ gem.add_development_dependency 'testem'
25
+ gem.add_development_dependency 'simple_assertions'
26
+ end
@@ -0,0 +1,113 @@
1
+ require 'benchmark/ips'
2
+
3
+ require 'rubyfox/sfsobject'
4
+
5
+ ENV['SF_DIR'] ||= File.join(File.dirname(__FILE__), '..', 'vendor', 'smartfox')
6
+ Rubyfox::SFSObject.boot!(ENV['SF_DIR'] + "/lib")
7
+
8
+ require 'rubyfox/sfsobject/core_ext'
9
+
10
+ empty = {}
11
+
12
+ short = {
13
+ :formation => "string",
14
+ :cards => [1] * 5,
15
+ :trainer => 1,
16
+ :modes => ["string", "string"]
17
+ }
18
+
19
+ long = {
20
+ :round => 1,
21
+ :turn => "string",
22
+ :picks => [
23
+ {
24
+ :user => "string",
25
+ :slot => 1,
26
+ :card => 1,
27
+ :slot2 => 1,
28
+ :card2 => 1,
29
+ :with => "string",
30
+ :modification => 1
31
+ }
32
+ ] * 2,
33
+ :match_finished => true,
34
+ :subs => [
35
+ :user => "string",
36
+ :out_slot => 1,
37
+ :in_from_slot => 1,
38
+ :in_to_slot => 1
39
+ ] * 2,
40
+ :eval => {
41
+ :scorer => "string",
42
+ :used_star_value => true,
43
+ :info => [{
44
+ :user => "string",
45
+ :results => [1, 2, 3],
46
+ :score => 1,
47
+ :with => "string",
48
+ :trainer => 1,
49
+ :modification => 1,
50
+ :modifications_left => 1,
51
+ :mods => [1, 2, 3],
52
+ :sum_values => [1, 2, 3]
53
+ }] * 2
54
+ }
55
+ }
56
+
57
+ emptyobject = empty.to_sfs
58
+ shortobject = short.to_sfs
59
+ longobject = long.to_sfs
60
+
61
+ emptyjson = emptyobject.to_json
62
+ shortjson = shortobject.to_json
63
+ longjson = longobject.to_json
64
+
65
+ Benchmark.ips do |x|
66
+ x.report("empty: hash -> sfs") do
67
+ empty.to_sfs
68
+ end
69
+
70
+ x.report("empty: json -> sfs") do
71
+ Rubyfox::SFSObject.from_json(emptyjson)
72
+ end
73
+
74
+ x.report("empty: sfs -> hash") do
75
+ emptyobject.to_hash
76
+ end
77
+
78
+ x.report("empty: sfs -> json") do
79
+ emptyobject.to_json
80
+ end
81
+
82
+ x.report("short: hash -> sfs") do
83
+ short.to_sfs
84
+ end
85
+
86
+ x.report("short: json -> sfs") do
87
+ Rubyfox::SFSObject.from_json(shortjson)
88
+ end
89
+
90
+ x.report("short: sfs -> hash") do
91
+ shortobject.to_hash
92
+ end
93
+
94
+ x.report("short: sfs -> json") do
95
+ shortobject.to_json
96
+ end
97
+
98
+ x.report("long: hash -> sfs") do
99
+ long.to_sfs
100
+ end
101
+
102
+ x.report("long: json -> sfs") do
103
+ Rubyfox::SFSObject.from_json(longjson)
104
+ end
105
+
106
+ x.report("long: sfs -> hash") do
107
+ longobject.to_hash
108
+ end
109
+
110
+ x.report("long: sfs -> json") do
111
+ longobject.to_json
112
+ end
113
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'minitest/autorun'
2
+ require 'testem'
3
+ require 'simple_assertions'
4
+
5
+ require 'rubyfox/sfsobject'
6
+
7
+ ENV['SF_DIR'] ||= File.join(File.dirname(__FILE__), 'vendor', 'smartfox')
8
+ Rubyfox::SFSObject.boot!(ENV['SF_DIR'] + "/lib")
9
+
10
+ class RubyfoxCase < Testem
11
+ include SimpleAssertions::AssertRaises
12
+ end
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ require 'rubyfox/sfsobject/core_ext'
6
+
7
+ class RubyfoxSFSObjectAccessorTest < RubyfoxCase
8
+ let(:sfs_object) { Rubyfox::SFSObject.new }
9
+
10
+ test "mixed keys" do
11
+ sfs_object["string"] = 1
12
+ sfs_object[:symbol] = 2
13
+ assert_equal 1, sfs_object["string"]
14
+ assert_equal 1, sfs_object[:string]
15
+ assert_equal 2, sfs_object[:symbol]
16
+ assert_equal 2, sfs_object["symbol"]
17
+ end
18
+
19
+ context "plain" do
20
+ test "nil" do
21
+ assert_accessor :null => nil
22
+ end
23
+
24
+ test "string" do
25
+ assert_accessor :string => "value"
26
+ assert_accessor :string => "üöäÜÖÄß"
27
+ end
28
+
29
+ test "boolean" do
30
+ assert_accessor :true => true, :false => false
31
+ end
32
+
33
+ test "fixnum" do
34
+ assert_accessor :fixnum => 1
35
+ assert_accessor :fixnum => (2 ** 31 - 1)
36
+ assert_accessor :fixnum => -(2 ** 31)
37
+ end
38
+
39
+ test "fixnum too big for int" do
40
+ assert_raises RangeError, :message => /too big for int/ do
41
+ assert_accessor :fixnum => (2 ** 31)
42
+ end
43
+ end
44
+
45
+ test "cannot handle bignum" do
46
+ assert_raises ArgumentError, :message => /Bignum/ do
47
+ assert_accessor :fixnum => (2 ** 63)
48
+ end
49
+ end
50
+
51
+ test "float" do
52
+ assert_accessor :float => 1.0
53
+ assert_accessor :float => 1.0 / 3
54
+ end
55
+
56
+ test "sub hashes" do
57
+ assert_accessor :sub => { :key => "value" }
58
+ assert_accessor :sub => { :deep => { :key => "value" } }
59
+ end
60
+ end
61
+
62
+ context "array" do
63
+ # TODO nil array?
64
+
65
+ test "string" do
66
+ assert_accessor :string => ["foo", "bar"]
67
+ assert_accessor :string => ["Föhn", "BÄR"]
68
+ end
69
+
70
+ test "boolean" do
71
+ assert_accessor :bool => [ true, false ]
72
+ end
73
+
74
+ test "fixnum" do
75
+ assert_accessor :fixnum => [ 1, 2, 3 ]
76
+ end
77
+
78
+ test "float" do
79
+ assert_accessor :float => [ 1.0, 1.0 / 3 ]
80
+ end
81
+
82
+ test "sub hashes" do
83
+ assert_accessor :sub => [{ :key => "value" }]
84
+ assert_accessor :sub => [{ :deep => [{ :key => "value" }] }]
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def assert_accessor(pair)
91
+ key, value = pair.first
92
+ sfs_object[key] = value
93
+ assert_equal value, sfs_object[key]
94
+ end
95
+ end
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ require 'rubyfox/sfsobject/java'
4
+
5
+ class RubyfoxSFSObjectBasicTest < RubyfoxCase
6
+ let(:sfs_object) { Rubyfox::SFSObject.new }
7
+
8
+ test "to_json works" do
9
+ assert_equal "{}", sfs_object.to_json
10
+ end
11
+
12
+ test "from_json works" do
13
+ json = sfs_object.to_json
14
+ object = Rubyfox::SFSObject.from_json(json)
15
+ assert_equal json, object.to_json
16
+ end
17
+ end
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+ require 'rubyfox/sfsobject/bulk'
5
+
6
+ class RubyfoxSFSObjectBulkTest < RubyfoxCase
7
+ test "empty" do
8
+ assert_conversion Hash.new
9
+ end
10
+
11
+ test "converts keys to symbols" do
12
+ assert_conversion({ "key" => nil }, { :key => nil })
13
+ end
14
+
15
+ context "plain" do
16
+ test "nil" do
17
+ assert_conversion :null => nil
18
+ end
19
+
20
+ test "string" do
21
+ assert_conversion :string => "value"
22
+ assert_conversion :string => "üöäÜÖÄß"
23
+ end
24
+
25
+ test "boolean" do
26
+ assert_conversion :true => true, :false => false
27
+ end
28
+
29
+ test "fixnum" do
30
+ assert_conversion :fixnum => 1
31
+ assert_conversion :fixnum => (2 ** 31 - 1)
32
+ assert_conversion :fixnum => -(2 ** 31)
33
+ end
34
+
35
+ test "fixnum too big for int" do
36
+ assert_raises RangeError, :message => /too big for int/ do
37
+ assert_conversion :fixnum => (2 ** 31)
38
+ end
39
+ end
40
+
41
+ test "cannot handle bignum" do
42
+ assert_raises ArgumentError, :message => /Bignum/ do
43
+ assert_conversion :fixnum => (2 ** 63)
44
+ end
45
+ end
46
+
47
+ test "float" do
48
+ assert_conversion :float => 1.0
49
+ assert_conversion :float => 1.0 / 3
50
+ end
51
+
52
+ test "sub hashes" do
53
+ assert_conversion :sub => { :key => "value" }
54
+ assert_conversion :sub => { :deep => { :key => "value" } }
55
+ end
56
+ end
57
+
58
+ context "array" do
59
+ # TODO nil array?
60
+
61
+ test "string" do
62
+ assert_conversion :string => ["foo", "bar"]
63
+ assert_conversion :string => ["Föhn", "BÄR"]
64
+ end
65
+
66
+ test "boolean" do
67
+ assert_conversion :bool => [ true, false ]
68
+ end
69
+
70
+ test "fixnum" do
71
+ assert_conversion :fixnum => [ 1, 2, 3 ]
72
+ end
73
+
74
+ test "float" do
75
+ assert_conversion :float => [ 1.0, 1.0 / 3 ]
76
+ end
77
+
78
+ test "sub hashes" do
79
+ assert_conversion :sub => [{ :key => "value" }]
80
+ assert_conversion :sub => [{ :deep => [{ :key => "value" }] }]
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def assert_conversion(input, output=input)
87
+ object = Rubyfox::SFSObject::Bulk.to_sfs(input)
88
+ assert_equal output, Rubyfox::SFSObject::Bulk.to_hash(object)
89
+ end
90
+ end
@@ -0,0 +1,27 @@
1
+ require 'helper'
2
+ require 'rubyfox/sfsobject/core_xt'
3
+
4
+ class RubyfoxSFSObjectCoreExtTest < RubyfoxCase
5
+ context "bulk" do
6
+ test "methods" do
7
+ hash = { :key => "value"}
8
+ assert_equal hash, hash.to_sfs.to_hash
9
+ end
10
+ end
11
+
12
+ context "schema" do
13
+ test "methods" do
14
+ schema = { :key => String }
15
+ hash = { :key => "value" }
16
+ assert_equal hash, hash.to_sfs(schema).to_hash(schema)
17
+ end
18
+
19
+ test "fail w/o valid schema" do
20
+ schema = nil
21
+ hash = { :key => "value" }
22
+ assert_raises NoMethodError, :message => /each/ do
23
+ assert_equal hash, hash.to_sfs(schema).to_hash(schema)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+ require 'rubyfox/sfsobject/schema'
5
+
6
+ class RubyfoxSFSObjectSchemaTest < RubyfoxCase
7
+ test "empty" do
8
+ assert_schema({}, {})
9
+ assert_schema({}, {:key => "value"}, {})
10
+ end
11
+
12
+ context "plain" do
13
+ test "nil is unknown" do
14
+ assert_raises ArgumentError, :message => /wrapper for null.*not found/ do
15
+ assert_schema({ :null => nil }, { :null => "foo" })
16
+ end
17
+ end
18
+
19
+ test "string" do
20
+ assert_schema({ :string => String }, { :string => "value" })
21
+ assert_schema({ :string => String }, { :string => "üöäÜÖÄß" })
22
+ end
23
+
24
+ test "bool" do
25
+ assert_schema({ :bool => Boolean }, { :bool => true })
26
+ assert_schema({ :bool => Boolean }, { :bool => false })
27
+ end
28
+
29
+ test "fixnum" do
30
+ assert_schema({ :fixnum => Fixnum }, { :fixnum => 1 })
31
+ assert_schema({ :fixnum => Fixnum }, { :fixnum => (2 ** 31 - 1) })
32
+ assert_schema({ :fixnum => Fixnum }, { :fixnum => -(2 ** 31) })
33
+ end
34
+
35
+ test "fixnum too big for int" do
36
+ assert_raises RangeError, :message => /too big for int/ do
37
+ assert_schema({ :fixnum => Fixnum }, { :fixnum => (2 ** 31) })
38
+ end
39
+ end
40
+
41
+ test "float" do
42
+ assert_schema({ :float => Float }, { :float => 1.0 })
43
+ assert_schema({ :float => Float }, { :float => 1.0 / 3 })
44
+ end
45
+
46
+ test "nested hash" do
47
+ assert_schema(
48
+ { :sub => { :key => String } },
49
+ { :sub => { :key => "value" } }
50
+ )
51
+ end
52
+ end
53
+
54
+ context "array" do
55
+ test "nil is unknown" do
56
+ assert_raises ArgumentError, :message => /wrapper for null.*not found/ do
57
+ assert_schema({ :null => [nil] }, { :null => "foo" })
58
+ end
59
+ end
60
+
61
+ test "string" do
62
+ assert_schema({ :string => [String] }, { :string => ["foo", "bar"] })
63
+ assert_schema({ :string => [String] }, { :string => ["foo", "üöäÜÖÄß"] })
64
+ assert_schema({ :string => [String] }, { :string => ["foo", 23] }) # strange?!
65
+ end
66
+
67
+ test "bool" do
68
+ assert_schema({ :bool => [Boolean] }, { :bool => [true, false] })
69
+ assert_schema({ :bool => [Boolean] }, { :bool => [true, 23] }) # strange?
70
+ end
71
+
72
+ test "fixnum" do
73
+ assert_schema({ :fixnum => [Fixnum] }, { :fixnum => [1, 2] })
74
+ assert_schema({ :fixnum => [Fixnum] }, { :fixnum => [(2 ** 31 - 1), -(2 ** 31)] })
75
+ end
76
+
77
+ test "float" do
78
+ assert_schema({ :float => [Float] }, { :float => [1.0, 1.0 / 3] })
79
+ end
80
+
81
+ test "nested hash" do
82
+ assert_schema(
83
+ { :sub => [ { :key => String } ] },
84
+ { :sub => [ { :key => "foo" }, { :key => "bar" }] }
85
+ )
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def assert_schema(schema, input, output=input)
92
+ object = Rubyfox::SFSObject::Schema.to_sfs(schema, input)
93
+ hash = Rubyfox::SFSObject::Schema.to_hash(schema, object)
94
+ assert_equal output, hash
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyfox-sfsobject
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: java
7
+ authors:
8
+ - Peter Suschlik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: !binary |-
21
+ MA==
22
+ none: false
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: !binary |-
28
+ MA==
29
+ none: false
30
+ prerelease: false
31
+ type: :development
32
+ - !ruby/object:Gem::Dependency
33
+ name: rdoc
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: !binary |-
39
+ MA==
40
+ none: false
41
+ requirement: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: !binary |-
46
+ MA==
47
+ none: false
48
+ prerelease: false
49
+ type: :development
50
+ - !ruby/object:Gem::Dependency
51
+ name: minitest
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: !binary |-
57
+ MA==
58
+ none: false
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: !binary |-
64
+ MA==
65
+ none: false
66
+ prerelease: false
67
+ type: :development
68
+ - !ruby/object:Gem::Dependency
69
+ name: testem
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: !binary |-
75
+ MA==
76
+ none: false
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: !binary |-
82
+ MA==
83
+ none: false
84
+ prerelease: false
85
+ type: :development
86
+ - !ruby/object:Gem::Dependency
87
+ name: simple_assertions
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: !binary |-
93
+ MA==
94
+ none: false
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: !binary |-
100
+ MA==
101
+ none: false
102
+ prerelease: false
103
+ type: :development
104
+ description: Map SFSObject into Ruby Hash
105
+ email:
106
+ - ps@neopoly.de
107
+ executables: []
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - .gitignore
112
+ - .rvmrc
113
+ - .travis.yml
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.rdoc
117
+ - Rakefile
118
+ - lib/rubyfox/sfsobject.rb
119
+ - lib/rubyfox/sfsobject/accessor.rb
120
+ - lib/rubyfox/sfsobject/bulk.rb
121
+ - lib/rubyfox/sfsobject/core_ext.rb
122
+ - lib/rubyfox/sfsobject/java.rb
123
+ - lib/rubyfox/sfsobject/schema.rb
124
+ - lib/rubyfox/sfsobject/version.rb
125
+ - rubyfox-sfsobject.gemspec
126
+ - test/benchmark/perf.rb
127
+ - test/helper.rb
128
+ - test/rubyfox/sfsobject/accessor_test.rb
129
+ - test/rubyfox/sfsobject/basic_test.rb
130
+ - test/rubyfox/sfsobject/bulk_test.rb
131
+ - test/rubyfox/sfsobject/core_ext.rb
132
+ - test/rubyfox/sfsobject/schema_test.rb
133
+ homepage: https://github.com/neopoly/rubyfox-sfsobject
134
+ licenses: []
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ segments:
144
+ - 0
145
+ hash: 2
146
+ version: !binary |-
147
+ MA==
148
+ none: false
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ segments:
154
+ - 0
155
+ hash: 2
156
+ version: !binary |-
157
+ MA==
158
+ none: false
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 1.8.24
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: ''
165
+ test_files:
166
+ - test/benchmark/perf.rb
167
+ - test/helper.rb
168
+ - test/rubyfox/sfsobject/accessor_test.rb
169
+ - test/rubyfox/sfsobject/basic_test.rb
170
+ - test/rubyfox/sfsobject/bulk_test.rb
171
+ - test/rubyfox/sfsobject/core_ext.rb
172
+ - test/rubyfox/sfsobject/schema_test.rb