1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-05 15:16:07 +02:00

Feature/#8 datetime (#152)

This commit is contained in:
3timeslazy 2018-10-30 22:59:18 +03:00 committed by Tim Voronov
parent f6e465d556
commit d8ae6cca8b
4 changed files with 86 additions and 1 deletions

View File

@ -0,0 +1,9 @@
package datetime
import "github.com/MontFerret/ferret/pkg/runtime/core"
func NewLib() map[string]core.Function {
return map[string]core.Function{
"NOW": Now,
}
}

View File

@ -0,0 +1,20 @@
package datetime
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
// Now returns new DateTime object with Time equal to time.Now().
// @returns (DateTime) - New DateTime object.
func Now(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 0, 0)
if err != nil {
return values.None, err
}
return values.NewCurrentDateTime(), nil
}

View File

@ -0,0 +1,57 @@
package datetime_test
import (
"context"
"testing"
"time"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/datetime"
. "github.com/smartystreets/goconvey/convey"
)
type testCase struct {
Name string
Expected core.Value
TimeArg time.Time
Args []core.Value
ShouldErr bool
}
func TestNow(t *testing.T) {
tcs := []*testCase{
&testCase{
Name: "When too many arguments",
Expected: values.None,
Args: []core.Value{
values.NewCurrentDateTime(),
},
ShouldErr: true,
},
}
for _, tc := range tcs {
tc.Do(t)
}
}
func (tc *testCase) Do(t *testing.T) {
Convey(tc.Name, t, func() {
var expected core.Value
expected = values.NewDateTime(tc.TimeArg)
dt, err := datetime.Now(context.Background(), tc.Args...)
if tc.ShouldErr {
So(err, ShouldBeError)
expected = values.None
} else {
So(err, ShouldBeNil)
}
So(dt, ShouldEqual, expected)
})
}

View File

@ -5,7 +5,6 @@ import (
"testing"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/strings"
. "github.com/smartystreets/goconvey/convey"