2026-03-26 11:15:24 +00:00
|
|
|
//go:build !js
|
|
|
|
|
|
2026-02-20 08:29:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-03-26 15:24:16 +00:00
|
|
|
core "dappco.re/go/core"
|
2026-02-20 08:29:54 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-26 11:15:24 +00:00
|
|
|
func TestRun_WritesBundle(t *testing.T) {
|
2026-03-26 15:24:16 +00:00
|
|
|
input := core.NewReader(`{"H":"nav-bar","C":"main-content"}`)
|
|
|
|
|
output := core.NewBuilder()
|
2026-02-20 08:29:54 +00:00
|
|
|
|
2026-03-26 15:24:16 +00:00
|
|
|
err := run(input, output)
|
2026-02-20 08:29:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
js := output.String()
|
|
|
|
|
assert.Contains(t, js, "NavBar")
|
|
|
|
|
assert.Contains(t, js, "MainContent")
|
|
|
|
|
assert.Contains(t, js, "customElements.define")
|
2026-03-26 15:24:16 +00:00
|
|
|
assert.Equal(t, 2, countSubstr(js, "extends HTMLElement"))
|
2026-02-20 08:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:15:24 +00:00
|
|
|
func TestRun_InvalidJSON(t *testing.T) {
|
2026-03-26 15:24:16 +00:00
|
|
|
input := core.NewReader(`not json`)
|
|
|
|
|
output := core.NewBuilder()
|
2026-02-20 08:29:54 +00:00
|
|
|
|
2026-03-26 15:24:16 +00:00
|
|
|
err := run(input, output)
|
2026-02-20 08:29:54 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
assert.Contains(t, err.Error(), "invalid JSON")
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:15:24 +00:00
|
|
|
func TestRun_InvalidTag(t *testing.T) {
|
2026-03-26 15:24:16 +00:00
|
|
|
input := core.NewReader(`{"H":"notag"}`)
|
|
|
|
|
output := core.NewBuilder()
|
2026-02-20 08:29:54 +00:00
|
|
|
|
2026-03-26 15:24:16 +00:00
|
|
|
err := run(input, output)
|
2026-02-20 08:29:54 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
assert.Contains(t, err.Error(), "hyphen")
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:15:24 +00:00
|
|
|
func TestRun_EmptySlots(t *testing.T) {
|
2026-03-26 15:24:16 +00:00
|
|
|
input := core.NewReader(`{}`)
|
|
|
|
|
output := core.NewBuilder()
|
2026-02-20 08:29:54 +00:00
|
|
|
|
2026-03-26 15:24:16 +00:00
|
|
|
err := run(input, output)
|
2026-02-20 08:29:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Empty(t, output.String())
|
|
|
|
|
}
|
2026-03-26 15:24:16 +00:00
|
|
|
|
|
|
|
|
func countSubstr(s, substr string) int {
|
|
|
|
|
if substr == "" {
|
|
|
|
|
return len(s) + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
|
for i := 0; i <= len(s)-len(substr); {
|
|
|
|
|
j := indexSubstr(s[i:], substr)
|
|
|
|
|
if j < 0 {
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
count++
|
|
|
|
|
i += j + len(substr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func indexSubstr(s, substr string) int {
|
|
|
|
|
if substr == "" {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
if len(substr) > len(s) {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i <= len(s)-len(substr); i++ {
|
|
|
|
|
if s[i:i+len(substr)] == substr {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
}
|