@@ -17,18 +17,28 @@ def test_inspect_class
17
17
assert_match ( /^Topic\( id: integer, title: string/ , Topic . inspect )
18
18
end
19
19
20
- def test_inspect_instance
20
+ def test_inspect_instance_includes_just_id_by_default
21
21
topic = topics ( :first )
22
- assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "[email protected] ", written_on: "#{ topic . written_on . to_fs ( :inspect ) } ", bonus_time: "#{ topic . bonus_time . to_fs ( :inspect ) } ", last_read: "#{ topic . last_read . to_fs ( :inspect ) } ", content: "Have a nice day", important: nil, binary_content: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{ topic . created_at . to_fs ( :inspect ) } ", updated_at: "#{ topic . updated_at . to_fs ( :inspect ) } ">) , topic . inspect
22
+ assert_equal %(#<Topic id: 1>) , topic . inspect
23
+ end
24
+
25
+ def test_inspect_includes_attributes_from_attributes_for_inspect
26
+ Topic . with ( attributes_for_inspect : [ :id , :title , :author_name ] ) do
27
+ topic = topics ( :first )
28
+
29
+ assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David">) , topic . inspect
30
+ end
23
31
end
24
32
25
33
def test_inspect_instance_with_lambda_date_formatter
26
34
before = Time ::DATE_FORMATS [ :inspect ]
27
- Time ::DATE_FORMATS [ :inspect ] = -> ( date ) { "my_format" }
28
- topic = topics ( :first )
29
35
30
- assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "[email protected] ", written_on: "my_format", bonus_time: "my_format", last_read: "2004-04-15", content: "Have a nice day", important: nil, binary_content: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "my_format", updated_at: "my_format">) , topic . inspect
36
+ Topic . with ( attributes_for_inspect : [ :id , :last_read ] ) do
37
+ Time ::DATE_FORMATS [ :inspect ] = -> ( date ) { "my_format" }
38
+ topic = topics ( :first )
31
39
40
+ assert_equal %(#<Topic id: 1, last_read: "2004-04-15">) , topic . inspect
41
+ end
32
42
ensure
33
43
Time ::DATE_FORMATS [ :inspect ] = before
34
44
end
@@ -38,8 +48,10 @@ def test_inspect_new_instance
38
48
end
39
49
40
50
def test_inspect_limited_select_instance
41
- assert_equal %(#<Topic id: 1>) , Topic . all . merge! ( select : "id" , where : "id = 1" ) . first . inspect
42
- assert_equal %(#<Topic id: 1, title: "The First Topic">) , Topic . all . merge! ( select : "id, title" , where : "id = 1" ) . first . inspect
51
+ Topic . with ( attributes_for_inspect : [ :id , :title ] ) do
52
+ assert_equal %(#<Topic id: 1>) , Topic . all . merge! ( select : "id" , where : "id = 1" ) . first . inspect
53
+ assert_equal %(#<Topic id: 1, title: "The First Topic">) , Topic . all . merge! ( select : "id, title" , where : "id = 1" ) . first . inspect
54
+ end
43
55
end
44
56
45
57
def test_inspect_instance_with_non_primary_key_id_attribute
@@ -51,36 +63,35 @@ def test_inspect_class_without_table
51
63
assert_equal "NonExistentTable(Table doesn't exist)" , NonExistentTable . inspect
52
64
end
53
65
66
+ def test_inspect_with_attributes_for_inspect_all_lists_all_attributes
67
+ Topic . with ( attributes_for_inspect : :all ) do
68
+ topic = topics ( :first )
69
+
70
+ assert_equal <<~STRING . squish , topic . inspect
71
+ #<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "[email protected] ", written_on: "#{ topic . written_on . to_fs ( :inspect ) } ", bonus_time: "#{ topic . bonus_time . to_fs ( :inspect ) } ", last_read: "#{ topic . last_read . to_fs ( :inspect ) } ", content: "Have a nice day", important: nil, binary_content: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{ topic . created_at . to_fs ( :inspect ) } ", updated_at: "#{ topic . updated_at . to_fs ( :inspect ) } ">
72
+ STRING
73
+ end
74
+ end
75
+
54
76
def test_inspect_relation_with_virtual_field
55
77
relation = Topic . limit ( 1 ) . select ( "1 as virtual_field" )
56
- assert_match ( /virtual_field: 1/ , relation . inspect )
78
+ assert_match ( /virtual_field: 1/ , relation . first . full_inspect )
79
+ end
80
+
81
+ def test_full_inspect_lists_all_attributes
82
+ topic = topics ( :first )
83
+
84
+ assert_equal <<~STRING . squish , topic . full_inspect
85
+ #<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "[email protected] ", written_on: "#{ topic . written_on . to_fs ( :inspect ) } ", bonus_time: "#{ topic . bonus_time . to_fs ( :inspect ) } ", last_read: "#{ topic . last_read . to_fs ( :inspect ) } ", content: "Have a nice day", important: nil, binary_content: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{ topic . created_at . to_fs ( :inspect ) } ", updated_at: "#{ topic . updated_at . to_fs ( :inspect ) } ">
86
+ STRING
57
87
end
58
88
59
89
def test_pretty_print_new
60
90
topic = Topic . new
61
91
actual = +""
62
92
PP . pp ( topic , StringIO . new ( actual ) )
63
93
expected = <<~PRETTY
64
- #<Topic:0xXXXXXX
65
- id: nil,
66
- title: nil,
67
- author_name: nil,
68
- author_email_address: "[email protected] ",
69
- written_on: nil,
70
- bonus_time: nil,
71
- last_read: nil,
72
- content: nil,
73
- important: nil,
74
- binary_content: nil,
75
- approved: true,
76
- replies_count: 0,
77
- unique_replies_count: 0,
78
- parent_id: nil,
79
- parent_title: nil,
80
- type: nil,
81
- group: nil,
82
- created_at: nil,
83
- updated_at: nil>
94
+ #<Topic:0xXXXXXX id: nil>
84
95
PRETTY
85
96
assert actual . start_with? ( expected . split ( "XXXXXX" ) . first )
86
97
assert actual . end_with? ( expected . split ( "XXXXXX" ) . last )
@@ -91,30 +102,42 @@ def test_pretty_print_persisted
91
102
actual = +""
92
103
PP . pp ( topic , StringIO . new ( actual ) )
93
104
expected = <<~PRETTY
94
- #<Topic:0x\\ w+
95
- id: 1,
96
- title: "The First Topic",
97
- author_name: "David",
98
- author_email_address: "[email protected] ",
99
- written_on: 2003-07-16 14:28:11(?:\. 2233)? UTC,
100
- bonus_time: 2000-01-01 14:28:00 UTC,
101
- last_read: Thu, 15 Apr 2004,
102
- content: "Have a nice day",
103
- important: nil,
104
- binary_content: nil,
105
- approved: false,
106
- replies_count: 1,
107
- unique_replies_count: 0,
108
- parent_id: nil,
109
- parent_title: nil,
110
- type: nil,
111
- group: nil,
112
- created_at: [^,]+,
113
- updated_at: [^,>]+>
105
+ #<Topic:0x\\ w+ id: 1>
114
106
PRETTY
115
107
assert_match ( /\A #{ expected } \z / , actual )
116
108
end
117
109
110
+ def test_pretty_print_full
111
+ Topic . with ( attributes_for_inspect : :all ) do
112
+ topic = topics ( :first )
113
+ actual = +""
114
+ PP . pp ( topic , StringIO . new ( actual ) )
115
+ expected = <<~PRETTY
116
+ #<Topic:0x\\ w+
117
+ id: 1,
118
+ title: "The First Topic",
119
+ author_name: "David",
120
+ author_email_address: "[email protected] ",
121
+ written_on: 2003-07-16 14:28:11(?:\. 2233)? UTC,
122
+ bonus_time: 2000-01-01 14:28:00 UTC,
123
+ last_read: Thu, 15 Apr 2004,
124
+ content: "Have a nice day",
125
+ important: nil,
126
+ binary_content: nil,
127
+ approved: false,
128
+ replies_count: 1,
129
+ unique_replies_count: 0,
130
+ parent_id: nil,
131
+ parent_title: nil,
132
+ type: nil,
133
+ group: nil,
134
+ created_at: [^,]+,
135
+ updated_at: [^,>]+>
136
+ PRETTY
137
+ assert_match ( /\A #{ expected } \z / , actual )
138
+ end
139
+ end
140
+
118
141
def test_pretty_print_uninitialized
119
142
topic = Topic . allocate
120
143
actual = +""
0 commit comments