File: is_representable_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 (77 lines) | stat: -rw-r--r-- 1,999 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'test_helper'

class IsRepresentableTest < BaseTest
  describe "representable: false, extend:" do
    representer!(:inject => :song_representer) do
      property :song,
        :representable => false,
        :extend        => song_representer
    end

    it "does extend but doesn't call #to_hash" do
      Struct.new(:song).new(song = Object.new).extend(representer).
        to_hash.must_equal("song" => song)
      song.must_be_kind_of Representable::Hash
    end
  end


  describe "representable: true, no extend:" do
    representer!(:inject => :song_representer) do
      property :song,
        :representable => true
    end

    it "doesn't extend but calls #to_hash" do
      song = Object.new
      song.instance_eval do
        def to_hash(*)
          1
        end
      end

      Struct.new(:song).new(song).extend(representer).
        to_hash.must_equal("song" => 1)
      song.wont_be_kind_of Representable::Hash
    end
  end

  # TODO: TEST implement for from_hash.

  describe "representable: false, with class:" do
    representer!(:inject => :song_representer) do
      property :song,
        :representable => false, :class => OpenStruct, :extend => song_representer
    end

    it "does extend but doesn't call #from_hash" do
      hit = Struct.new(:song).new.extend(representer).
        from_hash("song" => 1)

      hit.song.must_equal OpenStruct.new
      hit.song.must_be_kind_of Representable::Hash
    end
  end


  describe "representable: true, without extend: but class:" do
    SongReader = Class.new do
      def from_hash(*)
        "Piano?"
      end
    end

    representer!(:inject => :song_representer) do
      property :song,
        :representable => true, :class => SongReader
    end

    it "doesn't extend but calls #from_hash" do
      hit = Struct.new(:song).new.extend(representer).
        from_hash("song" => "Sonata No.2")

      hit.song.must_equal "Piano?"
      hit.song.wont_be_kind_of Representable::Hash
    end
  end
end