Debian Codegraph @

txtar/archive_test.go

raw symbols SCIP
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
5 package txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/
6
7 import (
8 "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/"
9 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
10 "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/"
11 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
12 )
13
14 func TestParsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/TestParse().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
15 var tests = []struct {
16 name string
17 text string
18 parsed *Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#
19 }{
20 {
21 name: "basic",
22 text: `comment1
23 comment2
24 -- file1 --
25 File 1 text.
26 -- foo ---
27 More file 1 text.
28 -- file 2 --
29 File 2 text.
30 -- empty --
31 -- noNL --
32 hello world
33 -- empty filename line --
34 some content
35 -- --`,
36 parsed: &Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#{
37 Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Comment.: []byte("comment1\ncomment2\n"),
38 Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files.: []Filescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/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 name: "crlf",
49 text: "comment\r\n-- file --\r\ndata\r\n",
50 parsed: &Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#{
51 Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Comment.: []byte("comment\r\n"),
52 Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files.: []Filescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#{{"file", []byte("data\r\n")}},
53 },
54 },
55 {
56 name: "utf8 and comment",
57 text: `# This is a test comment
58 -- hello.txt --
59 Hello
60 -- unicode.txt --
61 Go语言
62 `,
63 parsed: &Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#{
64 Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Comment.: []byte("# This is a test comment\n"),
65 Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files.: []Filescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#{
66 {"hello.txt", []byte("Hello\n")},
67 {"unicode.txt", []byte("Go语言\n")},
68 },
69 },
70 },
71 }
72 for _, tt := range tests {
73 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
74 a := Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(tt.text))
75 if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(a, tt.parsed) {
76 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Parse: wrong output:\nhave:\n%s\nwant:\n%s", shortArchivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/shortArchive().(a), shortArchivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/shortArchive().(tt.parsed))
77 }
78 text := Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Format().(a)
79 a = Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().(text)
80 if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(a, tt.parsed) {
81 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Parse after Format: wrong output:\nhave:\n%s\nwant:\n%s", shortArchivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/shortArchive().(a), shortArchivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/shortArchive().(tt.parsed))
82 }
83 })
84 }
85 }
86
87 func TestFormatscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/TestFormat().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
88 var tests = []struct {
89 name string
90 input *Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#
91 wanted string
92 }{
93 {
94 name: "basic",
95 input: &Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#{
96 Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Comment.: []byte("comment1\ncomment2\n"),
97 Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files.: []Filescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#{
98 {"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
99 {"file 2", []byte("File 2 text.\n")},
100 {"empty", []byte{}},
101 {"noNL", []byte("hello world")},
102 },
103 },
104 wanted: `comment1
105 comment2
106 -- file1 --
107 File 1 text.
108 -- foo ---
109 More file 1 text.
110 -- file 2 --
111 File 2 text.
112 -- empty --
113 -- noNL --
114 hello world
115 `,
116 },
117 }
118 for _, tt := range tests {
119 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
120 result := Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Format().(tt.input)
121 if string(result) != tt.wanted {
122 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Wrong output. \nGot:\n%s\nWant:\n%s\n", string(result), tt.wanted)
123 }
124 })
125 }
126 }
127
128 func shortArchivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/shortArchive().(a *Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#) string {
129 var buf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#
130 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, "comment: %q\n", a.Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Comment.)
131 for _, f := range a.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files. {
132 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, "file %q: %q\n", f.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name., f.Datascip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Data.)
133 }
134 return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().()
135 }