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
|
require 'test_helper'
class YamlPublicMethodsTest < Minitest::Spec
#---
# from_yaml
class BandRepresenter < Representable::Decorator
include Representable::YAML
property :id
property :name
end
let(:data) { %{---
id: 1
name: Rancid
} }
it { BandRepresenter.new(Band.new).from_yaml(data)[:id, :name].must_equal [1, "Rancid"] }
it { BandRepresenter.new(Band.new).parse(data)[:id, :name].must_equal [1, "Rancid"] }
#---
# to_yaml
let(:band) { Band.new("1", "Rancid") }
it { BandRepresenter.new(band).to_yaml.must_equal data }
it { BandRepresenter.new(band).render.must_equal data }
end
class YamlTest < Minitest::Spec
def self.yaml_representer(&block)
Module.new do
include Representable::YAML
instance_exec(&block)
end
end
def yaml_representer(&block)
self.class.yaml_representer(&block)
end
describe "property" do
let(:yaml) { yaml_representer do property :best_song end }
let(:album) { Album.new.tap do |album|
album.best_song = "Liar"
end }
describe "#to_yaml" do
it "renders plain property" do
album.extend(yaml).to_yaml.must_equal(
"---
best_song: Liar
")
end
it "always renders values into strings" do
Album.new.tap { |a| a.best_song = 8675309 }.extend(yaml).to_yaml.must_equal(
"---
best_song: 8675309
"
)
end
end
describe "#from_yaml" do
it "parses plain property" do
album.extend(yaml).from_yaml(
"---
best_song: This Song Is Recycled
").best_song.must_equal "This Song Is Recycled"
end
end
describe "with :class and :extend" do
yaml_song = yaml_representer do property :name end
let(:yaml_album) { Module.new do
include Representable::YAML
property :best_song, :extend => yaml_song, :class => Song
end }
let(:album) { Album.new.tap do |album|
album.best_song = Song.new("Liar")
end }
describe "#to_yaml" do
it "renders embedded typed property" do
album.extend(yaml_album).to_yaml.must_equal "---
best_song:
name: Liar
"
end
end
describe "#from_yaml" do
it "parses embedded typed property" do
album.extend(yaml_album).from_yaml("---
best_song:
name: Go With Me
").must_equal Album.new(nil,Song.new("Go With Me"))
end
end
end
end
describe "collection" do
let(:yaml) { yaml_representer do collection :songs end }
let(:album) { Album.new.tap do |album|
album.songs = ["Jackhammer", "Terrible Man"]
end }
describe "#to_yaml" do
it "renders a block style list per default" do
album.extend(yaml).to_yaml.must_equal "---
songs:
- Jackhammer
- Terrible Man
"
end
it "renders a flow style list when :style => :flow set" do
yaml = yaml_representer { collection :songs, :style => :flow }
album.extend(yaml).to_yaml.must_equal "---
songs: [Jackhammer, Terrible Man]
"
end
end
describe "#from_yaml" do
it "parses a block style list" do
album.extend(yaml).from_yaml("---
songs:
- Off Key Melody
- Sinking").must_equal Album.new(["Off Key Melody", "Sinking"])
end
it "parses a flow style list" do
album.extend(yaml).from_yaml("---
songs: [Off Key Melody, Sinking]").must_equal Album.new(["Off Key Melody", "Sinking"])
end
end
describe "with :class and :extend" do
let(:yaml_album) { Module.new do
include Representable::YAML
collection :songs, :class => Song do
property :name
property :track
end
end }
let(:album) { Album.new([Song.new("Liar", 1), Song.new("What I Know", 2)]) }
describe "#to_yaml" do
it "renders collection of typed property" do
album.extend(yaml_album).to_yaml.must_equal "---
songs:
- name: Liar
track: 1
- name: What I Know
track: 2
"
end
end
describe "#from_yaml" do
it "parses collection of typed property" do
album.extend(yaml_album).from_yaml("---
songs:
- name: One Shot Deal
track: 4
- name: Three Way Dance
track: 5").must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
end
end
end
end
end
|