OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / pelletier / go-toml / cmd / tomljson / main_test.go
1 package main
2
3 import (
4         "bytes"
5         "io/ioutil"
6         "os"
7         "strings"
8         "testing"
9 )
10
11 func expectBufferEquality(t *testing.T, name string, buffer *bytes.Buffer, expected string) {
12         output := buffer.String()
13         if output != expected {
14                 t.Errorf("incorrect %s:\n%s\n\nexpected %s:\n%s", name, output, name, expected)
15                 t.Log([]rune(output))
16                 t.Log([]rune(expected))
17         }
18 }
19
20 func expectProcessMainResults(t *testing.T, input string, args []string, exitCode int, expectedOutput string, expectedError string) {
21         inputReader := strings.NewReader(input)
22         outputBuffer := new(bytes.Buffer)
23         errorBuffer := new(bytes.Buffer)
24
25         returnCode := processMain(args, inputReader, outputBuffer, errorBuffer)
26
27         expectBufferEquality(t, "output", outputBuffer, expectedOutput)
28         expectBufferEquality(t, "error", errorBuffer, expectedError)
29
30         if returnCode != exitCode {
31                 t.Error("incorrect return code:", returnCode, "expected", exitCode)
32         }
33 }
34
35 func TestProcessMainReadFromStdin(t *testing.T) {
36         input := `
37                 [mytoml]
38                 a = 42`
39         expectedOutput := `{
40   "mytoml": {
41     "a": 42
42   }
43 }
44 `
45         expectedError := ``
46         expectedExitCode := 0
47
48         expectProcessMainResults(t, input, []string{}, expectedExitCode, expectedOutput, expectedError)
49 }
50
51 func TestProcessMainReadFromFile(t *testing.T) {
52         input := `
53                 [mytoml]
54                 a = 42`
55
56         tmpfile, err := ioutil.TempFile("", "example.toml")
57         if err != nil {
58                 t.Fatal(err)
59         }
60         if _, err := tmpfile.Write([]byte(input)); err != nil {
61                 t.Fatal(err)
62         }
63
64         defer os.Remove(tmpfile.Name())
65
66         expectedOutput := `{
67   "mytoml": {
68     "a": 42
69   }
70 }
71 `
72         expectedError := ``
73         expectedExitCode := 0
74
75         expectProcessMainResults(t, ``, []string{tmpfile.Name()}, expectedExitCode, expectedOutput, expectedError)
76 }
77
78 func TestProcessMainReadFromMissingFile(t *testing.T) {
79         expectedError := `open /this/file/does/not/exist: no such file or directory
80 `
81         expectProcessMainResults(t, ``, []string{"/this/file/does/not/exist"}, -1, ``, expectedError)
82 }