@@ -22,6 +22,33 @@ fn string_to_parser(ps: &ParseSess, source_str: String) -> Parser<'_> {
2222 new_parser_from_source_str ( ps, PathBuf :: from ( "bogofile" ) . into ( ) , source_str)
2323}
2424
25+ fn create_test_handler ( ) -> ( Handler , Lrc < SourceMap > , Arc < Mutex < Vec < u8 > > > ) {
26+ let output = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
27+ let source_map = Lrc :: new ( SourceMap :: new ( FilePathMapping :: empty ( ) ) ) ;
28+ let fallback_bundle = rustc_errors:: fallback_fluent_bundle (
29+ vec ! [ crate :: DEFAULT_LOCALE_RESOURCE , rustc_parse:: DEFAULT_LOCALE_RESOURCE ] ,
30+ false ,
31+ ) ;
32+ let emitter = EmitterWriter :: new (
33+ Box :: new ( Shared { data : output. clone ( ) } ) ,
34+ Some ( source_map. clone ( ) ) ,
35+ None ,
36+ fallback_bundle,
37+ false ,
38+ false ,
39+ false ,
40+ Some ( 140 ) ,
41+ false ,
42+ false ,
43+ TerminalUrl :: No ,
44+ ) ;
45+ let handler = Handler :: with_emitter ( Box :: new ( emitter) ) ;
46+ ( handler, source_map, output)
47+ }
48+
49+ /// Returns the result of parsing the given string via the given callback.
50+ ///
51+ /// If there are any errors, this will panic.
2552pub ( crate ) fn with_error_checking_parse < ' a , T , F > ( s : String , ps : & ' a ParseSess , f : F ) -> T
2653where
2754 F : FnOnce ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
3259 x
3360}
3461
62+ /// Verifies that parsing the given string using the given callback will
63+ /// generate an error that contains the given text.
64+ pub ( crate ) fn with_expected_parse_error < T , F > ( source_str : & str , expected_output : & str , f : F )
65+ where
66+ F : for < ' a > FnOnce ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
67+ {
68+ let ( handler, source_map, output) = create_test_handler ( ) ;
69+ let ps = ParseSess :: with_span_handler ( handler, source_map) ;
70+ let mut p = string_to_parser ( & ps, source_str. to_string ( ) ) ;
71+ let result = f ( & mut p) ;
72+ assert ! ( result. is_ok( ) ) ;
73+
74+ let bytes = output. lock ( ) . unwrap ( ) ;
75+ let actual_output = str:: from_utf8 ( & bytes) . unwrap ( ) ;
76+ println ! ( "expected output:\n ------\n {}------" , expected_output) ;
77+ println ! ( "actual output:\n ------\n {}------" , actual_output) ;
78+
79+ assert ! ( actual_output. contains( expected_output) )
80+ }
81+
3582/// Maps a string to tts, using a made-up filename.
3683pub ( crate ) fn string_to_stream ( source_str : String ) -> TokenStream {
3784 let ps = ParseSess :: new (
@@ -130,13 +177,7 @@ impl<T: Write> Write for Shared<T> {
130177
131178fn test_harness ( file_text : & str , span_labels : Vec < SpanLabel > , expected_output : & str ) {
132179 create_default_session_if_not_set_then ( |_| {
133- let output = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
134-
135- let fallback_bundle = rustc_errors:: fallback_fluent_bundle (
136- vec ! [ crate :: DEFAULT_LOCALE_RESOURCE , rustc_parse:: DEFAULT_LOCALE_RESOURCE ] ,
137- false ,
138- ) ;
139- let source_map = Lrc :: new ( SourceMap :: new ( FilePathMapping :: empty ( ) ) ) ;
180+ let ( handler, source_map, output) = create_test_handler ( ) ;
140181 source_map. new_source_file ( Path :: new ( "test.rs" ) . to_owned ( ) . into ( ) , file_text. to_owned ( ) ) ;
141182
142183 let primary_span = make_span ( & file_text, & span_labels[ 0 ] . start , & span_labels[ 0 ] . end ) ;
@@ -148,20 +189,6 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
148189 println ! ( "text: {:?}" , source_map. span_to_snippet( span) ) ;
149190 }
150191
151- let emitter = EmitterWriter :: new (
152- Box :: new ( Shared { data : output. clone ( ) } ) ,
153- Some ( source_map. clone ( ) ) ,
154- None ,
155- fallback_bundle,
156- false ,
157- false ,
158- false ,
159- None ,
160- false ,
161- false ,
162- TerminalUrl :: No ,
163- ) ;
164- let handler = Handler :: with_emitter ( Box :: new ( emitter) ) ;
165192 #[ allow( rustc:: untranslatable_diagnostic) ]
166193 handler. span_err ( msp, "foo" ) ;
167194
0 commit comments