json2ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.coveralls.yml +0 -0
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +78 -0
- data/LICENSE.txt +21 -0
- data/README.md +78 -0
- data/Rakefile +6 -0
- data/bin/json2ruby +5 -0
- data/json2ruby.gemspec +27 -0
- data/lib/json2ruby.rb +9 -0
- data/lib/json2ruby/attribute.rb +25 -0
- data/lib/json2ruby/cli.rb +183 -0
- data/lib/json2ruby/collection.rb +63 -0
- data/lib/json2ruby/entity.rb +91 -0
- data/lib/json2ruby/primitive.rb +34 -0
- data/lib/json2ruby/ruby_writer.rb +50 -0
- data/lib/json2ruby/version.rb +3 -0
- data/spec/attribute_spec.rb +58 -0
- data/spec/cli_spec.rb +310 -0
- data/spec/collection_spec.rb +130 -0
- data/spec/entity_spec.rb +178 -0
- data/spec/primitive_spec.rb +51 -0
- data/spec/ruby_writer_spec.rb +117 -0
- data/spec/spec_helper.rb +18 -0
- metadata +225 -0
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
module JSON2Ruby
|
4
|
+
class Collection
|
5
|
+
attr_accessor :name, :original_name, :ruby_types
|
6
|
+
|
7
|
+
def self.short_name
|
8
|
+
"Collection"
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(name, ruby_types = {})
|
12
|
+
@name = name
|
13
|
+
@ruby_types = ruby_types
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.parse_from(name, obj_array, options = {})
|
17
|
+
ob = self.new(name)
|
18
|
+
obj_array.each do |v|
|
19
|
+
if v.kind_of?(Array)
|
20
|
+
arr = Collection.parse_from(Entity.get_next_unknown, v, options)
|
21
|
+
ob.ruby_types[arr.attr_hash] = arr
|
22
|
+
elsif v.kind_of?(String)
|
23
|
+
ob.ruby_types[RUBYSTRING.attr_hash] = RUBYSTRING
|
24
|
+
elsif v.kind_of?(Integer) && !options[:forcenumeric]
|
25
|
+
ob.ruby_types[RUBYINTEGER.attr_hash] = RUBYINTEGER
|
26
|
+
elsif v.kind_of?(Float) && !options[:forcenumeric]
|
27
|
+
ob.ruby_types[RUBYFLOAT.attr_hash] = RUBYFLOAT
|
28
|
+
elsif (v.kind_of?(Float) || v.kind_of?(Integer)) && options[:forcenumeric]
|
29
|
+
ob.ruby_types[RUBYNUMERIC.attr_hash] = RUBYNUMERIC
|
30
|
+
elsif !!v==v
|
31
|
+
ob.ruby_types[RUBYBOOLEAN.attr_hash] = RUBYBOOLEAN
|
32
|
+
elsif v.kind_of?(Hash)
|
33
|
+
ent = Entity.parse_from(Entity.get_next_unknown, v, options)
|
34
|
+
ob.ruby_types[ent.attr_hash] = ent
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
x = ob.attr_hash
|
39
|
+
return Entity.entities[x] if Entity.entities.has_key?(x)
|
40
|
+
Entity.entities[x] = ob
|
41
|
+
ob
|
42
|
+
end
|
43
|
+
|
44
|
+
def attr_hash
|
45
|
+
md5 = Digest::MD5.new
|
46
|
+
@ruby_types.each do |k,typ|
|
47
|
+
md5.update typ.attr_hash
|
48
|
+
end
|
49
|
+
md5.hexdigest
|
50
|
+
end
|
51
|
+
|
52
|
+
def ==(other)
|
53
|
+
return false if other.class != self.class
|
54
|
+
attr_hash == other.attr_hash
|
55
|
+
end
|
56
|
+
|
57
|
+
def comment
|
58
|
+
x = "#{@name}[]"
|
59
|
+
x += " (#{@original_name})" unless @original_name.nil?
|
60
|
+
x
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
module JSON2Ruby
|
4
|
+
|
5
|
+
class Entity
|
6
|
+
attr_accessor :name, :original_name, :attributes
|
7
|
+
|
8
|
+
def self.short_name
|
9
|
+
"Entity"
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(name, attributes = {})
|
13
|
+
@name = name
|
14
|
+
@attributes = attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
def attr_hash
|
18
|
+
md5 = Digest::MD5.new
|
19
|
+
@attributes.each do |k,v|
|
20
|
+
md5.update "#{k}:#{v.attr_hash}"
|
21
|
+
end
|
22
|
+
md5.hexdigest
|
23
|
+
end
|
24
|
+
|
25
|
+
def ==(other)
|
26
|
+
return false if other.class != self.class
|
27
|
+
attr_hash == other.attr_hash
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.reset_parse
|
31
|
+
@@objs = {
|
32
|
+
RUBYSTRING.attr_hash => RUBYSTRING,
|
33
|
+
RUBYINTEGER.attr_hash => RUBYINTEGER,
|
34
|
+
RUBYFLOAT.attr_hash => RUBYFLOAT,
|
35
|
+
RUBYBOOLEAN.attr_hash => RUBYBOOLEAN,
|
36
|
+
RUBYNUMERIC.attr_hash => RUBYNUMERIC,
|
37
|
+
}
|
38
|
+
@@unknowncount = 0
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.parse_from(name, obj_hash, options = {})
|
42
|
+
ob = self.new(name)
|
43
|
+
obj_hash.each do |k,v|
|
44
|
+
|
45
|
+
orig = k
|
46
|
+
k = k.gsub(/[^A-Za-z0-9_]/, "_")
|
47
|
+
|
48
|
+
if v.kind_of?(Array)
|
49
|
+
att = Collection.parse_from(k, v, options)
|
50
|
+
elsif v.kind_of?(String)
|
51
|
+
att = RUBYSTRING
|
52
|
+
elsif v.kind_of?(Integer) && !options[:forcenumeric]
|
53
|
+
att = RUBYINTEGER
|
54
|
+
elsif v.kind_of?(Float) && !options[:forcenumeric]
|
55
|
+
att = RUBYFLOAT
|
56
|
+
elsif (v.kind_of?(Integer) || v.kind_of?(Float)) && options[:forcenumeric]
|
57
|
+
att = RUBYNUMERIC
|
58
|
+
elsif !!v==v
|
59
|
+
att = RUBYBOOLEAN
|
60
|
+
elsif v.kind_of?(Hash)
|
61
|
+
att = self.parse_from(k, v, options)
|
62
|
+
end
|
63
|
+
att.original_name = orig if orig != k
|
64
|
+
ob.attributes[k] = att
|
65
|
+
end
|
66
|
+
|
67
|
+
x = ob.attr_hash
|
68
|
+
return @@objs[x] if @@objs.has_key?(x)
|
69
|
+
@@objs[x] = ob
|
70
|
+
ob
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.entities
|
74
|
+
@@objs
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.get_next_unknown
|
78
|
+
@@unknowncount ||= 0
|
79
|
+
@@unknowncount += 1
|
80
|
+
"Unknown#{@@unknowncount}"
|
81
|
+
end
|
82
|
+
|
83
|
+
def comment
|
84
|
+
x = @name
|
85
|
+
x += " (#{@original_name})" unless @original_name.nil?
|
86
|
+
x
|
87
|
+
end
|
88
|
+
|
89
|
+
reset_parse
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module JSON2Ruby
|
2
|
+
|
3
|
+
class Primitive
|
4
|
+
attr_accessor :name, :original_name, :attr_hash
|
5
|
+
|
6
|
+
def self.short_name
|
7
|
+
"Primitive"
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(name, attr_hash)
|
11
|
+
@name = name
|
12
|
+
@attr_hash = attr_hash
|
13
|
+
end
|
14
|
+
|
15
|
+
def attr_hash
|
16
|
+
@attr_hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def comment
|
20
|
+
@name
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Primitive Representing a Ruby String
|
25
|
+
RUBYSTRING = Primitive.new("String","0123456789ABCDEF0123456789ABCDEF")
|
26
|
+
# Primitive Representing a Ruby Integer
|
27
|
+
RUBYINTEGER = Primitive.new("Integer","0123456789ABCDEF0123456789ABCDE0")
|
28
|
+
# Primitive Representing a Ruby Float
|
29
|
+
RUBYFLOAT = Primitive.new("Float","0123456789ABCDEF0123456789ABCDE1")
|
30
|
+
# Primitive Representing a Ruby Boolean
|
31
|
+
RUBYBOOLEAN = Primitive.new("Boolean","0123456789ABCDEF0123456789ABCDE2")
|
32
|
+
# Psuedo-Primitive Representing a Numeric
|
33
|
+
RUBYNUMERIC = Primitive.new("Numeric","0123456789ABCDEF0123456789ABCDE2")
|
34
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'json'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
module JSON2Ruby
|
6
|
+
class RubyWriter
|
7
|
+
def self.to_code(entity, indent = 0, options = {})
|
8
|
+
x = ""
|
9
|
+
if options.has_key?(:require)
|
10
|
+
options[:require].each { |r| x += "require '#{r}'\r\n" }
|
11
|
+
x += "\r\n"
|
12
|
+
end
|
13
|
+
idt = (' '*indent)
|
14
|
+
x += "#{(' '*indent)}#{options[:modules] ? "module" : "class"} #{entity.name}"
|
15
|
+
x += " < #{options[:superclass_name]}" if options.has_key?(:superclass_name)
|
16
|
+
x += "\r\n"
|
17
|
+
if options.has_key?(:extend)
|
18
|
+
options[:extend].each { |r| x += "#{(' '*(indent+2))}extend #{r}\r\n" }
|
19
|
+
x += "\r\n"
|
20
|
+
end
|
21
|
+
if options.has_key?(:include)
|
22
|
+
options[:include].each { |r| x += "#{(' '*(indent+2))}include #{r}\r\n" }
|
23
|
+
x += "\r\n"
|
24
|
+
end
|
25
|
+
x += attributes_to_ruby(entity, indent+2, options)
|
26
|
+
x += "#{(' '*indent)}end\r\n"
|
27
|
+
x
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.attributes_to_ruby(entity, indent, options = {})
|
31
|
+
ident = (' '*indent)
|
32
|
+
x = ""
|
33
|
+
entity.attributes.each do |k,v|
|
34
|
+
if (v.is_a?(Collection))
|
35
|
+
x += "#{ident}#{options[:collectionmethod]} :#{k}"
|
36
|
+
else
|
37
|
+
x += "#{ident}#{options[:attributemethod]} :#{k}"
|
38
|
+
end
|
39
|
+
if options[:includetypes]
|
40
|
+
unless v.is_a?(Primitive)
|
41
|
+
name = !options[:namespace].nil? && options[:namespace]!="" ? (options[:namespace]+"::"+v.name) : v.name
|
42
|
+
x += ", '#{name}'"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
x += " # #{v.comment}\r\n"
|
46
|
+
end
|
47
|
+
x
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe JSON2Ruby::Attribute do
|
4
|
+
describe '#initialize' do
|
5
|
+
it 'should set up correct attrs' do
|
6
|
+
x = JSON2Ruby::Attribute.new('hello','world')
|
7
|
+
expect(x.name).to eq("hello")
|
8
|
+
expect(x.ruby_type).to eq("world")
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should have correct defaults' do
|
12
|
+
x = JSON2Ruby::Attribute.new('hello')
|
13
|
+
expect(x.name).to eq("hello")
|
14
|
+
expect(x.ruby_type).to eq("_unknown")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#short_name' do
|
19
|
+
it 'should have the correct short name' do
|
20
|
+
expect(JSON2Ruby::Attribute.short_name).to eq('Attribute')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#attr_hash' do
|
25
|
+
it 'should return correct hash' do
|
26
|
+
x = JSON2Ruby::Attribute.new('hello','world')
|
27
|
+
expect(x.attr_hash).to eq("6de41d334b7ce946682da48776a10bb9")
|
28
|
+
x = JSON2Ruby::Attribute.new('foo','bar')
|
29
|
+
expect(x.attr_hash).to eq("4e99e8c12de7e01535248d2bac85e732")
|
30
|
+
x = JSON2Ruby::Attribute.new('hello','bar')
|
31
|
+
expect(x.attr_hash).to eq("4b8ef9a9e864fb4035c8e45d41b085f7")
|
32
|
+
x = JSON2Ruby::Attribute.new('foo','world')
|
33
|
+
expect(x.attr_hash).to eq("b4738da083596d81a33ec6aabba9460b")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#==' do
|
38
|
+
it 'should return false if class mismatch' do
|
39
|
+
x = JSON2Ruby::Attribute.new('hello','world')
|
40
|
+
|
41
|
+
expect(x=={}).to be(false)
|
42
|
+
expect(x==[]).to be(false)
|
43
|
+
expect(x==1).to be(false)
|
44
|
+
expect(x=="hello").to be(false)
|
45
|
+
expect(x==nil).to be(false)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should return true if and only if attr_hash matches' do
|
49
|
+
x = JSON2Ruby::Attribute.new('hello','world')
|
50
|
+
y = JSON2Ruby::Attribute.new('foo','bar')
|
51
|
+
z = JSON2Ruby::Attribute.new('hello','world')
|
52
|
+
|
53
|
+
expect(x==x).to be(true)
|
54
|
+
expect(x==y).to be(false)
|
55
|
+
expect(x==z).to be(true)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/cli_spec.rb
ADDED
@@ -0,0 +1,310 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe JSON2Ruby::CLI do
|
4
|
+
|
5
|
+
describe '#run' do
|
6
|
+
it 'should call in correct order' do
|
7
|
+
options = { :one => 1, :outputdir => File.dirname(__FILE__) }
|
8
|
+
|
9
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("json2ruby v#{JSON2Ruby::VERSION}\n")
|
10
|
+
expect(JSON2Ruby::CLI).to receive(:get_cli_options).and_return(options)
|
11
|
+
expect(JSON2Ruby::CLI).to receive(:ensure_output_dir).with(options)
|
12
|
+
expect(JSON2Ruby::CLI).to receive(:parse_files).with(options).and_return("rootclasses")
|
13
|
+
expect(JSON2Ruby::CLI).to receive(:write_files).with("rootclasses",JSON2Ruby::RubyWriter,options)
|
14
|
+
|
15
|
+
JSON2Ruby::CLI.run
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#ensure_output_dir' do
|
20
|
+
it 'should check dir and create if necessary' do
|
21
|
+
dir = File.dirname(__FILE__)
|
22
|
+
options = { :one => 1, :outputdir => dir }
|
23
|
+
|
24
|
+
expect(Dir).to receive(:exists?).with(dir).and_return(false)
|
25
|
+
expect(Dir).to receive(:mkdir).with(dir)
|
26
|
+
|
27
|
+
expect(JSON2Ruby::CLI).not_to receive(:puts)
|
28
|
+
|
29
|
+
JSON2Ruby::CLI.ensure_output_dir(options)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should not create dir if exists' do
|
33
|
+
dir = File.dirname(__FILE__)
|
34
|
+
options = { :one => 1, :outputdir => dir }
|
35
|
+
|
36
|
+
expect(Dir).to receive(:exists?).with(dir).and_return(true)
|
37
|
+
expect(Dir).not_to receive(:mkdir)
|
38
|
+
|
39
|
+
JSON2Ruby::CLI.ensure_output_dir(options)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should be verbose if option set' do
|
43
|
+
dir = File.dirname(__FILE__)
|
44
|
+
options = { :one => 1, :outputdir => dir, :verbose =>true }
|
45
|
+
|
46
|
+
expect(Dir).to receive(:exists?).with(dir).and_return(false)
|
47
|
+
expect(Dir).to receive(:mkdir).with(dir)
|
48
|
+
|
49
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("Output Directory: #{dir}")
|
50
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("Creating Output Directory...")
|
51
|
+
|
52
|
+
JSON2Ruby::CLI.ensure_output_dir(options)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#get_cli_options' do
|
57
|
+
|
58
|
+
it 'should have correct defaults' do
|
59
|
+
ARGV = []
|
60
|
+
expect(JSON2Ruby::CLI.get_cli_options).to eq({
|
61
|
+
:outputdir=> File.expand_path("../classes",File.dirname(__FILE__)),
|
62
|
+
:namespace=>"",
|
63
|
+
:attributemethod=>"attr_accessor",
|
64
|
+
:collectionmethod=>"attr_accessor",
|
65
|
+
:includetypes=>false,
|
66
|
+
:baseless=>false,
|
67
|
+
:forceoverwrite=>false,
|
68
|
+
:verbose=>false,
|
69
|
+
:modulenames=>[]
|
70
|
+
})
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should parse -o' do
|
74
|
+
ARGV=["-o","test"]
|
75
|
+
options = JSON2Ruby::CLI.get_cli_options
|
76
|
+
expect(options[:outputdir]).to eq("test")
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should parse -n' do
|
80
|
+
ARGV=["-n","TEST::TEST2"]
|
81
|
+
options = JSON2Ruby::CLI.get_cli_options
|
82
|
+
expect(options[:namespace]).to eq("TEST::TEST2")
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should parse -s' do
|
86
|
+
ARGV=["-s","TEST"]
|
87
|
+
options = JSON2Ruby::CLI.get_cli_options
|
88
|
+
expect(options[:superclass_name]).to eq("TEST")
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should parse -r' do
|
92
|
+
ARGV=["-r","json/test"]
|
93
|
+
options = JSON2Ruby::CLI.get_cli_options
|
94
|
+
expect(options[:require]).to eq(["json/test"])
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should parse multiple -r' do
|
98
|
+
ARGV=["-r","json/test","-r","onetwo"]
|
99
|
+
options = JSON2Ruby::CLI.get_cli_options
|
100
|
+
expect(options[:require]).to eq(["json/test","onetwo"])
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should parse -i' do
|
104
|
+
ARGV=["-i","json/test"]
|
105
|
+
options = JSON2Ruby::CLI.get_cli_options
|
106
|
+
expect(options[:include]).to eq(["json/test"])
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should parse multiple -i' do
|
110
|
+
ARGV=["-i","json/test","-i","onetwo"]
|
111
|
+
options = JSON2Ruby::CLI.get_cli_options
|
112
|
+
expect(options[:include]).to eq(["json/test","onetwo"])
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should parse -e' do
|
116
|
+
ARGV=["-e","json/test"]
|
117
|
+
options = JSON2Ruby::CLI.get_cli_options
|
118
|
+
expect(options[:extend]).to eq(["json/test"])
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should parse multiple -e' do
|
122
|
+
ARGV=["-e","json/test","-e","onetwo"]
|
123
|
+
options = JSON2Ruby::CLI.get_cli_options
|
124
|
+
expect(options[:extend]).to eq(["json/test","onetwo"])
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should parse -M' do
|
128
|
+
ARGV=["-M","-e","one"]
|
129
|
+
options = JSON2Ruby::CLI.get_cli_options
|
130
|
+
expect(options[:modules]).to eq(true)
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should parse -a' do
|
134
|
+
ARGV=["-a","TESTA"]
|
135
|
+
options = JSON2Ruby::CLI.get_cli_options
|
136
|
+
expect(options[:attributemethod]).to eq("TESTA")
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'should parse -c' do
|
140
|
+
ARGV=["-c","TESTB"]
|
141
|
+
options = JSON2Ruby::CLI.get_cli_options
|
142
|
+
expect(options[:collectionmethod]).to eq("TESTB")
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'should parse -t' do
|
146
|
+
ARGV=["-t"]
|
147
|
+
options = JSON2Ruby::CLI.get_cli_options
|
148
|
+
expect(options[:includetypes]).to eq(true)
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'should parse -b' do
|
152
|
+
ARGV=["-b"]
|
153
|
+
options = JSON2Ruby::CLI.get_cli_options
|
154
|
+
expect(options[:baseless]).to eq(true)
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should parse -f' do
|
158
|
+
ARGV=["-f"]
|
159
|
+
options = JSON2Ruby::CLI.get_cli_options
|
160
|
+
expect(options[:forceoverwrite]).to eq(true)
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'should parse -N' do
|
164
|
+
ARGV=["-N"]
|
165
|
+
options = JSON2Ruby::CLI.get_cli_options
|
166
|
+
expect(options[:forcenumeric]).to eq(true)
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'should parse -v' do
|
170
|
+
ARGV=["-v"]
|
171
|
+
options = JSON2Ruby::CLI.get_cli_options
|
172
|
+
expect(options[:verbose]).to eq(true)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe '#self.parse_files' do
|
177
|
+
it 'should reset entity and parse from files' do
|
178
|
+
options = {
|
179
|
+
:verbose => false
|
180
|
+
}
|
181
|
+
|
182
|
+
ARGV = ['/test/to/file','/test/to/file2']
|
183
|
+
|
184
|
+
expect(File).to receive(:read).with("/test/to/file").and_return('{"one":1}')
|
185
|
+
expect(File).to receive(:read).with("/test/to/file2").and_return('{"two":2}')
|
186
|
+
|
187
|
+
expect(JSON2Ruby::Entity).to receive(:parse_from).with("file", {"one"=>1}, options).and_return(3)
|
188
|
+
expect(JSON2Ruby::Entity).to receive(:parse_from).with("file2", {"two"=>2}, options).and_return(4)
|
189
|
+
|
190
|
+
expect(JSON2Ruby::CLI.parse_files(options)).to eq([3,4])
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'should be verbose when option set' do
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe '#self.write_files' do
|
198
|
+
it 'should call File.exists? and File.write with correct data' do
|
199
|
+
options = { :one => 1, :outputdir => File.dirname(__FILE__), :modulenames => [ "TestModule" ] }
|
200
|
+
|
201
|
+
ent = JSON2Ruby::Entity.new("TestEntity", { "one" => JSON2Ruby::RUBYSTRING })
|
202
|
+
ents = { ent.attr_hash => ent }
|
203
|
+
|
204
|
+
JSON2Ruby::Entity.class_variable_set(:@@objs, ents)
|
205
|
+
|
206
|
+
writer = {}
|
207
|
+
expect(writer).to receive(:to_code).with(ent,2,options).and_return("CODE")
|
208
|
+
|
209
|
+
expect(File).to receive(:exists?).with("#{options[:outputdir]}/TestEntity.rb").and_return(false)
|
210
|
+
expect(File).to receive(:write).with("#{options[:outputdir]}/TestEntity.rb", "module TestModule\r\nCODEend\r\n")
|
211
|
+
|
212
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("Done, Generated 1 file")
|
213
|
+
|
214
|
+
JSON2Ruby::CLI.write_files([],writer, options)
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'should not write collections' do
|
218
|
+
options = { :one => 1, :outputdir => File.dirname(__FILE__), :modulenames => [ "TestModule" ] }
|
219
|
+
|
220
|
+
ents = {}
|
221
|
+
ent = JSON2Ruby::Collection.new("TestCollection", { })
|
222
|
+
ents[ent.attr_hash] = ent
|
223
|
+
|
224
|
+
ent = JSON2Ruby::Entity.new("TestEntity", { "one" => JSON2Ruby::RUBYSTRING })
|
225
|
+
ents[ent.attr_hash] = ent
|
226
|
+
|
227
|
+
JSON2Ruby::Entity.class_variable_set(:@@objs, ents)
|
228
|
+
|
229
|
+
writer = {}
|
230
|
+
expect(writer).to receive(:to_code).with(ent,2,options).and_return("CODE")
|
231
|
+
|
232
|
+
expect(File).to receive(:exists?).with("#{options[:outputdir]}/TestEntity.rb").and_return(false)
|
233
|
+
expect(File).to receive(:write).with("#{options[:outputdir]}/TestEntity.rb", "module TestModule\r\nCODEend\r\n")
|
234
|
+
|
235
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("Done, Generated 1 file")
|
236
|
+
|
237
|
+
JSON2Ruby::CLI.write_files([],writer, options)
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'should alert and not write file if it exists and not --forceoverwrite' do
|
241
|
+
options = { :one => 1, :outputdir => File.dirname(__FILE__), :modulenames => [ "TestModule" ] }
|
242
|
+
|
243
|
+
ent = JSON2Ruby::Entity.new("TestEntity", { "one" => JSON2Ruby::RUBYSTRING })
|
244
|
+
ents = { ent.attr_hash => ent }
|
245
|
+
|
246
|
+
JSON2Ruby::Entity.class_variable_set(:@@objs, ents)
|
247
|
+
|
248
|
+
writer = {}
|
249
|
+
expect(writer).to receive(:to_code).with(ent,2,options).and_return("CODE")
|
250
|
+
|
251
|
+
expect(File).to receive(:exists?).with("#{options[:outputdir]}/TestEntity.rb").and_return(true)
|
252
|
+
expect(File).not_to receive(:write)
|
253
|
+
|
254
|
+
expect($stderr).to receive(:puts).with("File #{options[:outputdir]}/TestEntity.rb exists. Use -f to overwrite.")
|
255
|
+
|
256
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("Done, Generated 0 files")
|
257
|
+
|
258
|
+
JSON2Ruby::CLI.write_files([],writer, options)
|
259
|
+
end
|
260
|
+
|
261
|
+
it 'should write file if it exists and --forceoverwrite' do
|
262
|
+
options = { :one => 1, :outputdir => File.dirname(__FILE__), :modulenames => [ "TestModule" ], :forceoverwrite=>true }
|
263
|
+
|
264
|
+
ent = JSON2Ruby::Entity.new("TestEntity", { "one" => JSON2Ruby::RUBYSTRING })
|
265
|
+
ents = { ent.attr_hash => ent }
|
266
|
+
|
267
|
+
JSON2Ruby::Entity.class_variable_set(:@@objs, ents)
|
268
|
+
|
269
|
+
writer = {}
|
270
|
+
expect(writer).to receive(:to_code).with(ent,2,options).and_return("CODE")
|
271
|
+
|
272
|
+
expect(File).to receive(:exists?).with("#{options[:outputdir]}/TestEntity.rb").and_return(true)
|
273
|
+
expect(File).to receive(:write).with("#{options[:outputdir]}/TestEntity.rb", "module TestModule\r\nCODEend\r\n")
|
274
|
+
|
275
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("Done, Generated 1 file")
|
276
|
+
|
277
|
+
JSON2Ruby::CLI.write_files([],writer, options)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
describe '#self.display_entity' do
|
282
|
+
it 'should call puts correctly for entity' do
|
283
|
+
ent = JSON2Ruby::Entity.new("TestEntity")
|
284
|
+
ent.attributes = {
|
285
|
+
"test1" => JSON2Ruby::Attribute.new("testAttr"),
|
286
|
+
"testEntity" => JSON2Ruby::Entity.new("testEntity"),
|
287
|
+
"testCollection" => JSON2Ruby::Collection.new("testCol"),
|
288
|
+
}
|
289
|
+
|
290
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("- TestEntity (Entity - 012345670123456789ABCDEF89ABCDEF)")
|
291
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with(" test1: testAttr")
|
292
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with(" testEntity: testEntity")
|
293
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with(" testCollection: testCol")
|
294
|
+
|
295
|
+
JSON2Ruby::CLI.display_entity("012345670123456789ABCDEF89ABCDEF",ent)
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'should call puts correctly for collection' do
|
299
|
+
ent = JSON2Ruby::Collection.new("TestCollection")
|
300
|
+
ent.ruby_types = {
|
301
|
+
JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING
|
302
|
+
}
|
303
|
+
|
304
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with("- TestCollection (Collection - 0123456701234567222BCDEF89ABCDEF)")
|
305
|
+
expect(JSON2Ruby::CLI).to receive(:puts).with(" (Types): String")
|
306
|
+
|
307
|
+
JSON2Ruby::CLI.display_entity("0123456701234567222BCDEF89ABCDEF",ent)
|
308
|
+
end
|
309
|
+
end
|
310
|
+
end
|