@@ -28,6 +28,24 @@ def backtrace
28
28
end
29
29
end
30
30
31
+ class TestTemplate
32
+ attr_reader :method_name
33
+
34
+ def initialize ( method_name )
35
+ @method_name = method_name
36
+ end
37
+
38
+ def spot ( backtrace_location )
39
+ { first_lineno : 1 , script_lines : [ "compiled @ #{ backtrace_location . base_label } :#{ backtrace_location . lineno } " ] }
40
+ end
41
+
42
+ def translate_location ( backtrace_location , spot )
43
+ # note: extract_source_fragment_lines pulls lines from script_lines for indexes near first_lineno
44
+ # since we're mocking the behavior, we need to leave the first_lineno close to 1
45
+ { first_lineno : 1 , script_lines : [ "translated @ #{ backtrace_location . base_label } :#{ backtrace_location . lineno } " ] }
46
+ end
47
+ end
48
+
31
49
setup do
32
50
@cleaner = ActiveSupport ::BacktraceCleaner . new
33
51
@cleaner . remove_filters!
@@ -70,7 +88,7 @@ def backtrace
70
88
end
71
89
72
90
class_eval "def throw_syntax_error; eval %(
73
- 'abc' + pluralize 'def'
91
+ pluralize { # create a syntax error without a parser warning
74
92
); end" , "lib/file.rb" , 42
75
93
76
94
test "#source_extracts works with eval syntax error" do
@@ -79,8 +97,8 @@ def backtrace
79
97
wrapper = ExceptionWrapper . new ( nil , TopErrorProxy . new ( exception , 1 ) )
80
98
81
99
assert_called_with ( wrapper , :source_fragment , [ "lib/file.rb" , 42 ] , returns : "foo" ) do
82
- assert_equal [ code : "foo" , line_number : 42 ] , wrapper . source_extracts
83
- end
100
+ assert_equal [ code : "foo" , line_number : 42 ] , wrapper . source_extracts
101
+ end
84
102
end
85
103
86
104
test "#source_extracts works with nil backtrace_locations" do
@@ -108,6 +126,62 @@ def backtrace
108
126
assert_equal ( { code : code , line_number : lineno + 2 } , wrapper . source_extracts . first )
109
127
end
110
128
129
+ class_eval "def _app_views_tests_show_html_erb;
130
+ raise TestError; end" , "app/views/tests/show.html.erb" , 2
131
+
132
+ test "#source_extracts wraps template lines in a SourceMapLocation" do
133
+ exception = begin _app_views_tests_show_html_erb ; rescue TestError => ex ; ex ; end
134
+
135
+ template = TestTemplate . new ( "_app_views_tests_show_html_erb" )
136
+ resolver = Data . define ( :built_templates ) . new ( built_templates : [ template ] )
137
+
138
+ wrapper = nil
139
+ assert_called ( ActionView ::PathRegistry , :all_resolvers , nil , returns : [ resolver ] ) do
140
+ wrapper = ExceptionWrapper . new ( nil , TopErrorProxy . new ( exception , 1 ) )
141
+ end
142
+
143
+ assert_equal [ {
144
+ code : { 1 => "translated @ _app_views_tests_show_html_erb:3" } ,
145
+ line_number : 1
146
+ } ] , wrapper . source_extracts
147
+ end
148
+
149
+ class_eval "def _app_views_tests_nested_html_erb;
150
+ [1].each do
151
+ [2].each do
152
+ raise TestError
153
+ end
154
+ end
155
+ end" , "app/views/tests/nested.html.erb" , 2
156
+
157
+ test "#source_extracts works with nested template code" do
158
+ exception = begin _app_views_tests_nested_html_erb ; rescue TestError => ex ; ex ; end
159
+
160
+ template = TestTemplate . new ( "_app_views_tests_nested_html_erb" )
161
+ resolver = Data . define ( :built_templates ) . new ( built_templates : [ template ] )
162
+
163
+ wrapper = nil
164
+ assert_called ( ActionView ::PathRegistry , :all_resolvers , nil , returns : [ resolver ] ) do
165
+ wrapper = ExceptionWrapper . new ( nil , TopErrorProxy . new ( exception , 5 ) )
166
+ end
167
+
168
+ extracts = wrapper . source_extracts
169
+ assert_equal ( {
170
+ code : { 1 => "translated @ _app_views_tests_nested_html_erb:5" } ,
171
+ line_number : 1
172
+ } , extracts [ 0 ] )
173
+ # extracts[1] is Array#each (unreliable backtrace across rubies)
174
+ assert_equal ( {
175
+ code : { 1 => "translated @ _app_views_tests_nested_html_erb:4" } ,
176
+ line_number : 1
177
+ } , extracts [ 2 ] )
178
+ # extracts[3] is Array#each (unreliable backtrace across rubies)
179
+ assert_equal ( {
180
+ code : { 1 => "translated @ _app_views_tests_nested_html_erb:3" } ,
181
+ line_number : 1
182
+ } , extracts [ 4 ] )
183
+ end
184
+
111
185
test "#application_trace returns traces only from the application" do
112
186
exception = begin index ; rescue TestError => ex ; ex ; end
113
187
wrapper = ExceptionWrapper . new ( @cleaner , TopErrorProxy . new ( exception , 1 ) )
0 commit comments