Skip to content

Commit

Permalink
refactor: minor update to test
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Nov 12, 2024
1 parent 63c73f8 commit ddd603c
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ import (
)

func TestCSSID(t *testing.T) {
// NOTE: See issue #978. Minimum recommended hs (Hash Suffix) length is 6. Else, naming conflicts become too common.

// Check for minimum hs length
name := "classA"
css := "background-color:white;"
id := templ.CSSID(name, css)
minLength := len(name) + 1 + 6

if len(id) < minLength {
t.Errorf("Expected total length of id to be at least %d, got %d", minLength, len(id))
}

// Check for specific failure case when len(hs) == 4
css1 := "grid-column:1;grid-row:1;" // After hash: f781266f
css2 := "grid-column:13;grid-row:6;" // After hash: f781f18b
id1 := templ.CSSID(name, css1)
id2 := templ.CSSID(name, css2)

if id1 == id2 {
t.Errorf("Different class instances are expected to have distinct names. Got same name for both (%s).", id1)
}
t.Run("minimum hash suffix length is 8", func(t *testing.T) {
// See issue #978.
name := "classA"
css := "background-color:white;"
actual := len(templ.CSSID(name, css))
expected := len(name) + 1 + 8
if expected != actual {
t.Errorf("expected length %d, got %d", expected, actual)
}
})
t.Run("known hash collisions are avoided", func(t *testing.T) {
name := "classA"
// Note that the first 4 characters of the hash are the same.
css1 := "grid-column:1;grid-row:1;" // After hash: f781266f
css2 := "grid-column:13;grid-row:6;" // After hash: f781f18b
id1 := templ.CSSID(name, css1)
id2 := templ.CSSID(name, css2)
if id1 == id2 {
t.Errorf("hash collision: %s == %s", id1, id2)
}
})
}

func TestCSSHandler(t *testing.T) {
Expand Down

0 comments on commit ddd603c

Please sign in to comment.