File: filter_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 (57 lines) | stat: -rw-r--r-- 2,017 bytes parent folder | download
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
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[: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