Skip to content

Commit

Permalink
refactor: use the buffered validator (PLC-lang#1076)
Browse files Browse the repository at this point in the history
* refactor: use the buffered validator

All the validation tests now use the buffered validator this makes it
easy to refactor the diagnostics without worrying about breaking the
location of the tests. Ideally these tests should no longer change if we
change diagnostics

* Empty diagnostics are no longer snapshots
  • Loading branch information
ghaith authored Jan 24, 2024
1 parent 8204311 commit baea087
Show file tree
Hide file tree
Showing 275 changed files with 7,374 additions and 1,958 deletions.
12 changes: 7 additions & 5 deletions src/codegen/tests/initialization_test/type_initializers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use insta::assert_snapshot;

use crate::{
test_utils::tests::{codegen, codegen_debug_without_unwrap, codegen_without_unwrap, parse_and_validate},
test_utils::tests::{
codegen, codegen_debug_without_unwrap, codegen_without_unwrap, parse_and_validate_buffered,
},
DebugLevel,
};

Expand Down Expand Up @@ -538,7 +540,7 @@ fn partly_uninitialized_const_struct_will_get_default_values() {

#[test]
fn partly_uninitialized_const_struct_will_not_report_errors() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
TYPE MyOtherDINT : DINT := 2 ; END_TYPE
TYPE MyDINT : MyOtherDINT; END_TYPE
Expand All @@ -556,12 +558,12 @@ fn partly_uninitialized_const_struct_will_not_report_errors() {
END_VAR
"#,
);
assert_eq!(diagnostics, vec![]);
assert!(diagnostics.is_empty())
}

#[test]
fn enums_with_inline_initializer_do_not_report_errors() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
VAR_GLOBAL
x : (red, yellow, green) := red;
Expand All @@ -579,7 +581,7 @@ fn enums_with_inline_initializer_do_not_report_errors() {
END_FUNCTION
"#,
);
assert_eq!(diagnostics, vec![]);
assert!(diagnostics.is_empty())
}

#[test]
Expand Down

This file was deleted.

12 changes: 0 additions & 12 deletions src/validation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,3 @@ mod reference_resolve_tests;
mod statement_validation_tests;
mod variable_length_array_test;
mod variable_validation_tests;

#[macro_export]
macro_rules! assert_validation_snapshot {
($diagnostics:expr) => {{
let mut res = String::new();
for ele in $diagnostics {
res.push_str(&format!("{:?}\n", ele));
}

insta::assert_snapshot!(res);
}};
}
81 changes: 41 additions & 40 deletions src/validation/tests/assignment_validation_tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::assert_validation_snapshot;
use crate::test_utils::tests::{parse_and_validate, parse_and_validate_buffered};
use insta::assert_snapshot;

use crate::test_utils::tests::parse_and_validate_buffered;

#[test]
fn constant_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
VAR_GLOBAL CONSTANT
v_global : BOOL;
Expand All @@ -16,12 +17,12 @@ fn constant_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(diagnostics)
}

#[test]
fn real_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -75,12 +76,12 @@ fn real_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(diagnostics)
}

#[test]
fn int_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -159,12 +160,12 @@ fn int_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(diagnostics)
}

#[test]
fn duration_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -218,12 +219,12 @@ fn duration_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(diagnostics)
}

#[test]
fn bit_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -277,12 +278,12 @@ fn bit_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(diagnostics)
}

#[test]
fn string_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -341,12 +342,12 @@ fn string_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
fn char_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -409,12 +410,12 @@ fn char_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
fn date_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -468,12 +469,12 @@ fn date_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
fn pointer_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -526,12 +527,12 @@ fn pointer_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
fn array_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand Down Expand Up @@ -588,12 +589,12 @@ fn array_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
fn struct_assignment_validation() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
TYPE STRUCT1 :
STRUCT
Expand Down Expand Up @@ -671,12 +672,12 @@ fn struct_assignment_validation() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
fn assigning_literal_with_incompatible_encoding_to_char_is_validated() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION main : DINT
VAR
Expand All @@ -688,7 +689,7 @@ fn assigning_literal_with_incompatible_encoding_to_char_is_validated() {
END_FUNCTION"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
Expand Down Expand Up @@ -775,7 +776,7 @@ fn action_call_parameters_are_only_validated_outside_of_parent_pou_contexts() {

#[test]
fn implicit_invalid_action_call_assignments_are_validated() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION_BLOCK fb_t
VAR
Expand Down Expand Up @@ -804,12 +805,12 @@ fn implicit_invalid_action_call_assignments_are_validated() {
"#,
);

assert_validation_snapshot!(&diagnostics)
assert_snapshot!(&diagnostics)
}

#[test]
fn invalid_method_call_assignments_are_validated() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
CLASS cl_t
VAR
Expand All @@ -836,12 +837,12 @@ fn invalid_method_call_assignments_are_validated() {
"#,
);

assert_validation_snapshot!(&diagnostics)
assert_snapshot!(&diagnostics)
}

#[test]
fn invalid_function_block_instantiation_is_validated() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION_BLOCK fb_t
VAR_INPUT
Expand All @@ -861,12 +862,12 @@ fn invalid_function_block_instantiation_is_validated() {
END_PROGRAM"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
fn implicit_action_downcasts_are_validated() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
r#"
FUNCTION_BLOCK fb_t
VAR
Expand Down Expand Up @@ -897,12 +898,12 @@ fn implicit_action_downcasts_are_validated() {
"#,
);

assert_validation_snapshot!(&diagnostics);
assert_snapshot!(&diagnostics);
}

#[test]
fn assigning_to_input_by_ref_should_deliver_improvment_suggestion() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
"
FUNCTION fn : DINT
VAR_INPUT
Expand Down Expand Up @@ -937,12 +938,12 @@ fn assigning_to_input_by_ref_should_deliver_improvment_suggestion() {
",
);

assert_validation_snapshot!(diagnostics);
assert_snapshot!(diagnostics);
}

#[test]
fn enum_variants_mismatch() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
"
TYPE Animal: (Dog, Cat, Horse); END_TYPE
Expand All @@ -960,12 +961,12 @@ fn enum_variants_mismatch() {
END_PROGRAM",
);

assert_validation_snapshot!(diagnostics);
assert_snapshot!(diagnostics);
}

#[test]
fn string_type_alias_assignment_can_be_validated() {
let diagnostics = parse_and_validate(
let diagnostics = parse_and_validate_buffered(
"
TYPE MY_STR : STRING; END_TYPE
TYPE MY_OTHER_STR: STRING[256]; END_TYPE
Expand All @@ -982,5 +983,5 @@ fn string_type_alias_assignment_can_be_validated() {
",
);

assert_validation_snapshot!(diagnostics);
assert_snapshot!(diagnostics);
}
Loading

0 comments on commit baea087

Please sign in to comment.