Skip to content
This repository was archived by the owner on Apr 27, 2023. It is now read-only.

Commit 51c9972

Browse files
committed
Add more diagnostics tests
1 parent b70ec4f commit 51c9972

19 files changed

+195
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
~foo{<
2+
// Do something with foo
3+
// foo is cool name right?
4+
>}
5+
6+
// Create anouther foo
7+
~foo{<
8+
// Here we go again
9+
>}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Error(runtime::already_declared)
2+
3+
💥 Identifier already declared
4+
╭─[tests/diagnostics/already_declared_function.oy:1:1]
5+
1 │ ~foo{<
6+
· ─┬─
7+
· ╰── Identifier `foo` already declared here
8+
2 │ // Do something with foo
9+
3 │ // foo is cool name right?
10+
4 │ >}
11+
5 │
12+
6 │ // Create anouther foo
13+
7 │ ~foo{<
14+
· ─┬─
15+
· ╰── And you tried to declare it again here
16+
8 │ // Here we go again
17+
9 │ >}
18+
╰────
19+
help: Try renaming `foo` or removing the previous declaration.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
~main<argc><argv>{<
2+
name = "Awiteb";
3+
age = 20;
4+
country = "Saudi Arabia";
5+
name = "Awiteb";
6+
return 0;
7+
>}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Error(runtime::already_declared)
2+
3+
💥 Identifier already declared
4+
╭─[tests/diagnostics/already_declared_variable.oy:1:1]
5+
1 │ ~main<argc><argv>{<
6+
2 │ name = "Awiteb";
7+
· ───────┬───────
8+
· ╰── Identifier `name` already declared here
9+
3 │ age = 20;
10+
4 │ country = "Saudi Arabia";
11+
5 │ name = "Awiteb";
12+
· ───────┬───────
13+
· ╰── And you tried to declare it again here
14+
6 │ return 0;
15+
7 │ >}
16+
╰────
17+
help: Try renaming `name` or removing the previous declaration.

tests/diagnostics/format_error.oy

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
~main<argc><argv>{<
2+
str = format<"Hi {}, {0} you are {} years old"><["Awiteb"]>;
3+
println<[str]>;
4+
return 0;
5+
>}

tests/diagnostics/format_error.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Error(runtime::format)
2+
3+
💥 Format error
4+
╭─[tests/diagnostics/format_error.oy:1:1]
5+
1 │ ~main<argc><argv>{<
6+
2 │ str = format<"Hi {}, {0} you are {} years old"><["Awiteb"]>;
7+
· ──────────────────────────┬──────────────────────────
8+
· ╰── Too many placeholders without index for format string
9+
3 │ println<[str]>;
10+
4 │ return 0;
11+
5 │ >}
12+
╰────
13+
help: | The format string has 2 placeholders without index
14+
| and there are only 1 arguments, you can add more
15+
| arguments or remove the extra placeholders
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
~main<argc><argv>{<
2+
// Doing some stuff here, but you can't see it (:
3+
4+
return 284;
5+
>}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Error(runtime::functions::invalid_exit_code)
2+
3+
💥 Invalid exit code
4+
╭─[tests/diagnostics/invalid_exit_code.oy:1:1]
5+
1 │ ~main<argc><argv>{<
6+
2 │ // Doing some stuff here, but you can't see it (:
7+
3 │
8+
4 │ return 284;
9+
· ─┬─
10+
· ╰── Invalid exit code `284`
11+
5 │ >}
12+
╰────
13+
help: Try using a number between 0 and 255.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
~foo<name>{<
2+
return "Hi";
3+
>}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Error(runtime::functions::missing_main)
2+
3+
💥 Missing main function
4+
help: Try adding a main function.

