forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: add helper to avoid long format strings
A relatively common pattern in the ghw codebase is concatenating quite a lot of strings inside a format string. This leads to a pattern like '%s%s%s%s%s' which are hard to read and to debug. Ad a simple helper to concatenate strings to simplify the format strings. This may be the definitive solution here, but it should improve the maintenability and readability, and it's very cheap to implement and maintain. Signed-off-by: Francesco Romani <fromani@redhat.com>
- Loading branch information
Showing
6 changed files
with
75 additions
and
21 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
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
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,57 @@ | ||
// | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
// | ||
|
||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jaypipes/ghw/pkg/util" | ||
) | ||
|
||
// nolint: gocyclo | ||
func TestConcatStrings(t *testing.T) { | ||
type testCase struct { | ||
items []string | ||
expected string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
items: []string{}, | ||
expected: "", | ||
}, | ||
{ | ||
items: []string{"simple"}, | ||
expected: "simple", | ||
}, | ||
{ | ||
items: []string{ | ||
"foo", | ||
"bar", | ||
"baz", | ||
}, | ||
expected: "foobarbaz", | ||
}, | ||
{ | ||
items: []string{ | ||
"foo ", | ||
" bar ", | ||
" baz", | ||
}, | ||
expected: "foo bar baz", | ||
}, | ||
} | ||
|
||
for _, tCase := range testCases { | ||
t.Run(tCase.expected, func(t *testing.T) { | ||
got := util.ConcatStrings(tCase.items...) | ||
if got != tCase.expected { | ||
t.Errorf("expected %q got %q", tCase.expected, got) | ||
} | ||
}) | ||
} | ||
} |