txtar/archive: add unit test function for Format

Unit test function TestFormat is added for Format in archive package. Moreover, Test method is renamed to TestParse, because it only tests Parse function.

Change-Id: Ibd94fa7ada456ffcbcf1d02eb3a6c3beba5ba84d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/208177
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/txtar/archive_test.go b/txtar/archive_test.go
index 3f734f6..1f79f8d 100644
--- a/txtar/archive_test.go
+++ b/txtar/archive_test.go
@@ -11,14 +11,15 @@
 	"testing"
 )
 
-var tests = []struct {
-	name   string
-	text   string
-	parsed *Archive
-}{
-	{
-		name: "basic",
-		text: `comment1
+func TestParse(t *testing.T) {
+	var tests = []struct {
+		name   string
+		text   string
+		parsed *Archive
+	}{
+		{
+			name: "basic",
+			text: `comment1
 comment2
 -- file1 --
 File 1 text.
@@ -29,19 +30,17 @@
 -- empty --
 -- noNL --
 hello world`,
-		parsed: &Archive{
-			Comment: []byte("comment1\ncomment2\n"),
-			Files: []File{
-				{"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
-				{"file 2", []byte("File 2 text.\n")},
-				{"empty", []byte{}},
-				{"noNL", []byte("hello world\n")},
+			parsed: &Archive{
+				Comment: []byte("comment1\ncomment2\n"),
+				Files: []File{
+					{"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
+					{"file 2", []byte("File 2 text.\n")},
+					{"empty", []byte{}},
+					{"noNL", []byte("hello world\n")},
+				},
 			},
 		},
-	},
-}
-
-func Test(t *testing.T) {
+	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 			a := Parse([]byte(tt.text))
@@ -57,6 +56,47 @@
 	}
 }
 
+func TestFormat(t *testing.T) {
+	var tests = []struct {
+		name   string
+		input  *Archive
+		wanted string
+	}{
+		{
+			name: "basic",
+			input: &Archive{
+				Comment: []byte("comment1\ncomment2\n"),
+				Files: []File{
+					{"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
+					{"file 2", []byte("File 2 text.\n")},
+					{"empty", []byte{}},
+					{"noNL", []byte("hello world\n")},
+				},
+			},
+			wanted: `comment1
+comment2
+-- file1 --
+File 1 text.
+-- foo ---
+More file 1 text.
+-- file 2 --
+File 2 text.
+-- empty --
+-- noNL --
+hello world
+`,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			result := Format(tt.input)
+			if !reflect.DeepEqual(string(result), tt.wanted) {
+				t.Errorf("Wrong output. \nGot:\n%s\nWant:\n%s\n", string(result), tt.wanted)
+			}
+		})
+	}
+}
+
 func shortArchive(a *Archive) string {
 	var buf bytes.Buffer
 	fmt.Fprintf(&buf, "comment: %q\n", a.Comment)