- Fix WASM size gate: 3 MB → 3.5 MB to match size_test.go - Fix dependencies: remove stale replace directive, add go-io and go-log - Add error handling and I/O conventions to coding standards - Add tests: reader error (cmd/codegen), duplicate tag dedup, bundle error propagation (codegen) — coverage: codegen 89→96%, cmd/codegen 71→78% Co-Authored-By: Virgil <virgil@lethean.io>
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package codegen
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGenerateClass_Good(t *testing.T) {
|
|
js, err := GenerateClass("photo-grid", "C")
|
|
require.NoError(t, err)
|
|
assert.Contains(t, js, "class PhotoGrid extends HTMLElement")
|
|
assert.Contains(t, js, "attachShadow")
|
|
assert.Contains(t, js, `mode: "closed"`)
|
|
assert.Contains(t, js, "photo-grid")
|
|
}
|
|
|
|
func TestGenerateClass_Bad_InvalidTag(t *testing.T) {
|
|
_, err := GenerateClass("invalid", "C")
|
|
assert.Error(t, err, "custom element names must contain a hyphen")
|
|
}
|
|
|
|
func TestGenerateRegistration_Good(t *testing.T) {
|
|
js := GenerateRegistration("photo-grid", "PhotoGrid")
|
|
assert.Contains(t, js, "customElements.define")
|
|
assert.Contains(t, js, `"photo-grid"`)
|
|
assert.Contains(t, js, "PhotoGrid")
|
|
}
|
|
|
|
func TestTagToClassName_Good(t *testing.T) {
|
|
tests := []struct{ tag, want string }{
|
|
{"photo-grid", "PhotoGrid"},
|
|
{"nav-breadcrumb", "NavBreadcrumb"},
|
|
{"my-super-widget", "MySuperWidget"},
|
|
}
|
|
for _, tt := range tests {
|
|
got := TagToClassName(tt.tag)
|
|
assert.Equal(t, tt.want, got, "TagToClassName(%q)", tt.tag)
|
|
}
|
|
}
|
|
|
|
func TestGenerateBundle_Good(t *testing.T) {
|
|
slots := map[string]string{
|
|
"H": "nav-bar",
|
|
"C": "main-content",
|
|
}
|
|
js, err := GenerateBundle(slots)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, js, "NavBar")
|
|
assert.Contains(t, js, "MainContent")
|
|
assert.Equal(t, 2, strings.Count(js, "extends HTMLElement"))
|
|
}
|
|
|
|
func TestGenerateBundle_Good_DeduplicatesTags(t *testing.T) {
|
|
slots := map[string]string{
|
|
"H": "nav-bar",
|
|
"F": "nav-bar",
|
|
}
|
|
js, err := GenerateBundle(slots)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 1, strings.Count(js, "extends HTMLElement"),
|
|
"duplicate tag should produce only one class definition")
|
|
}
|
|
|
|
func TestGenerateBundle_Bad_InvalidTag(t *testing.T) {
|
|
slots := map[string]string{
|
|
"H": "notag",
|
|
}
|
|
_, err := GenerateBundle(slots)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "hyphen")
|
|
}
|