Skip to content

Commit 3607640

Browse files
committed
Add better examples
1 parent d61393d commit 3607640

File tree

2 files changed

+84
-45
lines changed

2 files changed

+84
-45
lines changed

docs/examples/dates_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package examples
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/antonmedv/expr"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestExamples_dates(t *testing.T) {
12+
code := `
13+
Now() > Date("2020-01-01") &&
14+
Now() - CreatedAt > Duration("24h")
15+
`
16+
17+
options := []expr.Option{
18+
expr.Env(Env{}),
19+
20+
// Operators override for date comprising.
21+
expr.Operator("==", "Equal"),
22+
expr.Operator("<", "Before"),
23+
expr.Operator("<=", "BeforeOrEqual"),
24+
expr.Operator(">", "After"),
25+
expr.Operator(">=", "AfterOrEqual"),
26+
27+
// Time and duration manipulation.
28+
expr.Operator("+", "Add"),
29+
expr.Operator("-", "Sub"),
30+
31+
// Operators override for duration comprising.
32+
expr.Operator("==", "EqualDuration"),
33+
expr.Operator("<", "BeforeDuration"),
34+
expr.Operator("<=", "BeforeOrEqualDuration"),
35+
expr.Operator(">", "AfterDuration"),
36+
expr.Operator(">=", "AfterOrEqualDuration"),
37+
}
38+
39+
program, err := expr.Compile(code, options...)
40+
require.NoError(t, err)
41+
42+
env := Env{
43+
CreatedAt: Env{}.Date("2018-07-14"), // first commit date
44+
}
45+
46+
output, err := expr.Run(program, env)
47+
require.NoError(t, err)
48+
require.Equal(t, true, output)
49+
}
50+
51+
type Env struct {
52+
datetime
53+
CreatedAt time.Time
54+
}
55+
56+
type datetime struct{}
57+
58+
func (datetime) Date(s string) time.Time {
59+
t, err := time.Parse("2006-01-02", s)
60+
if err != nil {
61+
panic(err)
62+
}
63+
return t
64+
}
65+
func (datetime) Duration(s string) time.Duration {
66+
d, err := time.ParseDuration(s)
67+
if err != nil {
68+
panic(err)
69+
}
70+
return d
71+
}
72+
func (datetime) Now() time.Time { return time.Now() }
73+
func (datetime) Equal(a, b time.Time) bool { return a.Equal(b) }
74+
func (datetime) Before(a, b time.Time) bool { return a.Before(b) }
75+
func (datetime) BeforeOrEqual(a, b time.Time) bool { return a.Before(b) || a.Equal(b) }
76+
func (datetime) After(a, b time.Time) bool { return a.After(b) }
77+
func (datetime) AfterOrEqual(a, b time.Time) bool { return a.After(b) || a.Equal(b) }
78+
func (datetime) Add(a time.Time, b time.Duration) time.Time { return a.Add(b) }
79+
func (datetime) Sub(a, b time.Time) time.Duration { return a.Sub(b) }
80+
func (datetime) EqualDuration(a, b time.Duration) bool { return a == b }
81+
func (datetime) BeforeDuration(a, b time.Duration) bool { return a < b }
82+
func (datetime) BeforeOrEqualDuration(a, b time.Duration) bool { return a <= b }
83+
func (datetime) AfterDuration(a, b time.Duration) bool { return a > b }
84+
func (datetime) AfterOrEqualDuration(a, b time.Duration) bool { return a >= b }

docs/examples/demo.go

-45
This file was deleted.

0 commit comments

Comments
 (0)