Skip to content

Commit

Permalink
Improve string handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
sters committed Mar 1, 2024
1 parent f7d20aa commit 80c22c7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
3 changes: 2 additions & 1 deletion yamldiff/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ func dumpPrimitive(b io.Writer, diffPrefix string, level int, somethingPrefix st
case float32, float64:
fmt.Fprintf(b, "%s %s%s%f\n", diffPrefix, indent(level), somethingPrefix, v)
case string:
fmt.Fprintf(b, "%s %s%s\"%s\"\n", diffPrefix, indent(level), somethingPrefix, v)
// try escape special characters
fmt.Fprintf(b, "%s %s%s%#v\n", diffPrefix, indent(level), somethingPrefix, v)
default:
fmt.Fprintf(b, "%s %s%s%#v\n", diffPrefix, indent(level), somethingPrefix, v)
}
Expand Down
73 changes: 60 additions & 13 deletions yamldiff/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ package yamldiff
import (
"fmt"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func confirmYaml(t *testing.T, yamlA, yamlB RawYamlList, want string, opts []DoOptionFunc) {
t.Helper()

result := "\n"
for _, diff := range Do(yamlA, yamlB, opts...) {
result += diff.Dump()
result += "\n"
}

assert.Equal(t, strings.TrimSpace(want), strings.TrimSpace(result))
}

func Test(t *testing.T) {
yamlA := `
apiVersion: v1
Expand Down Expand Up @@ -117,19 +130,9 @@ differs: fromB
fmt.Fprintf(os.Stderr, "%+v", err)
}

confirm := func(expect string, opts []DoOptionFunc) {
result := "\n"
for _, diff := range Do(yamlsA, yamlsB, opts...) {
result += diff.Dump()
result += "\n"
}

assert.Equal(t, expect, result)
}

confirm(resultOfNoOptions(), []DoOptionFunc{})
confirm(resultOfWithEmpty(), []DoOptionFunc{EmptyAsNull()})
confirm(resultOfWithZero(), []DoOptionFunc{ZeroAsNull()})
confirmYaml(t, yamlsA, yamlsB, resultOfNoOptions(), []DoOptionFunc{})
confirmYaml(t, yamlsA, yamlsB, resultOfWithEmpty(), []DoOptionFunc{EmptyAsNull()})
confirmYaml(t, yamlsA, yamlsB, resultOfWithZero(), []DoOptionFunc{ZeroAsNull()})
}

func resultOfNoOptions() string {
Expand Down Expand Up @@ -326,3 +329,47 @@ func resultOfWithZero() string {
`
}

func TestForSpecificIssues(t *testing.T) {
tests := map[string]struct {
yamlA string
yamlB string
option []DoOptionFunc
want string
}{
// TODO: It should be extracted as multiline format to understand easily. It needs to refactor (*diff).dump()
"#52": {
yamlA: `
data:
config: |
logging.a: false
logging.b: false`,
yamlB: `
data:
config: |
logging.a: false
logging.c: false`,
want: `
data:
- config: "logging.a: false\nlogging.b: false"
+ config: "logging.a: false\nlogging.c: false"`,
},
}

for key, test := range tests {
key, test := key, test
t.Run(key, func(t *testing.T) {
yamlA, err := Load(test.yamlA)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v", err)
}

yamlB, err := Load(test.yamlB)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v", err)
}

confirmYaml(t, yamlA, yamlB, test.want, test.option)
})
}
}

0 comments on commit 80c22c7

Please sign in to comment.