Sha256: 2ea025ee4077df49052b9ea708592bf0ef62642543446de8deac98e53c80e4e4

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

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

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/test/serialize_deserialize_test.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/test/serialize_deserialize_test.rb
representable-3.2.0 test/serialize_deserialize_test.rb
representable-3.1.1 test/serialize_deserialize_test.rb
representable-3.1.0 test/serialize_deserialize_test.rb