Skip to content

Commit

Permalink
refactor: rename ToString functions to String, per go convention (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jun 17, 2022
1 parent 733cf99 commit a930d22
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion internal/reporter/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r Report) describeIgnores() string {
)
}

func (r Report) ToString() string {
func (r Report) String() string {
count := r.countKnownVulnerabilities()
ignoreMsg := r.describeIgnores()
word := "known"
Expand Down
20 changes: 10 additions & 10 deletions internal/reporter/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,19 @@ func TestReport_HasIgnoredVulnerabilities(t *testing.T) {
}
}

func TestReport_ToString_NoVulnerabilities(t *testing.T) {
func TestReport_String_NoVulnerabilities(t *testing.T) {
t.Parallel()

msg := "no known vulnerabilities found"

r := reporter.Report{}

if actual := r.ToString(); !strings.Contains(actual, msg) {
if actual := r.String(); !strings.Contains(actual, msg) {
t.Errorf("Expected \"%s\" to contain \"%s\" but it did not", actual, msg)
}
}

func TestReport_ToString_OneVulnerability(t *testing.T) {
func TestReport_String_OneVulnerability(t *testing.T) {
t.Parallel()

expected := strings.Join([]string{
Expand Down Expand Up @@ -276,12 +276,12 @@ func TestReport_ToString_OneVulnerability(t *testing.T) {
},
}

if actual := r.ToString(); expected != actual {
if actual := r.String(); expected != actual {
t.Errorf("\nExpected:\n%s\nActual:\n%s", expected, actual)
}
}

func TestReport_ToString_MultipleVulnerabilities(t *testing.T) {
func TestReport_String_MultipleVulnerabilities(t *testing.T) {
t.Parallel()

expected := strings.Join([]string{
Expand Down Expand Up @@ -334,12 +334,12 @@ func TestReport_ToString_MultipleVulnerabilities(t *testing.T) {
},
}

if actual := r.ToString(); expected != actual {
if actual := r.String(); expected != actual {
t.Errorf("\nExpected:\n%s\nActual:\n%s", expected, actual)
}
}

func TestReport_ToString_AllIgnoredVulnerabilities(t *testing.T) {
func TestReport_String_AllIgnoredVulnerabilities(t *testing.T) {
t.Parallel()

msg := "no new vulnerabilities found (2 were ignored)"
Expand Down Expand Up @@ -376,12 +376,12 @@ func TestReport_ToString_AllIgnoredVulnerabilities(t *testing.T) {
},
}

if actual := r.ToString(); !strings.Contains(actual, msg) {
if actual := r.String(); !strings.Contains(actual, msg) {
t.Errorf("Expected \"%s\" to contain \"%s\" but it did not", actual, msg)
}
}

func TestReport_ToString_SomeIgnoredVulnerability(t *testing.T) {
func TestReport_String_SomeIgnoredVulnerability(t *testing.T) {
t.Parallel()

expected := strings.Join([]string{
Expand Down Expand Up @@ -424,7 +424,7 @@ func TestReport_ToString_SomeIgnoredVulnerability(t *testing.T) {
},
}

if actual := r.ToString(); expected != actual {
if actual := r.String(); expected != actual {
t.Errorf("\nExpected:\n%s\nActual:\n%s", expected, actual)
}
}
4 changes: 2 additions & 2 deletions internal/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (r *Reporter) PrintText(msg string) {
}

type Result interface {
ToString() string
String() string
}

func (r *Reporter) PrintResult(result Result) {
Expand All @@ -58,7 +58,7 @@ func (r *Reporter) PrintResult(result Result) {
return
}

fmt.Fprint(r.stdout, result.ToString())
fmt.Fprint(r.stdout, result.String())
}

// PrintJSONResults prints any results that this reporter has collected to
Expand Down
12 changes: 6 additions & 6 deletions internal/reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

type TestResult struct {
String string `json:"value"`
Value string `json:"value"`
ErrorWhenMarshalling bool `json:"-"`
}

func (r TestResult) ToString() string {
return r.String
func (r TestResult) String() string {
return r.Value
}

func (r TestResult) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestReporter_PrintResult(t *testing.T) {
stderr := &bytes.Buffer{}
r := reporter.New(stdout, stderr, false)

r.PrintResult(TestResult{String: msg})
r.PrintResult(TestResult{Value: msg})

if gotStdout := stdout.String(); gotStdout != msg {
t.Errorf("Expected stdout to have \"%s\", but got \"%s\"", msg, gotStdout)
Expand All @@ -82,7 +82,7 @@ func TestReporter_PrintResult_OutputAsJSON(t *testing.T) {
stderr := &bytes.Buffer{}
r := reporter.New(stdout, stderr, true)

r.PrintResult(TestResult{String: msg})
r.PrintResult(TestResult{Value: msg})

if gotStdout := stdout.String(); gotStdout != "" {
t.Errorf("Expected stdout to be empty, but got \"%s\"", gotStdout)
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestReporter_PrintResult_OutputAsJSON_Error(t *testing.T) {
stderr := &bytes.Buffer{}
r := reporter.New(stdout, stderr, true)

r.PrintResult(TestResult{String: msg, ErrorWhenMarshalling: true})
r.PrintResult(TestResult{Value: msg, ErrorWhenMarshalling: true})

if gotStdout := stdout.String(); gotStdout != "" {
t.Errorf("Expected stdout to be empty, but got \"%s\"", gotStdout)
Expand Down
2 changes: 1 addition & 1 deletion pkg/lockfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type Lockfile struct {
Packages Packages `json:"packages"`
}

func (l Lockfile) ToString() string {
func (l Lockfile) String() string {
lines := make([]string, 0, len(l.Packages))

for _, details := range l.Packages {
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestListParsers(t *testing.T) {
}
}

func TestLockfile_ToString(t *testing.T) {
func TestLockfile_String(t *testing.T) {
t.Parallel()

expected := strings.Join([]string{
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestLockfile_ToString(t *testing.T) {
},
}

if actual := lockf.ToString(); expected != actual {
if actual := lockf.String(); expected != actual {
t.Errorf("\nExpected:\n%s\nActual:\n%s", expected, actual)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/semantic/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func expectCompareResult(
if actualResult := a.Compare(b); actualResult != expectedResult {
t.Errorf(
"Expected %s to be %s %s, but it was %s",
a.ToString(),
a.String(),
compareWord(t, expectedResult),
b.ToString(),
b.String(),
compareWord(t, actualResult),
)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/semantic/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func expectParsedVersionToMatchOriginalString(t *testing.T, str string) semantic

actualVersion := semantic.Parse(str)

if actualVersion.ToString() != str {
if actualVersion.String() != str {
t.Errorf(
"Parsed version as a string did not equal original: %s != %s",
actualVersion.ToString(),
actualVersion.String(),
str,
)
}
Expand Down Expand Up @@ -85,10 +85,10 @@ func expectParsedVersionToMatchString(

actualVersion := semantic.Parse(str)

if actualVersion.ToString() != expectedString {
if actualVersion.String() != expectedString {
t.Errorf(
"Parsed version as a string did not equal expected: %s != %s",
actualVersion.ToString(),
actualVersion.String(),
expectedString,
)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/semantic/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (components *Components) Fetch(n int) int {
return (*components)[n]
}

func (v Version) ToString() string {
func (v Version) String() string {
str := ""

if v.LeadingV {
Expand Down

0 comments on commit a930d22

Please sign in to comment.