feat(html): expose layout variant positions
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
cb75de9bf3
commit
3fcd8daf59
2 changed files with 35 additions and 2 deletions
13
layout.go
13
layout.go
|
|
@ -249,3 +249,16 @@ func (e *layoutVariantError) InvalidSlots() []byte {
|
|||
}
|
||||
return append([]byte(nil), e.invalidSlots...)
|
||||
}
|
||||
|
||||
// InvalidPositions returns a copy of the 1-based positions of the invalid slot
|
||||
// characters in the original variant string.
|
||||
func (e *layoutVariantError) InvalidPositions() []int {
|
||||
if e == nil || len(e.invalidPositions) == 0 {
|
||||
return nil
|
||||
}
|
||||
positions := make([]int, len(e.invalidPositions))
|
||||
for i, pos := range e.invalidPositions {
|
||||
positions[i] = pos + 1
|
||||
}
|
||||
return positions
|
||||
}
|
||||
|
|
|
|||
|
|
@ -183,6 +183,13 @@ func TestLayout_VariantError(t *testing.T) {
|
|||
wantRender: `<header role="banner" data-block="H-0">header</header>` +
|
||||
`<main role="main" data-block="C-0">main</main>`,
|
||||
},
|
||||
{
|
||||
name: "multiple invalid slots",
|
||||
variant: "H1X?",
|
||||
wantErr: true,
|
||||
wantErrString: "html: invalid layout variant H1X? (invalid slots: '1' at position 2, 'X' at position 3, '?' at position 4)",
|
||||
wantRender: `<header role="banner" data-block="H-0">header</header>`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
@ -201,8 +208,21 @@ func TestLayout_VariantError(t *testing.T) {
|
|||
t.Fatalf("VariantError().Error() = %q, want %q", got, tt.wantErrString)
|
||||
}
|
||||
if err, ok := layout.VariantError().(*layoutVariantError); ok {
|
||||
if got := string(err.InvalidSlots()); got != "X" {
|
||||
t.Fatalf("InvalidSlots() = %q, want %q", got, "X")
|
||||
switch tt.variant {
|
||||
case "HXC":
|
||||
if got := string(err.InvalidSlots()); got != "X" {
|
||||
t.Fatalf("InvalidSlots() = %q, want %q", got, "X")
|
||||
}
|
||||
if got := err.InvalidPositions(); len(got) != 1 || got[0] != 2 {
|
||||
t.Fatalf("InvalidPositions() = %v, want %v", got, []int{2})
|
||||
}
|
||||
case "H1X?":
|
||||
if got := string(err.InvalidSlots()); got != "1X?" {
|
||||
t.Fatalf("InvalidSlots() = %q, want %q", got, "1X?")
|
||||
}
|
||||
if got := err.InvalidPositions(); len(got) != 3 || got[0] != 2 || got[1] != 3 || got[2] != 4 {
|
||||
t.Fatalf("InvalidPositions() = %v, want %v", got, []int{2, 3, 4})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("VariantError() has unexpected concrete type %T", layout.VariantError())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue