-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
98 lines (92 loc) · 1.79 KB
/
example_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
package tbln_test
import (
"bytes"
"log"
"os"
"strings"
"github.com/noborus/tbln"
)
func Example() {
in := `; TableName: simple
; name: | id | name |
; type: | int | text |
| 1 | Bob |
| 2 | Alice |
`
at, err := tbln.ReadAll(strings.NewReader(in))
if err != nil {
log.Fatal(err)
}
at.SetTableName("newtable")
err = tbln.WriteAll(os.Stdout, at)
if err != nil {
log.Fatal(err)
}
// Output:
//; TableName: newtable
//; name: | id | name |
//; type: | int | text |
//| 1 | Bob |
//| 2 | Alice |
}
func ExampleTBLN() {
var err error
tb := tbln.NewTBLN()
tb.SetTableName("sample")
// SetNames sets column names
err = tb.SetNames([]string{"id", "name"})
if err != nil {
log.Fatal(err)
}
// SetTypes sets the column type
err = tb.SetTypes([]string{"int", "text"})
if err != nil {
log.Fatal(err)
}
// Add a row.
// The number of columns should be the same
// number of columns in Names and Types.
err = tb.AddRows([]string{"1", "Bob"})
if err != nil {
log.Fatal(err)
}
err = tb.AddRows([]string{"2", "Alice"})
if err != nil {
log.Fatal(err)
}
err = tbln.WriteAll(os.Stdout, tb)
if err != nil {
log.Fatal(err)
}
// Output:
//; TableName: sample
//; name: | id | name |
//; type: | int | text |
//| 1 | Bob |
//| 2 | Alice |
}
func ExampleDiffAll() {
TestDiff1 := `; name: | id | name | age |
; type: | int | text | int |
; primarykey: | id |
; TableName: test1
| 1 | Bob | 19 |
`
TestDiff2 := `; name: | id | name | age |
; type: | int | text | int |
; primarykey: | id |
; TableName: test1
| 1 | Bob | 19 |
| 2 | Alice | 14 |
`
err := tbln.DiffAll(os.Stdout,
tbln.NewReader(bytes.NewBufferString(TestDiff1)),
tbln.NewReader(bytes.NewBufferString(TestDiff2)),
tbln.AllDiff)
if err != nil {
log.Fatal(err)
}
// Output:
//| 1 | Bob | 19 |
//+| 2 | Alice | 14 |
}