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(AIP-132): refine List request response regex #1420

Merged
merged 1 commit into from
Aug 27, 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
8 changes: 4 additions & 4 deletions rules/internal/utils/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (

var (
getReqMessageRegexp = regexp.MustCompile("^Get[A-Za-z0-9]*Request$")
listReqMessageRegexp = regexp.MustCompile("^List[A-Za-z0-9]*Request$")
listRespMessageRegexp = regexp.MustCompile("^List([A-Za-z0-9]*)Response$")
listRevisionsReqMessageRegexp = regexp.MustCompile(`^List(?:[A-Za-z0-9]+)RevisionsRequest$`)
listRevisionsRespMessageRegexp = regexp.MustCompile(`^List(?:[A-Za-z0-9]+)RevisionsResponse$`)
listReqMessageRegexp = regexp.MustCompile("^List[A-Z]+[A-Za-z0-9]*Request$")
listRespMessageRegexp = regexp.MustCompile("^List([A-Z]+[A-Za-z0-9]*)Response$")
listRevisionsReqMessageRegexp = regexp.MustCompile(`^List(?:[A-Z]+[A-Za-z0-9]+)RevisionsRequest$`)
listRevisionsRespMessageRegexp = regexp.MustCompile(`^List(?:[A-Z]+[A-Za-z0-9]+)RevisionsResponse$`)
createReqMessageRegexp = regexp.MustCompile("^Create[A-Za-z0-9]*Request$")
updateReqMessageRegexp = regexp.MustCompile("^Update[A-Za-z0-9]*Request$")
deleteReqMessageRegexp = regexp.MustCompile("^Delete[A-Za-z0-9]*Request$")
Expand Down
156 changes: 156 additions & 0 deletions rules/internal/utils/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,159 @@ func TestListResponseResourceName(t *testing.T) {
})
}
}

func TestIsListResponseMessage(t *testing.T) {
for _, test := range []struct {
name string
RPC string
want bool
}{
{
name: "valid list response",
RPC: "ListBooks",
want: true,
},
{
name: "not list response",
RPC: "ArchiveBook",
want: false,
},
{
name: "lookalike response",
RPC: "Listen",
want: false,
},
{
name: "multiword lookalike response",
RPC: "ListenForever",
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
message {{.RPC}}Response {}
`, test)
m := file.GetMessageTypes()[0]
if got, want := IsListResponseMessage(m), test.want; got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
}

func TestIsListRequestMessage(t *testing.T) {
for _, test := range []struct {
name string
RPC string
want bool
}{
{
name: "valid list request",
RPC: "ListBooks",
want: true,
},
{
name: "not list request",
RPC: "ArchiveBook",
want: false,
},
{
name: "lookalike request",
RPC: "Listen",
want: false,
},
{
name: "multiword lookalike request",
RPC: "ListenForever",
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
message {{.RPC}}Request {}
`, test)
m := file.GetMessageTypes()[0]
if got, want := IsListRequestMessage(m), test.want; got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
}

func TestIsListRevisionsResponseMessage(t *testing.T) {
for _, test := range []struct {
name string
RPC string
want bool
}{
{
name: "valid list revisions response",
RPC: "ListBook",
want: true,
},
{
name: "not list revisions response",
RPC: "ArchiveBook",
want: false,
},
{
name: "lookalike response",
RPC: "Listen",
want: false,
},
{
name: "multiword lookalike response",
RPC: "ListenForever",
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
message {{.RPC}}RevisionsResponse {}
`, test)
m := file.GetMessageTypes()[0]
if got, want := IsListRevisionsResponseMessage(m), test.want; got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
}

func TestIsListRevisionsRequestMessage(t *testing.T) {
for _, test := range []struct {
name string
RPC string
want bool
}{
{
name: "valid list revisions request",
RPC: "ListBook",
want: true,
},
{
name: "not list revisions request",
RPC: "ArchiveBook",
want: false,
},
{
name: "lookalike request",
RPC: "Listen",
want: false,
},
{
name: "multiword lookalike request",
RPC: "ListenForever",
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
message {{.RPC}}RevisionsRequest {}
`, test)
m := file.GetMessageTypes()[0]
if got, want := IsListRevisionsRequestMessage(m), test.want; got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
}
Loading