forked from go-yaml/yaml
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
emitter: add indentless_block_sequence option (#13)
* emitter: add indentless_block_sequence option Add an option that will force block sequence items not to be indented. * add formatting test suites
- Loading branch information
Showing
18 changed files
with
186 additions
and
75 deletions.
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
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,3 @@ | ||
# Format Tests | ||
|
||
This is where I will write any formatting tests that mess with decoding and immediately encoding a document. |
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,53 @@ | ||
package formattest | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/braydonk/yaml" | ||
) | ||
|
||
func TestExplicitDocumentStart(t *testing.T) { | ||
formatTestCase{ | ||
name: "explicit document start", | ||
folder: "document_start", | ||
configureDecoder: noopDecoder, | ||
configureEncoder: func(enc *yaml.Encoder) { | ||
enc.SetExplicitDocumentStart(true) | ||
}, | ||
}.Run(t) | ||
} | ||
|
||
func TestIndentless(t *testing.T) { | ||
formatTestCase{ | ||
name: "indentless array", | ||
folder: "indentless", | ||
configureDecoder: noopDecoder, | ||
configureEncoder: func(enc *yaml.Encoder) { | ||
enc.SetIndentlessBlockSequence(true) | ||
}, | ||
}.Run(t) | ||
} | ||
|
||
func TestIndentedToIndentless(t *testing.T) { | ||
formatTestCase{ | ||
name: "indented to indentless array", | ||
folder: "indented_to_indentless", | ||
configureDecoder: noopDecoder, | ||
configureEncoder: func(enc *yaml.Encoder) { | ||
enc.SetIndentlessBlockSequence(true) | ||
}, | ||
}.Run(t) | ||
} | ||
|
||
func TestBlockScalar(t *testing.T) { | ||
formatTestCase{ | ||
name: "block scalar decoding and encoding", | ||
folder: "block_scalar", | ||
configureDecoder: func(dec *yaml.Decoder) { | ||
dec.SetScanBlockScalarAsLiteral(true) | ||
}, | ||
configureEncoder: func(enc *yaml.Encoder) { | ||
enc.SetAssumeBlockAsLiteral(true) | ||
}, | ||
}.Run(t) | ||
} |
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,77 @@ | ||
package formattest | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/braydonk/yaml" | ||
) | ||
|
||
type decoderConfigureFunc func(*yaml.Decoder) | ||
|
||
func noopDecoder(dec *yaml.Decoder) {} | ||
|
||
type encoderConfigureFunc func(*yaml.Encoder) | ||
|
||
type formatTestCase struct { | ||
name string | ||
folder string | ||
configureDecoder decoderConfigureFunc | ||
configureEncoder encoderConfigureFunc | ||
} | ||
|
||
func (tc formatTestCase) Run(t *testing.T) { | ||
t.Run(tc.name, func(t *testing.T) { | ||
// Read test input | ||
input, err := tc.readTestdataFile("input.yaml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Configure Decoder | ||
reader := bytes.NewReader(input) | ||
decoder := yaml.NewDecoder(reader) | ||
tc.configureDecoder(decoder) | ||
|
||
// Decode input document | ||
var n yaml.Node | ||
err = decoder.Decode(&n) | ||
if err != nil && !errors.Is(err, io.EOF) { | ||
t.Fatalf("expect EOF, got:\n%v", err) | ||
} | ||
|
||
// Configure Encoder | ||
var buf bytes.Buffer | ||
enc := yaml.NewEncoder(&buf) | ||
tc.configureEncoder(enc) | ||
|
||
// Encode the decoded input document | ||
err = enc.Encode(&n) | ||
if err != nil { | ||
t.Fatalf("expected nil err, got:\n%v", err) | ||
} | ||
|
||
// Read the expected output | ||
expected, err := tc.readTestdataFile("expected.yaml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if buf.String() != string(expected) { | ||
t.Fatalf("expected:\n%s\nactual:\n%s", string(expected), buf.String()) | ||
} | ||
}) | ||
} | ||
|
||
func (tc formatTestCase) readTestdataFile(path string) ([]byte, error) { | ||
fullPath := filepath.Join("testdata", tc.folder, path) | ||
content, err := os.ReadFile(fullPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("path %s not found", fullPath) | ||
} | ||
return content, nil | ||
} |
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,4 @@ | ||
commands: > | ||
[ -f "/usr/local/bin/foo" ] && | ||
echo "skip install" || | ||
go install github.com/foo/foo@latest |
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,4 @@ | ||
commands: > | ||
[ -f "/usr/local/bin/foo" ] && | ||
echo "skip install" || | ||
go install github.com/foo/foo@latest |
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,2 @@ | ||
--- | ||
a: 1 |
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 @@ | ||
a: 1 |
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,3 @@ | ||
a: | ||
- 1 | ||
- 2 |
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,3 @@ | ||
a: | ||
- 1 | ||
- 2 |
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,7 @@ | ||
a: | ||
- 1 | ||
- 2 | ||
b: | ||
c: | ||
- 1 | ||
- 2 |
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,7 @@ | ||
a: | ||
- 1 | ||
- 2 | ||
b: | ||
c: | ||
- 1 | ||
- 2 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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