github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/datetime/helpers_test.go (about)

     1  package datetime_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/MontFerret/ferret/pkg/runtime/core"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values"
    10  
    11  	. "github.com/smartystreets/goconvey/convey"
    12  )
    13  
    14  type testCase struct {
    15  	Name      string
    16  	Expected  core.Value
    17  	Args      []core.Value
    18  	ShouldErr bool
    19  }
    20  
    21  func (tc *testCase) Do(t *testing.T, fn core.Function) {
    22  	Convey(tc.Name, t, func() {
    23  		expected := tc.Expected
    24  
    25  		actual, err := fn(context.Background(), tc.Args...)
    26  
    27  		if tc.ShouldErr {
    28  			So(err, ShouldBeError)
    29  			expected = values.None
    30  		} else {
    31  			So(err, ShouldBeNil)
    32  		}
    33  
    34  		So(actual.Type().Equals(expected.Type()), ShouldBeTrue)
    35  		So(actual.Compare(expected), ShouldEqual, 0)
    36  	})
    37  }
    38  
    39  func mustDefaultLayoutDt(timeString string) values.DateTime {
    40  	dt, err := defaultLayoutDt(timeString)
    41  
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  
    46  	return dt
    47  }
    48  
    49  func mustLayoutDt(layout, value string) values.DateTime {
    50  	dt, err := layoutDt(layout, value)
    51  
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  
    56  	return dt
    57  }
    58  
    59  func defaultLayoutDt(timeString string) (values.DateTime, error) {
    60  	return layoutDt(values.DefaultTimeLayout, timeString)
    61  }
    62  
    63  func layoutDt(layout, value string) (values.DateTime, error) {
    64  	t, err := time.Parse(layout, value)
    65  
    66  	if err != nil {
    67  		return values.DateTime{}, err
    68  	}
    69  
    70  	return values.NewDateTime(t), nil
    71  }