mirror of
https://github.com/openharmony/third_party_rust_codespan.git
synced 2026-07-21 03:55:28 -04:00
Move tests
This commit is contained in:
@@ -137,147 +137,155 @@ mod tests {
|
||||
use crate::diagnostic::{Diagnostic, Label};
|
||||
use crate::termcolor::Buffer;
|
||||
|
||||
fn emit_fizz_buzz(writer: &mut impl WriteColor) {
|
||||
let mut files = Files::new();
|
||||
mod fizz_buzz {
|
||||
use super::*;
|
||||
|
||||
let file_id = files.add(
|
||||
"FizzBuzz.fun",
|
||||
unindent::unindent(
|
||||
r#"
|
||||
module FizzBuzz where
|
||||
fn emit_test(writer: &mut impl WriteColor) {
|
||||
let mut files = Files::new();
|
||||
|
||||
fizz₁ : Nat → String
|
||||
fizz₁ num = case (mod num 5) (mod num 3) of
|
||||
0 0 => "FizzBuzz"
|
||||
0 _ => "Fizz"
|
||||
_ 0 => "Buzz"
|
||||
_ _ => num
|
||||
let file_id = files.add(
|
||||
"FizzBuzz.fun",
|
||||
unindent::unindent(
|
||||
r#"
|
||||
module FizzBuzz where
|
||||
|
||||
fizz₂ num =
|
||||
case (mod num 5) (mod num 3) of
|
||||
fizz₁ : Nat → String
|
||||
fizz₁ num = case (mod num 5) (mod num 3) of
|
||||
0 0 => "FizzBuzz"
|
||||
0 _ => "Fizz"
|
||||
_ 0 => "Buzz"
|
||||
_ _ => num
|
||||
"#,
|
||||
),
|
||||
);
|
||||
|
||||
let diagnostics = vec![
|
||||
// Incompatible match clause error
|
||||
Diagnostic::new_error(
|
||||
"`case` clauses have incompatible types",
|
||||
Label::new(file_id, 163..166, "expected `String`, found `Nat`"),
|
||||
)
|
||||
.with_code("E0308")
|
||||
.with_notes(vec![unindent::unindent(
|
||||
"
|
||||
expected type `String`
|
||||
found type `Nat`
|
||||
",
|
||||
)])
|
||||
.with_secondary_labels(vec![
|
||||
Label::new(file_id, 62..166, "`case` clauses have incompatible types"),
|
||||
Label::new(file_id, 41..47, "expected type `String` found here"),
|
||||
]),
|
||||
// Incompatible match clause error
|
||||
Diagnostic::new_error(
|
||||
"`case` clauses have incompatible types",
|
||||
Label::new(file_id, 303..306, "expected `String`, found `Nat`"),
|
||||
)
|
||||
.with_code("E0308")
|
||||
.with_notes(vec![unindent::unindent(
|
||||
"
|
||||
expected type `String`
|
||||
found type `Nat`
|
||||
",
|
||||
)])
|
||||
.with_secondary_labels(vec![
|
||||
Label::new(file_id, 186..306, "`case` clauses have incompatible types"),
|
||||
Label::new(file_id, 233..243, "this is found to be of type `String`"),
|
||||
Label::new(file_id, 259..265, "this is found to be of type `String`"),
|
||||
Label::new(file_id, 281..287, "this is found to be of type `String`"),
|
||||
]),
|
||||
];
|
||||
fizz₂ num =
|
||||
case (mod num 5) (mod num 3) of
|
||||
0 0 => "FizzBuzz"
|
||||
0 _ => "Fizz"
|
||||
_ 0 => "Buzz"
|
||||
_ _ => num
|
||||
"#,
|
||||
),
|
||||
);
|
||||
|
||||
for diagnostic in &diagnostics {
|
||||
emit(writer, &Config::default(), &files, &diagnostic).unwrap();
|
||||
let diagnostics = vec![
|
||||
// Incompatible match clause error
|
||||
Diagnostic::new_error(
|
||||
"`case` clauses have incompatible types",
|
||||
Label::new(file_id, 163..166, "expected `String`, found `Nat`"),
|
||||
)
|
||||
.with_code("E0308")
|
||||
.with_notes(vec![unindent::unindent(
|
||||
"
|
||||
expected type `String`
|
||||
found type `Nat`
|
||||
",
|
||||
)])
|
||||
.with_secondary_labels(vec![
|
||||
Label::new(file_id, 62..166, "`case` clauses have incompatible types"),
|
||||
Label::new(file_id, 41..47, "expected type `String` found here"),
|
||||
]),
|
||||
// Incompatible match clause error
|
||||
Diagnostic::new_error(
|
||||
"`case` clauses have incompatible types",
|
||||
Label::new(file_id, 303..306, "expected `String`, found `Nat`"),
|
||||
)
|
||||
.with_code("E0308")
|
||||
.with_notes(vec![unindent::unindent(
|
||||
"
|
||||
expected type `String`
|
||||
found type `Nat`
|
||||
",
|
||||
)])
|
||||
.with_secondary_labels(vec![
|
||||
Label::new(file_id, 186..306, "`case` clauses have incompatible types"),
|
||||
Label::new(file_id, 233..243, "this is found to be of type `String`"),
|
||||
Label::new(file_id, 259..265, "this is found to be of type `String`"),
|
||||
Label::new(file_id, 281..287, "this is found to be of type `String`"),
|
||||
]),
|
||||
];
|
||||
|
||||
for diagnostic in &diagnostics {
|
||||
emit(writer, &Config::default(), &files, &diagnostic).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_color() {
|
||||
let mut buffer = Buffer::no_color();
|
||||
emit_test(&mut buffer);
|
||||
let result = String::from_utf8_lossy(buffer.as_slice());
|
||||
insta::assert_snapshot_matches!("no_color", result);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fizz_buzz_no_color() {
|
||||
let mut buffer = Buffer::no_color();
|
||||
emit_fizz_buzz(&mut buffer);
|
||||
let result = String::from_utf8_lossy(buffer.as_slice());
|
||||
insta::assert_snapshot_matches!("fizz_buzz_no_color", result);
|
||||
}
|
||||
mod tabbed {
|
||||
use super::*;
|
||||
|
||||
fn emit_tabbed(writer: &mut impl WriteColor, config: &Config) {
|
||||
let mut files = Files::new();
|
||||
fn emit_test(writer: &mut impl WriteColor, config: &Config) {
|
||||
let mut files = Files::new();
|
||||
|
||||
let file_id = files.add(
|
||||
"FizzBuzz.fun",
|
||||
[
|
||||
"Entity:",
|
||||
"\tArmament:",
|
||||
"\t\tWeapon: DogJaw",
|
||||
"\t\tReloadingCondition:\tattack-cooldown",
|
||||
"\tFoo: Bar",
|
||||
]
|
||||
.join("\n"),
|
||||
);
|
||||
let file_id = files.add(
|
||||
"FizzBuzz.fun",
|
||||
[
|
||||
"Entity:",
|
||||
"\tArmament:",
|
||||
"\t\tWeapon: DogJaw",
|
||||
"\t\tReloadingCondition:\tattack-cooldown",
|
||||
"\tFoo: Bar",
|
||||
]
|
||||
.join("\n"),
|
||||
);
|
||||
|
||||
let diagnostics = vec![
|
||||
Diagnostic::new_warning(
|
||||
"unknown weapon `DogJaw`",
|
||||
Label::new(file_id, 29..35, "the weapon"),
|
||||
),
|
||||
Diagnostic::new_warning(
|
||||
"unknown condition `attack-cooldown`",
|
||||
Label::new(file_id, 58..73, "the condition"),
|
||||
),
|
||||
Diagnostic::new_warning(
|
||||
"unknown field `Foo`",
|
||||
Label::new(file_id, 75..78, "the field"),
|
||||
),
|
||||
];
|
||||
let diagnostics = vec![
|
||||
Diagnostic::new_warning(
|
||||
"unknown weapon `DogJaw`",
|
||||
Label::new(file_id, 29..35, "the weapon"),
|
||||
),
|
||||
Diagnostic::new_warning(
|
||||
"unknown condition `attack-cooldown`",
|
||||
Label::new(file_id, 58..73, "the condition"),
|
||||
),
|
||||
Diagnostic::new_warning(
|
||||
"unknown field `Foo`",
|
||||
Label::new(file_id, 75..78, "the field"),
|
||||
),
|
||||
];
|
||||
|
||||
for diagnostic in &diagnostics {
|
||||
emit(writer, config, &files, &diagnostic).unwrap();
|
||||
for diagnostic in &diagnostics {
|
||||
emit(writer, config, &files, &diagnostic).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fizz_tabbed_default_no_color() {
|
||||
let config = Config::default();
|
||||
let mut buffer = Buffer::no_color();
|
||||
emit_tabbed(&mut buffer, &config);
|
||||
let result = String::from_utf8_lossy(buffer.as_slice());
|
||||
insta::assert_snapshot_matches!("tabbed_default_no_color", result);
|
||||
}
|
||||
#[test]
|
||||
fn tab_width_default_no_color() {
|
||||
let config = Config::default();
|
||||
let mut buffer = Buffer::no_color();
|
||||
emit_test(&mut buffer, &config);
|
||||
let result = String::from_utf8_lossy(buffer.as_slice());
|
||||
insta::assert_snapshot_matches!("tab_width_default_no_color", result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fizz_tabbed_tab_3_no_color() {
|
||||
let config = Config {
|
||||
tab_width: 3,
|
||||
..Config::default()
|
||||
};
|
||||
let mut buffer = Buffer::no_color();
|
||||
emit_tabbed(&mut buffer, &config);
|
||||
let result = String::from_utf8_lossy(buffer.as_slice());
|
||||
insta::assert_snapshot_matches!("tabbed_tab_3_no_color", result);
|
||||
}
|
||||
#[test]
|
||||
fn tab_width_3_no_color() {
|
||||
let config = Config {
|
||||
tab_width: 3,
|
||||
..Config::default()
|
||||
};
|
||||
let mut buffer = Buffer::no_color();
|
||||
emit_test(&mut buffer, &config);
|
||||
let result = String::from_utf8_lossy(buffer.as_slice());
|
||||
insta::assert_snapshot_matches!("tab_width_3_no_color", result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fizz_tabbed_tab_6_no_color() {
|
||||
let config = Config {
|
||||
tab_width: 6,
|
||||
..Config::default()
|
||||
};
|
||||
let mut buffer = Buffer::no_color();
|
||||
emit_tabbed(&mut buffer, &config);
|
||||
let result = String::from_utf8_lossy(buffer.as_slice());
|
||||
insta::assert_snapshot_matches!("tabbed_tab_6_no_color", result);
|
||||
#[test]
|
||||
fn tab_width_6_no_color() {
|
||||
let config = Config {
|
||||
tab_width: 6,
|
||||
..Config::default()
|
||||
};
|
||||
let mut buffer = Buffer::no_color();
|
||||
emit_test(&mut buffer, &config);
|
||||
let result = String::from_utf8_lossy(buffer.as_slice());
|
||||
insta::assert_snapshot_matches!("tab_width_6_no_color", result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
---
|
||||
created: "2019-06-06T14:02:34.225144Z"
|
||||
created: "2019-06-09T12:57:51.853099Z"
|
||||
creator: insta@0.8.1
|
||||
source: codespan-reporting/src/emitter/mod.rs
|
||||
expression: result
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
---
|
||||
created: "2019-06-09T12:36:32.876902Z"
|
||||
created: "2019-06-09T12:56:07.797326Z"
|
||||
creator: insta@0.8.1
|
||||
source: codespan-reporting/src/emitter/mod.rs
|
||||
expression: result
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
---
|
||||
created: "2019-06-09T12:36:32.876957Z"
|
||||
created: "2019-06-09T12:56:07.797326Z"
|
||||
creator: insta@0.8.1
|
||||
source: codespan-reporting/src/emitter/mod.rs
|
||||
expression: result
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
---
|
||||
created: "2019-06-09T12:37:44.588546Z"
|
||||
created: "2019-06-09T12:56:07.797419Z"
|
||||
creator: insta@0.8.1
|
||||
source: codespan-reporting/src/emitter/mod.rs
|
||||
expression: result
|
||||
Reference in New Issue
Block a user