-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcheatsheet_test.go
216 lines (198 loc) · 4.96 KB
/
cheatsheet_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"testing"
"text/template"
"github.com/marianogappa/chart/chartjs"
"github.com/marianogappa/chart/dataset"
"github.com/marianogappa/chart/format"
)
type cheatsheetExample struct {
ID int
Title string
OptionsLine string
Lines []string
HTML string
}
func TestCheatsheet(t *testing.T) {
fs, err := filepath.Glob("testdata/*.csv")
if err != nil {
t.Errorf("Could not get testdata [%v]", err)
t.FailNow()
}
cheetsheetExamples := make([]cheatsheetExample, len(fs))
for j, f := range fs {
fh, err := os.Open(f)
if err != nil {
t.Errorf("[%v] Could not open file", f)
t.FailNow()
}
rd := bufio.NewReader(fh)
optionsLine, err := rd.ReadString('\n')
if err == io.EOF {
t.Errorf("[%v] doesn't have options line", f)
t.FailNow()
}
r := regexp.MustCompile("'.+'|\".+\"|\\S+")
m := r.FindAllString(optionsLine, -1)
o, err := resolveOptions(m)
log.Println(o.title)
if err != nil {
t.Errorf("[%v] can't resolve options line [%v]", f, optionsLine)
t.FailNow()
}
if _, err = rd.ReadString('\n'); err != nil {
t.Errorf("[%v] doesn't have empty line after options line", f)
t.FailNow()
}
var rdr io.Reader
rdr, o.lineFormat = format.Parse(rd, o.separator, o.dateFormat)
d := dataset.MustNew(rdr, o.lineFormat)
if !o.lineFormat.HasFloats && !o.lineFormat.HasDateTimes && o.lineFormat.HasStrings {
d.FSS, d.SSS, o.lineFormat = preprocessFreq(d.SSS, o.lineFormat)
}
o.chartType, err = resolveChartType(o.chartType, o.lineFormat, d.Len())
if err != nil {
t.Errorf("[%v] error resolving chart type when o.chartType=%v and d.lineFormat=%v: [%v]", f, o.chartType, o.lineFormat, err)
t.FailNow()
}
ch := chartjs.New(
chartjs.NewChartType(o.chartType.String()),
*d,
chartjs.Options{
Title: o.title,
ScaleType: chartjs.NewScaleType(o.scaleType.String()),
XLabel: o.xLabel,
YLabel: o.yLabel,
ZeroBased: o.zeroBased,
ColorType: chartjs.NewColorType(o.colorType.String()),
},
)
b := bytes.Buffer{}
err = ch.Build(chartjs.OutputChart, &b)
if err != nil {
t.Errorf("[%v] breaks building chart with: [%v]", f, err)
t.FailNow()
}
cheetsheetExamples[j] = cheatsheetExample{
ID: j,
Title: f,
OptionsLine: optionsLine,
Lines: mustReadLines(f, t),
HTML: b.String(),
}
}
bb := bytes.Buffer{}
chartjs.New(chartjs.ChartType(0), dataset.Dataset{}, chartjs.Options{}).MustBuild(chartjs.OutputHTMLHeader, &bb)
bb.WriteString(cheetsheetString)
chartjs.New(chartjs.ChartType(0), dataset.Dataset{}, chartjs.Options{}).MustBuild(chartjs.OutputHTMLFooter, &bb)
cheetsheetExamplesTemplate, err := template.New("").Parse(bb.String())
if err != nil {
t.Errorf("Could not parse cheatsheet examples page: [%v]", err)
t.FailNow()
}
var b bytes.Buffer
if err := cheetsheetExamplesTemplate.Execute(&b, cheetsheetExamples); err != nil {
t.Errorf("[Could not execute HTML template for cheatsheet [%v]", err)
t.FailNow()
}
err = ioutil.WriteFile("index.html", b.Bytes(), os.ModePerm)
if err != nil {
t.Errorf("Could not write file [%v] [%v]", "index.html", err)
t.FailNow()
}
}
func mustReadLines(path string, t *testing.T) []string {
file, err := os.Open(path)
if err != nil {
t.Errorf("[%v] Could not open file", path)
t.FailNow()
}
defer file.Close()
var (
lines []string
i int
)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
i++
if i < 3 {
continue
}
lines = append(lines, scanner.Text())
}
if scanner.Err() != nil {
t.Errorf("[%v] Error scanning file", scanner.Err())
t.FailNow()
}
return lines
}
var cheetsheetString = `
<style>
body {
background-color: #EEE;
padding: 15px;
}
h1 {
font-size: 30px;
padding-top: 15px;
padding-bottom: 25px;
padding-left: 15px;
}
h2 {
font-family: Lucida Console, Monaco, monospace;
font-size: 13px;
padding-bottom: 15px;
color: #00FF00;
}
.chart {
height: 300px;
width: 650px;
padding-bottom: 15px;
background-color: #FFF;
padding: 10px;
padding-top: 30px;
padding-bottom: 35px;
border-radius: 10px;
}
.pre {
font-family: Lucida Console, Monaco, monospace;
font-size: 13px;
display: block;
padding-bottom: 15px;
color: #00FF00;
}
.chart-wrapper {
background-color: #000;
border-radius: 5px;
padding: 15px;
margin: 10px;
width: 670px;
display: inline-block;
box-shadow: #333 1px 1px 8px;
vertical-align: top;
}
</style>
<h1>Chart Cheatsheet</h1>
{{range .}}
<div class="chart-wrapper">
<h2><pre>$ cat {{.Title}}</pre></h2>
<div class="pre"><pre>{{range .Lines}}{{.}}<br>{{end}}</pre></div>
<h2><pre>$ cat {{.Title}} | chart {{.OptionsLine}}</pre></h2>
<div class="chart">
<canvas id="chart{{.ID}}" height="300px" width="650px"></canvas>
</div>
<script>
var ctx{{.ID}} = document.getElementById("chart{{.ID}}");
var chart{{.ID}} = new Chart(ctx{{.ID}}, {{.HTML}});
</script>
</div>
{{end}}
`