forked from Rosalita/GoViolin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale_test.go
97 lines (76 loc) · 2.62 KB
/
scale_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
package main
import (
"testing"
"net/http"
"net/http/httptest"
"github.com/stretchr/testify/assert"
"net/url"
"fmt"
)
type TD struct {
Data string
Expected string
}
func TestSettingDefaultScaleOptionsNotNil(t *testing.T) {
assert := assert.New(t)
options1, options2, options3, options4 := setDefaultScaleOptions()
assert.NotNil(options1)
assert.NotNil(options2)
assert.NotNil(options3)
assert.NotNil(options4)
}
func TestSharpToS(t *testing.T) {
assert := assert.New(t)
testData := []TD{
TD{"G#", "Gs"},
TD{"D#", "Ds"},
TD{"Bb", "Bb"},
}
for _, test := range testData {
assert.Equal(changeSharpToS(test.Data), test.Expected)
}
}
func TestScalePage(t *testing.T) {
// Create a request to pass to handler
r, err := http.NewRequest("GET", "http://localhost:8080/scale", nil)
if err != nil {
t.Fatal(err)
}
// Create a new responserecorder (which satisfies http.ResponseWriter) to record the response
rr := httptest.NewRecorder()
handler := http.HandlerFunc(Scale)
// Handlers satisfy http.Handler, so can call their ServeHTTP method directly to pass in the request and responserecorder.
handler.ServeHTTP(rr, r)
// assert status code of response recorder is OK
assert.Equal(t, rr.Code, http.StatusOK, "HandlerFunc(Scale) returned wrong status code: got %v want %v", rr.Code, http.StatusOK)
}
func TestSetRadioButtons(t *testing.T){
// Generate the values for 1 octave C major scale so can pass these values to pass to handler
form := url.Values{}
form.Set("Key", "C")
form.Add("Octave", "1")
form.Add("ScaleArp", "Scale")
form.Add("Pitch", "Major")
// url.Values is a map[string][]string
// url.Values{"Key": {"C"}, "Octave": {"1"},"ScaleArp": {"Scale"}, "Pitch": {"Major"}}
// Create a new POST request with no body
r, err := http.NewRequest("POST", "http://localhost:8080/scaleshow", nil)
if err != nil {
t.Fatal(err)
}
// r.PostForm is currently an empty map
fmt.Println(r.PostForm)
// Set r.PostForm to the url.Values that will be passed to the form
r.PostForm = form
// create a new responserecorder
rr := httptest.NewRecorder()
// create a handler which is set to the handler function that is being tested
handler := http.HandlerFunc(ScaleShow)
// Get the handler to serve the request storing result in the responserecorder
handler.ServeHTTP(rr, r)
fmt.Println(rr.Body)
fmt.Println(rr.Code)
fmt.Println(rr.HeaderMap)
fmt.Println(rr.Flushed)
assert.Equal(t, rr.Code, http.StatusOK, "HandlerFunc(Scaleshow) returned wrong status code: got %v want %v", rr.Code, http.StatusOK)
}