-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reminder): proper lexer function
- Loading branch information
Showing
5 changed files
with
291 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package reminder | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func ParseClock(s string) (hour int, minute int, err error) { | ||
scanner := bufio.NewScanner(strings.NewReader(s)) | ||
scanner.Split(bufio.ScanRunes) | ||
|
||
var colonMark bool | ||
var s_hour string | ||
var s_minute string | ||
for scanner.Scan() { | ||
t := scanner.Text() | ||
if t == ":" { | ||
colonMark = true | ||
continue | ||
} | ||
|
||
if !colonMark { | ||
s_hour += t | ||
} else { | ||
s_minute += t | ||
} | ||
} | ||
|
||
hour, err = strconv.Atoi(s_hour) | ||
if err != nil { | ||
return | ||
} | ||
|
||
minute, err = strconv.Atoi(s_minute) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if hour > 24 { | ||
err = fmt.Errorf("invalid hour, exceeds 24") | ||
return | ||
} | ||
|
||
if hour < 0 { | ||
err = fmt.Errorf("invalid hour, negative number") | ||
return | ||
} | ||
|
||
if minute > 60 { | ||
err = fmt.Errorf("invalid minute, exceeds 60") | ||
return | ||
} | ||
|
||
if minute < 0 { | ||
err = fmt.Errorf("invalid minute, negative number") | ||
return | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package reminder_test | ||
|
||
import ( | ||
"errors" | ||
"github.com/teknologi-umum/captcha/reminder" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestParseClock(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expectHour int | ||
expectMinute int | ||
expectError error | ||
}{ | ||
{ | ||
name: "happy case 1", | ||
input: "00:00", | ||
expectHour: 0, | ||
expectMinute: 0, | ||
expectError: nil, | ||
}, | ||
{ | ||
name: "happy case 2", | ||
input: "23:59", | ||
expectHour: 23, | ||
expectMinute: 59, | ||
expectError: nil, | ||
}, | ||
{ | ||
name: "happy case 3", | ||
input: "05:05", | ||
expectHour: 5, | ||
expectMinute: 5, | ||
expectError: nil, | ||
}, | ||
{ | ||
name: "happy case 4", | ||
input: "20:20", | ||
expectHour: 20, | ||
expectMinute: 20, | ||
expectError: nil, | ||
}, | ||
{ | ||
name: "hour is not a number", | ||
input: "abc:00", | ||
expectHour: 0, | ||
expectMinute: 0, | ||
expectError: strconv.ErrSyntax, | ||
}, | ||
{ | ||
name: "minute is not a number", | ||
input: "15:abc", | ||
expectHour: 15, | ||
expectMinute: 0, | ||
expectError: strconv.ErrSyntax, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
hour, minute, err := reminder.ParseClock(testCase.input) | ||
if hour != testCase.expectHour { | ||
t.Errorf("expecting hour to be %d, got %d", testCase.expectHour, hour) | ||
} | ||
|
||
if minute != testCase.expectMinute { | ||
t.Errorf("expecting minute to be %d, got %d", testCase.expectMinute, minute) | ||
} | ||
|
||
if !errors.Is(err, testCase.expectError) { | ||
t.Errorf("expecting error to be %v, got %v", testCase.expectError, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.