mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
8584cd59af
This enables clients to move back and forth between parsed objects and text patches. The generated patches are semantically equal to the parsed object and should re-parse to the same object, but may not be byte-for-byte identical to the original input. In my testing, formatted text patches are usually identical to the input, but there may be cases where this is not true. Binary patches always differ. This is because Go's 'compress/flate' package ends streams with an empty block instead of adding the end-of-stream flag to the last non-empty block, like Git's C implementation. Since the streams will always be different for this reason, I chose to also enable default compression (the test patches I generated with Git used no compression.) The main tests for this feature involve parsing, formatting, and then re-parsing a patch to make sure we get equal objects. Formatting is handled by a new internal formatter type, which allows writing all data to the same stream. This isn't exposed publicly right now, but will be useful if there's a need for more flexible formatting functions in the future, like formatting to a user-provided io.Writer.
29 lines
646 B
Go
29 lines
646 B
Go
package gitdiff
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFormatter_WriteQuotedName(t *testing.T) {
|
|
tests := []struct {
|
|
Input string
|
|
Expected string
|
|
}{
|
|
{"noquotes.txt", `noquotes.txt`},
|
|
{"no quotes.txt", `no quotes.txt`},
|
|
{"new\nline", `"new\nline"`},
|
|
{"escape\x1B null\x00", `"escape\033 null\000"`},
|
|
{"snowman \u2603 snowman", `"snowman \342\230\203 snowman"`},
|
|
{"\"already quoted\"", `"\"already quoted\""`},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
var b strings.Builder
|
|
newFormatter(&b).WriteQuotedName(test.Input)
|
|
if b.String() != test.Expected {
|
|
t.Errorf("expected %q, got %q", test.Expected, b.String())
|
|
}
|
|
}
|
|
}
|