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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
require 'test_helper'
require 'json'
module JsonTest
class JSONPublicMethodsTest < Minitest::Spec
#---
# from_json
class BandRepresenter < Representable::Decorator
include Representable::JSON
property :id
property :name
end
let(:json) { '{"id":1,"name":"Rancid"}' }
it { BandRepresenter.new(Band.new).from_json(json)[:id, :name].must_equal [1, "Rancid"] }
it { BandRepresenter.new(Band.new).parse(json)[:id, :name].must_equal [1, "Rancid"] }
#---
# to_json
let(:band) { Band.new(1, "Rancid") }
it { BandRepresenter.new(band).to_json.must_equal json }
it { BandRepresenter.new(band).render.must_equal json }
end
class APITest < Minitest::Spec
Json = Representable::JSON
Def = Representable::Definition
describe "JSON module" do
before do
@Band = Class.new do
include Representable::JSON
property :name
property :label
attr_accessor :name, :label
def initialize(name=nil)
self.name = name if name
end
end
@band = @Band.new
end
describe "#from_json" do
before do
@band = @Band.new
@json = {:name => "Nofx", :label => "NOFX"}.to_json
end
it "parses JSON and assigns properties" do
@band.from_json(@json)
assert_equal ["Nofx", "NOFX"], [@band.name, @band.label]
end
end
describe "#from_hash" do
before do
@band = @Band.new
@hash = {"name" => "Nofx", "label" => "NOFX"}
end
it "receives hash and assigns properties" do
@band.from_hash(@hash)
assert_equal ["Nofx", "NOFX"], [@band.name, @band.label]
end
it "respects :wrap option" do
@band.from_hash({"band" => {"name" => "This Is A Standoff"}}, :wrap => :band)
assert_equal "This Is A Standoff", @band.name
end
it "respects :wrap option over representation_wrap" do
@Band.class_eval do
self.representation_wrap = :group
end
@band.from_hash({"band" => {"name" => "This Is A Standoff"}}, :wrap => :band)
assert_equal "This Is A Standoff", @band.name
end
end
describe "#to_json" do
it "delegates to #to_hash and returns string" do
assert_json "{\"name\":\"Rise Against\"}", @Band.new("Rise Against").to_json
end
end
describe "#to_hash" do
it "returns unwrapped hash" do
hash = @Band.new("Rise Against").to_hash
assert_equal({"name"=>"Rise Against"}, hash)
end
it "respects :wrap option" do
assert_equal({:band=>{"name"=>"NOFX"}}, @Band.new("NOFX").to_hash(:wrap => :band))
end
it "respects :wrap option over representation_wrap" do
@Band.class_eval do
self.representation_wrap = :group
end
assert_equal({:band=>{"name"=>"Rise Against"}}, @Band.new("Rise Against").to_hash(:wrap => :band))
end
end
describe "#build_for" do
it "returns TextBinding" do
assert_kind_of Representable::Hash::Binding, Representable::Hash::Binding.build_for(Def.new(:band))
end
it "returns CollectionBinding" do
assert_kind_of Representable::Hash::Binding::Collection, Representable::Hash::Binding.build_for(Def.new(:band, :collection => true))
end
end
end
describe "DCI" do
module SongRepresenter
include Representable::JSON
property :name
end
module AlbumRepresenter
include Representable::JSON
property :best_song, :class => Song, :extend => SongRepresenter
collection :songs, :class => Song, :extend => SongRepresenter
end
it "allows adding the representer by using #extend" do
module BandRepresenter
include Representable::JSON
property :name
end
civ = Object.new
civ.instance_eval do
def name; "CIV"; end
def name=(v)
@name = v
end
end
civ.extend(BandRepresenter)
assert_json "{\"name\":\"CIV\"}", civ.to_json
end
it "extends contained models when serializing" do
@album = Album.new([Song.new("I Hate My Brain"), mr=Song.new("Mr. Charisma")], mr)
@album.extend(AlbumRepresenter)
assert_json "{\"best_song\":{\"name\":\"Mr. Charisma\"},\"songs\":[{\"name\":\"I Hate My Brain\"},{\"name\":\"Mr. Charisma\"}]}", @album.to_json
end
it "extends contained models when deserializing" do
#@album = Album.new(Song.new("I Hate My Brain"), Song.new("Mr. Charisma"))
@album = Album.new
@album.extend(AlbumRepresenter)
@album.from_json("{\"best_song\":{\"name\":\"Mr. Charisma\"},\"songs\":[{\"name\":\"I Hate My Brain\"},{\"name\":\"Mr. Charisma\"}]}")
assert_equal "Mr. Charisma", @album.best_song.name
end
end
end
class PropertyTest < Minitest::Spec
describe "property :name" do
class Band
include Representable::JSON
property :name
attr_accessor :name
end
it "#from_json creates correct accessors" do
band = Band.new.from_json({:name => "Bombshell Rocks"}.to_json)
assert_equal "Bombshell Rocks", band.name
end
it "#to_json serializes correctly" do
band = Band.new
band.name = "Cigar"
assert_json '{"name":"Cigar"}', band.to_json
end
end
describe ":class => Item" do
class Label
include Representable::JSON
property :name
attr_accessor :name
end
class Album
include Representable::JSON
property :label, :class => Label
attr_accessor :label
end
it "#from_json creates one Item instance" do
album = Album.new.from_json('{"label":{"name":"Fat Wreck"}}')
assert_equal "Fat Wreck", album.label.name
end
it "#to_json serializes" do
label = Label.new; label.name = "Fat Wreck"
album = Album.new; album.label = label
assert_json '{"label":{"name":"Fat Wreck"}}', album.to_json
end
describe ":different_name, :class => Label" do
before do
@Album = Class.new do
include Representable::JSON
property :seller, :class => Label
attr_accessor :seller
end
end
it "#to_xml respects the different name" do
label = Label.new; label.name = "Fat Wreck"
album = @Album.new; album.seller = label
assert_json "{\"seller\":{\"name\":\"Fat Wreck\"}}", album.to_json(:wrap => false)
end
end
end
describe ":as => :songName" do
class Song
include Representable::JSON
property :name, :as => :songName
attr_accessor :name
end
it "respects :as in #from_json" do
song = Song.new.from_json({:songName => "Run To The Hills"}.to_json)
assert_equal "Run To The Hills", song.name
end
it "respects :as in #to_json" do
song = Song.new; song.name = "22 Acacia Avenue"
assert_json '{"songName":"22 Acacia Avenue"}', song.to_json
end
end
end
class CollectionTest < Minitest::Spec
describe "collection :name" do
class CD
include Representable::JSON
collection :songs
attr_accessor :songs
end
it "#from_json creates correct accessors" do
cd = CD.new.from_json({:songs => ["Out in the cold", "Microphone"]}.to_json)
assert_equal ["Out in the cold", "Microphone"], cd.songs
end
it "zzz#to_json serializes correctly" do
cd = CD.new
cd.songs = ["Out in the cold", "Microphone"]
assert_json '{"songs":["Out in the cold","Microphone"]}', cd.to_json
end
end
describe "collection :name, :class => Band" do
class Band
include Representable::JSON
property :name
attr_accessor :name
def initialize(name="")
self.name = name
end
end
class Compilation
include Representable::JSON
collection :bands, :class => Band
attr_accessor :bands
end
describe "#from_json" do
it "pushes collection items to array" do
cd = Compilation.new.from_json({:bands => [
{:name => "Cobra Skulls"},
{:name => "Diesel Boy"}]}.to_json)
assert_equal ["Cobra Skulls", "Diesel Boy"], cd.bands.map(&:name).sort
end
end
it "responds to #to_json" do
cd = Compilation.new
cd.bands = [Band.new("Diesel Boy"), Band.new("Bad Religion")]
assert_json '{"bands":[{"name":"Diesel Boy"},{"name":"Bad Religion"}]}', cd.to_json
end
end
describe ":as => :songList" do
class Songs
include Representable::JSON
collection :tracks, :as => :songList
attr_accessor :tracks
end
it "respects :as in #from_json" do
songs = Songs.new.from_json({:songList => ["Out in the cold", "Microphone"]}.to_json)
assert_equal ["Out in the cold", "Microphone"], songs.tracks
end
it "respects option in #to_json" do
songs = Songs.new
songs.tracks = ["Out in the cold", "Microphone"]
assert_json '{"songList":["Out in the cold","Microphone"]}', songs.to_json
end
end
end
class HashTest < Minitest::Spec
describe "hash :songs" do
representer!(:module => Representable::JSON) do
hash :songs
end
subject { OpenStruct.new.extend(representer) }
it "renders with #to_json" do
subject.songs = {:one => "65", :two => "Emo Boy"}
assert_json "{\"songs\":{\"one\":\"65\",\"two\":\"Emo Boy\"}}", subject.to_json
end
it "parses with #from_json" do
assert_equal({"one" => "65", "two" => ["Emo Boy"]}, subject.from_json("{\"songs\":{\"one\":\"65\",\"two\":[\"Emo Boy\"]}}").songs)
end
end
describe "hash :songs, :class => Song" do
representer!(:module => Representable::JSON) do
hash :songs, :extend => Module.new { include Representable::JSON; property :name }, :class => Song
end
it "renders" do
OpenStruct.new(:songs => {"7" => Song.new("Contemplation")}).extend(representer).to_hash.must_equal("songs"=>{"7"=>{"name"=>"Contemplation"}})
end
describe "parsing" do
subject { OpenStruct.new.extend(representer) }
let(:hsh) { {"7"=>{"name"=>"Contemplation"}} }
it "parses incoming hash" do
subject.from_hash("songs"=>hsh).songs.must_equal({"7"=>Song.new("Contemplation")})
end
it "doesn't modify the incoming hash" do
subject.from_hash("songs"=> incoming_hash = hsh.dup)
hsh.must_equal incoming_hash
end
end
end
end
end
|