Sha256: b86c0fe4a2ad30c18e30c924c2a0757051a331e06fcfa1c07ac512fb255b5891

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

require 'test_helper'

class FilterPipelineTest < MiniTest::Spec
  let (:block1) { lambda { |input, options| "1: #{input}" } }
  let (:block2) { lambda { |input, options| "2: #{input}" } }

  subject { Representable::Pipeline[block1, block2] }

  it { subject.call("Horowitz", {}).must_equal "2: 1: Horowitz" }
end


class FilterTest < MiniTest::Spec
  representer! do
    property :title

    property :track,
      :parse_filter  => lambda { |input, options| "#{input.downcase},#{options[:doc]}" },
      :render_filter => lambda { |val, options| "#{val.upcase},#{options[:doc]},#{options[:user_options]}" }
  end

  # gets doc and options.
  it {
    song = OpenStruct.new.extend(representer).from_hash("title" => "VULCAN EARS", "track" => "Nine")
    song.title.must_equal "VULCAN EARS"
    song.track.must_equal "nine,{\"title\"=>\"VULCAN EARS\", \"track\"=>\"Nine\"}"
  }

  it { OpenStruct.new("title" => "vulcan ears", "track" => "Nine").extend(representer).to_hash.must_equal( {"title"=>"vulcan ears", "track"=>"NINE,{\"title\"=>\"vulcan ears\"},{}"}) }


  describe "#parse_filter" do
    representer! do
      property :track,
        :parse_filter => [
          lambda { |input, options| "#{input}-1" },
          lambda { |input, options| "#{input}-2" }],
        :render_filter => [
          lambda { |val, options| "#{val}-1" },
          lambda { |val, options| "#{val}-2" }]
    end

    # order matters.
    it { OpenStruct.new.extend(representer).from_hash("track" => "Nine").track.must_equal "Nine-1-2" }
    it { OpenStruct.new("track" => "Nine").extend(representer).to_hash.must_equal({"track"=>"Nine-1-2"}) }
  end
end


# class RenderFilterTest < MiniTest::Spec
#   representer! do
#     property :track, :render_filter => [lambda { |val, options| "#{val}-1" } ]
#     property :track, :render_filter => [lambda { |val, options| "#{val}-2" } ], :inherit => true
#   end

#   it { OpenStruct.new("track" => "Nine").extend(representer).to_hash.must_equal({"track"=>"Nine-1-2"}) }
# end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
representable-2.4.0.rc3 test/filter_test.rb
representable-2.4.0.rc2 test/filter_test.rb
representable-2.4.0.rc1 test/filter_test.rb