SeraphScript/src/integration_tests/add_function.rs

370 lines
9.5 KiB
Rust

////////////////////////////
// Automatically Generated//
////////////////////////////
use crate::logger::messages::Message;
use crate::parsing::lexer::lex;
use crate::parsing::lexer::lex_tokens::LexToken;
use crate::parsing::parser::parse;
use crate::parsing::parser::parsed_statement::ParsedStatement;
use crate::span::Span;
fn ignore_error(_msg: Message, _: Span) {
}
fn panic_on_error(msg: Message, _: Span) {
std::panic::panic_any(msg.stringify());
}
#[test]
fn integration_add_function() {
let script = "int add(int a, int b) {
return a + b;
}";
let lexed_tokens = lex(script, &mut panic_on_error);
println!("{}", serde_json::to_string(&lexed_tokens).unwrap());
let expected_tokens: Vec<LexToken> =
serde_json::from_str(r#"[
{
"token_type": "IntKeyword",
"span": {
"start": 0,
"end": 3
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 3,
"end": 4
}
},
{
"token_type": {
"Identifier": "add"
},
"span": {
"start": 4,
"end": 7
}
},
{
"token_type": "OpenBracket",
"span": {
"start": 7,
"end": 8
}
},
{
"token_type": "IntKeyword",
"span": {
"start": 8,
"end": 11
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 11,
"end": 12
}
},
{
"token_type": {
"Identifier": "a"
},
"span": {
"start": 12,
"end": 13
}
},
{
"token_type": "Comma",
"span": {
"start": 13,
"end": 14
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 14,
"end": 15
}
},
{
"token_type": "IntKeyword",
"span": {
"start": 15,
"end": 18
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 18,
"end": 19
}
},
{
"token_type": {
"Identifier": "b"
},
"span": {
"start": 19,
"end": 20
}
},
{
"token_type": "CloseBracket",
"span": {
"start": 20,
"end": 21
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 21,
"end": 22
}
},
{
"token_type": "OpenCurlyBracket",
"span": {
"start": 22,
"end": 23
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 23,
"end": 24
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 24,
"end": 25
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 25,
"end": 26
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 26,
"end": 27
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 27,
"end": 28
}
},
{
"token_type": "ReturnKeyword",
"span": {
"start": 28,
"end": 34
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 34,
"end": 35
}
},
{
"token_type": {
"Identifier": "a"
},
"span": {
"start": 35,
"end": 36
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 36,
"end": 37
}
},
{
"token_type": "Plus",
"span": {
"start": 37,
"end": 38
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 38,
"end": 39
}
},
{
"token_type": {
"Identifier": "b"
},
"span": {
"start": 39,
"end": 40
}
},
{
"token_type": "Semicolon",
"span": {
"start": 40,
"end": 41
}
},
{
"token_type": "WhiteSpace",
"span": {
"start": 41,
"end": 42
}
},
{
"token_type": "CloseCurlyBracket",
"span": {
"start": 42,
"end": 43
}
},
{
"token_type": "EndOfFile",
"span": {
"start": 43,
"end": 43
}
}
]"#).unwrap();
assert_eq!(lexed_tokens, expected_tokens);
let parsed_tree = parse(lexed_tokens, &mut panic_on_error);
println!("{}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{
"Script": {
"statements": [
{
"FuncDeclaration": {
"modifiers": 0,
"field_mod": null,
"is_destructor": false,
"returns_reference": false,
"return_type": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"modifiers": []
}
},
"name": "add",
"param_list": {
"ParamList": {
"parameters": [
{
"parameter_type": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"modifiers": []
}
},
"type_mod": null,
"identifier": "a",
"default": null
},
{
"parameter_type": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"modifiers": []
}
},
"type_mod": null,
"identifier": "b",
"default": null
}
]
}
},
"is_const": false,
"func_attr": 0,
"block": {
"StatBlock": {
"statements": [
{
"ReturnStatement": {
"expression": {
"BinaryExpr": {
"left": {
"VarAccess": {
"scope": null,
"identifier": "a"
}
},
"operator": "Addition",
"right": {
"VarAccess": {
"scope": null,
"identifier": "b"
}
}
}
}
}
}
]
}
}
}
}
]
}
}"#).unwrap();
assert_eq!(parsed_tree, expected_tree);
}
// A substring of a script should never panic, even though it might be completely invalid.
#[test]
fn integration_add_function_substring() {
let mut script = "int add(int a, int b) {
return a + b;
}".to_string();
for _ in 0..script.len() {
script.pop();
let lexed_tokens = lex(script.as_str(), &mut ignore_error);
let _parsed_tree = parse(lexed_tokens, &mut ignore_error);
}
}