@@ -32,8 +32,9 @@ defmodule ExUnit.Formatter do
3232
3333 import Exception , only: [ format_stacktrace_entry: 1 ]
3434
35- # Width of error counter field
36- @ prefix_width 5
35+ @ label_padding " "
36+ @ counter_padding " "
37+ @ inspect_padding @ counter_padding <> @ label_padding
3738
3839 @ doc """
3940 Formats time taken running the test suite.
@@ -99,40 +100,42 @@ defmodule ExUnit.Formatter do
99100 @ doc """
100101 Receives a test and formats its failure.
101102 """
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 )
106107 end
107108
108109 @ doc """
109110 Receives a test case and formats its failure.
110111 """
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 )
115116 end
116117
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+
118121 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 ) ) ]
123126
124127 fields
125128 |> filter_interesting_fields
126- |> format_each_reason ( terminal )
127- |> make_into_lines
129+ |> format_each_reason ( formatter )
130+ |> make_into_lines ( @ counter_padding )
128131 end
129132
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
132135 end
133136
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
136139 end
137140
138141 defp filter_interesting_fields ( fields ) do
@@ -141,15 +144,9 @@ defmodule ExUnit.Formatter do
141144 end )
142145 end
143146
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
151148 Enum . map ( reasons , fn { label , value } ->
152- format_label ( label , label_length , terminal ) <> value
149+ format_label ( label , formatter ) <> value
153150 end )
154151 end
155152
@@ -161,20 +158,30 @@ defmodule ExUnit.Formatter do
161158 end
162159 end
163160
164- defp format_label ( :note , _length , _terminal ) do
161+ defp format_label ( :note , _formatter ) do
165162 ""
166163 end
167164
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
170175 end
171176
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 )
174181 end
175182
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 "
178185 end
179186
180187 defp format_stacktrace ( [ { test_case , test , _ , location } | _ ] , test_case , test , color ) do
@@ -195,17 +202,17 @@ defmodule ExUnit.Formatter do
195202 defp with_counter ( counter , msg ) do "#{ counter } ) #{ msg } " end
196203
197204 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 )
199206
200207 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 )
202209
203210 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 )
205212
206213 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 )
208215
209216 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 )
211218end
0 commit comments