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

fix(reminder): parse clock with seconds #54

Merged
merged 1 commit into from
Dec 28, 2023
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
8 changes: 6 additions & 2 deletions reminder/clock_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ func ParseClock(s string) (hour int, minute int, err error) {
for scanner.Scan() {
t := scanner.Text()
if t == ":" {
colonMark = true
continue
if !colonMark {
colonMark = true
continue
}

break
}

if !colonMark {
Expand Down
7 changes: 7 additions & 0 deletions reminder/clock_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func TestParseClock(t *testing.T) {
expectMinute: -123,
expectError: reminder.ErrParseClock,
},
{
name: "with specify seconds",
input: "22:32:00",
expectHour: 22,
expectMinute: 32,
expectError: nil,
},
}

for _, testCase := range testCases {
Expand Down
6 changes: 3 additions & 3 deletions reminder/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var timePreposition = []string{"at", "in", "on", "di", "jam", "pada", "dalam"}
var conjunction = []string{"and", "or", "dan", "&", "atau"}
var validSubjects = []string{"me", "aku", "saya", "gw", "gua", "gue", "gweh"}

var clockRegex = regexp.MustCompile("[0-9]{1,2}:[0-9]{2}")
var clockRegex = regexp.MustCompile("^[0-9]{1,2}:[0-9]{2}(:[0-9]{2})?$")

func ParseText(ctx context.Context, text string) (Reminder, error) {
span := sentry.StartSpan(ctx, "reminder.parse_text")
Expand All @@ -72,7 +72,7 @@ func ParseText(ctx context.Context, text string) (Reminder, error) {
// predicate = for, in, at, on

if len(reminder.Subject) == 0 || expectedNextPartCategory == Subject {
//ValidateSubject:
// ValidateSubject:
if len(reminder.Subject) == 3 {
lastPartCategory = Subject
expectedNextPartCategory = None
Expand Down Expand Up @@ -139,7 +139,7 @@ func ParseText(ctx context.Context, text string) (Reminder, error) {
now := time.Now()

// switch case is faster rather than doing slices.Contains like this
//if slices.Contains(timeDuration, part) {}
// if slices.Contains(timeDuration, part) {}
switch strings.ToLower(part) {
case "minute", "minutes", "menit":
partialTimeString += "m"
Expand Down
50 changes: 50 additions & 0 deletions reminder/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"regexp"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -93,6 +95,15 @@ func TestParseText(t *testing.T) {
Object: "",
},
},
{
name: "weird case from ii64",
input: "me to check open PR at 22:32:00 UTC+7",
expect: reminder.Reminder{
Subject: []string{"me"},
Time: time.Date(now.Year(), now.Month(), now.Day(), 22, 32, 0, 0, time.FixedZone("UTC+7", 7*60*60)),
Object: "check open PR UTC+7",
},
},
}

for i, testCase := range testCases {
Expand Down Expand Up @@ -135,3 +146,42 @@ func TestParseText(t *testing.T) {
}
})
}

func TestClockRegex(t *testing.T) {
var clockRegex = regexp.MustCompile("^[0-9]{1,2}:[0-9]{2}(:[0-9]{2})?$")

testCases := []struct {
input string
expectMatch bool
}{
{
input: "00:00",
expectMatch: true,
},
{
input: "123:123",
expectMatch: false,
},
{
input: "1:10",
expectMatch: true,
},
{
input: "00:00:01",
expectMatch: true,
},
{
input: "00:00:",
expectMatch: false,
},
}

for i, testCase := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
match := clockRegex.MatchString(testCase.input)
if match != testCase.expectMatch {
t.Errorf("[%d] expecting %q to be %v, got %v", i, testCase.input, testCase.expectMatch, match)
}
})
}
}