@@ -32,8 +32,9 @@ defmodule ExUnit.Formatter do
32
32
33
33
import Exception , only: [ format_stacktrace_entry: 1 ]
34
34
35
- # Width of error counter field
36
- @ prefix_width 5
35
+ @ label_padding " "
36
+ @ counter_padding " "
37
+ @ inspect_padding @ counter_padding <> @ label_padding
37
38
38
39
@ doc """
39
40
Formats time taken running the test suite.
@@ -99,40 +100,42 @@ defmodule ExUnit.Formatter do
99
100
@ doc """
100
101
Receives a test and formats its failure.
101
102
"""
102
- def format_test_failure ( test_case , test , { kind , reason , stack } , counter , terminal ) do
103
- test_info ( with_counter ( counter , "#{ test } (#{ inspect test_case } )" ) , terminal )
104
- <> format_kind_reason ( kind , reason , terminal )
105
- <> format_stacktrace ( stack , test_case , test , terminal )
103
+ def format_test_failure ( test_case , test , { kind , reason , stack } , counter , width , formatter ) do
104
+ test_info ( with_counter ( counter , "#{ test } (#{ inspect test_case } )" ) , formatter )
105
+ <> format_kind_reason ( kind , reason , width , formatter )
106
+ <> format_stacktrace ( stack , test_case , test , formatter )
106
107
end
107
108
108
109
@ doc """
109
110
Receives a test case and formats its failure.
110
111
"""
111
- def format_test_case_failure ( test_case , { kind , reason , stacktrace } , counter , terminal ) do
112
- test_case_info ( with_counter ( counter , "#{ inspect test_case } : " ) , terminal )
113
- <> format_kind_reason ( kind , reason , terminal )
114
- <> format_stacktrace ( stacktrace , test_case , nil , terminal )
112
+ def format_test_case_failure ( test_case , { kind , reason , stacktrace } , counter , width , formatter ) do
113
+ test_case_info ( with_counter ( counter , "#{ inspect test_case } : " ) , formatter )
114
+ <> format_kind_reason ( kind , reason , width , formatter )
115
+ <> format_stacktrace ( stacktrace , test_case , nil , formatter )
115
116
end
116
117
117
- defp format_kind_reason ( :error , ExUnit.AssertionError [ ] = record , terminal ) do
118
+ defp format_kind_reason ( :error , ExUnit.AssertionError [ ] = record , width , formatter ) do
119
+ width = if width == :infinity , do: width , else: width - byte_size ( @ inspect_padding )
120
+
118
121
fields =
119
- [ note: if_value ( record . message , & format_message ( & 1 , terminal ) ) ,
120
- code: record . expr ,
121
- lhs: if_value ( record . left , & inspect / 1 ) ,
122
- rhs: if_value ( record . right , & inspect / 1 ) ]
122
+ [ note: if_value ( record . message , & format_message ( & 1 , formatter ) ) ,
123
+ code: if_value ( record . expr , & macro_multiline ( & 1 , width ) ) ,
124
+ lhs: if_value ( record . left , & inspect_multiline ( & 1 , width ) ) ,
125
+ rhs: if_value ( record . right , & inspect_multiline ( & 1 , width ) ) ]
123
126
124
127
fields
125
128
|> filter_interesting_fields
126
- |> format_each_reason ( terminal )
127
- |> make_into_lines
129
+ |> format_each_reason ( formatter )
130
+ |> make_into_lines ( @ counter_padding )
128
131
end
129
132
130
- defp format_kind_reason ( :error , exception , terminal ) do
131
- error_info "** (#{ inspect exception . __record__ ( :name ) } ) #{ exception . message } " , terminal
133
+ defp format_kind_reason ( :error , exception , _width , formatter ) do
134
+ error_info "** (#{ inspect exception . __record__ ( :name ) } ) #{ exception . message } " , formatter
132
135
end
133
136
134
- defp format_kind_reason ( kind , reason , terminal ) do
135
- error_info "** (#{ kind } ) #{ inspect ( reason ) } " , terminal
137
+ defp format_kind_reason ( kind , reason , _width , formatter ) do
138
+ error_info "** (#{ kind } ) #{ inspect ( reason ) } " , formatter
136
139
end
137
140
138
141
defp filter_interesting_fields ( fields ) do
@@ -141,15 +144,9 @@ defmodule ExUnit.Formatter do
141
144
end )
142
145
end
143
146
144
- defp format_each_reason ( reasons , terminal ) do
145
- label_length =
146
- reasons
147
- |> Enum . map ( fn { label , _ } -> String . length ( atom_to_binary ( label ) ) end )
148
- |> Enum . max
149
- |> Kernel . + ( 2 )
150
-
147
+ defp format_each_reason ( reasons , formatter ) do
151
148
Enum . map ( reasons , fn { label , value } ->
152
- format_label ( label , label_length , terminal ) <> value
149
+ format_label ( label , formatter ) <> value
153
150
end )
154
151
end
155
152
@@ -161,20 +158,30 @@ defmodule ExUnit.Formatter do
161
158
end
162
159
end
163
160
164
- defp format_label ( :note , _length , _terminal ) do
161
+ defp format_label ( :note , _formatter ) do
165
162
""
166
163
end
167
164
168
- defp format_label ( label , length , terminal ) do
169
- terminal . ( :error_info , String . ljust ( "#{ label } :" , length ) )
165
+ defp format_label ( label , formatter ) do
166
+ formatter . ( :error_info , String . ljust ( "#{ label } :" , byte_size ( @ label_padding ) ) )
167
+ end
168
+
169
+ defp format_message ( value , formatter ) do
170
+ formatter . ( :error_info , value )
171
+ end
172
+
173
+ defp macro_multiline ( expr , _width ) do
174
+ expr |> Macro . to_string
170
175
end
171
176
172
- defp format_message ( value , terminal ) do
173
- terminal . ( :error_info , value )
177
+ defp inspect_multiline ( expr , width ) do
178
+ expr
179
+ |> inspect ( pretty: true , width: width )
180
+ |> String . replace ( "\n " , "\n " <> @ inspect_padding )
174
181
end
175
182
176
- defp make_into_lines ( reasons ) do
177
- " " <> Enum . join ( reasons , "\n " ) <> "\n "
183
+ defp make_into_lines ( reasons , padding ) do
184
+ padding <> Enum . join ( reasons , "\n " <> padding ) <> "\n "
178
185
end
179
186
180
187
defp format_stacktrace ( [ { test_case , test , _ , location } | _ ] , test_case , test , color ) do
@@ -195,17 +202,17 @@ defmodule ExUnit.Formatter do
195
202
defp with_counter ( counter , msg ) do "#{ counter } ) #{ msg } " end
196
203
197
204
defp test_case_info ( msg , nil ) , do: msg <> "failure on setup_all/teardown_all callback, tests invalidated\n "
198
- defp test_case_info ( msg , terminal ) , do: test_case_info ( terminal . ( :test_case_info , msg ) , nil )
205
+ defp test_case_info ( msg , formatter ) , do: test_case_info ( formatter . ( :test_case_info , msg ) , nil )
199
206
200
207
defp test_info ( msg , nil ) , do: msg <> "\n "
201
- defp test_info ( msg , terminal ) , do: test_info ( terminal . ( :test_info , msg ) , nil )
208
+ defp test_info ( msg , formatter ) , do: test_info ( formatter . ( :test_info , msg ) , nil )
202
209
203
210
defp error_info ( msg , nil ) , do: " " <> msg <> "\n "
204
- defp error_info ( msg , terminal ) , do: error_info ( terminal . ( :error_info , msg ) , nil )
211
+ defp error_info ( msg , formatter ) , do: error_info ( formatter . ( :error_info , msg ) , nil )
205
212
206
213
defp location_info ( msg , nil ) , do: " " <> msg <> "\n "
207
- defp location_info ( msg , terminal ) , do: location_info ( terminal . ( :location_info , msg ) , nil )
214
+ defp location_info ( msg , formatter ) , do: location_info ( formatter . ( :location_info , msg ) , nil )
208
215
209
216
defp stacktrace_info ( msg , nil ) , do: " " <> msg <> "\n "
210
- defp stacktrace_info ( msg , terminal ) , do: stacktrace_info ( terminal . ( :stacktrace_info , msg ) , nil )
217
+ defp stacktrace_info ( msg , formatter ) , do: stacktrace_info ( formatter . ( :stacktrace_info , msg ) , nil )
211
218
end
0 commit comments