tests/diagnostics/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ macro_rules! test_diagnostics {
2020
if let Err(err) = program {
2121
let diagnostic = err.as_diagnostic(&source, concat!("tests/diagnostics/", stringify!($diagnostic), ".oy"));
2222
assert_eq!(format!("{}", diagnostic), expected);
23-
} else {
24-
panic!("The program has no errors, but it should have one.")
23+
} else if let Err(err) = ocypode_lang::runtime::interpreter::Interpreter::new().interpret(
24+
program.unwrap(),0,vec![]
25+
)
26+
{
27+
let diagnostic = err.as_diagnostic(source, concat!("tests/diagnostics/", stringify!($diagnostic), ".oy").to_string());
28+
assert_eq!(format!("{}", diagnostic), expected);
2529
}
2630
}
2731
)+
@@ -32,11 +36,20 @@ test_diagnostics!(
3236
invalid_function_name
3337
invalid_variable_name
3438
invalid_parameter_name
39+
missing_main_function
3540
main_function_contains_no_parameters
3641
main_function_contains_more_than_tow_parameters
3742
main_function_contains_one_parameter
3843
main_function_contains_one_invalid_parameter
3944
main_function_contains_tow_invalid_parameters
4045
main_function_invalid_second_parameter
4146
main_function_cannot_be_public
47+
undeclared_ident
48+
already_declared_function
49+
already_declared_variable
50+
invalid_exit_code
51+
not_callable
52+
uncorrect_argument
53+
unexpected_type
54+
format_error
4255
);

tests/diagnostics/not_callable.oy

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
~foo<name>{<
2+
return len<name>;
3+
>}
4+
5+
~main<argc><argv>{<
6+
my_var = foo<"Awiteb">;
7+
my_var<>;
8+
return 0;
9+
>}

tests/diagnostics/not_callable.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Error(runtime::idents::not_callable)
2+
3+
💥 Calling a non-function
4+
╭─[tests/diagnostics/not_callable.oy:3:1]
5+
3 │ >}
6+
4 │
7+
5 │ ~main<argc><argv>{<
8+
6 │ my_var = foo<"Awiteb">;
9+
· ───────────┬──────────
10+
· ╰── This is not a function
11+
7 │ my_var<>;
12+
· ────┬───
13+
· ╰── And you tried to call it here
14+
8 │ return 0;
15+
9 │ >}
16+
╰────
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
~foo<name>{<
2+
return len<name>;
3+
>}
4+
5+
~main<argc><argv>{<
6+
my_var = foo<"Awiteb"><99>; // Is take the age right?
7+
return 0;
8+
>}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Error(runtime::functions::uncorrect_arguments)
2+
3+
💥 Uncorrect arguments
4+
╭─[tests/diagnostics/uncorrect_argument.oy:1:1]
5+
1 │ ~foo<name>{<
6+
· ─┬─
7+
· ╰── Function `foo` is defined here
8+
2 │ return len<name>;
9+
3 │ >}
10+
4 │
11+
5 │ ~main<argc><argv>{<
12+
6 │ my_var = foo<"Awiteb"><99>; // Is take the age right?
13+
· ────────┬────────
14+
· ╰── Uncorrect arguments for function `foo`
15+
7 │ return 0;
16+
8 │ >}
17+
╰────
18+
help: `foo` function takes 1 arguments, which are `name`. But you are passed 2 arguments.

tests/diagnostics/undeclared_ident.oy

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
~main<argc><argv>{<
2+
return name;
3+
>}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Error(runtime::idents::undeclared_ident)
2+
3+
💥 Use of undeclared identifier
4+
╭─[tests/diagnostics/undeclared_ident.oy:1:1]
5+
1 │ ~main<argc><argv>{<
6+
2 │ return name;
7+
· ──┬─
8+
· ╰── Undeclared identifier `name`
9+
3 │ >}
10+
╰────
11+
help: Try declaring `name` before using it.

tests/diagnostics/unexpected_type.oy

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
~main<argc><argv>{<
2+
// So I want the length of the integer 43 (:
3+
my_var = len<43>;
4+
return 0;
5+
>}

tests/diagnostics/unexpected_type.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Error(runtime::types::unexpected_type)
2+
3+
💥 Unexpected type
4+
╭─[tests/diagnostics/unexpected_type.oy:1:1]
5+
1 │ ~main<argc><argv>{<
6+
2 │ // So I want the length of the integer 43 (:
7+
3 │ my_var = len<43>;
8+
· ─┬
9+
· ╰── Unexpected type `int`
10+
4 │ return 0;
11+
5 │ >}
12+
╰────
13+
help: Try using a `Array or String` type.

0 commit comments

Comments
 (0)