Skip to content

Commit

Permalink
test(format): improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaasler committed Jul 20, 2021
1 parent 4c8fb70 commit 8c5aa09
Show file tree
Hide file tree
Showing 3 changed files with 528 additions and 1 deletion.
14 changes: 14 additions & 0 deletions internal/testing/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ func AssertError(t *testing.T, expected string, got error) {
t.Fatalf("Error '%s' does not contain '%s'", got, expected)
}
}

// AssertTrue fails if a false is passed.
func AssertTrue(t *testing.T, got bool) {
if !got {
t.Fatal("True expected")
}
}

// AssertFalse fails if a true is passed.
func AssertFalse(t *testing.T, got bool) {
if got {
t.Fatal("False expected")
}
}
109 changes: 109 additions & 0 deletions pkg/format/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ SOFTWARE.
package format

import (
"os"
"testing"

itesting "github.com/amplia-iiot/yutil/internal/testing"
Expand Down Expand Up @@ -297,3 +298,111 @@ func TestFormatContent(t *testing.T) {
itesting.AssertEqual(t, i.expected, formatted)
}
}

func TestFormatContentInvalid(t *testing.T) {
for _, i := range []struct {
content string
expected string
}{
{
content: `;`,
expected: "cannot unmarshal",
},
{
content: `data: {`,
expected: "did not find expected node content",
},
} {
formatted, err := FormatContent(i.content)
itesting.AssertError(t, i.expected, err)
if formatted != "" {
t.Errorf("Should not have formatted")
}
}
}

func TestStdinContent(t *testing.T) {
for _, i := range []struct {
stdin string
expected string
}{
// Formatted to multiple lines
{
stdin: `data: {one: 1, two: 2}`,
expected: `data:
one: 1
two: 2
`,
},
// Keys are alphabetically ordered
{
stdin: `data: {b: b, c: c, a: a}`,
expected: `data:
a: a
b: b
c: c
`,
},
// Null should be formatted `null`
{
stdin: `data:`,
expected: `data: null
`,
},
// String do not have quotes
{
stdin: `data: "one"`,
expected: `data: one
`,
},
{
stdin: `data: 'this is a string'`,
expected: `data: this is a string
`,
},
} {
itesting.SimulateStdinContent(t, i.stdin, func() {
formatted, err := FormatStdin()
if err != nil {
t.Error(err)
}
itesting.AssertEqual(t, i.expected, formatted)
})
}
}

func TestFormatstdinInvalid(t *testing.T) {
for _, i := range []struct {
stdin string
stdinFile os.File
expected string
}{
// Parsing error
{
stdin: `;`,
expected: "cannot unmarshal",
},
{
stdin: `data: {`,
expected: "did not find expected node content",
},
// Stdin error
{
stdinFile: *os.Stderr,
expected: "bad file descriptor",
},
} {
test := func() {
formatted, err := FormatStdin()
itesting.AssertError(t, i.expected, err)
if formatted != "" {
t.Errorf("Should not have formatted")
}
}
if i.stdin != "" {
itesting.SimulateStdinContent(t, i.stdin, test)
} else {
itesting.SimulateStdinFile(i.stdinFile, test)
}
}
}
Loading

0 comments on commit 8c5aa09

Please sign in to comment.