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

Test output as comment #335

Merged
merged 11 commits into from
Jul 26, 2024
8 changes: 7 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ Amber uses `cargo test` for tests. `stdlib` and `validity` tests usually work by

We have [`validity tests`](src/tests/validity.rs) to check if the compiler outputs a valid bash code, [`stdlib tests`](src/tests/stdlib.rs) and [`CLI tests`](src/tests/cli.rs).

The majority of `stdlib` tests are Written in pure Amber in the folder [`tests/stdlib`](src/tests/stdlib). For every test there is a `*.output.txt` file that contains the expected output.
The majority of `stdlib` tests are written in pure Amber in the folder [`tests/stdlib`](src/tests/stdlib).
For every test there are 3 ways to check the result following this order:

* if a `// Output` comment on top that include the output to match
* if there is a `*.output.txt` file that contains the expected output
* "Succeded" will used as default value if the previous cases are not satisfied

Tests will be executed without recompilation. Amber will load the scripts and verify the output in the designated file to determine if the test passes.
The `validity` tests are full in Amber in their folder the folder [`tests/validity`](src/tests/validity).

Expand Down
31 changes: 26 additions & 5 deletions src/tests/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,32 @@ fn stdlib_test(input: &str) {
let code =
fs::read_to_string(input).unwrap_or_else(|_| panic!("Failed to open {input} test file"));

let output = match Path::new(&input.replace(".ab", ".output.txt")).exists() {
true => fs::read_to_string(input.replace(".ab", ".output.txt"))
.unwrap_or_else(|_| panic!("Failed to open {input}.output.txt file")),
_ => "Succeded".to_string(),
};
let mut is_output = false;
let mut output = "".to_owned();
for line in code.lines() {
if line.starts_with("// Output") {
is_output = true;
continue;
} else if line.is_empty() && is_output {
is_output = false;
break;
}

if is_output {
if ! output.is_empty() {
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
output.push_str("\n");
}
output.push_str(&line.replace("//", "").trim());
}
}

if output.is_empty() {
output = match Path::new(&input.replace(".ab", ".output.txt")).exists() {
true => fs::read_to_string(input.replace(".ab", ".output.txt"))
.expect(&format!("Failed to open {input}.output.txt file")),
_ => "Succeded".to_string()
};
}

test_amber!(code, output);
}
Expand Down
4 changes: 4 additions & 0 deletions src/tests/stdlib/array_first_index.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/array"

// Output
// 2

main {
echo array_first_index([1, 2, 3, 4], 3)
}
1 change: 0 additions & 1 deletion src/tests/stdlib/array_first_index.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/array_search.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/array"

// Output
// 6

