generated from repros-dev/repro-template
-
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.
Moved util package here from cirss/geist
- Loading branch information
Timothy McPhillips
committed
Mar 14, 2021
1 parent
fb7dadf
commit 5abad5e
Showing
5 changed files
with
138 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
* | ||
pkg | ||
bin |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/cirss/go-cli | ||
|
||
go 1.16 | ||
|
||
require github.com/ucarion/jcs v0.1.2 // indirect |
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,8 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/ucarion/jcs v0.1.2 h1:QXKHIA1K7SysMfgf3Q6yyM0jQkpGXoknZiO/eljV7iA= | ||
github.com/ucarion/jcs v0.1.2/go.mod h1:y+kohV2KVa/vR01rJrw7z3Ka/H3kFo+5Nl/bi773ojk= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,71 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ucarion/jcs" | ||
) | ||
|
||
func canonicalJSON(jsonString string) (string, error) { | ||
var originalJSON interface{} | ||
json.Unmarshal([]byte(jsonString), &originalJSON) | ||
canonicalJSONString, err := jcs.Format(originalJSON) | ||
return canonicalJSONString, err | ||
} | ||
|
||
func IntEquals(t *testing.T, actual int, expected int) { | ||
if actual != expected { | ||
t.Log(fmt.Sprintf("IntEquals:\n\nexpected:\n%d\nactual:\n%d\n", expected, actual)) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func StringEquals(t *testing.T, actual string, expected string) { | ||
if actual != expected { | ||
t.Log("assertStringEquals:\n\nexpected:\n" + expected + "\nactual:\n" + actual + "\n") | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TrimEachLine(s string) string { | ||
var sb strings.Builder | ||
lines := strings.Split(s, "\n") | ||
for i, line := range lines { | ||
trimmedLine := Trim(line) | ||
sb.WriteString(trimmedLine) | ||
if i < len(lines)-1 { | ||
sb.WriteString("\n") | ||
} | ||
} | ||
return sb.String() | ||
} | ||
|
||
func Trim(s string) string { | ||
return strings.Trim(s, " \n\r\t") | ||
} | ||
|
||
func LineContentsEqual(t *testing.T, actual string, expect string) { | ||
actualContent := strings.Split(actual, "\n") | ||
expectContent := strings.Split(expect, "\n") | ||
lineCount := len(actualContent) | ||
if lineCount != len(expectContent) { | ||
StringEquals(t, actual, expect) | ||
} | ||
for i := 0; i < lineCount; i++ { | ||
StringEquals(t, Trim(actualContent[i]), Trim(expectContent[i])) | ||
} | ||
} | ||
|
||
func JSONEquals(t *testing.T, actual string, expected string) { | ||
actualCanonical, _ := canonicalJSON(actual) | ||
expectedCanonical, _ := canonicalJSON(expected) | ||
if actualCanonical != expectedCanonical { | ||
t.Log("assertEquivalentJSON:\n" + | ||
"\nexpected: " + expectedCanonical + | ||
"\nactual: " + actualCanonical + "\n") | ||
t.Fail() | ||
} | ||
} |
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,55 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTrimEachLine_EmptyString(t *testing.T) { | ||
s := "" | ||
StringEquals(t, TrimEachLine(s), "") | ||
} | ||
|
||
func TestTrimEachLine_Spaces(t *testing.T) { | ||
s := " " | ||
StringEquals(t, TrimEachLine(s), "") | ||
} | ||
|
||
func TestTrimEachLine_SpacesAndFinalNewline(t *testing.T) { | ||
s := " \n" | ||
StringEquals(t, TrimEachLine(s), "\n") | ||
} | ||
|
||
func TestTrimEachLine_ThreeNewlines(t *testing.T) { | ||
s := "\n\n\n" | ||
StringEquals(t, TrimEachLine(s), "\n\n\n") | ||
} | ||
|
||
func TestTrimEachLine_ThreeLinesOfSpacesWithFinalNewline(t *testing.T) { | ||
s := " \n \n \n" | ||
StringEquals(t, TrimEachLine(s), "\n\n\n") | ||
} | ||
|
||
func TestTrimEachLine_ThreeLinesOfSpaces_NoFinalNewline(t *testing.T) { | ||
s := " \n \n " | ||
StringEquals(t, TrimEachLine(s), "\n\n") | ||
} | ||
|
||
func TestTrimEachLine_Letters(t *testing.T) { | ||
s := "abcdefg" | ||
StringEquals(t, TrimEachLine(s), "abcdefg") | ||
} | ||
|
||
func TestTrimEachLine_LettersAndFinalNewline(t *testing.T) { | ||
s := "abcdefg\n" | ||
StringEquals(t, TrimEachLine(s), "abcdefg\n") | ||
} | ||
|
||
func TestTrimEachLine_ThreeLinesOfLettersWithFinalNewline(t *testing.T) { | ||
s := "abcdefg\nhijklmnop\nqrstuv\n" | ||
StringEquals(t, TrimEachLine(s), "abcdefg\nhijklmnop\nqrstuv\n") | ||
} | ||
|
||
func TestTrimEachLine_ThreeLinesOfLetters_NoFinalNewline(t *testing.T) { | ||
s := "abcdefg\nhijklmnop\nqrstuv" | ||
StringEquals(t, TrimEachLine(s), "abcdefg\nhijklmnop\nqrstuv") | ||
} |