/
representer_spec.rb
179 lines (139 loc) · 4.28 KB
/
representer_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require 'spec_helper'
module Vintner
describe Representer do
it "should declare a simple property" do
class Dummy
include Vintner::Representer
property :title
end
Dummy.properties.should include(:title)
end
it "should declare a property with a getter" do
class Dummy
include Vintner::Representer
property :title do
get do |model|
model.formatted_title
end
end
end
Dummy.properties[:title].getter_defined?.should be(true)
end
it "should declare a property with a setter" do
class Dummy
include Vintner::Representer
property :title do
set do |model, value|
model.formatted_title = value
end
end
end
Dummy.properties[:title].setter_defined?.should be(true)
end
describe "Representation" do
before :each do
class Dummy
include Vintner::Representer
property :title do
get do |model|
model.formatted_title
end
set do |model, value|
model.formatted_title = value
end
end
representation do |json|
json.meta do |meta|
meta.property :title
end
end
end
end
it "should declare a representation" do
Dummy.representation.should_not be_nil
end
it "should export json" do
hash = {meta:{title:"some title"}}
model = Struct.new(:formatted_title).new("some title")
Dummy.export(model).should ==(hash.to_json)
end
it "should import json" do
hash = {meta:{title:"some title"}}
model = Struct.new(:formatted_title).new
Dummy.import(model, hash.to_json).formatted_title.should ==("some title")
end
end
describe "Complex representation" do
before :each do
class Dummy
include Vintner::Representer
property :title do
get do |model|
model.formatted_title
end
set do |model, value|
model.formatted_title = value
end
end
property :position do
get do |model|
{:x => model.position_x, :y => model.position_y}
end
set do |model, value|
model.position_x = value['x']
model.position_y = value['y']
end
end
representation do |json|
json.meta do |meta|
meta.property :title
end
json.property :position
end
end
end
it "should export json" do
hash = {:meta=>{:title => "test"}, :position => {:x => 4, :y => 5}}
model = Struct.new(:formatted_title, :position_x, :position_y).new("test", 4,5)
Dummy.export(model).should ==(hash.to_json)
end
it "should import json" do
hash = {:meta=>{:title => "test"}, :position => {:x => 4, :y => 5}}
model = Struct.new(:formatted_title, :position_x, :position_y).new
Dummy.import(model, hash.to_json).formatted_title.should ==("test")
Dummy.import(model, hash.to_json).position_x.should ==(4)
Dummy.import(model, hash.to_json).position_y.should ==(5)
end
end
describe "Immediate values" do
before :each do
@hash = {:meta=>{:version => 4, :title => "test", :stuff => "stuff"}}
@model = Struct.new(:formatted_title, :stuff).new("test", "stuff")
class Dummy
include Vintner::Representer
property :title do
get do |model|
model.formatted_title
end
set do |model, title|
model.formatted_title = title
end
end
representation do |json, model|
json.meta do |meta|
meta.version 4
meta.property :title
meta.stuff model.stuff
end
end
end
end
it "should export immediate values " do
Dummy.export(@model).should ==(@hash.to_json)
end
it "should ignore it when importing" do
Dummy.import(@model, @hash.to_json).formatted_title.should ==('test')
end
end
end
end