-
Notifications
You must be signed in to change notification settings - Fork 0
/
datafun_test.go
118 lines (80 loc) · 2.14 KB
/
datafun_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package datafun
import (
"testing"
"path"
//"io"
"io/ioutil"
"os"
"strings"
"fmt"
"github.com/jprichardson/goatee-go"
"github.com/jprichardson/readline-go"
)
func TestAddInputOutputOptionsExist (*testing.T) {
return;
program := Init()
_, exists := program.Opts["input"]
t.F (exists)
_, exists = program.Opts["output"]
t.F (exists)
AddInputOutputOptions(program)
_, exists = program.Opts["input"]
t.T (exists)
_, exists = program.Opts["output"]
t.T (exists)
}
func TestAddInputOutputOptionsInput (*testing.T) {
program := Init()
AddInputOutputOptions(program)
tempFile := path.Join(os.TempDir(), "temp-input")
ioutil.WriteFile(tempFile, []byte("line1\nline2"), 0644)
//println(tempFile)
os.Args = []string{"program", "-i", tempFile}
program.Parse()
expect := ""
readline.ReadLine(program.Opts["input"].Value.(*os.File), func (line string) {
expect += line + "\n"
})
t.EQ ("line1\nline2", strings.TrimRight(expect, "\n"))
}
func TestAddInputOutputOptionsInputDefault (*testing.T) {
program := Init()
AddInputOutputOptions(program)
os.Args = []string{"program"}
program.Parse()
t.EQ (program.Opts["input"].Value, os.Stdin)
}
func TestAddInputOutputOptionsOutput (*testing.T) {
program := Init()
AddInputOutputOptions(program)
tempFile := path.Join(os.TempDir(), "temp-output")
os.Args = []string{"program", "-o", tempFile}
program.Parse()
fo := program.Opts["output"].Value.(*os.File)
fmt.Fprintf(fo, "line4\nline5")
fo.Close()
data, _ := ioutil.ReadFile(tempFile)
t.EQ ("line4\nline5", string(data))
}
func TestAddInputOutputOptionsOutputDefault (*testing.T) {
program := Init()
AddInputOutputOptions(program)
os.Args = []string{"program"}
program.Parse()
t.EQ (program.Opts["output"].Value, os.Stdout)
}
func TestAddChunkOption (*testing.T) {
program := Init()
AddChunkOption(program)
os.Args = []string{"program", "-c", "500"}
program.Parse()
t.EQ (program.Opts["chunk"].StringValue, "500")
}
func TestAddCsvOption (*testing.T) {
program := Init()
AddCsvOption(program)
os.Args = []string{"program", "--csv"}
program.Parse()
val := program.Opts["csv"].Value.(bool)
t.T (val)
}