add tests for ron.go
This commit is contained in:
parent
9e5005a516
commit
b1531ba8fb
4
ron.go
4
ron.go
@ -31,7 +31,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func DefaultEngine() *Engine {
|
||||
func defaultEngine() *Engine {
|
||||
return &Engine{
|
||||
mux: http.NewServeMux(),
|
||||
LogLevel: slog.LevelInfo,
|
||||
@ -39,7 +39,7 @@ func DefaultEngine() *Engine {
|
||||
}
|
||||
|
||||
func New(opts ...EngineOptions) *Engine {
|
||||
config := DefaultEngine()
|
||||
config := defaultEngine()
|
||||
return config.apply(opts...)
|
||||
}
|
||||
|
||||
|
||||
100
ron_test.go
100
ron_test.go
@ -7,6 +7,106 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_defaultEngine(t *testing.T) {
|
||||
e := defaultEngine()
|
||||
if e == nil {
|
||||
t.Error("Expected Engine, Actual: nil")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_New(t *testing.T) {
|
||||
e := New()
|
||||
if e == nil {
|
||||
t.Error("Expected Engine, Actual: nil")
|
||||
}
|
||||
if e.Renderer != nil {
|
||||
t.Error("No expected Renderer, Actual: Renderer")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_applyEngineConfig(t *testing.T) {
|
||||
e := New(func(e *Engine) {
|
||||
e.Renderer = NewHTMLRender()
|
||||
e.LogLevel = 1
|
||||
})
|
||||
if e.Renderer == nil {
|
||||
t.Error("Expected Renderer, Actual: nil")
|
||||
}
|
||||
if e.LogLevel != 1 {
|
||||
t.Errorf("Expected LogLevel: 1, Actual: %d", e.LogLevel)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ServeHTTP(t *testing.T) {
|
||||
e := New()
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusNotFound {
|
||||
t.Errorf("Expected status code: %d, Actual: %d", http.StatusNotFound, status)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GET(t *testing.T) {
|
||||
e := New()
|
||||
e.GET("/", func(c *Context) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("GET"))
|
||||
})
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status)
|
||||
}
|
||||
|
||||
if rr.Body.String() != "GET" {
|
||||
t.Errorf("Expected: GET, Actual: %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_POST(t *testing.T) {
|
||||
e := New()
|
||||
e.POST("/", func(c *Context) {
|
||||
c.W.WriteHeader(http.StatusOK)
|
||||
c.W.Write([]byte("POST"))
|
||||
})
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/", nil)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status)
|
||||
}
|
||||
|
||||
if rr.Body.String() != "POST" {
|
||||
t.Errorf("Expected: POST, Actual: %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Static(t *testing.T) {
|
||||
os.Mkdir("assets", os.ModePerm)
|
||||
f, _ := os.Create("assets/style.css")
|
||||
f.WriteString("body { background-color: red; }")
|
||||
f.Close()
|
||||
defer os.Remove("assets/style.css")
|
||||
|
||||
e := New()
|
||||
e.Static("assets", "assets")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/assets/style.css", nil)
|
||||
e.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status)
|
||||
}
|
||||
}
|
||||
|
||||
type Foo struct {
|
||||
Bar string `json:"bar"`
|
||||
Taz int `json:"something"`
|
||||
|
||||
@ -26,7 +26,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func DefaultHTMLRender() *Render {
|
||||
func defaultHTMLRender() *Render {
|
||||
return &Render{
|
||||
EnableCache: false,
|
||||
TemplatesPath: "templates",
|
||||
@ -36,7 +36,7 @@ func DefaultHTMLRender() *Render {
|
||||
}
|
||||
|
||||
func NewHTMLRender(opts ...RenderOptions) *Render {
|
||||
config := DefaultHTMLRender()
|
||||
config := defaultHTMLRender()
|
||||
return config.apply(opts...)
|
||||
}
|
||||
|
||||
|
||||
@ -17,7 +17,7 @@ func Test_DefaultHTMLRender(t *testing.T) {
|
||||
templateCache: make(templateCache),
|
||||
}
|
||||
|
||||
actual := DefaultHTMLRender()
|
||||
actual := defaultHTMLRender()
|
||||
if reflect.DeepEqual(expected, actual) == false {
|
||||
t.Errorf("Expected: %v, Actual: %v", expected, actual)
|
||||
}
|
||||
@ -49,7 +49,7 @@ func Test_HTMLRender(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_apply(t *testing.T) {
|
||||
func Test_applyRenderConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
expected *Render
|
||||
@ -60,7 +60,7 @@ func Test_apply(t *testing.T) {
|
||||
TemplatesPath: "templates",
|
||||
Functions: make(template.FuncMap),
|
||||
templateCache: make(templateCache),
|
||||
}, DefaultHTMLRender()},
|
||||
}, defaultHTMLRender()},
|
||||
{
|
||||
name: "Two OptionFunc", expected: &Render{
|
||||
EnableCache: true,
|
||||
@ -105,7 +105,7 @@ func createDummyFilesAndRender() *Render {
|
||||
f.Write([]byte("{{ template \"layout/another\" .}}{{ define \"base/content\" }}<p>page.another.gohtml</p><p>{{ .Data.foo }}</p>{{ end }}"))
|
||||
f.Close()
|
||||
|
||||
render := DefaultHTMLRender()
|
||||
render := defaultHTMLRender()
|
||||
return render
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user