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

     1  package datetime_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  	"github.com/MontFerret/ferret/pkg/stdlib/datetime"
     9  )
    10  
    11  func TestDateCompare(t *testing.T) {
    12  	expectedTrue := values.NewBoolean(true)
    13  	expectedFalse := values.NewBoolean(false)
    14  
    15  	tcs := []*testCase{
    16  		&testCase{
    17  			Name:      "When less than 3 arguments",
    18  			Expected:  values.None,
    19  			Args:      []core.Value{values.NewInt(0), values.NewInt(0)},
    20  			ShouldErr: true,
    21  		},
    22  		&testCase{
    23  			Name:     "When more than 4 arguments",
    24  			Expected: values.None,
    25  			Args: []core.Value{
    26  				values.NewInt(0), values.NewInt(0), values.NewInt(0),
    27  				values.NewInt(0), values.NewInt(0),
    28  			},
    29  			ShouldErr: true,
    30  		},
    31  		&testCase{
    32  			Name:     "when wrong type of arguments",
    33  			Expected: values.None,
    34  			Args: []core.Value{
    35  				values.NewCurrentDateTime(),
    36  				values.NewCurrentDateTime(),
    37  				values.NewInt(0),
    38  			},
    39  			ShouldErr: true,
    40  		},
    41  		&testCase{
    42  			Name:     "when wrong type of optional argument",
    43  			Expected: values.None,
    44  			Args: []core.Value{
    45  				values.NewCurrentDateTime(),
    46  				values.NewCurrentDateTime(),
    47  				values.NewString("year"),
    48  				values.NewInt(0),
    49  			},
    50  			ShouldErr: true,
    51  		},
    52  		&testCase{
    53  			Name:     "when start unit less that end unit",
    54  			Expected: values.None,
    55  			Args: []core.Value{
    56  				values.NewCurrentDateTime(),
    57  				values.NewCurrentDateTime(),
    58  				values.NewString("day"),
    59  				values.NewString("year"),
    60  			},
    61  			ShouldErr: true,
    62  		},
    63  		&testCase{
    64  			Name:     "when years are equal",
    65  			Expected: expectedTrue,
    66  			Args: []core.Value{
    67  				values.NewCurrentDateTime(),
    68  				values.NewCurrentDateTime(),
    69  				values.NewString("year"),
    70  			},
    71  		},
    72  		&testCase{
    73  			Name:     "when years are not equal",
    74  			Expected: expectedFalse,
    75  			Args: []core.Value{
    76  				mustLayoutDt("2006-01-02", "1999-02-07"),
    77  				mustLayoutDt("2006-01-02", "2000-02-07"),
    78  				values.NewString("year"),
    79  				values.NewString("year"),
    80  			},
    81  		},
    82  		&testCase{
    83  			Name:     "when months are equal",
    84  			Expected: expectedTrue,
    85  			Args: []core.Value{
    86  				mustLayoutDt("2006-01-02", "1999-02-07"),
    87  				mustLayoutDt("2006-01-02", "2000-02-09"),
    88  				values.NewString("year"),
    89  				values.NewString("days"),
    90  			},
    91  		},
    92  		&testCase{
    93  			Name:     "when days are equal",
    94  			Expected: expectedTrue,
    95  			Args: []core.Value{
    96  				values.NewCurrentDateTime(),
    97  				values.NewCurrentDateTime(),
    98  				values.NewString("days"),
    99  				values.NewString("days"),
   100  			},
   101  		},
   102  	}
   103  
   104  	for _, tc := range tcs {
   105  		tc.Do(t, datetime.DateCompare)
   106  	}
   107  }