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

     1  package datetime_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  
    10  	"github.com/MontFerret/ferret/pkg/stdlib/datetime"
    11  )
    12  
    13  func TestDate(t *testing.T) {
    14  	now := time.Now()
    15  
    16  	tcs := []*testCase{
    17  		{
    18  			Name:     "When more than 2 arguments",
    19  			Expected: values.None,
    20  			Args: []core.Value{
    21  				values.NewString(time.Now().Format(time.RFC3339)),
    22  				values.NewString(time.RFC3339),
    23  				values.NewString("foo"),
    24  			},
    25  			ShouldErr: true,
    26  		},
    27  		{
    28  			Name:      "When 0 arguments",
    29  			Expected:  values.None,
    30  			Args:      []core.Value{},
    31  			ShouldErr: true,
    32  		},
    33  		{
    34  			Name:      "When first argument isn't string",
    35  			Expected:  values.None,
    36  			Args:      []core.Value{values.NewInt(0)},
    37  			ShouldErr: true,
    38  		},
    39  		{
    40  			Name:     "When incorrect timeStrings",
    41  			Expected: values.None,
    42  			Args: []core.Value{
    43  				values.NewString("bla-bla"),
    44  			},
    45  			ShouldErr: true,
    46  		},
    47  		{
    48  			Name:     "When string is in default format",
    49  			Expected: mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
    50  			Args: []core.Value{
    51  				values.NewString("1999-02-07T15:04:05Z"),
    52  			},
    53  		},
    54  		{
    55  			Name:     "When second argument isn't string",
    56  			Expected: values.None,
    57  			Args: []core.Value{
    58  				values.NewString("1999-02-07T15:04:05Z"),
    59  				values.NewInt(1),
    60  			},
    61  			ShouldErr: true,
    62  		},
    63  		{
    64  			Name:     "When string is in custom format",
    65  			Expected: mustLayoutDt(time.RFC822, now.Format(time.RFC822)),
    66  			Args: []core.Value{
    67  				values.NewString(now.Format(time.RFC822)),
    68  				values.NewString(time.RFC822),
    69  			},
    70  			ShouldErr: false,
    71  		},
    72  	}
    73  
    74  	for _, tc := range tcs {
    75  		tc.Do(t, datetime.Date)
    76  	}
    77  }