Skip to content

Commit f6b0aac

Browse files
committed
refactor: src/assert_snapshot
1 parent 9cc65bf commit f6b0aac

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/assert_snapshot.sh

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
#!/usr/bin/env bash
22
# shellcheck disable=SC2155
33

4+
function assert_match_snapshot() {
5+
local actual=$(echo -n "$1" | tr -d '\r')
6+
local snapshot_file=$(snapshot::resolve_file "${2:-}" "${FUNCNAME[1]}")
7+
8+
if [[ ! -f "$snapshot_file" ]]; then
9+
snapshot::initialize "$snapshot_file" "$actual"
10+
return
11+
fi
12+
13+
snapshot::compare "$actual" "$snapshot_file" "${FUNCNAME[1]}"
14+
}
15+
16+
function assert_match_snapshot_ignore_colors() {
17+
local actual=$(echo -n "$1" | sed 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')
18+
local snapshot_file=$(snapshot::resolve_file "${2:-}" "${FUNCNAME[1]}")
19+
20+
if [[ ! -f "$snapshot_file" ]]; then
21+
snapshot::initialize "$snapshot_file" "$actual"
22+
return
23+
fi
24+
25+
snapshot::compare "$actual" "$snapshot_file" "${FUNCNAME[1]}"
26+
}
27+
428
function snapshot::match_with_placeholder() {
529
local actual="$1"
630
local snapshot="$2"
@@ -24,31 +48,7 @@ function snapshot::match_with_placeholder() {
2448
fi
2549
}
2650

27-
function assert_match_snapshot() {
28-
local actual=$(echo -n "$1" | tr -d '\r')
29-
local snapshot_file=$(_resolve_snapshot_file "${2:-}" "${FUNCNAME[1]}")
30-
31-
if [[ ! -f "$snapshot_file" ]]; then
32-
_initialize_snapshot "$snapshot_file" "$actual"
33-
return
34-
fi
35-
36-
_compare_snapshot "$actual" "$snapshot_file" "${FUNCNAME[1]}"
37-
}
38-
39-
function assert_match_snapshot_ignore_colors() {
40-
local actual=$(echo -n "$1" | sed 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')
41-
local snapshot_file=$(_resolve_snapshot_file "${2:-}" "${FUNCNAME[1]}")
42-
43-
if [[ ! -f "$snapshot_file" ]]; then
44-
_initialize_snapshot "$snapshot_file" "$actual"
45-
return
46-
fi
47-
48-
_compare_snapshot "$actual" "$snapshot_file" "${FUNCNAME[1]}"
49-
}
50-
51-
function _resolve_snapshot_file() {
51+
function snapshot::resolve_file() {
5252
local file_hint="$1"
5353
local func_name="$2"
5454

@@ -62,15 +62,15 @@ function _resolve_snapshot_file() {
6262
fi
6363
}
6464

65-
function _initialize_snapshot() {
65+
function snapshot::initialize() {
6666
local path="$1"
6767
local content="$2"
6868
mkdir -p "$(dirname "$path")"
6969
echo "$content" > "$path"
7070
state::add_assertions_snapshot
7171
}
7272

73-
function _compare_snapshot() {
73+
function snapshot::compare() {
7474
local actual="$1"
7575
local snapshot_path="$2"
7676
local func_name="$3"

0 commit comments

Comments
 (0)