|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
| 3 | +function assert_arrays_equal() { |
| 4 | + bashunit::assert::should_skip && return 0 |
| 5 | + |
| 6 | + local label |
| 7 | + label="$(bashunit::assert::label)" |
| 8 | + |
| 9 | + local -a expected_values=() |
| 10 | + local -a actual_values=() |
| 11 | + local found_separator=false |
| 12 | + local argument |
| 13 | + |
| 14 | + for argument in "$@"; do |
| 15 | + if [ "$found_separator" = false ] && [ "$argument" = "--" ]; then |
| 16 | + found_separator=true |
| 17 | + continue |
| 18 | + fi |
| 19 | + |
| 20 | + if [ "$found_separator" = true ]; then |
| 21 | + actual_values[${#actual_values[@]}]="$argument" |
| 22 | + else |
| 23 | + expected_values[${#expected_values[@]}]="$argument" |
| 24 | + fi |
| 25 | + done |
| 26 | + |
| 27 | + if [ "$found_separator" = false ]; then |
| 28 | + bashunit::assert::mark_failed |
| 29 | + bashunit::console_results::print_failed_test "$label" "--" "but got " "missing array separator" |
| 30 | + return |
| 31 | + fi |
| 32 | + |
| 33 | + if [ "${#expected_values[@]}" -ne "${#actual_values[@]}" ]; then |
| 34 | + bashunit::assert::mark_failed |
| 35 | + bashunit::console_results::print_failed_test \ |
| 36 | + "$label" "${expected_values[*]}" "but got " "${actual_values[*]}" \ |
| 37 | + "Expected length" "${#expected_values[@]}, actual length ${#actual_values[@]}" |
| 38 | + return |
| 39 | + fi |
| 40 | + |
| 41 | + local index |
| 42 | + for ((index = 0; index < ${#expected_values[@]}; index++)); do |
| 43 | + if [ "${expected_values[$index]}" != "${actual_values[$index]}" ]; then |
| 44 | + bashunit::assert::mark_failed |
| 45 | + bashunit::console_results::print_failed_test \ |
| 46 | + "$label" "${expected_values[*]}" "but got " "${actual_values[*]}" \ |
| 47 | + "Different index" "$index" |
| 48 | + return |
| 49 | + fi |
| 50 | + done |
| 51 | + |
| 52 | + bashunit::state::add_assertions_passed |
| 53 | +} |
| 54 | + |
3 | 55 | function assert_array_contains() { |
4 | 56 | bashunit::assert::should_skip && return 0 |
5 | 57 |
|
|
0 commit comments