mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-30 18:17:55 +00:00 
			
		
		
		
	 46bb98b044
			
		
	
	46bb98b044
	
	
	
		
			
			* Add date/time argument type. * Add an argument to select which time formats are valid. * Increase minimum date for epoch timestamps to avoid ambiguity.
		
			
				
	
	
		
			116 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			116 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package command
 | |
| 
 | |
| import (
 | |
| 	"testing"
 | |
| 	"time"
 | |
| )
 | |
| 
 | |
| func Test_TimeParsing(t *testing.T) {
 | |
| 	var zeroTime time.Time
 | |
| 
 | |
| 	testCases := []struct {
 | |
| 		Input    string
 | |
| 		Formats  TimeFormat
 | |
| 		Valid    bool
 | |
| 		Expected time.Time
 | |
| 	}{
 | |
| 		{
 | |
| 			"2020-08-24",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			true,
 | |
| 			time.Date(2020, 8, 24, 0, 0, 0, 0, time.UTC),
 | |
| 		},
 | |
| 		{
 | |
| 			"2099-09",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			false,
 | |
| 			zeroTime,
 | |
| 		},
 | |
| 		{
 | |
| 			"2099-09",
 | |
| 			TimeVar_TimeOrDay | TimeVar_Month,
 | |
| 			true,
 | |
| 			time.Date(2099, 9, 1, 0, 0, 0, 0, time.UTC),
 | |
| 		},
 | |
| 		{
 | |
| 			"2021-01-02T03:04:05-02:00",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			true,
 | |
| 			time.Date(2021, 1, 2, 5, 4, 5, 0, time.UTC),
 | |
| 		},
 | |
| 		{
 | |
| 			"2021-01-02T03:04:05",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			false, // Missing timezone not supported
 | |
| 			time.Date(2021, 1, 2, 3, 4, 5, 0, time.UTC),
 | |
| 		},
 | |
| 		{
 | |
| 			"2021-01-02T03:04:05+02:00",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			true,
 | |
| 			time.Date(2021, 1, 2, 1, 4, 5, 0, time.UTC),
 | |
| 		},
 | |
| 		{
 | |
| 			"1598313593",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			true,
 | |
| 			time.Date(2020, 8, 24, 23, 59, 53, 0, time.UTC),
 | |
| 		},
 | |
| 		{
 | |
| 			"2037",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			false,
 | |
| 			zeroTime,
 | |
| 		},
 | |
| 		{
 | |
| 			"20201212",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			false,
 | |
| 			zeroTime,
 | |
| 		},
 | |
| 		{
 | |
| 			"9999999999999999999999999999999999999999999999",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			false,
 | |
| 			zeroTime,
 | |
| 		},
 | |
| 		{
 | |
| 			"2021-13-02T03:04:05-02:00",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			false,
 | |
| 			zeroTime,
 | |
| 		},
 | |
| 		{
 | |
| 			"2021-12-02T24:04:05+00:00",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			false,
 | |
| 			zeroTime,
 | |
| 		},
 | |
| 		{
 | |
| 			"2021-01-02T03:04:05.234567890Z",
 | |
| 			TimeVar_TimeOrDay,
 | |
| 			true,
 | |
| 			time.Date(2021, 1, 2, 3, 4, 5, 234567890, time.UTC),
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for _, tc := range testCases {
 | |
| 		var result time.Time
 | |
| 		timeVal := newTimeValue(zeroTime, &result, false, tc.Formats)
 | |
| 		err := timeVal.Set(tc.Input)
 | |
| 		if err == nil && !tc.Valid {
 | |
| 			t.Errorf("Time %q parsed without error as %v, but is not valid", tc.Input, result)
 | |
| 			continue
 | |
| 		}
 | |
| 		if err != nil {
 | |
| 			if tc.Valid {
 | |
| 				t.Errorf("Time %q parsed as error, but is valid", tc.Input)
 | |
| 			}
 | |
| 			continue
 | |
| 		}
 | |
| 		if !tc.Expected.Equal(result) {
 | |
| 			t.Errorf("Time %q parsed incorrectly, expected %v but got %v", tc.Input, tc.Expected.UTC(), result.UTC())
 | |
| 		}
 | |
| 	}
 | |
| }
 |