-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combine color and string utils under stringutils package
- Loading branch information
Showing
7 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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,20 @@ | ||
// Copyright 2025 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package stringutils provides helpers to format strings | ||
package stringutils | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// Test StripColor function | ||
func TestStripColor(t *testing.T) { | ||
coloredText := SuccessColor.Sprintf("Success: %s", "operation completed") | ||
expected := "Success: operation completed" | ||
|
||
strippedText := StripColor(coloredText) | ||
if strippedText != expected { | ||
t.Errorf("StripColor(%q) = %q; want %q", coloredText, strippedText, expected) | ||
} | ||
} |
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,25 @@ | ||
// Copyright 2025 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package stringutils | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func TrimRightSpace(s string) string { | ||
return strings.TrimRightFunc(s, unicode.IsSpace) | ||
} | ||
|
||
func BeginsWith(s, prefix string) bool { | ||
return strings.HasPrefix(s, prefix) | ||
} | ||
|
||
// Rpad adds padding to the right of a string. | ||
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29 | ||
func Rpad(s string, padding int) string { | ||
tmpl := fmt.Sprintf("%%-%ds", padding) | ||
return fmt.Sprintf(tmpl, s) | ||
} |
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,68 @@ | ||
// Copyright 2025 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package stringutils | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTrimRightSpace(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"hello ", "hello"}, | ||
{"test\n\t", "test"}, | ||
{" no trim needed", " no trim needed"}, | ||
{"", ""}, | ||
} | ||
|
||
for _, tt := range tests { | ||
result := TrimRightSpace(tt.input) | ||
if result != tt.expected { | ||
t.Errorf("TrimRightSpace(%q) = %q; want %q", tt.input, result, tt.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestBeginsWith(t *testing.T) { | ||
tests := []struct { | ||
s string | ||
prefix string | ||
expected bool | ||
}{ | ||
{"hello world", "hello", true}, | ||
{"hello world", "world", false}, | ||
{"", "", true}, | ||
{"", "nonempty", false}, | ||
{"prefix test", "prefix", true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
result := BeginsWith(tt.s, tt.prefix) | ||
if result != tt.expected { | ||
t.Errorf("BeginsWith(%q, %q) = %v; want %v", tt.s, tt.prefix, result, tt.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestRpad(t *testing.T) { | ||
tests := []struct { | ||
s string | ||
padding int | ||
expected string | ||
}{ | ||
{"test", 8, "test "}, | ||
{"hello", 3, "hello"}, | ||
{"pad", 6, "pad "}, | ||
{"", 4, " "}, | ||
} | ||
|
||
for _, tt := range tests { | ||
result := Rpad(tt.s, tt.padding) | ||
if result != tt.expected { | ||
t.Errorf("Rpad(%q, %d) = %q; want %q", tt.s, tt.padding, result, tt.expected) | ||
} | ||
} | ||
} |