/ cmd / commands / arg_parse_test.go
arg_parse_test.go
  1  package commands
  2  
  3  import (
  4  	"testing"
  5  	"time"
  6  
  7  	"github.com/stretchr/testify/require"
  8  )
  9  
 10  var now = time.Date(2017, 11, 10, 7, 8, 9, 1234, time.UTC)
 11  
 12  var partTimeTests = []struct {
 13  	in          string
 14  	expected    uint64
 15  	errExpected bool
 16  }{
 17  	{
 18  		"12345",
 19  		uint64(12345),
 20  		false,
 21  	},
 22  	{
 23  		"-0s",
 24  		uint64(now.Unix()),
 25  		false,
 26  	},
 27  	{
 28  		"-1s",
 29  		uint64(time.Date(2017, 11, 10, 7, 8, 8, 1234, time.UTC).Unix()),
 30  		false,
 31  	},
 32  	{
 33  		"-2h",
 34  		uint64(time.Date(2017, 11, 10, 5, 8, 9, 1234, time.UTC).Unix()),
 35  		false,
 36  	},
 37  	{
 38  		"-3d",
 39  		uint64(time.Date(2017, 11, 7, 7, 8, 9, 1234, time.UTC).Unix()),
 40  		false,
 41  	},
 42  	{
 43  		"-4w",
 44  		uint64(time.Date(2017, 10, 13, 7, 8, 9, 1234, time.UTC).Unix()),
 45  		false,
 46  	},
 47  	{
 48  		"-5M",
 49  		uint64(now.Unix() - 30.44*5*24*60*60),
 50  		false,
 51  	},
 52  	{
 53  		"-6y",
 54  		uint64(now.Unix() - 365.25*6*24*60*60),
 55  		false,
 56  	},
 57  	{
 58  		"-999999999999999999s",
 59  		uint64(now.Unix() - 999999999999999999),
 60  		false,
 61  	},
 62  	{
 63  		"-9999999999999999991s",
 64  		0,
 65  		true,
 66  	},
 67  	{
 68  		"-7z",
 69  		0,
 70  		true,
 71  	},
 72  }
 73  
 74  // Test that parsing absolute and relative times works.
 75  func TestParseTime(t *testing.T) {
 76  	for _, test := range partTimeTests {
 77  		actual, err := parseTime(test.in, now)
 78  		if test.errExpected == (err == nil) {
 79  			t.Fatalf("unexpected error for %s:\n%v\n", test.in, err)
 80  		}
 81  		if actual != test.expected {
 82  			t.Fatalf(
 83  				"for %s actual and expected do not match:\n%d\n%d\n",
 84  				test.in,
 85  				actual,
 86  				test.expected,
 87  			)
 88  		}
 89  	}
 90  }
 91  
 92  var stripPrefixTests = []struct {
 93  	in       string
 94  	expected string
 95  }{
 96  	{
 97  		"lightning:ln123",
 98  		"ln123",
 99  	},
100  	{
101  		"lightning: ln123",
102  		"ln123",
103  	},
104  	{
105  		"ln123",
106  		"ln123",
107  	},
108  }
109  
110  func TestStripPrefix(t *testing.T) {
111  	t.Parallel()
112  
113  	for _, test := range stripPrefixTests {
114  		actual := StripPrefix(test.in)
115  		require.Equal(t, test.expected, actual)
116  	}
117  }