Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Date] Implement %y year without century parser / formatter; respect digits that are below maximum format length #135

Merged
merged 8 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions internal/func_time_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package internal

import "testing"

func TestTimeParser(t *testing.T) {
for _, test := range []struct {
name string
text []rune
minValue int64
maxValue int64
expectedProgress int
expectedResult int
expectedErr string
}{
{
name: "single digit but multiple allowed; non-digit character terminates",
text: []rune{'2', '/'},
minValue: 1,
maxValue: 12,
expectedResult: 2,
expectedProgress: 1,
},
{
name: "multiple digits; non-digit character terminates",
text: []rune{'1', '2', '/'},
minValue: 1,
maxValue: 12,
expectedResult: 12,
expectedProgress: 2,
},
{
name: "leading zero digit but multiple allowed; non-digit character terminates",
text: []rune{'0', '2', '/'},
minValue: 1,
maxValue: 12,
expectedResult: 2,
expectedProgress: 2,
},

{
name: "leading zero digit but multiple allowed; non-digit character terminates",
text: []rune{'0', '0', '2', '/'},
minValue: 1,
maxValue: 9999,
expectedResult: 2,
expectedProgress: 3,
},
{
name: "multiple digits but exceeds limit; non-digit character terminates",
text: []rune{'2', '2', '/'},
minValue: 1,
maxValue: 12,
expectedErr: "part [22] is greater than maximum value [12]",
},
{
name: "multiple digits but lower than start bound; non-digit character terminates",
text: []rune{'0', '0', '/'},
minValue: 1,
maxValue: 12,
expectedErr: "part [0] is less than minimum value [1]",
},
{
name: "multiple digits but lower than start bound; non-digit character terminates",
text: []rune{'4', '-'},
minValue: 1,
maxValue: 12,
expectedResult: 4,
expectedProgress: 1,
},
} {

t.Run(test.name, func(t *testing.T) {
progress, result, err := parseDigitRespectingOptionalPlaces(test.text, test.minValue, test.maxValue)
if err != nil {
if test.expectedErr != err.Error() {
t.Fatalf("unexpected error message: expected [%s] but got [%s]", test.expectedErr, err.Error())
} else {
// expected error occurred, consider test successful
return
}
}

if progress != test.expectedProgress {
t.Fatalf("unexpected progress: expected [%d] but got [%d]", test.expectedProgress, progress)
}

if result != int64(test.expectedResult) {
t.Fatalf("unexpected result: expected [%d] but got [%d]", test.expectedResult, result)
}

})
}
}
Loading
Loading