Serializer.js/tests/main.go

171 lines
3.8 KiB
Go
Raw Normal View History

2017-08-26 23:44:47 +02:00
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"reflect"
"text/tabwriter"
"time"
"github.com/fatih/color"
"github.com/julienschmidt/httprouter"
)
var templates *template.Template
var tw *tabwriter.Writer
var testData = []*Person{
&Person{
Name: "Foo",
Surname: "Bar",
Age: 57,
BirthDate: time.Date(1960, 1, 1, 0, 0, 0, 0, time.UTC),
2018-05-02 18:04:48 +02:00
Animal: "Dog",
Checklist: []string{
"Milk",
},
2017-08-26 23:44:47 +02:00
},
nil,
&Person{
Name: "Crow",
Surname: "CrowCrow",
Age: 22,
BirthDate: time.Date(1995, 1, 5, 0, 0, 0, 0, time.UTC),
Languages: []Language{
2018-05-02 18:04:48 +02:00
{
2017-08-26 23:44:47 +02:00
Language: "Dutch",
SkillLevel: 9.99,
2017-08-27 14:21:06 +02:00
Native: true,
2017-08-26 23:44:47 +02:00
},
2018-05-02 18:04:48 +02:00
{
2017-08-26 23:44:47 +02:00
Language: "English",
SkillLevel: 8.4,
2017-08-27 14:21:06 +02:00
Native: false,
2017-08-26 23:44:47 +02:00
},
2018-05-02 18:04:48 +02:00
{
2017-08-27 14:21:06 +02:00
Language: "Japanese",
2017-08-26 23:44:47 +02:00
SkillLevel: -9000.9,
2017-08-27 14:21:06 +02:00
Native: false,
2017-08-26 23:44:47 +02:00
},
},
2018-05-02 18:04:48 +02:00
Animal: "Dog",
Checklist: []string{
"Milk",
},
2017-08-26 23:44:47 +02:00
},
}
func main() {
router := httprouter.New()
router.GET("/", getTest)
router.POST("/", postTest)
router.ServeFiles("/static/*filepath", http.Dir("../"))
var err error
templates, err = template.ParseGlob(`templates/*.gohtml`)
if err != nil {
fmt.Println(err)
return
}
tw = tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
server := httptest.NewServer(router)
2017-08-27 14:22:06 +02:00
testBrowser("chrome", "--headless", "--disable-gpu", server.URL)
testBrowser("chromium", "--headless", "--disable-gpu", server.URL)
// Firefox is being annoying, enable if you like
// testBrowser("firefox", "-headless", server.URL)
2017-08-26 23:44:47 +02:00
}
// Person ...
type Person struct {
Name string `json:"name"`
Surname string `json:"surname"`
Age int `json:"age"`
BirthDate time.Time `json:"birth_date"`
Languages []Language `json:"languages"`
2018-05-02 18:04:48 +02:00
Checklist []string `json:"checklist"`
Animal string `json:"animal"`
2017-08-26 23:44:47 +02:00
}
2018-05-02 18:04:48 +02:00
// Language ...
2017-08-26 23:44:47 +02:00
type Language struct {
Language string `json:"language"`
SkillLevel float64 `json:"skill_level"`
2017-08-27 14:21:06 +02:00
Native bool `json:"native"`
2017-08-26 23:44:47 +02:00
}
func getTest(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
templates.ExecuteTemplate(w, `test.gohtml`, testData)
}
func postTest(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var p []*Person
2017-08-27 14:22:06 +02:00
body := json.NewDecoder(r.Body)
2017-08-29 12:58:02 +02:00
2017-08-27 14:22:06 +02:00
err := body.Decode(&p)
2017-08-26 23:44:47 +02:00
if err != nil {
2017-08-27 14:22:06 +02:00
fmt.Println(color.RedString(err.Error()))
2017-08-26 23:44:47 +02:00
}
for k := range p {
if p[k] == nil && testData[k] == nil {
continue
}
isEq(*p[k], *testData[k])
}
tw.Flush()
}
2017-08-27 14:22:06 +02:00
func testBrowser(command string, flags ...string) {
fmt.Println("\nTesting", color.RedString(command))
err := exec.Command(command, flags...).Run()
if err != nil {
fmt.Println(color.RedString(err.Error()))
}
}
2017-08-26 23:44:47 +02:00
func isEq(p1, p2 Person) {
assert(p1.Name, p2.Name, "Name")
assert(p1.Surname, p2.Surname, "Surname")
assert(p1.Age, p2.Age, "Age")
assert(p1.BirthDate, p2.BirthDate, "BirthDate")
2018-05-02 18:04:48 +02:00
assert(p1.Animal, p2.Animal, "Animal")
2017-08-29 12:58:02 +02:00
if !assert(len(p2.Languages), len(p1.Languages), "Languages length") {
return
}
2018-05-02 18:04:48 +02:00
if !assert(len(p2.Checklist), len(p1.Checklist), "Checklist length") {
return
}
2017-08-29 12:58:02 +02:00
for k := range p2.Languages {
2017-08-26 23:44:47 +02:00
assert(p1.Languages[k].Language, p2.Languages[k].Language, "Languages.Language")
assert(p1.Languages[k].SkillLevel, p2.Languages[k].SkillLevel, "Languages.SkillLevel")
2017-08-27 14:21:06 +02:00
assert(p1.Languages[k].Native, p2.Languages[k].Native, "Languages.Native")
2017-08-26 23:44:47 +02:00
}
2018-05-02 18:04:48 +02:00
for k := range p2.Checklist {
assert(p1.Checklist[k], p2.Checklist[k], "Checklist")
}
2017-08-26 23:44:47 +02:00
}
2017-08-29 12:58:02 +02:00
func assert(v, v2 interface{}, ident string) bool {
pass := true
2017-08-26 23:44:47 +02:00
if !reflect.DeepEqual(v, v2) {
fmt.Fprint(tw, color.RedString("FAIL\t"))
2017-08-29 12:58:02 +02:00
pass = false
2017-08-26 23:44:47 +02:00
} else {
fmt.Fprint(tw, color.GreenString("PASS\t"))
}
fmt.Fprintf(tw, "%s\t%s\n", color.BlueString(ident), color.YellowString(fmt.Sprintf("'%v'\t'%v'", v, v2)))
2017-08-29 12:58:02 +02:00
return pass
2017-08-26 23:44:47 +02:00
}