File: represent_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 (88 lines) | stat: -rw-r--r-- 2,803 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'test_helper'

class RepresentTest < Minitest::Spec
  let(:songs) { [song, Song.new("Can't Take Them All")] }
  let(:song) { Song.new("Days Go By") }

  for_formats(
    :hash => [Representable::Hash, out=[{"name" => "Days Go By"}, {"name"=>"Can't Take Them All"}], out],
    # :json => [Representable::JSON, out="[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]", out],
    # :xml  => [Representable::XML,  out="<a><song></song><song></song></a>", out]
  ) do |format, mod, output, input|

    # Representer.represents detects collection.
    describe "Module#to_/from_#{format}" do
      let(:format) { format }

      let(:representer) {
        Module.new do
          include mod
          property :name

          collection_representer :class => Song # TODOOOOOOOOOOOO: test without Song and fix THIS FUCKINGNoMethodError: undefined method `name=' for {"name"=>"Days Go By"}:Hash ERROR!!!!!!!!!!!!!!!
        end
      }

      it { render(representer.represent(songs)).must_equal_document output }
      it { parse(representer.represent([]), input).must_equal songs }
    end

    # Decorator.represents detects collection.
    describe "Decorator#to_/from_#{format}" do
      let(:format) { format }
      let(:representer) {
        Class.new(Representable::Decorator) do
          include mod
          property :name

          collection_representer :class => Song
        end
      }

      it { render(representer.represent(songs)).must_equal_document output }
      it("ficken") { parse(representer.represent([]), input).must_equal songs }
    end
  end


  for_formats(
    :hash => [Representable::Hash, out={"name" => "Days Go By"}, out],
    :json => [Representable::JSON, out="{\"name\":\"Days Go By\"}", out],
    # :xml  => [Representable::XML,  out="<a><song></song><song></song></a>", out]
  ) do |format, mod, output, input|

    # Representer.represents detects singular.
    describe "Module#to_/from_#{format}" do
      let(:format) { format }

      let(:representer) {
        Module.new do
          include mod
          property :name

          collection_representer :class => Song
        end
      }

      it { render(representer.represent(song)).must_equal_document output }
      it { parse(representer.represent(Song.new), input).must_equal song }
    end


    # Decorator.represents detects singular.
    describe "Decorator#to_/from_#{format}" do
      let(:format) { format }
      let(:representer) {
        Class.new(Representable::Decorator) do
          include mod
          property :name

          collection_representer :class => Song
        end
      }

      it { render(representer.represent(song)).must_equal_document output }
      it { parse(representer.represent(Song.new), input).must_equal song }
    end
  end
end