Sha256: 36fd3e15c7f508694e19c729cafce14041636f132c3eb6228009221092a1f812

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

require 'test_helper'

class FeaturesTest < MiniTest::Spec
  module Title
    def title; "Is It A Lie"; end
  end
  module Length
    def length; "2:31"; end
  end

  definition = lambda {
    feature Title
    feature Length

    # exec_context: :decorator, so the readers are called on the Decorator instance (which gets readers from features!).
    property :title, exec_context: :decorator
    property :length, exec_context: :decorator
    property :details do
      property :title, exec_context: :decorator
    end
  }

  let(:song) { OpenStruct.new(:details => Object.new) }

  describe "Module" do
    representer! do
      instance_exec(&definition)
    end

    it { _(song.extend(representer).to_hash).must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
  end


  describe "Decorator" do
    representer!(:decorator => true) do
      instance_exec(&definition)
    end

    it { _(representer.new(song).to_hash).must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
  end
end

class FeatureInclusionOrderTest < MiniTest::Spec
  module Title
    def title
      "I was first!"
    end
  end

  module OverridingTitle
    def title
      "I am number two, " + super
    end
  end

  representer!(decorator: true) do
    feature Title
    feature OverridingTitle

    property :title, exec_context: :decorator

    property :song do
      property :title, exec_context: :decorator
    end
  end

  it do
    _(representer.new(OpenStruct.new(song: Object)).to_hash).must_equal({"title"=>"I am number two, I was first!", "song"=>{"title"=>"I am number two, I was first!"}})
  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/features_test.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/test/features_test.rb
representable-3.2.0 test/features_test.rb
representable-3.1.1 test/features_test.rb
representable-3.1.0 test/features_test.rb