@@ -67,8 +67,8 @@ def render(implicit_locals: [], **locals)
67
67
68
68
def spot_highlight ( compiled , highlight , first_column : nil , **options )
69
69
# rindex by default since our tests usually put the highlight last
70
- first_column ||= compiled . rindex ( highlight ) || 999
71
- last_column = first_column + highlight . size
70
+ first_column ||= compiled . byterindex ( highlight ) || 999
71
+ last_column = first_column + highlight . bytesize
72
72
spot = {
73
73
first_column :, last_column :, snippet : compiled ,
74
74
first_lineno : 1 , last_lineno : 1 , script_lines : compiled . lines ,
@@ -332,121 +332,99 @@ def test_template_translate_location
332
332
source = "<%= nomethoderror %>"
333
333
compiled = "'.freeze; @output_buffer.append= nomethoderror ; @output_buffer.safe_append='\n "
334
334
335
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
336
335
spot = spot_highlight ( compiled , highlight )
337
336
expected = spot_highlight ( source , highlight , snippet : compiled )
338
337
339
- assert_equal expected , new_template ( source ) . translate_location ( backtrace_location , spot )
338
+ assert_equal expected , new_template ( source ) . translate_location ( nil , spot )
340
339
end
341
340
342
- # merely tests for the case where the backtrace and spot disagree about lineno
343
- def test_template_translate_location_lineno_offset
344
- highlight = "nomethoderror"
345
- source = "<%= nomethoderror %>"
346
- compiled = "'.freeze; @output_buffer.append= nomethoderror ; @output_buffer.safe_append='"
347
-
348
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
349
- spot = spot_highlight ( compiled , highlight , first_lineno : 2 , last_lineno : 2 )
350
- expected = spot_highlight ( source , highlight , snippet : compiled )
351
-
352
- assert_equal expected , new_template ( source ) . translate_location ( backtrace_location , spot )
353
- end
354
-
355
- # We are testing the failure case here. `find_offset` doesn't correctly handle the case
356
- # where the line number is not the same in the backtrace and template.
357
341
def test_template_translate_location_with_multiline_code_source
358
342
highlight = "nomethoderror"
359
343
source = "<%=\n good(\n nomethoderror\n ) %>"
360
344
extracted_line = " nomethoderror\n "
361
- compiled = "ValidatedOutputBuffer.wrap(@output_buffer, ({}), ' \n good(\n nomethoderror\n " \
362
- ") '.freeze, true).safe_none_append=( \n good(\n nomethoderror\n ) );\n @output_buffer"
345
+ compiled = "ValidatedOutputBuffer.wrap(@output_buffer, ({}), '\n good(\n nomethoderror\n ) '.freeze, true).safe_none_append=(\n good(\n nomethoderror\n ) );\n @output_buffer"
363
346
364
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 6 )
365
347
spot = spot_highlight ( compiled , highlight , first_column : 1 , first_lineno : 6 , last_lineno : 6 , snippet : extracted_line )
348
+ expected = spot_highlight ( source , highlight , first_column : 1 , first_lineno : 3 , last_lineno : 3 , snippet : extracted_line )
366
349
367
- assert_equal spot , new_template ( source ) . translate_location ( backtrace_location , spot )
350
+ assert_equal expected , new_template ( source ) . translate_location ( nil , spot )
368
351
end
369
352
370
353
def test_template_translate_location_with_multibye_string_before_highlight
371
- highlight = "nomethoderror"
372
- source = String . new ( "\u{a5} <%= nomethoderror %>" , encoding : Encoding ::UTF_8 ) # yen symbol
373
- compiled = String . new ( "\u{a5} '.freeze; @output_buffer.append= nomethoderror ; @output_buffer.safe_append='\n " , encoding : Encoding ::UTF_8 )
354
+ highlight = "nope"
355
+ # ensure the byte offset is enough to make us miss the highlight if wrong
356
+ multibyte = String . new ( "\u{a5} \u{a5} \u{a5} \u{a5} \u{a5} \u{a5} \u{a5} " , encoding : Encoding ::UTF_8 ) # yen symbol
357
+ source = "#{ multibyte } <%= nope %>"
358
+ compiled = "#{ multibyte } '.freeze; @output_buffer.append= nope ; @output_buffer.safe_append='\n "
374
359
375
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
376
360
spot = spot_highlight ( compiled , highlight )
377
361
expected = spot_highlight ( source , highlight , snippet : compiled )
378
362
379
- assert_equal expected , new_template ( source ) . translate_location ( backtrace_location , spot )
363
+ assert_equal expected , new_template ( source ) . translate_location ( nil , spot )
380
364
end
381
365
382
366
def test_template_translate_location_no_match_in_compiled
383
367
highlight = "nomatch"
384
368
source = "<%= nomatch %>"
385
369
compiled = "this source does not contain the highlight, so the original spot is returned"
386
370
387
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
388
371
spot = spot_highlight ( compiled , highlight , first_column : 50 )
389
372
390
- assert_equal spot , new_template ( source ) . translate_location ( backtrace_location , spot )
373
+ assert_equal spot , new_template ( source ) . translate_location ( nil , spot )
391
374
end
392
375
393
376
def test_template_translate_location_text_includes_highlight
394
377
highlight = "nomethoderror"
395
378
source = " nomethoderror <%= nomethoderror %>"
396
379
compiled = " nomethoderror '.freeze; @output_buffer.append= nomethoderror ; @output_buffer.safe_append='\n "
397
380
398
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
399
381
spot = spot_highlight ( compiled , highlight )
400
382
expected = spot_highlight ( source , highlight , snippet : compiled )
401
383
402
- assert_equal expected , new_template ( source ) . translate_location ( backtrace_location , spot )
384
+ assert_equal expected , new_template ( source ) . translate_location ( nil , spot )
403
385
end
404
386
405
387
def test_template_translate_location_space_separated_erb_tags
406
388
highlight = "nomethoderror"
407
389
source = "<%= goodcode %> <%= nomethoderror %>"
408
390
compiled = "'.freeze; @output_buffer.append= goodcode ; @output_buffer.safe_append=' '.freeze; @output_buffer.append= nomethoderror ; @output_buffer.safe_append='\n "
409
391
410
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
411
392
spot = spot_highlight ( compiled , highlight )
412
393
expected = spot_highlight ( source , highlight , snippet : compiled )
413
394
414
- assert_equal expected , new_template ( source ) . translate_location ( backtrace_location , spot )
395
+ assert_equal expected , new_template ( source ) . translate_location ( nil , spot )
415
396
end
416
397
417
398
def test_template_translate_location_consecutive_erb_tags
418
399
highlight = "nomethoderror"
419
400
source = "<%= goodcode %><%= nomethoderror %>"
420
401
compiled = "'.freeze; @output_buffer.append= goodcode ; @output_buffer.append= nomethoderror ; @output_buffer.safe_append='\n "
421
402
422
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
423
403
spot = spot_highlight ( compiled , highlight )
424
404
expected = spot_highlight ( source , highlight , snippet : compiled )
425
405
426
- assert_equal expected , new_template ( source ) . translate_location ( backtrace_location , spot )
406
+ assert_equal expected , new_template ( source ) . translate_location ( nil , spot )
427
407
end
428
408
429
409
def test_template_translate_location_repeated_highlight_in_compiled_template
430
410
highlight = "nomethoderror"
431
411
source = "<%= nomethoderror %>"
432
412
compiled = "ValidatedOutputBuffer.wrap(@output_buffer, ({}), ' nomethoderror '.freeze, true).safe_none_append= nomethoderror ; @output_buffer.safe_append='\n "
433
413
434
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
435
414
spot = spot_highlight ( compiled , highlight )
436
415
expected = spot_highlight ( source , highlight , snippet : compiled )
437
416
438
- assert_equal expected , new_template ( source ) . translate_location ( backtrace_location , spot )
417
+ assert_equal expected , new_template ( source ) . translate_location ( nil , spot )
439
418
end
440
419
441
420
def test_template_translate_location_flaky_pathological_template
442
421
highlight = "flakymethod"
443
422
source = "<%= flakymethod %> flakymethod <%= flakymethod " # fails on second call, no tailing %>
444
423
compiled = "ValidatedOutputBuffer.wrap(@output_buffer, ({}), ' flakymethod '.freeze, true).safe_none_append=( flakymethod );@output_buffer.safe_append=' flakymethod '.freeze;ValidatedOutputBuffer.wrap(@output_buffer, ({}), ' flakymethod '.freeze, true).safe_none_append=( flakymethod "
445
424
446
- backtrace_location = Data . define ( :lineno ) . new ( lineno : 1 )
447
425
spot = spot_highlight ( compiled , highlight )
448
426
expected = spot_highlight ( source , highlight , snippet : compiled )
449
427
450
- assert_equal expected , new_template ( source ) . translate_location ( backtrace_location , spot )
428
+ assert_equal expected , new_template ( source ) . translate_location ( nil , spot )
451
429
end
452
430
end
0 commit comments