From d9146750bac153232ee4512ff508aa3ae0298355 Mon Sep 17 00:00:00 2001 From: noe Date: Sun, 6 Apr 2025 20:55:46 -0700 Subject: [PATCH] move presentation to templates --- presentation/role_test.go | 31 -------------- .../components}/role.go | 40 +++++++++---------- templates/components/role_test.go | 38 +++++++++++++----- templates/layouts/main_test.go | 16 ++++++++ testing/testing.go | 14 +++---- 5 files changed, 70 insertions(+), 69 deletions(-) delete mode 100644 presentation/role_test.go rename {presentation => templates/components}/role.go (74%) create mode 100644 templates/layouts/main_test.go diff --git a/presentation/role_test.go b/presentation/role_test.go deleted file mode 100644 index 5f00843..0000000 --- a/presentation/role_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package presentation_test - -import ( - "testing" - - "git.sapphic.engineer/roleypoly/v4/presentation" - "git.sapphic.engineer/roleypoly/v4/types/fixtures" - "github.com/stretchr/testify/assert" -) - -func TestRole(t *testing.T) { - r := presentation.Role(&fixtures.CategoryMulti, &fixtures.RoleWithDarkColor, true) - assert.Equal(t, fixtures.RoleWithDarkColor.ID, r.ID) - assert.Equal(t, fixtures.RoleWithDarkColor.Name, r.Name) - assert.Equal(t, presentation.InputCheckbox, r.InputType) - assert.Equal(t, "#a20000", r.Colors.Main) - assert.True(t, r.Selected) - - r = presentation.Role(&fixtures.CategorySingle, &fixtures.RoleWithDarkColor, false) - assert.Equal(t, presentation.InputRadio, r.InputType) - assert.False(t, r.Selected) - - r = presentation.Role(&fixtures.CategorySingle, &fixtures.RoleWithLightColor, true) - assert.True(t, r.Selected) -} - -func TestGetColors(t *testing.T) { - c := presentation.GetColors(0xa20000) - assert.Equal(t, "#a20000", c.Main) - assert.Equal(t, "#ffd8d8", c.Alt) -} diff --git a/presentation/role.go b/templates/components/role.go similarity index 74% rename from presentation/role.go rename to templates/components/role.go index 2ce89ae..efdf081 100644 --- a/presentation/role.go +++ b/templates/components/role.go @@ -1,10 +1,24 @@ -package presentation +package components import ( "git.sapphic.engineer/roleypoly/v4/types" "git.sapphic.engineer/roleypoly/v4/utils" ) +type RoleTemplateData struct { + ID string + CategoryID string + Name string + Selected bool + InputType InputType + Colors RoleColors +} + +type RoleColors struct { + Main string + Alt string +} + type InputType string const ( @@ -12,24 +26,15 @@ const ( InputRadio InputType = "radio" ) -type PresentableRole struct { - ID string - CategoryID string - Name string - Selected bool - InputType InputType - Colors PresentableRoleColors -} - -func Role(category *types.Category, role *types.Role, selected bool) PresentableRole { +func Role(category *types.Category, role *types.Role, selected bool) RoleTemplateData { inputType := InputCheckbox if category.Type == types.CategorySingle { inputType = InputRadio } - colors := GetColors(role.Color) + colors := NewRoleColors(role.Color) - return PresentableRole{ + return RoleTemplateData{ ID: role.ID, CategoryID: category.ID, Name: role.Name, @@ -39,18 +44,13 @@ func Role(category *types.Category, role *types.Role, selected bool) Presentable } } -type PresentableRoleColors struct { - Main string - Alt string -} - -func GetColors(roleColor uint32) PresentableRoleColors { +func NewRoleColors(roleColor uint32) RoleColors { // TODO: no color r, g, b := utils.IntToRgb(roleColor) altR, altG, altB := utils.AltColor(r, g, b) - return PresentableRoleColors{ + return RoleColors{ Main: utils.RgbToString(r, g, b), Alt: utils.RgbToString(altR, altG, altB), } diff --git a/templates/components/role_test.go b/templates/components/role_test.go index 4126689..c52437e 100644 --- a/templates/components/role_test.go +++ b/templates/components/role_test.go @@ -4,23 +4,18 @@ import ( "fmt" "testing" - "git.sapphic.engineer/roleypoly/v4/presentation" + "git.sapphic.engineer/roleypoly/v4/templates/components" "git.sapphic.engineer/roleypoly/v4/templates/templatetesting" - "git.sapphic.engineer/roleypoly/v4/types" "git.sapphic.engineer/roleypoly/v4/types/fixtures" "git.sapphic.engineer/roleypoly/v4/utils" "github.com/stretchr/testify/assert" ) -func renderRole(t *testing.T, c *types.Category, r *types.Role, s bool) string { - data := presentation.Role(c, r, s) - return templatetesting.Template(t, "components/role", data) -} - -func TestRole(t *testing.T) { +func TestRoleTemplate(t *testing.T) { c := &fixtures.CategoryMulti r := &fixtures.RoleWithDarkColor - html := renderRole(t, c, r, false) + data := components.Role(c, r, true) + html := templatetesting.Template(t, "components/role", data) assert.Contains(t, html, "--role-color: #a20000;", "role color is set") assert.Contains(t, html, `type="checkbox"`, "multi has input type=checkbox") assert.Contains(t, html, fmt.Sprintf("--contrast-color: %s;", utils.RgbToString(utils.AltColor(162, 0, 0))), "contrast color is set") @@ -31,10 +26,31 @@ func TestRole(t *testing.T) { c = &fixtures.CategorySingle r = &fixtures.RoleWithLightColor - html = renderRole(t, c, r, true) + data = components.Role(c, r, false) + html = templatetesting.Template(t, "components/role", data) assert.Contains(t, html, `type="radio"`, "single has input type=radio") assert.Contains(t, html, fmt.Sprintf("--contrast-color: %s;", utils.RgbToString(utils.AltColor(0xff, 0xaa, 0x88))), "contrast color") assert.Contains(t, html, fmt.Sprintf(`name="%s"`, utils.RoleInputName(c)), "single has name attr") } -// TODO: these can probably be string utils that are injected as functions into template +func TestRole(t *testing.T) { + r := components.Role(&fixtures.CategoryMulti, &fixtures.RoleWithDarkColor, true) + assert.Equal(t, fixtures.RoleWithDarkColor.ID, r.ID) + assert.Equal(t, fixtures.RoleWithDarkColor.Name, r.Name) + assert.Equal(t, components.InputCheckbox, r.InputType) + assert.Equal(t, "#a20000", r.Colors.Main) + assert.True(t, r.Selected) + + r = components.Role(&fixtures.CategorySingle, &fixtures.RoleWithDarkColor, false) + assert.Equal(t, components.InputRadio, r.InputType) + assert.False(t, r.Selected) + + r = components.Role(&fixtures.CategorySingle, &fixtures.RoleWithLightColor, true) + assert.True(t, r.Selected) +} + +func TestNewRoleColors(t *testing.T) { + c := components.NewRoleColors(0xa20000) + assert.Equal(t, "#a20000", c.Main) + assert.Equal(t, "#ffd8d8", c.Alt) +} diff --git a/templates/layouts/main_test.go b/templates/layouts/main_test.go new file mode 100644 index 0000000..374d0a2 --- /dev/null +++ b/templates/layouts/main_test.go @@ -0,0 +1,16 @@ +package layouts_test + +import ( + "testing" + + "git.sapphic.engineer/roleypoly/v4/templates/templatetesting" + "github.com/stretchr/testify/assert" +) + +func TestMainLayout(t *testing.T) { + r := templatetesting.Template(t, "layouts/main", struct{ HeadTitle string }{HeadTitle: "roleypoly"}) + assert.Contains(t, r, "%%EMBED%%", "has {{embed}}") + assert.Contains(t, r, "", "loaded navigation (close)") + assert.Contains(t, r, "roleypoly", "sets title") +} diff --git a/testing/testing.go b/testing/testing.go index b6514c7..71d4f82 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -7,7 +7,7 @@ import ( "git.sapphic.engineer/roleypoly/v4/auth/authmiddleware" "git.sapphic.engineer/roleypoly/v4/discord" - "git.sapphic.engineer/roleypoly/v4/presentation" + "git.sapphic.engineer/roleypoly/v4/templates/components" "git.sapphic.engineer/roleypoly/v4/types" "git.sapphic.engineer/roleypoly/v4/types/fixtures" ) @@ -39,12 +39,12 @@ func (t *TestingController) TestTemplate(c fiber.Ctx) error { cat1 := fixtures.Category(fixtures.CategoryMulti) cat2 := fixtures.Category(fixtures.CategorySingle) return c.Render("tests/"+which, fiber.Map{ - "TestRole": presentation.Role(cat1, &fixtures.RoleWithDarkColor, false), - "TestRole2": presentation.Role(cat1, &fixtures.RoleWithDarkMediumColor, false), - "TestRole3": presentation.Role(cat1, &fixtures.RoleWithLightColor, true), - "TestRole4": presentation.Role(cat1, &fixtures.RoleWithDarkColor, false), - "TestRole5": presentation.Role(cat2, &fixtures.RoleWithLightColor, true), - "TestRole6": presentation.Role(cat1, &fixtures.RoleWithLightMediumColor, false), + "TestRole": components.Role(cat1, &fixtures.RoleWithDarkColor, false), + "TestRole2": components.Role(cat1, &fixtures.RoleWithDarkMediumColor, false), + "TestRole3": components.Role(cat1, &fixtures.RoleWithLightColor, true), + "TestRole4": components.Role(cat1, &fixtures.RoleWithDarkColor, false), + "TestRole5": components.Role(cat2, &fixtures.RoleWithLightColor, true), + "TestRole6": components.Role(cat1, &fixtures.RoleWithLightMediumColor, false), }, "layouts/main") }