-
Notifications
You must be signed in to change notification settings - Fork 7
/
0-root_test.go
89 lines (76 loc) · 2.71 KB
/
0-root_test.go
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
package prayer_test
import (
"fmt"
"testing"
"time"
"github.com/hablullah/go-prayer"
"github.com/hablullah/go-prayer/internal/datatest"
)
func TestCalculate(t *testing.T) {
testCalculate(t, datatest.Tromso) // North Frigid
testCalculate(t, datatest.London) // North Temperate
testCalculate(t, datatest.Jakarta) // Torrid
testCalculate(t, datatest.Wellington) // South Temperate
}
func testCalculate(t *testing.T, td datatest.TestData) {
// Calculate schedules
schedules, err := prayer.Calculate(prayer.Config{
Latitude: td.Latitude,
Longitude: td.Longitude,
Timezone: td.Timezone,
TwilightConvention: prayer.AstronomicalTwilight(),
AsrConvention: prayer.Shafii,
HighLatitudeAdapter: prayer.NearestLatitude(),
PreciseToSeconds: true,
}, 2023)
msg := fmt.Sprintf("schedule in %s has error: %v", td.Name, err)
assertNil(t, err, msg)
nExpected, nResult := len(td.Schedules), len(schedules)
msg = fmt.Sprintf("%s schedule size: want %d got %d", td.Name, nExpected, nResult)
assertEqual(t, nExpected, nResult, msg)
for i := range schedules {
result := schedules[i]
expected := td.Schedules[i]
assertSchedule(t, td, expected, result)
}
}
func assertSchedule(t *testing.T, td datatest.TestData, e, r prayer.Schedule) {
// Calculate diff
diffFajr := e.Fajr.Sub(r.Fajr).Abs()
diffSunrise := e.Sunrise.Sub(r.Sunrise).Abs()
diffZuhr := e.Zuhr.Sub(r.Zuhr).Abs()
diffAsr := e.Asr.Sub(r.Asr).Abs()
diffMaghrib := e.Maghrib.Sub(r.Maghrib).Abs()
diffIsha := e.Isha.Sub(r.Isha).Abs()
// Prepare log message
msgFormat := "%s, %s => want %q got %q (%v)"
fajrMsg := fmt.Sprintf(msgFormat, td.Name, "Fajr", e.Fajr, r.Fajr, diffFajr)
sunriseMsg := fmt.Sprintf(msgFormat, td.Name, "Sunrise", e.Sunrise, r.Sunrise, diffSunrise)
zuhrMsg := fmt.Sprintf(msgFormat, td.Name, "Zuhr", e.Zuhr, r.Zuhr, diffZuhr)
asrMsg := fmt.Sprintf(msgFormat, td.Name, "Asr", e.Asr, r.Asr, diffAsr)
maghribMsg := fmt.Sprintf(msgFormat, td.Name, "Maghrib", e.Maghrib, r.Maghrib, diffMaghrib)
ishaMsg := fmt.Sprintf(msgFormat, td.Name, "Isha", e.Isha, r.Isha, diffIsha)
// Diff only allowed up to 5 seconds
maxDiff := 5 * time.Second
assertLTE(t, diffFajr, maxDiff, fajrMsg)
assertLTE(t, diffSunrise, maxDiff, sunriseMsg)
assertLTE(t, diffZuhr, maxDiff, zuhrMsg)
assertLTE(t, diffAsr, maxDiff, asrMsg)
assertLTE(t, diffMaghrib, maxDiff, maghribMsg)
assertLTE(t, diffIsha, maxDiff, ishaMsg)
}
func assertNil(t *testing.T, v any, msg string) {
if v != nil {
t.Error(msg)
}
}
func assertEqual[T comparable](t *testing.T, a T, b T, msg string) {
if a != b {
t.Error(msg)
}
}
func assertLTE[T int | float64 | time.Duration](t *testing.T, a T, b T, msg string) {
if a > b {
t.Error(msg)
}
}