From bfd0ad298da5a596058efc2e7868931791387d94 Mon Sep 17 00:00:00 2001 From: Kraemii Date: Wed, 20 Nov 2024 10:15:58 +0100 Subject: [PATCH] Add snapshot testing for statement parsing For statements it is hard to write any unit tests, as a statement is a rather complex struct also containing some meta information. It is much easier to test the statement parsing, looking at the generated statement and review it instead. --- rust/Cargo.lock | 62 +++++++ rust/Cargo.toml | 1 + .../nasl/syntax/snapshots/statement_add.snap | 6 + .../syntax/snapshots/statement_array.snap | 6 + .../syntax/snapshots/statement_assign.snap | 6 + .../snapshots/statement_assign_return.snap | 6 + .../syntax/snapshots/statement_block.snap | 6 + .../syntax/snapshots/statement_break.snap | 6 + .../nasl/syntax/snapshots/statement_call.snap | 6 + .../syntax/snapshots/statement_continue.snap | 6 + .../syntax/snapshots/statement_declare.snap | 6 + .../nasl/syntax/snapshots/statement_div.snap | 6 + .../nasl/syntax/snapshots/statement_exit.snap | 6 + .../nasl/syntax/snapshots/statement_for.snap | 6 + .../syntax/snapshots/statement_foreach.snap | 6 + .../statement_function_declaration.snap | 6 + .../nasl/syntax/snapshots/statement_if.snap | 6 + .../syntax/snapshots/statement_include.snap | 6 + .../syntax/snapshots/statement_modulo.snap | 6 + .../nasl/syntax/snapshots/statement_mul.snap | 6 + .../snapshots/statement_named_parameter.snap | 6 + .../syntax/snapshots/statement_no_op.snap | 6 + .../syntax/snapshots/statement_parameter.snap | 6 + .../syntax/snapshots/statement_primitive.snap | 6 + .../syntax/snapshots/statement_repeat.snap | 6 + .../syntax/snapshots/statement_return.snap | 6 + .../snapshots/statement_return_assign.snap | 6 + .../nasl/syntax/snapshots/statement_sub.snap | 6 + .../syntax/snapshots/statement_variable.snap | 6 + .../syntax/snapshots/statement_while.snap | 6 + rust/src/nasl/syntax/statement.rs | 167 +++++++++++++++++- rust/src/nasl/syntax/token.rs | 8 + 32 files changed, 401 insertions(+), 5 deletions(-) create mode 100644 rust/src/nasl/syntax/snapshots/statement_add.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_array.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_assign.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_assign_return.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_block.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_break.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_call.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_continue.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_declare.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_div.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_exit.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_for.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_foreach.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_function_declaration.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_if.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_include.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_modulo.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_mul.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_named_parameter.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_no_op.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_parameter.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_primitive.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_repeat.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_return.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_return_assign.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_sub.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_variable.snap create mode 100644 rust/src/nasl/syntax/snapshots/statement_while.snap diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 2c201289c..8e274e6a0 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -264,6 +264,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "base64" version = "0.21.7" @@ -659,6 +665,18 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e57e3272f0190c3f1584272d613719ba5fc7df7f4942fe542e63d949cf3a649b" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -1075,6 +1093,12 @@ dependencies = [ "log", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -1818,6 +1842,20 @@ dependencies = [ "generic-array 0.14.7", ] +[[package]] +name = "insta" +version = "1.41.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8" +dependencies = [ + "console", + "lazy_static", + "linked-hash-map", + "ron", + "serde", + "similar", +] + [[package]] name = "ipnet" version = "2.10.1" @@ -2100,6 +2138,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -3093,6 +3137,17 @@ dependencies = [ "digest", ] +[[package]] +name = "ron" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" +dependencies = [ + "base64 0.13.1", + "bitflags 1.3.2", + "serde", +] + [[package]] name = "rsa" version = "0.9.7" @@ -3414,6 +3469,7 @@ dependencies = [ "hyper 1.5.1", "hyper-rustls 0.27.3", "hyper-util", + "insta", "itertools 0.12.1", "lazy-regex", "lazy_static", @@ -3741,6 +3797,12 @@ dependencies = [ "rand_core", ] +[[package]] +name = "similar" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e" + [[package]] name = "siphasher" version = "0.3.11" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c215b4e75..f04ba6f8e 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -107,6 +107,7 @@ members = ["crates/smoketest", "crates/nasl-function-proc-macro"] tracing-test = "0.2.5" criterion = "0" once_cell = "1.20.1" +insta = { version = "1.41.1", features = ["ron"] } [features] dep-graph-parallel = ["rayon", "crossbeam-channel"] diff --git a/rust/src/nasl/syntax/snapshots/statement_add.snap b/rust/src/nasl/syntax/snapshots/statement_add.snap new file mode 100644 index 000000000..4a0527d16 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_add.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a + 1 diff --git a/rust/src/nasl/syntax/snapshots/statement_array.snap b/rust/src/nasl/syntax/snapshots/statement_array.snap new file mode 100644 index 000000000..c508c8589 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_array.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a[1] diff --git a/rust/src/nasl/syntax/snapshots/statement_assign.snap b/rust/src/nasl/syntax/snapshots/statement_assign.snap new file mode 100644 index 000000000..22c6abf67 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_assign.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a = 1 diff --git a/rust/src/nasl/syntax/snapshots/statement_assign_return.snap b/rust/src/nasl/syntax/snapshots/statement_assign_return.snap new file mode 100644 index 000000000..0be69a8ee --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_assign_return.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +--a diff --git a/rust/src/nasl/syntax/snapshots/statement_block.snap b/rust/src/nasl/syntax/snapshots/statement_block.snap new file mode 100644 index 000000000..f6920bcec --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_block.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +{ ... } diff --git a/rust/src/nasl/syntax/snapshots/statement_break.snap b/rust/src/nasl/syntax/snapshots/statement_break.snap new file mode 100644 index 000000000..6199a97d4 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_break.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +break diff --git a/rust/src/nasl/syntax/snapshots/statement_call.snap b/rust/src/nasl/syntax/snapshots/statement_call.snap new file mode 100644 index 000000000..3a0ecb893 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_call.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a(); diff --git a/rust/src/nasl/syntax/snapshots/statement_continue.snap b/rust/src/nasl/syntax/snapshots/statement_continue.snap new file mode 100644 index 000000000..75cacb2bb --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_continue.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +continue diff --git a/rust/src/nasl/syntax/snapshots/statement_declare.snap b/rust/src/nasl/syntax/snapshots/statement_declare.snap new file mode 100644 index 000000000..1513504b9 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_declare.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +local_var a diff --git a/rust/src/nasl/syntax/snapshots/statement_div.snap b/rust/src/nasl/syntax/snapshots/statement_div.snap new file mode 100644 index 000000000..13bd85d34 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_div.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a / 1 diff --git a/rust/src/nasl/syntax/snapshots/statement_exit.snap b/rust/src/nasl/syntax/snapshots/statement_exit.snap new file mode 100644 index 000000000..7dc1c4c60 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_exit.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +exit(0); diff --git a/rust/src/nasl/syntax/snapshots/statement_for.snap b/rust/src/nasl/syntax/snapshots/statement_for.snap new file mode 100644 index 000000000..ba899109f --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_for.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +for (i = 0; i < 10; i++) { a } diff --git a/rust/src/nasl/syntax/snapshots/statement_foreach.snap b/rust/src/nasl/syntax/snapshots/statement_foreach.snap new file mode 100644 index 000000000..e25f5981f --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_foreach.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +foreach a(b) {c} diff --git a/rust/src/nasl/syntax/snapshots/statement_function_declaration.snap b/rust/src/nasl/syntax/snapshots/statement_function_declaration.snap new file mode 100644 index 000000000..c061317b6 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_function_declaration.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +function a((b)) { ... } diff --git a/rust/src/nasl/syntax/snapshots/statement_if.snap b/rust/src/nasl/syntax/snapshots/statement_if.snap new file mode 100644 index 000000000..c01520090 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_if.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +if (a) b else c diff --git a/rust/src/nasl/syntax/snapshots/statement_include.snap b/rust/src/nasl/syntax/snapshots/statement_include.snap new file mode 100644 index 000000000..4bfbd669a --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_include.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +include("test.inc"); diff --git a/rust/src/nasl/syntax/snapshots/statement_modulo.snap b/rust/src/nasl/syntax/snapshots/statement_modulo.snap new file mode 100644 index 000000000..928eed6e4 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_modulo.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a % 1 diff --git a/rust/src/nasl/syntax/snapshots/statement_mul.snap b/rust/src/nasl/syntax/snapshots/statement_mul.snap new file mode 100644 index 000000000..292116d8c --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_mul.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a * 1 diff --git a/rust/src/nasl/syntax/snapshots/statement_named_parameter.snap b/rust/src/nasl/syntax/snapshots/statement_named_parameter.snap new file mode 100644 index 000000000..e33ea0677 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_named_parameter.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a: b diff --git a/rust/src/nasl/syntax/snapshots/statement_no_op.snap b/rust/src/nasl/syntax/snapshots/statement_no_op.snap new file mode 100644 index 000000000..263a654e8 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_no_op.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +NoOp diff --git a/rust/src/nasl/syntax/snapshots/statement_parameter.snap b/rust/src/nasl/syntax/snapshots/statement_parameter.snap new file mode 100644 index 000000000..6e1327b20 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_parameter.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +(a, b) diff --git a/rust/src/nasl/syntax/snapshots/statement_primitive.snap b/rust/src/nasl/syntax/snapshots/statement_primitive.snap new file mode 100644 index 000000000..c97bc441d --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_primitive.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +1 diff --git a/rust/src/nasl/syntax/snapshots/statement_repeat.snap b/rust/src/nasl/syntax/snapshots/statement_repeat.snap new file mode 100644 index 000000000..305e6b0b1 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_repeat.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +repeat a until b diff --git a/rust/src/nasl/syntax/snapshots/statement_return.snap b/rust/src/nasl/syntax/snapshots/statement_return.snap new file mode 100644 index 000000000..a47b63448 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_return.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +return 0; diff --git a/rust/src/nasl/syntax/snapshots/statement_return_assign.snap b/rust/src/nasl/syntax/snapshots/statement_return_assign.snap new file mode 100644 index 000000000..8a8e922c0 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_return_assign.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a++ diff --git a/rust/src/nasl/syntax/snapshots/statement_sub.snap b/rust/src/nasl/syntax/snapshots/statement_sub.snap new file mode 100644 index 000000000..5ef6d91e9 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_sub.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a - 1 diff --git a/rust/src/nasl/syntax/snapshots/statement_variable.snap b/rust/src/nasl/syntax/snapshots/statement_variable.snap new file mode 100644 index 000000000..cfb94bcf2 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_variable.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +a diff --git a/rust/src/nasl/syntax/snapshots/statement_while.snap b/rust/src/nasl/syntax/snapshots/statement_while.snap new file mode 100644 index 000000000..e6add70e3 --- /dev/null +++ b/rust/src/nasl/syntax/snapshots/statement_while.snap @@ -0,0 +1,6 @@ +--- +source: src/nasl/syntax/statement.rs +expression: stmt +snapshot_kind: text +--- +while (a) {b} diff --git a/rust/src/nasl/syntax/statement.rs b/rust/src/nasl/syntax/statement.rs index 8a4587a59..78c12c5a4 100644 --- a/rust/src/nasl/syntax/statement.rs +++ b/rust/src/nasl/syntax/statement.rs @@ -5,12 +5,16 @@ use core::fmt; use std::ops::Range; +#[cfg(test)] +use serde::{Deserialize, Serialize}; + use crate::unexpected_statement; use super::{SyntaxError, Token, TokenCategory}; /// Specifies the order of assignment #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(Serialize, Deserialize))] pub enum AssignOrder { /// Assign first than return AssignReturn, @@ -20,6 +24,7 @@ pub enum AssignOrder { /// Is a executable step. #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(Serialize, Deserialize))] pub enum StatementKind { /// Either a Number, String, Boolean or Null Primitive, @@ -27,7 +32,7 @@ pub enum StatementKind { AttackCategory, /// Is a variable Variable, - /// Is a array variable, it contains the lookup token as well as an optional lookup statement + /// Is an array variable, it contains the lookup token as well as an optional lookup statement Array(Option>), /// Is a call of a function Call(Box), @@ -89,10 +94,11 @@ pub enum StatementKind { /// start returns a token of the beginning of that statement while end contains /// the end of the statement. So as an example of the statement: /// 'my_function(1);' start will point to 'my_function' and end to ';'. +#[cfg_attr(test, derive(Serialize, Deserialize))] pub struct Statement { - kind: StatementKind, start: Token, end: Option, + kind: StatementKind, } impl Statement { /// Returns the StatementKind. @@ -408,7 +414,7 @@ impl std::fmt::Display for Statement { StatementKind::Return(x) => write!(f, "return {x};"), StatementKind::Include(x) => write!(f, "include({x});"), StatementKind::Declare(y) => { - write!(f, "{x} {}", as_str_list(y),) + write!(f, "{} {}", x.category(), as_str_list(y),) } StatementKind::Parameter(x) => write!(f, "({})", as_str_list(x),), StatementKind::NamedParameter(s) => write!(f, "{}: {s}", x.category()), @@ -473,11 +479,13 @@ impl StatementKind { } #[cfg(test)] -mod position { +mod tests { + use insta::assert_snapshot; + use super::super::parse; #[test] - fn assignment() { + fn position() { let code = r#" a = 1 + 1; b = 2 * 2; @@ -536,4 +544,153 @@ mod position { assert_eq!(tests, expected.len()); } + + #[track_caller] + fn test_statement(name: &str, code: &str) { + insta::with_settings!({ prepend_module_to_snapshot => false }, { + let mut parser = parse(code); + let stmt = parser.next().unwrap().unwrap(); + assert_snapshot!(format!("statement_{name}"), stmt); + }); + } + + #[test] + fn primitive() { + test_statement("primitive", "1;"); + } + + #[test] + fn variable() { + test_statement("variable", "a;"); + } + + #[test] + fn array() { + test_statement("array", "a[1];"); + } + + #[test] + fn call() { + test_statement("call", "a();"); + } + + #[test] + fn exit() { + test_statement("exit", "exit(0);"); + } + + #[test] + fn return_stmt() { + test_statement("return", "return 0;"); + } + + #[test] + fn break_stmt() { + test_statement("break", "break;"); + } + + #[test] + fn continue_stmt() { + test_statement("continue", "continue;"); + } + + #[test] + fn include() { + test_statement("include", "include(\"test.inc\");"); + } + + #[test] + fn declare() { + test_statement("declare", "local_var a;"); + } + + #[test] + fn parameter() { + test_statement("parameter", "[a, b];"); + } + + #[test] + fn named_parameter() { + test_statement("named_parameter", "a: b;"); + } + + #[test] + fn assign() { + test_statement("assign", "a = 1;"); + } + + #[test] + fn add() { + test_statement("add", "a + 1;"); + } + + #[test] + fn sub() { + test_statement("sub", "a - 1;"); + } + + #[test] + fn mul() { + test_statement("mul", "a * 1;"); + } + + #[test] + fn div() { + test_statement("div", "a / 1;"); + } + + #[test] + fn modulo() { + test_statement("modulo", "a % 1;"); + } + + #[test] + fn return_assign() { + test_statement("return_assign", "a++;"); + } + + #[test] + fn assign_return() { + test_statement("assign_return", "--a;"); + } + + #[test] + fn if_stmt() { + test_statement("if", "if (a) b; else c;"); + } + + #[test] + fn for_stmt() { + test_statement("for", "for (i = 0; i < 10; i++) a;"); + } + + #[test] + fn while_stmt() { + test_statement("while", "while (a) b;"); + } + + #[test] + fn repeat() { + test_statement("repeat", "repeat a; until b;"); + } + + #[test] + fn foreach() { + test_statement("foreach", "foreach a(b) c;"); + } + + #[test] + fn block() { + test_statement("block", "{ a; }"); + } + + #[test] + fn function_declaration() { + test_statement("function_declaration", "function a(b) {c;}"); + } + + #[test] + fn no_op() { + test_statement("no_op", ";"); + } } diff --git a/rust/src/nasl/syntax/token.rs b/rust/src/nasl/syntax/token.rs index 09c5c3bf5..8a5744c03 100644 --- a/rust/src/nasl/syntax/token.rs +++ b/rust/src/nasl/syntax/token.rs @@ -6,11 +6,15 @@ use std::fmt::Display; use std::ops::Range; +#[cfg(test)] +use serde::{Deserialize, Serialize}; + use super::cursor::Cursor; use crate::storage::item::ACT; /// Identifies if number is base10, base 8, hex or binary #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(Serialize, Deserialize))] pub enum Base { /// Base 2: contains 01 is defined by 0b e.g.: `0b010101` Binary, @@ -60,6 +64,7 @@ impl Base { /// Is used to identify which Category type is unclosed #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(Serialize, Deserialize))] pub enum UnclosedCategory { /// Is a unclosed String. String, @@ -121,6 +126,7 @@ impl Display for IdentifierType { /// Unless Dynamic those are reserved words that cannot be reused otherwise. #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(Serialize, Deserialize))] pub enum IdentifierType { /// function declaration Function, @@ -201,6 +207,7 @@ make_keyword_matcher! { /// Is used to identify a Token #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(Serialize, Deserialize))] pub enum Category { /// `(` LeftParen, @@ -396,6 +403,7 @@ impl Display for Category { #[derive(Clone, Debug, PartialEq, Eq)] /// Contains the TokenType as well as the position. +#[cfg_attr(test, derive(Serialize, Deserialize))] pub struct Token { /// The category or kind of a token pub category: Category,