Skip to content

Commit

Permalink
Write more tests for this small package
Browse files Browse the repository at this point in the history
Signed-off-by: Jo Vandeginste <[email protected]>
  • Loading branch information
jovandeginste committed Feb 20, 2024
1 parent 2d22f9f commit 4466f6a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pkg/templatehelpers/icons.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package templatehelpers

func IconFor(what string) string {
import "html/template"

func IconFor(what string) template.HTML {
iconFunctions := []func(string) string{
categoryIcon,
sportIcon,
Expand All @@ -11,11 +13,11 @@ func IconFor(what string) string {

for _, f := range iconFunctions {
if icon := f(what); icon != "" {
return icon
return template.HTML(icon) //nolint:gosec
}
}

return "icon-solid icon-baseline icon-space-sm icon-before icon-question"
return template.HTML("icon-solid icon-baseline icon-space-sm icon-before icon-question")
}

func categoryIcon(what string) string {
Expand Down
16 changes: 15 additions & 1 deletion pkg/templatehelpers/icons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@ import (
"github.com/stretchr/testify/assert"
)

func TestIconForDefault(t *testing.T) {
for _, dummy := range []string{
"",
"invalid-value-to-get-icon",
";invalid-value-to-get-icon",
} {
d := IconFor(dummy)
assert.Contains(t, d, "icon-solid")
assert.Contains(t, d, "icon-baseline")
assert.Contains(t, d, "icon-space-sm")
assert.Contains(t, d, "icon-before")
assert.Contains(t, d, "icon-question")
}
}

func TestIconFor(t *testing.T) {
assert.Equal(t, "icon-solid icon-baseline icon-space-sm icon-before icon-question", IconFor(""))
assert.Contains(t, IconFor("distance"), "icon-road")
assert.Contains(t, IconFor("close"), "icon-xmark")
assert.Contains(t, IconFor("dashboard"), "icon-chart-line")
Expand Down
14 changes: 8 additions & 6 deletions pkg/templatehelpers/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ func BoolToCheckbox(b bool) template.HTML {
return ""
}

func BuildDecoratedAttribute(icon, name string, value interface{}) interface{} {
return struct {
Icon string
Name string
Value interface{}
}{
type DecoratedAttribute struct {
Icon string
Name string
Value interface{}
}

func BuildDecoratedAttribute(icon, name string, value interface{}) DecoratedAttribute {
return DecoratedAttribute{
Icon: icon,
Name: name,
Value: value,
Expand Down
69 changes: 69 additions & 0 deletions pkg/templatehelpers/template_funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package templatehelpers

import (
"html/template"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestNumericDuration(t *testing.T) {
assert.InDelta(t, 1, NumericDuration(time.Second), 0)
assert.InDelta(t, 3600, NumericDuration(time.Hour), 0)
}

func TestCountryCodeToFlag(t *testing.T) {
assert.Equal(t, "🇺🇦", CountryCodeToFlag("UA"))
assert.Equal(t, "🇧🇪", CountryCodeToFlag("BE"))
}

func TestLocalDate(t *testing.T) {
d := time.Date(2020, 1, 1, 15, 4, 0, 0, time.UTC)
localDate := d.Local()

assert.Equal(t, localDate.Format("2006-01-02 15:04"), LocalDate(d))
}

func TestToKilometer(t *testing.T) {
assert.Equal(t, "0.00 km", ToKilometer(1.23))
assert.Equal(t, "1.23 km", ToKilometer(1234))
assert.Equal(t, "1234.57 km", ToKilometer(1234567))
}

func TestHumanDistance(t *testing.T) {
assert.Equal(t, "1.23 m", HumanDistance(1.23))
assert.Equal(t, "1.23 km", ToKilometer(1234))
assert.Equal(t, "1.23 Mm", HumanDistance(1234567))
}

func TestHumanSpeed(t *testing.T) {
assert.Equal(t, "4.43 km/h", HumanSpeed(1.23))
assert.Equal(t, "10.01 km/h", HumanSpeed(2.78))
assert.Equal(t, "17.96 km/h", HumanSpeed(4.99))
}

func TestHumanTempo(t *testing.T) {
assert.Equal(t, "13.55 min/km", HumanTempo(1.23))
assert.Equal(t, "6.00 min/km", HumanTempo(2.78))
assert.Equal(t, "3.34 min/km", HumanTempo(4.99))
}

func TestBoolToHTML(t *testing.T) {
assert.Equal(t, template.HTML("<i class=\"text-green-500 fas fa-check\"></i>"), BoolToHTML(true))
assert.Equal(t, template.HTML("<i class=\"text-rose-500 fas fa-times\"></i>"), BoolToHTML(false))
}

func TestBoolToCheckbox(t *testing.T) {
assert.Equal(t, template.HTML("checked"), BoolToCheckbox(true))
assert.Equal(t, template.HTML(""), BoolToCheckbox(false))
}

func TestBuildDecoratedAttribute(t *testing.T) {
r := BuildDecoratedAttribute("the-icon", "the-name", "the-value")

assert.NotNil(t, r)
assert.Equal(t, "the-icon", r.Icon)
assert.Equal(t, "the-name", r.Name)
assert.Equal(t, "the-value", r.Value)
}

0 comments on commit 4466f6a

Please sign in to comment.