-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdurationfmt_translator_test.go
110 lines (99 loc) · 4.34 KB
/
durationfmt_translator_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package xl8r
import (
"fmt"
"strings"
"testing"
)
/***********************************************************************
* PLEASE BE ADVISED: *
* The conversions represented here are only for feature illustration *
* and testing and purposes. *
* Some of the conversions may not be 100% accurate. *
***********************************************************************/
var definedDurationFmtTestCodecs = []Codec[durationValue, *durationHubData]{
// order is not important here
// just using alphabetical for readability
fetchColonDlmHMSCodec(), // _:_:_ | _ : _ : _
fetchHHMMSSCodec(), // _ hh _ mm _ ss | _hh _mm _ss | _hh_mm_ss
fetchMinutesCodec(), // _ min[s|utes]
// etc ...
}
func TestDisplayIn_X_Format(t *testing.T) {
// a demo function that utilizes our Spoke-N-Hub Translator ...
displayDurationIn := func(requestedFormat string, stuff string, opts0 ...Opts) (r string, e error) {
translateDurationFormat, err := New(definedDurationFmtTestCodecs...)
if err != nil {
e = err
return
}
format := strings.ToLower(strings.TrimSpace(requestedFormat))
if !translateDurationFormat.Knows(format) {
e = fmt.Errorf("unknown format [ %s ]", requestedFormat)
return
}
sourceContent := durationValue(stuff)
var sourceFormat string
if origins := translateDurationFormat.Origins(sourceContent); len(origins) > 0 {
sourceFormat = origins[0]
} else {
e = fmt.Errorf(`no available interpreter for "%s"`, stuff)
return
}
if toR, toErr := translateDurationFormat.To(format, sourceFormat, durationValue(stuff), opts0...); toErr == nil {
r = string(toR)
} else {
e = toErr
}
return
}
sixtyMins := "60 mins"
sixtyMinsAndAHalf := "60.5 minute"
oneHourAndAHalfHMS := "01:30:00"
mins2600 := "2600 min"
formattingOpt := &Opts{
Dec: map[string]any{
"precision": 2, // we made up parameter that specifies to use 2 decimal places
},
}
tt := []struct {
durationValue, formatToUse, expected string
expectedErr error
useOpt *Opts
}{
{durationValue: sixtyMins, formatToUse: "minutes", expected: "60.000000 minutes"},
{durationValue: sixtyMins, formatToUse: "hhmmss", expected: "1hh 0mm 0ss"},
{durationValue: sixtyMins, formatToUse: "hh:mm:ss", expected: "1:0:0"},
{durationValue: sixtyMinsAndAHalf, formatToUse: "minutes", expected: "60.500000 minutes"},
{durationValue: sixtyMinsAndAHalf, formatToUse: "hhmmss", expected: "1hh 0mm 30ss"},
{durationValue: sixtyMinsAndAHalf, formatToUse: "hh:mm:ss", expected: "1:0:30"},
{durationValue: oneHourAndAHalfHMS, formatToUse: "minutes", expected: "90.000000 minutes"},
{durationValue: oneHourAndAHalfHMS, formatToUse: "hhmmss", expected: "1hh 30mm 0ss"},
{durationValue: oneHourAndAHalfHMS, formatToUse: "hh:mm:ss", expected: "1:30:0"},
{durationValue: mins2600, formatToUse: "minutes", expected: "2600.000000 minutes"},
{durationValue: mins2600, formatToUse: "hhmmss", expected: "43hh 20mm 0ss"},
{durationValue: mins2600, formatToUse: "hh:mm:ss", expected: "43:20:0"},
{durationValue: sixtyMins, formatToUse: "minutes", expected: "60.00 minutes", useOpt: formattingOpt},
{durationValue: sixtyMinsAndAHalf, formatToUse: "minutes", expected: "60.50 minutes", useOpt: formattingOpt},
{durationValue: oneHourAndAHalfHMS, formatToUse: "minutes", expected: "90.00 minutes", useOpt: formattingOpt},
{durationValue: mins2600, formatToUse: "minutes", expected: "2600.00 minutes", useOpt: formattingOpt},
{durationValue: mins2600, formatToUse: "hhmmss", expected: "43hh 20mm 0ss", useOpt: formattingOpt}, // our "hhmmss" codec ignores options
{durationValue: mins2600, formatToUse: "hh:mm:ss", expected: "43:20:0", useOpt: formattingOpt}, // our "hh:mm:ss" codec ignores options
}
for i, tx := range tt {
var result string
var err error
if opt := tx.useOpt; opt != nil {
result, err = displayDurationIn(tx.formatToUse, tx.durationValue, *tx.useOpt)
} else {
result, err = displayDurationIn(tx.formatToUse, tx.durationValue)
}
assrtEqual(t, tx.expected, result)
assrtEqual(t, err, tx.expectedErr)
var errMsg string
if err != nil {
errMsg = err.Error()
}
t.Logf(`# %d: displayDurationIn("%s","%s") ==>> "%s" %s`,
i, tx.formatToUse, tx.durationValue, result, errMsg)
}
}