forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_test.go
69 lines (54 loc) · 1.33 KB
/
output_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
)
type NopWriteCloser struct {
io.Writer
}
func (NopWriteCloser) Close() error {
return nil
}
func nopCloser(w io.Writer) io.WriteCloser {
return NopWriteCloser{w}
}
func TestWriteFile(t *testing.T) {
// t.Parallel() cannot be used
// set the function pointer back to its original value
// after we modify it for the test
saveTestHarnessWriteFile := testHarnessWriteFile
defer func() {
testHarnessWriteFile = saveTestHarnessWriteFile
}()
var output []byte
testHarnessWriteFile = func(_ string, in []byte, _ os.FileMode) error {
output = in
return nil
}
buf := &bytes.Buffer{}
writePackageName(buf, "pkg")
fmt.Fprintf(buf, "func hello() {}\n\n\nfunc world() {\nreturn\n}\n\n\n\n")
if err := writeFile("", "", buf); err != nil {
t.Error(err)
}
if string(output) != "package pkg\n\nfunc hello() {}\n\nfunc world() {\n\treturn\n}\n" {
t.Errorf("Wrong output: %q", output)
}
}
func TestFormatBuffer(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "package pkg\n\nfunc() {a}\n")
// Only test error case - happy case is taken care of by template test
_, err := formatBuffer(buf)
if err == nil {
t.Error("want an error")
}
if txt := err.Error(); !strings.Contains(txt, ">>>> func() {a}") {
t.Error("got:\n", txt)
}
}