github.com/decred/dcrlnd@v0.7.6/cmd/dcrlncli/arg_parse_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  var now = time.Date(2017, 11, 10, 7, 8, 9, 1234, time.UTC)
     9  
    10  var partTimeTests = []struct {
    11  	in          string
    12  	expected    uint64
    13  	errExpected bool
    14  }{
    15  	{
    16  		"12345",
    17  		uint64(12345),
    18  		false,
    19  	},
    20  	{
    21  		"-0s",
    22  		uint64(now.Unix()),
    23  		false,
    24  	},
    25  	{
    26  		"-1s",
    27  		uint64(time.Date(2017, 11, 10, 7, 8, 8, 1234, time.UTC).Unix()),
    28  		false,
    29  	},
    30  	{
    31  		"-2h",
    32  		uint64(time.Date(2017, 11, 10, 5, 8, 9, 1234, time.UTC).Unix()),
    33  		false,
    34  	},
    35  	{
    36  		"-3d",
    37  		uint64(time.Date(2017, 11, 7, 7, 8, 9, 1234, time.UTC).Unix()),
    38  		false,
    39  	},
    40  	{
    41  		"-4w",
    42  		uint64(time.Date(2017, 10, 13, 7, 8, 9, 1234, time.UTC).Unix()),
    43  		false,
    44  	},
    45  	{
    46  		"-5M",
    47  		uint64(now.Unix() - 30.44*5*24*60*60),
    48  		false,
    49  	},
    50  	{
    51  		"-6y",
    52  		uint64(now.Unix() - 365.25*6*24*60*60),
    53  		false,
    54  	},
    55  	{
    56  		"-999999999999999999s",
    57  		uint64(now.Unix() - 999999999999999999),
    58  		false,
    59  	},
    60  	{
    61  		"-9999999999999999991s",
    62  		0,
    63  		true,
    64  	},
    65  	{
    66  		"-7z",
    67  		0,
    68  		true,
    69  	},
    70  }
    71  
    72  // Test that parsing absolute and relative times works.
    73  func TestParseTime(t *testing.T) {
    74  	for _, test := range partTimeTests {
    75  		actual, err := parseTime(test.in, now)
    76  		if test.errExpected == (err == nil) {
    77  			t.Fatalf("unexpected error for %s:\n%v\n", test.in, err)
    78  		}
    79  		if actual != test.expected {
    80  			t.Fatalf(
    81  				"for %s actual and expected do not match:\n%d\n%d\n",
    82  				test.in,
    83  				actual,
    84  				test.expected,
    85  			)
    86  		}
    87  	}
    88  }