Skip to content

Commit

Permalink
Add tests for time.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonRomano committed Jun 23, 2017
1 parent 7ccae00 commit 174aee1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"strconv"
"testing"
"time"
)

// IsSet is a rule that makes sure the value passed in isn't an empty string
Expand Down Expand Up @@ -651,6 +652,41 @@ func TestUint64(t *testing.T) {
assert.Equal(t, uint64(0), stringId)
}

// TestTime tests handling of a time.Time as the result
func TestTime(t *testing.T) {
// Test success case
var successTime time.Time
err := v.Validate([]*v.Value{
{Result: &successTime, Name: "time", Input: "2012-11-01T22:08:41+00:00"},
})
assert.Nil(t, err)
assert.Equal(t, successTime.Year(), 2012)
assert.Equal(t, successTime.Month(), time.November)
assert.Equal(t, successTime.Day(), 1)

// Test empty case
var emptyTime time.Time
err = v.Validate([]*v.Value{
{Result: &emptyTime, Name: "time", Input: ""},
})
assert.Nil(t, err)
assert.Equal(t, emptyTime.Year(), 1)

// Test empty with isSet rule
err = v.Validate([]*v.Value{
{Result: &emptyTime, Name: "time", Input: "", Rules: []v.Rule{IsSet}},
})
assert.NotNil(t, err)
assert.Equal(t, emptyTime.Year(), 1)

// Test error case
var errorTime time.Time
err = v.Validate([]*v.Value{
{Result: &errorTime, Name: "time", Input: "abcd"},
})
assert.NotNil(t, err)
}

// TestCustomTypeHandler tests that we can create a new type handler
// and use it as expected
func TestCustomTypeHandler(t *testing.T) {
Expand Down

0 comments on commit 174aee1

Please sign in to comment.