Use human readable forms of ansi codes in reporting tests

This commit is contained in:
Chad Stearns 2020-03-20 22:03:08 -04:00
parent 86c1ec27a9
commit ab61d3d357
2 changed files with 19 additions and 11 deletions

View file

@ -103,12 +103,15 @@ fn newline() -> ReportText {
plain_text("\n") plain_text("\n")
} }
pub const RED_CODE: &str = "\u{001b}[31m";
pub const WHITE_CODE: &str = "\u{001b}[31m";
fn red(str: &str) -> String { fn red(str: &str) -> String {
let mut buf = String::new(); let mut buf = String::new();
buf.push_str("\u{001b}[31m"); buf.push_str(RED_CODE);
buf.push_str(str); buf.push_str(str);
buf.push_str("\u{001b}[0m"); buf.push_str(RESET_CODE);
buf buf
} }
@ -116,14 +119,14 @@ fn red(str: &str) -> String {
fn white(str: &str) -> String { fn white(str: &str) -> String {
let mut buf = String::new(); let mut buf = String::new();
buf.push_str("\u{001b}[31m"); buf.push_str(WHITE_CODE);
buf.push_str(str); buf.push_str(str);
buf.push_str(RESET); buf.push_str(RESET_CODE);
buf buf
} }
const RESET: &str = "\u{001b}[0m"; pub const RESET_CODE: &str = "\u{001b}[0m";
impl ReportText { impl ReportText {
/// Render to CI console output, where no colors are available. /// Render to CI console output, where no colors are available.

View file

@ -11,7 +11,9 @@ mod helpers;
mod test_report { mod test_report {
use crate::helpers::test_home; use crate::helpers::test_home;
use roc_module::symbol::{Interns, ModuleId}; use roc_module::symbol::{Interns, ModuleId};
use roc_reporting::report::{can_problem, plain_text, Report, ReportText, DEFAULT_PALETTE}; use roc_reporting::report::{
can_problem, plain_text, Report, ReportText, DEFAULT_PALETTE, RED_CODE, RESET_CODE,
};
use roc_types::pretty_print::name_all_type_vars; use roc_types::pretty_print::name_all_type_vars;
use roc_types::subs::Subs; use roc_types::subs::Subs;
use roc_types::types; use roc_types::types;
@ -82,6 +84,12 @@ mod test_report {
assert_eq!(buf, expected_rendering); assert_eq!(buf, expected_rendering);
} }
fn human_readable(str: &str) -> String {
return str
.replace(RED_CODE, "<red>")
.replace(RESET_CODE, "<reset>");
}
fn report_renders_in_color_from_src(src: &str, report: Report, expected_rendering: &str) { fn report_renders_in_color_from_src(src: &str, report: Report, expected_rendering: &str) {
let (_type_problems, _can_problems, mut subs, home, interns) = infer_expr_help(src); let (_type_problems, _can_problems, mut subs, home, interns) = infer_expr_help(src);
let mut buf: String = String::new(); let mut buf: String = String::new();
@ -96,7 +104,7 @@ mod test_report {
DEFAULT_PALETTE, DEFAULT_PALETTE,
); );
assert_eq!(buf, expected_rendering); assert_eq!(human_readable(&buf), expected_rendering);
} }
fn report_renders_in_color(report: Report, expected_rendering: &str) { fn report_renders_in_color(report: Report, expected_rendering: &str) {
@ -245,10 +253,7 @@ mod test_report {
#[test] #[test]
fn report_in_color() { fn report_in_color() {
report_renders_in_color( report_renders_in_color(to_simple_report(plain_text("y")), "<red>y<reset>");
to_simple_report(plain_text("y")),
"\u{001b}[31my\u{001b}[0m",
);
} }
#[test] #[test]