Add redacted effective rendering
This commit is contained in:
59
internal/yaml/write_test.go
Normal file
59
internal/yaml/write_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
||||
)
|
||||
|
||||
func TestWriteUsesStableExplicitFormatting(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := &document.Node{Kind: document.Mapping, Mapping: []document.Entry{
|
||||
{Key: "empty_map", Value: &document.Node{Kind: document.Mapping, Mapping: []document.Entry{}}},
|
||||
{Key: "empty_sequence", Value: &document.Node{Kind: document.Sequence, Sequence: []*document.Node{}}},
|
||||
{Key: "number", Value: &document.Node{Kind: document.Number, Text: "1.25e+2"}},
|
||||
{Key: "redacted", Value: &document.Node{Kind: document.String, Text: "[REDACTED]"}},
|
||||
{Key: "sequence", Value: &document.Node{Kind: document.Sequence, Sequence: []*document.Node{
|
||||
{Kind: document.String, Text: "first"},
|
||||
{Kind: document.String, Text: "true"},
|
||||
}}},
|
||||
{Key: "nil_value", Value: &document.Node{Kind: document.Null}},
|
||||
}}
|
||||
var output bytes.Buffer
|
||||
if err := Write(&output, root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "empty_map: {}\nempty_sequence: []\nnumber: 1.25e+2\nredacted: '[REDACTED]'\nsequence:\n - first\n - 'true'\nnil_value: null\n"
|
||||
if output.String() != want {
|
||||
t.Fatalf("output:\n%s\nwant:\n%s", output.String(), want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWritePropagatesWriterErrorsAndShortWrites(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := &document.Node{Kind: document.Mapping, Mapping: []document.Entry{{Key: "value", Value: &document.Node{Kind: document.String, Text: "safe"}}}}
|
||||
want := errors.New("write failed")
|
||||
if err := Write(failingWriter{err: want}, root); !errors.Is(err, want) {
|
||||
t.Fatalf("writer error = %v", err)
|
||||
}
|
||||
if err := Write(shortOutput{}, root); !errors.Is(err, io.ErrShortWrite) {
|
||||
t.Fatalf("short writer error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type failingWriter struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (writer failingWriter) Write(data []byte) (int, error) {
|
||||
return min(2, len(data)), writer.err
|
||||
}
|
||||
|
||||
type shortOutput struct{}
|
||||
|
||||
func (shortOutput) Write(data []byte) (int, error) {
|
||||
return max(0, len(data)-1), nil
|
||||
}
|
||||
Reference in New Issue
Block a user