readysite / pkg / application / options_test.go
1.8 KB
options_test.go
package application

import (
	"html/template"
	"net/http"
	"testing"
)

func TestWithEmailer(t *testing.T) {
	app := &App{}
	emailer := &mockEmailer{}

	opt := WithEmailer(emailer)
	opt(app)

	if app.Emailer != emailer {
		t.Error("expected Emailer to be set")
	}
}

func TestWithFunc(t *testing.T) {
	app := &App{
		funcs: make(template.FuncMap),
	}

	opt := WithFunc("double", func(n int) int { return n * 2 })
	opt(app)

	fn, ok := app.funcs["double"]
	if !ok {
		t.Fatal("expected 'double' func to be registered")
	}

	// Verify the function works
	doubleFn := fn.(func(int) int)
	if result := doubleFn(5); result != 10 {
		t.Errorf("expected 10, got %d", result)
	}
}

func TestWithValue(t *testing.T) {
	app := &App{
		funcs: make(template.FuncMap),
	}

	opt := WithValue("version", "1.0.0")
	opt(app)

	fn, ok := app.funcs["version"]
	if !ok {
		t.Fatal("expected 'version' func to be registered")
	}

	// Verify the function returns the value
	versionFn := fn.(func() any)
	if result := versionFn(); result != "1.0.0" {
		t.Errorf("expected '1.0.0', got %v", result)
	}
}

func TestWithController(t *testing.T) {
	app := &App{
		controllers: make(map[string]Controller),
	}

	ctrl := &testController{}
	opt := WithController("test", ctrl)
	opt(app)

	// Verify controller is registered
	if app.controllers["test"] != ctrl {
		t.Error("expected controller to be registered")
	}

	// Verify Setup was called
	if ctrl.app != app {
		t.Error("expected Setup to be called")
	}
}

// Mock implementations for testing

type mockEmailer struct{}

func (m *mockEmailer) Send(to, subject, templateName string, data map[string]any) error {
	return nil
}

type testController struct {
	BaseController
	app *App
}

func (c *testController) Setup(app *App) {
	c.app = app
	c.BaseController.Setup(app)
}

func (c *testController) Handle(r *http.Request) Controller {
	return c
}
← Back