-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognizer_test.go
141 lines (133 loc) · 4.4 KB
/
recognizer_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
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
package window
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/araddon/dateparse"
)
func Test_recognitionFail(t *testing.T) {
type test struct {
text string
err string
}
tests := []test{
{"", "failed to recognize the left bound"},
{"a", "failed to recognize the left bound"},
{"WITHIN", "failed to recognize the left bound"},
{"WITHIN one day", "failed to recognize the left bound"},
{"WITHIN 1 2d", "failed to recognize the left bound"},
{"WITHIN 1", "failed to recognize the left bound"},
{"3 days max", "failed to recognize the right bound"},
{"1 April 2022 to", "failed to recognize the right bound"},
{"1 April 2022 to ", "failed to recognize the right bound"},
{"1 minute and 1 ", "failed to recognize the left bound"},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
_, err := Start(tt.text)
minLen := len(tt.err)
if minLen > len(err.Error()) {
minLen = len(err.Error())
}
if err.Error()[:minLen] != tt.err[:minLen] {
t.Errorf("error [%s] should be [%s]", err, tt.err)
}
})
}
}
func Test_recognitionSuccess(t *testing.T) {
type test struct {
text string
windowFunc func() Specification
}
tests := []test{
// 1. Abs-Abs
{"1 Jan 1991 to 2 Feb 1992", func() Specification {
d1, _ := dateparse.ParseStrict("1 Jan 1991")
d2, _ := dateparse.ParseStrict("2 Feb 1992")
return makeSpecification(d1, d2)
}},
// 2. Abs-Rel
{"1 Jan 1991 within 1 day", func() Specification {
d1, _ := dateparse.ParseStrict("1 Jan 1991")
return makeSpecification(d1, 1*24*time.Hour)
}},
{"1 April 2022 within 1 day", func() Specification {
d1, _ := dateparse.ParseStrict("1 Apr 2022")
return makeSpecification(d1, 1*24*time.Hour)
}},
// 3. Abs-RelN
{"1 April 2022 to tomorrow", func() Specification {
d1, _ := dateparse.ParseStrict("1 Apr 2022")
return makeSpecification(d1, boundRelativeToNow{verbal: "tomorrow"})
}},
{"1 Jan 1991 to last week", func() Specification {
d1, _ := dateparse.ParseStrict("1 Jan 1991")
return makeSpecification(d1, boundRelativeToNow{inFuture: false, verbal: "week"})
}},
{"1 Jan 1991 to 1 day ahead", func() Specification {
d1, _ := dateparse.ParseStrict("1 Jan 1991")
return makeSpecification(d1, boundRelativeToNow{inFuture: true, duration: 24 * time.Hour})
}},
{"1 Jan 1991 until now", func() Specification {
d1, _ := dateparse.ParseStrict("1 Jan 1991")
return makeSpecification(d1, boundRelativeToNow{verbal: "now"})
}},
// 4. Rel-Abs
{"yesterday to 1 Apr 2022", func() Specification {
d1, _ := dateparse.ParseStrict("1 Apr 2022")
return makeSpecification(boundRelativeToNow{verbal: "yesterday"}, d1)
}},
// 5. Rel-Rel (Sliding window)
{"3 days", func() Specification {
return makeSpecification(3*24*time.Hour, nil)
}},
{"Within 3 days", func() Specification {
return makeSpecification(3*24*time.Hour, nil)
}},
// 6. Rel-RelN
{"3 days until yesterday", func() Specification {
return makeSpecification(3*24*time.Hour, boundRelativeToNow{verbal: "yesterday"})
}},
{"3 days until last year", func() Specification {
return makeSpecification(3*24*time.Hour, boundRelativeToNow{inFuture: false, verbal: "year"})
}},
{"30 days until 2 days ago", func() Specification {
return makeSpecification(
30*24*time.Hour,
boundRelativeToNow{inFuture: false, duration: 2 * 24 * time.Hour},
)
}},
// 7. RelN-Abs
{"30 days to 1 Apr 2022", func() Specification {
d1, _ := dateparse.ParseStrict("1 Apr 2022")
return makeSpecification(30*24*time.Hour, d1)
}},
// 8. RelN-Rel
{"yesterday within 30 days", func() Specification {
return makeSpecification(boundRelativeToNow{verbal: "yesterday"}, 30*24*time.Hour)
}},
{"next year within 3 days and 2 hours", func() Specification {
return makeSpecification(boundRelativeToNow{inFuture: true, verbal: "year"}, 3*24*time.Hour+2*time.Hour)
}},
// 9. RelN-RelN
{"from last month until 2 hours later", func() Specification {
return makeSpecification(
boundRelativeToNow{inFuture: false, verbal: "month"},
boundRelativeToNow{inFuture: true, duration: 2 * time.Hour},
)
}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
win, err := Start(tt.text)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(win, tt.windowFunc()) {
t.Errorf("window [%v] should be [%v]", win, tt.windowFunc())
}
})
}
}