summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_expand/src/tests.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
commitd1b2d29528b7794b41e66fc2136e395a02f8529b (patch)
treea4a17504b260206dec3cf55b2dca82929a348ac2 /compiler/rustc_expand/src/tests.rs
parentReleasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz
rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_expand/src/tests.rs')
-rw-r--r--compiler/rustc_expand/src/tests.rs76
1 files changed, 54 insertions, 22 deletions
diff --git a/compiler/rustc_expand/src/tests.rs b/compiler/rustc_expand/src/tests.rs
index 8a5e09475..8e3219c13 100644
--- a/compiler/rustc_expand/src/tests.rs
+++ b/compiler/rustc_expand/src/tests.rs
@@ -8,7 +8,8 @@ use rustc_span::{BytePos, Span};
use rustc_data_structures::sync::Lrc;
use rustc_errors::emitter::EmitterWriter;
-use rustc_errors::{Handler, MultiSpan, PResult, TerminalUrl};
+use rustc_errors::{Handler, MultiSpan, PResult};
+use termcolor::WriteColor;
use std::io;
use std::io::prelude::*;
@@ -22,6 +23,23 @@ fn string_to_parser(ps: &ParseSess, source_str: String) -> Parser<'_> {
new_parser_from_source_str(ps, PathBuf::from("bogofile").into(), source_str)
}
+fn create_test_handler() -> (Handler, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) {
+ let output = Arc::new(Mutex::new(Vec::new()));
+ let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty()));
+ let fallback_bundle = rustc_errors::fallback_fluent_bundle(
+ vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
+ false,
+ );
+ let emitter = EmitterWriter::new(Box::new(Shared { data: output.clone() }), fallback_bundle)
+ .sm(Some(source_map.clone()))
+ .diagnostic_width(Some(140));
+ let handler = Handler::with_emitter(Box::new(emitter));
+ (handler, source_map, output)
+}
+
+/// Returns the result of parsing the given string via the given callback.
+///
+/// If there are any errors, this will panic.
pub(crate) fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) -> T
where
F: FnOnce(&mut Parser<'a>) -> PResult<'a, T>,
@@ -32,6 +50,26 @@ where
x
}
+/// Verifies that parsing the given string using the given callback will
+/// generate an error that contains the given text.
+pub(crate) fn with_expected_parse_error<T, F>(source_str: &str, expected_output: &str, f: F)
+where
+ F: for<'a> FnOnce(&mut Parser<'a>) -> PResult<'a, T>,
+{
+ let (handler, source_map, output) = create_test_handler();
+ let ps = ParseSess::with_span_handler(handler, source_map);
+ let mut p = string_to_parser(&ps, source_str.to_string());
+ let result = f(&mut p);
+ assert!(result.is_ok());
+
+ let bytes = output.lock().unwrap();
+ let actual_output = str::from_utf8(&bytes).unwrap();
+ println!("expected output:\n------\n{}------", expected_output);
+ println!("actual output:\n------\n{}------", actual_output);
+
+ assert!(actual_output.contains(expected_output))
+}
+
/// Maps a string to tts, using a made-up filename.
pub(crate) fn string_to_stream(source_str: String) -> TokenStream {
let ps = ParseSess::new(
@@ -118,6 +156,20 @@ pub(crate) struct Shared<T: Write> {
pub data: Arc<Mutex<T>>,
}
+impl<T: Write> WriteColor for Shared<T> {
+ fn supports_color(&self) -> bool {
+ false
+ }
+
+ fn set_color(&mut self, _spec: &termcolor::ColorSpec) -> io::Result<()> {
+ Ok(())
+ }
+
+ fn reset(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+}
+
impl<T: Write> Write for Shared<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.data.lock().unwrap().write(buf)
@@ -130,13 +182,7 @@ impl<T: Write> Write for Shared<T> {
fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
create_default_session_if_not_set_then(|_| {
- let output = Arc::new(Mutex::new(Vec::new()));
-
- let fallback_bundle = rustc_errors::fallback_fluent_bundle(
- vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
- false,
- );
- let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty()));
+ let (handler, source_map, output) = create_test_handler();
source_map.new_source_file(Path::new("test.rs").to_owned().into(), file_text.to_owned());
let primary_span = make_span(&file_text, &span_labels[0].start, &span_labels[0].end);
@@ -148,20 +194,6 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
println!("text: {:?}", source_map.span_to_snippet(span));
}
- let emitter = EmitterWriter::new(
- Box::new(Shared { data: output.clone() }),
- Some(source_map.clone()),
- None,
- fallback_bundle,
- false,
- false,
- false,
- None,
- false,
- false,
- TerminalUrl::No,
- );
- let handler = Handler::with_emitter(true, None, Box::new(emitter));
#[allow(rustc::untranslatable_diagnostic)]
handler.span_err(msp, "foo");