@@ -24,9 +24,9 @@ class ObjectTest < MiniTest::Spec
2424 it do
2525 representer . prepare ( target ) . from_object ( source )
2626
27- assert_equal target . title , "The King Is Dead"
28- assert_equal target . album . name , "RUINER"
29- assert_equal target . album . songs [ 0 ] . title , "IN VINO VERITAS II"
27+ assert_equal "The King Is Dead" , target . title
28+ assert_equal "RUINER" , target . album . name
29+ assert_equal "IN VINO VERITAS II" , target . album . songs [ 0 ] . title
3030 end
3131
3232 # ignore nested object when nil
@@ -51,90 +51,10 @@ class ObjectTest < MiniTest::Spec
5151 end
5252 end
5353
54- # it do
55- # representer.prepare(source).to_object
56- # _(source.album.name).must_equal "Live"
57- # _(source.album.songs[0].title).must_equal 1
58- # end
59- end
60- end
61-
62- class ObjectPublicMethodsTest < Minitest ::Spec
63- Song = Struct . new ( :title , :album )
64- Album = Struct . new ( :id , :name , :songs , :free_concert_ticket_promo_code )
65- class AlbumRepresenter < Representable ::Decorator
66- include Representable ::Object
67- property :id
68- property :name , getter : -> ( *) { name . lstrip . strip }
69- property :cover_png , getter : -> ( options :, **) { options [ :cover_png ] }
70- collection :songs do
71- property :title , getter : -> ( *) { title . upcase }
72- property :album , getter : -> ( *) { album . upcase }
54+ it do
55+ representer . prepare ( source ) . to_object
56+ _ ( source . album . name ) . must_equal "Live"
57+ _ ( source . album . songs [ 0 ] . title ) . must_equal 1
7358 end
7459 end
75-
76- #---
77- # to_object
78- let ( :album ) { Album . new ( 1 , " Rancid " , [ Song . new ( "In Vino Veritas II" , "Rancid" ) , Song . new ( "The King Is Dead" , "Rancid" ) ] , "S3KR3TK0D3" ) }
79- let ( :cover_png ) { "example.com/cover.png" }
80- it do
81- represented = AlbumRepresenter . new ( album ) . to_object ( cover_png : cover_png )
82- assert_equal represented . id , album . id
83- refute_equal represented . name , album . name
84- assert_equal represented . name , album . name . lstrip . strip
85- refute_equal represented . songs [ 0 ] . title , album . songs [ 0 ] . title
86- assert_equal represented . songs [ 0 ] . title , album . songs [ 0 ] . title . upcase
87-
88- assert_respond_to album , :free_concert_ticket_promo_code
89- refute_respond_to represented , :free_concert_ticket_promo_code
90-
91- assert_equal represented . cover_png , cover_png
92- end
93-
94- it do
95- represented = AlbumRepresenter . new ( album ) . to_object ( cover_png : cover_png )
96- assert_equal represented . id , album . id
97- refute_equal represented . name , album . name
98- assert_equal represented . name , album . name . lstrip . strip
99- refute_equal represented . songs [ 0 ] . title , album . songs [ 0 ] . title
100- assert_equal represented . songs [ 0 ] . title , album . songs [ 0 ] . title . upcase
101-
102- assert_respond_to album , :free_concert_ticket_promo_code
103- refute_respond_to represented , :free_concert_ticket_promo_code
104-
105- assert_equal represented . cover_png , cover_png
106- end
107-
108- let ( :albums ) do [
109- Album . new ( 1 , "Rancid" , [ Song . new ( "In Vino Veritas II" , "Rancid" ) , Song . new ( "The King Is Dead" , "Rancid" ) ] , "S3KR3TK0D3" ) ,
110- Album . new ( 2 , "Punk powerhouse" , [ Song . new ( "Hard Outside The Box" , "Punk powerhous" ) , Song . new ( "Wonderful Noise" , "Punk powerhous" ) ] , "S3KR3TK0D3" ) ,
111- Album . new ( 3 , "Into the Beyond" , [ Song . new ( "Rhythm of the night" , "Into the Beyond" ) , Song . new ( "I'm blue" , "Into the Beyond" ) ] , "S3KR3TK0D3" ) ,
112- ]
113- end
114-
115- it do
116- represented = AlbumRepresenter . for_collection . new ( albums ) . to_object ( cover_png : cover_png )
117- assert_equal represented . size , albums . size
118- assert_respond_to albums [ 0 ] , :free_concert_ticket_promo_code
119- refute_respond_to represented [ 0 ] , :free_concert_ticket_promo_code
120- assert_equal represented [ 0 ] . cover_png , cover_png
121- assert_equal represented [ 0 ] . class . object_id , represented [ 1 ] . class . object_id
122- end
123-
124- let ( :wrapper ) { "cool_album" }
125- let ( :second_wrapper ) { "magnificent_album" }
126- it do
127- represented_array = AlbumRepresenter . for_collection . new ( albums ) . to_object ( wrap : wrapper )
128- represented_object = AlbumRepresenter . new ( album ) . to_object ( wrap : second_wrapper )
129-
130- assert_respond_to represented_array , wrapper
131-
132- assert_respond_to represented_array . send ( wrapper ) [ 0 ] , wrapper
133- first_song_title_represented = represented_array . send ( wrapper ) [ 0 ] . send ( wrapper ) . songs [ 0 ] . title
134- first_song_title_original = albums [ 0 ] . songs [ 0 ] . title
135- assert_equal first_song_title_represented , first_song_title_original . upcase
136-
137- assert_equal represented_array . send ( wrapper ) [ 0 ] . class . object_id , represented_array . send ( wrapper ) [ 1 ] . class . object_id # wrapper struct class is the same for collection
138- refute_equal represented_array . send ( wrapper ) [ 0 ] . class . object_id , represented_object . class . object_id # wrapper structs classes are different for different wrappers
139- end
14060end
0 commit comments