Skip to content

Commit be4d34a

Browse files
committed
Incorporate comments from the Peer Review
I modified the description in the When Writing Tests How to Compare Resylt and Expected for Slice FAQ and created the expect_helpers.odin template to provide an official version of the comparison function for string slices and general slices.
1 parent 80d33af commit be4d34a

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

dev/docs/faqs/when_writing_tests_how_to_compare_result_and_expected_for_slices.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,18 @@ When writing tests, the Odin framework provides useful procedures like `testing.
44
Unfortunately, if you want to compare slices, you will get an error stating that `type_is_comparable(T)` evaluates to false.
55

66
There are multiple ways to compare slices, including `slice.equal()` but they don't provide good error messages to the student.
7-
The method that you should use is as follow:
87

9-
1. Copy this helper function into your test file:
8+
The functions included in this [file][expect-helpers] will compare slices for you and, in case they don't match, provide informative error messages:
109

11-
```odin
12-
expect_slices_match :: proc(t: ^testing.T, actual, expected: []$E, loc := #caller_location) {
13-
result := fmt.aprintf("%v", actual)
14-
exp_str := fmt.aprintf("%v", expected)
15-
defer {
16-
delete(result)
17-
delete(exp_str)
18-
}
19-
20-
testing.expect_value(t, result, exp_str, loc = loc)
21-
}
22-
```
10+
- `expect_string_slices :: proc(t: ^testing.T, actual, expected: []string, loc := #caller_location)` compares slices of string and displays the unicode strings in result and expected when the test fails.
11+
- `expect_slices :: proc(t: ^testing.T, actual, expected: []$E, loc := #caller_location)` compares slices of any other type and displays human readable sets for both the result and expected values when the test fails.
2312

2413
2. use the helper function to compare the result and expected values.
2514

2615
```odin
27-
expect_slices_match(t, result, []int{1, 2, 3, 4, 5})
16+
expect_slices(t, result, []int{1, 2, 3, 4, 5})
17+
18+
expect_string_slices(t, result []string{"LISTEN", "Silent"})
2819
```
2920

3021
Why are we asking you to use that specific procedure?
@@ -36,7 +27,9 @@ Using the function above, the message looks like:
3627
[forth_test.odin:24:test_parsing_and_numbers__numbers_just_get_pushed_onto_the_stack()] expected result to be [1, 2, 3, 4], got [1, 2, 3, 4, 5]
3728
```
3829

39-
Note how informative the messages is?
30+
Note how informative the messages is!
4031
It shows both result and expected values in a human-readable form (while using `slice.equal()` would result in 'expected true, got false').
4132
In addition it correctly identifies the line where the test fails (not the line of the `expect_value()` in the helper procedure) and properly identify the result as 'result'.
4233
There are other ways to achieve the same result but this one is battle tested.
34+
35+
[expect-helpers]: https://github.com/exercism/odin/blob/main/dev/templates/expect_helpers.odin

dev/templates/expect_helpers.odin

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package expect_helpers
2+
3+
import "core:fmt"
4+
import "core:testing"
5+
6+
// If you need to compare slices of strings, copy this procedure in your
7+
// `<exercise_slug>_test.odin` and use `expect_strings()` to compare them.
8+
// This function provides informative error reports.
9+
expect_string_slices :: proc(t: ^testing.T, actual, expected: []string, loc := #caller_location) {
10+
result := fmt.aprintf("%s", actual)
11+
exp_str := fmt.aprintf("%s", expected)
12+
defer {
13+
delete(result)
14+
delete(exp_str)
15+
}
16+
17+
testing.expect_value(t, result, exp_str, loc = loc)
18+
}
19+
20+
// If you need to compare slices of any type but string, copy this procedure in your
21+
// `<exercise_slug>_test.odin` and use `expect_slices()` to compare them.
22+
// This function provides informative error reports.
23+
expect_slices :: proc(t: ^testing.T, actual, expected: []$E, loc := #caller_location) {
24+
result := fmt.aprintf("%v", actual)
25+
exp_str := fmt.aprintf("%v", expected)
26+
defer {
27+
delete(result)
28+
delete(exp_str)
29+
}
30+
31+
testing.expect_value(t, result, exp_str, loc = loc)
32+
}

0 commit comments

Comments
 (0)