File: reader_writer_test.rb

package info (click to toggle)
ruby-representable 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 952 kB
  • sloc: ruby: 6,495; makefile: 4
file content (19 lines) | stat: -rw-r--r-- 672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'test_helper'

class ReaderWriterTest < BaseTest
  representer! do
    property :name,
      :writer => lambda { |options| options[:doc]["title"] = "#{options[:user_options][:nr]}) #{options[:input]}" },
      :reader => lambda { |options| self.name = options[:doc]["title"].split(") ").last }
  end

  subject { OpenStruct.new(:name => "Disorder And Disarray").extend(representer) }

  it "uses :writer when rendering" do
    _(subject.to_hash(user_options: {nr: 14})).must_equal({"title" => "14) Disorder And Disarray"})
  end

  it "uses :reader when parsing" do
    _(subject.from_hash({"title" => "15) The Wars End"}).name).must_equal "The Wars End"
  end
end