From 4b079c6fdd8d7d3e224357566f278da3c159f441 Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Wed, 18 Apr 2018 17:36:22 -0700 Subject: [PATCH] tests to ensure more bad formats fail --- parseany.go | 4 +++- parseany_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/parseany.go b/parseany.go index dc8abc7..b2e5b32 100644 --- a/parseany.go +++ b/parseany.go @@ -45,7 +45,6 @@ const ( dateAlphaWsAlphaYearmaybe dateWeekdayComma dateWeekdayAbbrevComma - //dateWeekdayAbbrevCommaDash ) const ( // Time state @@ -356,6 +355,7 @@ iterRunes: i += (bytesConsumed - 1) } + //gou.Debugf("i=%d r=%s state=%d %s", i, string(r), p.stateDate, datestr) switch p.stateDate { case dateStart: if unicode.IsDigit(r) { @@ -421,6 +421,8 @@ iterRunes: case '年': // Chinese Year p.stateDate = dateDigitChineseYear + case ',': + return nil, fmt.Errorf("Unrecognized format %q", datestr) default: //if unicode.IsDigit(r) { continue diff --git a/parseany_test.go b/parseany_test.go index 8c911c1..8d6fe4a 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -523,11 +523,27 @@ func TestPStruct(t *testing.T) { assert.True(t, len(p.ts()) > 0) } +var testParseErrors = []dateTest{ + {in: "3", err: true}, + {in: `{"hello"}`, err: true}, + {in: "2009-15-12T22:15Z", err: true}, + {in: "5,000-9,999", err: true}, + {in: "xyzq-baad"}, +} + +func TestParseErrors(t *testing.T) { + for _, th := range testParseErrors { + v, err := ParseAny(th.in) + assert.NotEqual(t, nil, err, "%v for %v", v, th.in) + } +} + var testParseFormat = []dateTest{ // errors {in: "3", err: true}, {in: `{"hello"}`, err: true}, {in: "2009-15-12T22:15Z", err: true}, + {in: "5,000-9,999", err: true}, // {in: "oct 7, 1970", out: "Jan 2, 2006"}, // 03 February 2013