@@ -840,6 +840,101 @@ mod tests {
840840 "### )
841841 }
842842
843+ #[ test]
844+ fn span_numbers_are_char_offsets ( ) {
845+ let source = "🍎 == apple" ;
846+ let msg = Report :: < Range < usize > > :: build ( ReportKind :: Error , ( ) , 0 )
847+ . with_config ( no_color_and_ascii ( ) )
848+ . with_label ( Label :: new ( 2 ..4 ) . with_message ( "comparison operator" ) )
849+ . finish ( )
850+ . write_to_string ( Source :: from ( source) ) ;
851+ assert_snapshot ! ( msg, @r###"
852+ Error:
853+ ,-[<unknown>:1:1]
854+ |
855+ 1 | 🍎 == apple
856+ | ^|
857+ | `-- comparison operator
858+ ---'
859+ "### ) ;
860+ }
861+
862+ #[ test]
863+ fn empty_span ( ) {
864+ let source = "apple" ;
865+ let msg = Report :: < Range < usize > > :: build ( ReportKind :: Error , ( ) , 0 )
866+ . with_config ( no_color_and_ascii ( ) )
867+ . with_label ( Label :: new ( 0 ..0 ) . with_message ( "first character" ) )
868+ . finish ( )
869+ . write_to_string ( Source :: from ( source) ) ;
870+ assert_snapshot ! ( msg, @r###"
871+ Error:
872+ ,-[<unknown>:1:1]
873+ |
874+ 1 | apple
875+ | |
876+ | `- first character
877+ ---'
878+ "### ) ;
879+ }
880+
881+ #[ test]
882+ fn one_char_span ( ) {
883+ let source = "apple" ;
884+ let msg = Report :: < Range < usize > > :: build ( ReportKind :: Error , ( ) , 0 )
885+ . with_config ( no_color_and_ascii ( ) )
886+ . with_label ( Label :: new ( 0 ..1 ) . with_message ( "first character" ) )
887+ . finish ( )
888+ . write_to_string ( Source :: from ( source) ) ;
889+ // TODO: it would be nice if this rendered just like the empty_label test
890+ assert_snapshot ! ( msg, @r###"
891+ Error:
892+ ,-[<unknown>:1:1]
893+ |
894+ 1 | apple
895+ | |
896+ | `-- first character
897+ ---'
898+ "### ) ;
899+ }
900+
901+ #[ test]
902+ fn empty_span_directly_after_end ( ) {
903+ let source = "universe" ;
904+ let msg = Report :: < Range < usize > > :: build ( ReportKind :: Error , ( ) , 0 )
905+ . with_config ( no_color_and_ascii ( ) )
906+ . with_label ( Label :: new ( source. len ( ) ..source. len ( ) ) . with_message ( "outside" ) )
907+ . finish ( )
908+ . write_to_string ( Source :: from ( source) ) ;
909+ assert_snapshot ! ( msg, @r###"
910+ Error:
911+ ,-[<unknown>:1:1]
912+ |
913+ 1 | universe
914+ | |
915+ | `- outside
916+ ---'
917+ "### ) ;
918+ }
919+
920+ #[ test]
921+ fn span_out_of_bounds ( ) {
922+ let source = "universe" ;
923+ let msg = Report :: < Range < usize > > :: build ( ReportKind :: Error , ( ) , 0 )
924+ . with_config ( no_color_and_ascii ( ) )
925+ . with_label ( Label :: new ( source. len ( ) + 1 ..source. len ( ) + 1 ) . with_message ( "outside" ) )
926+ . finish ( )
927+ . write_to_string ( Source :: from ( source) ) ;
928+ // TODO: Report::write should probably panic on out-of-bound spans
929+ assert_snapshot ! ( msg, @r###"
930+ Error:
931+ ,-[<unknown>:1:1]
932+ |
933+ |
934+ ---'
935+ "### ) ;
936+ }
937+
843938 #[ test]
844939 fn two_labels_without_messages ( ) {
845940 let source = "apple == orange;" ;
0 commit comments