-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathassert_dates.sh
More file actions
159 lines (134 loc) · 4.25 KB
/
assert_dates.sh
File metadata and controls
159 lines (134 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
function bashunit::date::to_epoch() {
local input="$1"
# Already epoch seconds (all digits)
case "$input" in
*[!0-9]*) ;; # contains non-digits, continue to ISO parsing
*)
echo "$input"
return 0
;;
esac
# Format conversion (GNU vs BSD date)
local epoch
# Try GNU date first (-d flag)
epoch=$(date -d "$input" +%s 2>/dev/null) && {
echo "$epoch"
return 0
}
# Try BSD date (-j -f flag) with ISO 8601 datetime + timezone offset
epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S%z" "$input" +%s 2>/dev/null) && {
echo "$epoch"
return 0
}
# Try BSD date with ISO 8601 datetime format
epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S" "$input" +%s 2>/dev/null) && {
echo "$epoch"
return 0
}
# Try BSD date with space-separated datetime format
epoch=$(date -j -f "%Y-%m-%d %H:%M:%S" "$input" +%s 2>/dev/null) && {
echo "$epoch"
return 0
}
# Try BSD date with date-only format
epoch=$(date -j -f "%Y-%m-%d" "$input" +%s 2>/dev/null) && {
echo "$epoch"
return 0
}
# Unsupported format
echo "$input"
return 1
}
function assert_date_equals() {
bashunit::assert::should_skip && return 0
local expected
expected="$(bashunit::date::to_epoch "$1")"
local actual
actual="$(bashunit::date::to_epoch "$2")"
if [[ "$actual" -ne "$expected" ]]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be equal to" "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_date_before() {
bashunit::assert::should_skip && return 0
local expected
expected="$(bashunit::date::to_epoch "$1")"
local actual
actual="$(bashunit::date::to_epoch "$2")"
if ! [[ "$actual" -lt "$expected" ]]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be before" "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_date_after() {
bashunit::assert::should_skip && return 0
local expected
expected="$(bashunit::date::to_epoch "$1")"
local actual
actual="$(bashunit::date::to_epoch "$2")"
if ! [[ "$actual" -gt "$expected" ]]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be after" "${expected}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_date_within_range() {
bashunit::assert::should_skip && return 0
local from
from="$(bashunit::date::to_epoch "$1")"
local to
to="$(bashunit::date::to_epoch "$2")"
local actual
actual="$(bashunit::date::to_epoch "$3")"
if [[ "$actual" -lt "$from" ]] || [[ "$actual" -gt "$to" ]]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be between" "${from} and ${to}"
return
fi
bashunit::state::add_assertions_passed
}
function assert_date_within_delta() {
bashunit::assert::should_skip && return 0
local expected
expected="$(bashunit::date::to_epoch "$1")"
local actual
actual="$(bashunit::date::to_epoch "$2")"
local delta="$3"
local diff=$((actual - expected))
if [[ "$diff" -lt 0 ]]; then
diff=$((-diff))
fi
if [[ "$diff" -gt "$delta" ]]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be within" "${delta} seconds of ${expected}"
return
fi
bashunit::state::add_assertions_passed
}