-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from jpreese/service-names-end-with
Implement SERVICE_NAMES_END_WITH rule
- Loading branch information
Showing
6 changed files
with
146 additions
and
2 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
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,54 @@ | ||
package rules | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/yoheimuta/go-protoparser/parser" | ||
"github.com/yoheimuta/protolint/internal/addon/rules/internal/visitor" | ||
"github.com/yoheimuta/protolint/internal/linter/report" | ||
) | ||
|
||
// ServiceNamesEndWithRule verifies that all service names end with the specified value. | ||
type ServiceNamesEndWithRule struct { | ||
text string | ||
} | ||
|
||
// NewServiceNamesEndWithRule creates a new ServiceNamesEndWithRule. | ||
func NewServiceNamesEndWithRule(text string) ServiceNamesEndWithRule { | ||
return ServiceNamesEndWithRule{ | ||
text: text, | ||
} | ||
} | ||
|
||
// ID returns the ID of this rule. | ||
func (r ServiceNamesEndWithRule) ID() string { | ||
return "SERVICE_NAMES_END_WITH" | ||
} | ||
|
||
// Purpose returns the purpose of this rule. | ||
func (r ServiceNamesEndWithRule) Purpose() string { | ||
return "Verifies that all service names end with the specified value." | ||
} | ||
|
||
// Apply applies the rule to the proto. | ||
func (r ServiceNamesEndWithRule) Apply(proto *parser.Proto) ([]report.Failure, error) { | ||
v := &serviceNamesEndWithVisitor{ | ||
BaseAddVisitor: visitor.NewBaseAddVisitor(), | ||
text: r.text, | ||
} | ||
|
||
return visitor.RunVisitor(v, proto, r.ID()) | ||
} | ||
|
||
type serviceNamesEndWithVisitor struct { | ||
*visitor.BaseAddVisitor | ||
text string | ||
} | ||
|
||
// VisitService checks the service. | ||
func (v *serviceNamesEndWithVisitor) VisitService(service *parser.Service) bool { | ||
if !strings.HasSuffix(service.ServiceName, v.text) { | ||
v.AddFailuref(service.Meta.Pos, "Service name %q must end with %s", service.ServiceName, v.text) | ||
} | ||
return false | ||
} |
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 rules_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/yoheimuta/go-protoparser/parser" | ||
"github.com/yoheimuta/go-protoparser/parser/meta" | ||
"github.com/yoheimuta/protolint/internal/addon/rules" | ||
"github.com/yoheimuta/protolint/internal/linter/report" | ||
) | ||
|
||
func TestValidServiceNamesEndWithRule_Apply(t *testing.T) { | ||
validTestCase := struct { | ||
name string | ||
inputProto *parser.Proto | ||
wantFailures []report.Failure | ||
}{ | ||
name: "no failures for proto with valid service names", | ||
inputProto: &parser.Proto{ | ||
ProtoBody: []parser.Visitee{ | ||
&parser.Service{ | ||
ServiceName: "SomeServiceService", | ||
}, | ||
&parser.Service{ | ||
ServiceName: "AnotherService", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
t.Run(validTestCase.name, func(t *testing.T) { | ||
rule := rules.NewServiceNamesEndWithRule("Service") | ||
|
||
_, err := rule.Apply(validTestCase.inputProto) | ||
if err != nil { | ||
t.Errorf("got err %v, but want nil", err) | ||
return | ||
} | ||
}) | ||
} | ||
|
||
func TestInvalidServiceNamesEndWithRule_Apply(t *testing.T) { | ||
invalidTestCase := struct { | ||
name string | ||
inputProto *parser.Proto | ||
wantFailures []report.Failure | ||
}{ | ||
name: "failures for proto with invalid service names", | ||
inputProto: &parser.Proto{ | ||
ProtoBody: []parser.Visitee{ | ||
&parser.Service{ | ||
ServiceName: "SomeThing", | ||
}, | ||
&parser.Service{ | ||
ServiceName: "AnotherThing", | ||
}, | ||
}, | ||
}, | ||
wantFailures: []report.Failure{ | ||
report.Failuref(meta.Position{}, `Service name "SomeThing" must end with Service`), | ||
report.Failuref(meta.Position{}, `Service name "AnotherThing" must end with Service`), | ||
}, | ||
} | ||
|
||
t.Run(invalidTestCase.name, func(t *testing.T) { | ||
rule := rules.NewServiceNamesEndWithRule("Service") | ||
|
||
got, err := rule.Apply(invalidTestCase.inputProto) | ||
if err != nil { | ||
t.Errorf("got err %v, but want nil", err) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, invalidTestCase.wantFailures) { | ||
t.Errorf("got %v, but want %v", got, invalidTestCase.wantFailures) | ||
} | ||
}) | ||
} |
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
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
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,6 @@ | ||
package config | ||
|
||
// ServiceNamesEndWithOption represents the option for the SERVICE_NAMES_END_WITH rule. | ||
type ServiceNamesEndWithOption struct { | ||
Text string `yaml:"text"` | ||
} |