GoPLS Viewer

Home|gopls/txtar/archive_test.go
1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package txtar
6
7import (
8    "bytes"
9    "fmt"
10    "reflect"
11    "testing"
12)
13
14func TestParse(t *testing.T) {
15    var tests = []struct {
16        name   string
17        text   string
18        parsed *Archive
19    }{
20        {
21            name"basic",
22            text`comment1
23comment2
24-- file1 --
25File 1 text.
26-- foo ---
27More file 1 text.
28-- file 2 --
29File 2 text.
30-- empty --
31-- noNL --
32hello world
33-- empty filename line --
34some content
35-- --`,
36            parsed: &Archive{
37                Comment: []byte("comment1\ncomment2\n"),
38                Files: []File{
39                    {"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
40                    {"file 2", []byte("File 2 text.\n")},
41                    {"empty", []byte{}},
42                    {"noNL", []byte("hello world\n")},
43                    {"empty filename line", []byte("some content\n-- --\n")},
44                },
45            },
46        },
47    }
48    for _tt := range tests {
49        t.Run(tt.name, func(t *testing.T) {
50            a := Parse([]byte(tt.text))
51            if !reflect.DeepEqual(att.parsed) {
52                t.Fatalf("Parse: wrong output:\nhave:\n%s\nwant:\n%s"shortArchive(a), shortArchive(tt.parsed))
53            }
54            text := Format(a)
55            a = Parse(text)
56            if !reflect.DeepEqual(att.parsed) {
57                t.Fatalf("Parse after Format: wrong output:\nhave:\n%s\nwant:\n%s"shortArchive(a), shortArchive(tt.parsed))
58            }
59        })
60    }
61}
62
63func TestFormat(t *testing.T) {
64    var tests = []struct {
65        name   string
66        input  *Archive
67        wanted string
68    }{
69        {
70            name"basic",
71            input: &Archive{
72                Comment: []byte("comment1\ncomment2\n"),
73                Files: []File{
74                    {"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
75                    {"file 2", []byte("File 2 text.\n")},
76                    {"empty", []byte{}},
77                    {"noNL", []byte("hello world")},
78                },
79            },
80            wanted`comment1
81comment2
82-- file1 --
83File 1 text.
84-- foo ---
85More file 1 text.
86-- file 2 --
87File 2 text.
88-- empty --
89-- noNL --
90hello world
91`,
92        },
93    }
94    for _tt := range tests {
95        t.Run(tt.name, func(t *testing.T) {
96            result := Format(tt.input)
97            if string(result) != tt.wanted {
98                t.Errorf("Wrong output. \nGot:\n%s\nWant:\n%s\n"string(result), tt.wanted)
99            }
100        })
101    }
102}
103
104func shortArchive(a *Archivestring {
105    var buf bytes.Buffer
106    fmt.Fprintf(&buf"comment: %q\n"a.Comment)
107    for _f := range a.Files {
108        fmt.Fprintf(&buf"file %q: %q\n"f.Namef.Data)
109    }
110    return buf.String()
111}
112
MembersX
TestParse
TestParse.RangeStmt_899.tt
TestFormat
TestFormat.t
TestFormat.RangeStmt_1919.tt
shortArchive
testing
TestParse.RangeStmt_899.BlockStmt.BlockStmt.a
TestParse.RangeStmt_899.BlockStmt.BlockStmt.text
TestFormat.RangeStmt_1919.BlockStmt.BlockStmt.result
reflect
TestParse.t
shortArchive.buf
shortArchive.RangeStmt_2256.f
shortArchive.a
Members
X