Add use_cbor_discrimination tests.
parent
4d200998c1
commit
c9cb066c6e
|
@ -0,0 +1,65 @@
|
|||
require "./spec_helper"
|
||||
|
||||
class Game
|
||||
include CBOR::Serializable
|
||||
|
||||
abstract class Player
|
||||
include CBOR::Serializable
|
||||
|
||||
@[CBOR::Field(key: "hp")]
|
||||
property health_points : Int32 = 100
|
||||
|
||||
@[CBOR::Field(key: "dp")]
|
||||
property defense_points : Int32 = 5
|
||||
|
||||
use_cbor_discriminator "type", {
|
||||
magician: Magician,
|
||||
warrior: Warrior
|
||||
}
|
||||
def initialize
|
||||
end
|
||||
end
|
||||
|
||||
class Magician < Player
|
||||
property wand_level : Int32 = 1
|
||||
def initialize
|
||||
@type = "magician"
|
||||
end
|
||||
end
|
||||
|
||||
class Warrior < Player
|
||||
def initialize
|
||||
@defense_points = 20
|
||||
@type = "warrior"
|
||||
end
|
||||
end
|
||||
|
||||
property players : Array(Magician | Warrior)
|
||||
|
||||
def initialize
|
||||
@players = [] of (Magician | Warrior)
|
||||
end
|
||||
end
|
||||
|
||||
describe CBOR do
|
||||
describe "Complex object representations" do
|
||||
it "Game#to_cbor with use_cbor_discriminator" do
|
||||
game = Game.new
|
||||
magician = Game::Magician.new
|
||||
magician.wand_level = 5
|
||||
game.players << magician
|
||||
game.players << Game::Warrior.new
|
||||
game.to_cbor.hexstring.should eq "a167706c617965727382a46268701864626470056a77616e645f6c6576656c056474797065686d6167696369616ea362687018646264701464747970656777617272696f72"
|
||||
end
|
||||
|
||||
it "Game#from_cbor with use_cbor_discriminator" do
|
||||
game = Game.new
|
||||
magician = Game::Magician.new
|
||||
magician.wand_level = 5
|
||||
game.players << magician
|
||||
game.players << Game::Warrior.new
|
||||
new_game = Game.from_cbor game.to_cbor
|
||||
new_game.to_cbor.hexstring.should eq "a167706c617965727382a46268701864626470056a77616e645f6c6576656c056474797065686d6167696369616ea362687018646264701464747970656777617272696f72"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue