Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remaining assert_xx! comparison test macros #4936

Merged
merged 8 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions corelib/src/test/testing_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,50 @@ fn test_assert_ne_no_description() {
assert_ne!(1, 2);
}

#[test]
fn test_assert_lt_with_description() {
assert_lt!(1_u8, 2_u8, "Description");
}

#[test]
fn test_assert_lt_no_description() {
assert_lt!(1_u8, 2_u8);
}

#[test]
fn test_assert_le_with_description() {
assert_le!(1_u8, 2_u8, "Description");
assert_le!(1_u8, 1_u8, "Description");
}

#[test]
fn test_assert_le_no_description() {
assert_le!(1_u8, 2_u8);
assert_le!(1_u8, 1_u8);
}

#[test]
fn test_assert_gt_with_description() {
assert_gt!(2_u8, 1_u8, "Description");
}

#[test]
fn test_assert_gt_no_description() {
assert_gt!(2_u8, 1_u8);
}

#[test]
fn test_assert_ge_with_description() {
assert_ge!(2_u8, 1_u8, "Description");
assert_ge!(2_u8, 2_u8, "Description");
}

#[test]
fn test_assert_ge_no_description() {
assert_ge!(2_u8, 1_u8);
assert_ge!(2_u8, 2_u8);
}

#[test]
#[should_panic(expected: "assertion failed: `false`.")]
fn test_assert_macro_no_input() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn test_flow_safe_dispatcher() {
// If the test is failing do to gas usage changes, update the gas limit by taking `test_flow` test
// gas usage and remove 10000.
#[test]
#[available_gas(1170970)]
#[available_gas(1160970)]
#[should_panic(expected: ('Syscall out of gas', 'ENTRYPOINT_FAILED',))]
fn test_flow_out_of_gas() {
// Calling the `test_flow` test but a low gas limit.
Expand Down
30 changes: 29 additions & 1 deletion crates/cairo-lang-test-plugin/src/inline_macros/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ trait CompareAssertionPlugin: NamedPlugin {
{{
{maybe_assign_lhs}
{maybe_assign_rhs}
if !(@$lhs_value$ {operator} @$rhs_value$) {{
if !($lhs_value$ {operator} $rhs_value$) {{
let mut {f}: core::fmt::Formatter = core::traits::Default::default();
core::result::ResultTrait::<(), core::fmt::Error>::unwrap(
write!({f}, "assertion `{lhs_escaped} {operator} {rhs_escaped}` failed")
Expand Down Expand Up @@ -207,3 +207,31 @@ define_compare_assert_macro!(
"assert_ne",
"!="
);

define_compare_assert_macro!(
/// Macro for less-than assertion.
AssertLtMacro,
"assert_lt",
"<"
);

define_compare_assert_macro!(
/// Macro for less-than-or-equal assertion.
AssertLeMacro,
"assert_le",
"<="
);

define_compare_assert_macro!(
/// Macro for greater-than assertion.
AssertGtMacro,
"assert_gt",
">"
);

define_compare_assert_macro!(
/// Macro for greater-than-or-equal assertion.
AssertGeMacro,
"assert_ge",
">="
);
6 changes: 5 additions & 1 deletion crates/cairo-lang-test-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ pub fn test_plugin_suite() -> PluginSuite {
suite
.add_plugin::<TestPlugin>()
.add_inline_macro_plugin::<inline_macros::assert::AssertEqMacro>()
.add_inline_macro_plugin::<inline_macros::assert::AssertNeMacro>();
.add_inline_macro_plugin::<inline_macros::assert::AssertNeMacro>()
.add_inline_macro_plugin::<inline_macros::assert::AssertLtMacro>()
.add_inline_macro_plugin::<inline_macros::assert::AssertLeMacro>()
.add_inline_macro_plugin::<inline_macros::assert::AssertGtMacro>()
.add_inline_macro_plugin::<inline_macros::assert::AssertGeMacro>();
suite
}
24 changes: 22 additions & 2 deletions vscode-cairo/snippets/cairo.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,33 @@
"Creates an equality assertion with macro": {
"prefix": "assert_eq!",
"body": ["assert_eq!($1, $2);"],
"description": "Creates an inline macro for asserting equality."
"description": "Creates an inline macro for asserting equality"
},
"Creates an not equality assertion with macro": {
"Creates an not-equality assertion with macro": {
"prefix": "assert_ne!",
"body": ["assert_ne!($1, $2);"],
"description": "Creates an inline macro for asserting non-equality"
},
"Creates a less-than comparison assertion with macro": {
"prefix": "assert_lt!",
"body": ["assert_lt!($1, $2);"],
"description": "Creates an inline macro for asserting less-than"
},
"Creates a less-than-or-equal comparison assertion with macro": {
"prefix": "assert_le!",
"body": ["assert_le!($1, $2);"],
"description": "Creates an inline macro for asserting less-than-or-equal"
},
"Creates a greater-than comparison assertion with macro": {
"prefix": "assert_gt!",
"body": ["assert_gt!($1, $2);"],
"description": "Creates an inline macro for asserting greater-than"
},
"Creates a greater-than-or-equal comparison assertion with macro": {
"prefix": "assert_ge!",
"body": ["assert_ge!($1, $2);"],
"description": "Creates an inline macro for asserting greater-than-or-equal"
},
"Creates panic": {
"prefix": "panic!",
"body": ["panic!(\"$1\");"],
Expand Down
Loading