File: serialize_deserialize_test.rb

package info (click to toggle)
ruby-representable 3.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 6,432; makefile: 3
file content (33 lines) | stat: -rw-r--r-- 1,061 bytes parent folder | download | duplicates (3)
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
require 'test_helper'

class SerializeDeserializeTest < BaseTest
  subject { Struct.new(:song).new.extend(representer) }

  describe "deserialize" do
    representer! do
      property :song,
        :instance => lambda { |options| options[:input].to_s.upcase },
        :prepare  => lambda { |options| options[:input] },
        :deserialize => lambda { |options|
          "#{options[:input]} #{options[:fragment]} #{options[:user_options].inspect}"
        }
    end

    it { subject.from_hash({"song" => Object}, user_options: {volume: 9}).song.must_equal "OBJECT Object {:volume=>9}" }
  end

  describe "serialize" do
    representer! do
      property :song,
        :representable => true,
        :prepare  => lambda { |options| options[:fragment] },
        :serialize => lambda { |options|
          "#{options[:input]} #{options[:user_options].inspect}"
        }
    end

    before { subject.song = "Arrested In Shanghai" }

    it { subject.to_hash(user_options: {volume: 9}).must_equal({"song"=>"Arrested In Shanghai {:volume=>9}"}) }
  end
end