main {
let result = array_search([1, 2, 3, 4, 3], 3)
echo result[0]+result[1]
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/array_search.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/chars.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// h e l l o

main {
echo chars("hello")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/chars.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/color_echo.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/env"

// Output
// Hello Amber!

main {
color_echo("Hello Amber!", 33)
}
1 change: 0 additions & 1 deletion src/tests/stdlib/color_echo.output.txt

This file was deleted.

2 changes: 1 addition & 1 deletion src/tests/stdlib/contains.ab
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { contains } from "std/text"
main {
if contains("Hello World", "World") {
echo "found"
echo "Succeded"
}
}
1 change: 0 additions & 1 deletion src/tests/stdlib/contains.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/echo_info.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/env"

// Output
//  Hello Amber! 

main {
echo_info("Hello Amber!")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/echo_info.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/echo_success.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/env"

// Output
//  Hello Amber! 

main {
echo_success("Hello Amber!")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/echo_success.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/echo_warning.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/env"

// Output
//  Hello Amber! 

main {
echo_warning("Hello Amber!")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/echo_warning.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/error.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/env"

// Output
//  Hello Amber! 

main {
error("Hello Amber!", 0)
}
1 change: 0 additions & 1 deletion src/tests/stdlib/error.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/has_failed.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/env"

// Output
// Command failed

main {
if has_failed("ls /nonexistent") {
echo "Command failed"
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/has_failed.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/in_array.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/array"

// Output
// 1

main {
let result = in_array([1, 2, 3, 4, 3], 3)
echo result
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/in_array.output.txt

This file was deleted.

3 changes: 3 additions & 0 deletions src/tests/stdlib/includes_empty_num_array.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * from "std/array"

// Output
// Not Found

main {
let array = [Num]
if includes(array, 0) {
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/includes_empty_num_array.output.txt

This file was deleted.

3 changes: 3 additions & 0 deletions src/tests/stdlib/includes_empty_text_array.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * from "std/array"

// Output
// Not Found

main {
let array = [Text]
if includes(array, " ") {
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/includes_empty_text_array.output.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * from "std/array"

// Output
// Not Found

main {
let array = ["foo", "bar", "baz"]
if includes(array, "oo ba") {
Expand Down

This file was deleted.

3 changes: 3 additions & 0 deletions src/tests/stdlib/includes_prefix_match.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * from "std/array"

// Output
// Not Found

main {
let array = ["apple", "banana cherry"]
if includes(array, "banana") {
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/includes_prefix_match.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/input.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/env"

// Output
// Please enter your name:Hello, Amber

main {
unsafe $echo "Amber" >> /tmp/test_input$
unsafe $exec 0< /tmp/test_input$
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/input.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/join.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// apple,banana,cherry

main {
echo join(["apple", "banana", "cherry"], ", ")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/join.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/len_list.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// 4

main {
echo len([1, 2, 3, 4])
}
1 change: 0 additions & 1 deletion src/tests/stdlib/len_list.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/len_string.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// 5

main {
echo len("hello")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/len_string.output.txt

This file was deleted.

5 changes: 5 additions & 0 deletions src/tests/stdlib/lines.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { lines } from "std/text"

// Output
// line: hello
// line: world

main {
loop line in lines("hello\nworld") {
echo "line: " + line
Expand Down
2 changes: 0 additions & 2 deletions src/tests/stdlib/lines.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/load_env_file.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { load_env_file, get_env_var } from "std/env"
import { file_write } from "std/fs"

// Output
// yes

main {
let tmpdir = unsafe $mktemp -d /tmp/amber-XXXX$
unsafe $cd {tmpdir}$
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/load_env_file.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/lower.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// hello world

main {
echo lower("HELLO WORLD")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/lower.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/make_executable.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { make_executable } from "std/fs"

// Output
// created

main {
let tmpdir = unsafe $mktemp -d /tmp/amber-XXXX$
unsafe $touch {tmpdir}/amber-symbolic$
Expand Down
1 change: 0 additions & 1 deletion src/tests/stdlib/make_executable.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/parse.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// 123

main {
echo parse("123")?
}
1 change: 0 additions & 1 deletion src/tests/stdlib/parse.output.txt

This file was deleted.

3 changes: 3 additions & 0 deletions src/tests/stdlib/printf.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * from "std/env"

// Output
// Hello, Amber!

main {
printf("%s,%s!\n", ["Hello", " Amber"])
}
1 change: 0 additions & 1 deletion src/tests/stdlib/printf.output.txt

This file was deleted.

3 changes: 3 additions & 0 deletions src/tests/stdlib/printf_escape.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * from "std/env"

// Output
// Hello \\v Amber %%T

main {
echo printf_escape("Hello \v Amber %T")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/printf_escape.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/replace.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// apple apple

main {
echo replace("banana banana", "banana", "apple")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/replace.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/replace_regex.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// abc456def

main {
echo replace_regex("abc123def", "[0-9][0-9]*", "456")
}
1 change: 0 additions & 1 deletion src/tests/stdlib/replace_regex.output.txt

This file was deleted.

4 changes: 4 additions & 0 deletions src/tests/stdlib/split.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * from "std/text"

// Output
// banana

main {
let array = split("apple,banana,cherry", ",")
echo array[1]
Expand Down
Loading
Loading