-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsonfiddle_test.go
87 lines (77 loc) · 2.47 KB
/
jsonfiddle_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main_test
import (
"bytes"
"os"
"os/exec"
"strings"
"testing"
)
const (
cmdTest = "jsonfiddle"
dirTest = "test/"
extRef = ".ref" // extension for reference file
extGot = ".got" // extension for generated file
)
// testIt runs @cmdEasyGen with @argv and compares the generated
// output for @name with the corresponding @extRef
func testIt(t *testing.T, name string, argv ...string) {
var (
diffOut bytes.Buffer
generatedOutput = name + extGot
cmd = exec.Command(cmdTest, argv...)
)
t.Logf("Testing %s:\n\t%s %s\n", name, cmdTest, strings.Join(argv, " "))
// open the out file for writing
outfile, err := os.Create(generatedOutput)
if err != nil {
t.Errorf("write error [%s: %s] %s.", name, argv, err)
}
defer outfile.Close()
cmd.Stdout = outfile
err = cmd.Start()
if err != nil {
t.Errorf("start error [%s: %s] %s.", name, argv, err)
}
err = cmd.Wait()
if err != nil {
t.Errorf("exit error [%s: %s] %s.", name, argv, err)
}
cmd = exec.Command("diff", "-U1", name+extRef, generatedOutput)
cmd.Stdout = &diffOut
err = cmd.Start()
if err != nil {
t.Errorf("start error %s [%s: %s]", err, name, argv)
}
err = cmd.Wait()
if err != nil {
t.Errorf("cmp error %s [%s: %s]\n%s", err, name, argv, diffOut.String())
}
//os.Remove(generatedOutput)
}
func TestExec(t *testing.T) {
os.Chdir(dirTest)
// == Test Basic Functions
// -- fmt
t.Logf("\n\n== Testing Basic fmt Functions\n\n")
testIt(t, "CustomerC", "fmt", "-c", "-i", "Customer.json")
testIt(t, "Customer", "fmt", "-i", "Customer.json")
testIt(t, "Schedules", "fmt", "-i", "Schedules.json")
testIt(t, "CustomerP", "fmt", "-p", "-i", "CustomerP.json")
testIt(t, "CustomerPC", "fmt", "-c", "-p", "-i", "CustomerP.json")
// -- sort
t.Logf("\n\n== Testing Basic sort Functions\n\n")
testIt(t, "CustomerSI", "sort", "-i", "Customer.json")
testIt(t, "CustomerSC", "sort", "-c", "-i", "Customer.json")
testIt(t, "CustomerPS", "sort", "-p", "-i", "CustomerP.json")
testIt(t, "SchedulesSI", "sort", "-i", "Schedules.json")
// -- j2s
t.Logf("\n\n== Testing Basic j2s Functions\n\n")
testIt(t, "CustomerJ2S", "j2s", "-i", "Customer.json")
testIt(t, "GoodsJ2S", "j2s", "-i", "Goods.json")
testIt(t, "MenuItemsJ2S", "j2s", "-i", "MenuItems.json")
testIt(t, "SmartyStreetsJ2S", "j2s", "-i", "SmartyStreets.json")
testIt(t, "SchedulesJ2S", "j2s", "-i", "Schedules.json")
// -- x2j
//t.Logf("\n\n== Testing Basic x2j Functions\n\n")
//testIt(t, "Books", "x2j", "-i", "Books.xml")
}