-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
197 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package jaws | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
) | ||
|
||
type StringGetter interface { | ||
JawsGetString(e *Element) string | ||
} | ||
|
||
type stringGetter struct{ v string } | ||
|
||
func (g stringGetter) JawsGetString(e *Element) string { | ||
return g.v | ||
} | ||
|
||
func (g stringGetter) JawsSetString(*Element, string) error { | ||
return ErrValueNotSettable | ||
} | ||
|
||
func (g stringGetter) JawsGetTag(rq *Request) any { | ||
return nil | ||
} | ||
|
||
func makeStringGetter(v any) StringGetter { | ||
switch v := v.(type) { | ||
case StringGetter: | ||
return v | ||
case string: | ||
return stringGetter{v} | ||
case template.HTML: | ||
return stringGetter{string(v)} | ||
case template.HTMLAttr: | ||
return stringGetter{string(v)} | ||
} | ||
panic(fmt.Errorf("expected jaws.StringGetter or string, not %T", v)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package jaws | ||
|
||
import ( | ||
"html/template" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var _ StringGetter = (*testSetter[string])(nil) | ||
|
||
func Test_makeStringGetter_panic(t *testing.T) { | ||
defer func() { | ||
if x := recover(); x != nil { | ||
if err, ok := x.(error); ok { | ||
if strings.Contains(err.Error(), "uint32") { | ||
return | ||
} | ||
} | ||
} | ||
t.Fail() | ||
}() | ||
makeStringGetter(uint32(42)) | ||
} | ||
|
||
func Test_makeStringGetter(t *testing.T) { | ||
val := "<span>" | ||
ts := newTestSetter(val) | ||
|
||
tests := []struct { | ||
name string | ||
v any | ||
want StringGetter | ||
out string | ||
err error | ||
tag any | ||
}{ | ||
{ | ||
name: "StringGetter", | ||
v: ts, | ||
want: ts, | ||
out: val, | ||
tag: ts, | ||
}, | ||
{ | ||
name: "string", | ||
v: val, | ||
want: stringGetter{val}, | ||
out: val, | ||
err: ErrValueNotSettable, | ||
tag: nil, | ||
}, | ||
{ | ||
name: "template.HTML", | ||
v: template.HTML(val), | ||
want: stringGetter{val}, | ||
out: val, | ||
err: ErrValueNotSettable, | ||
tag: nil, | ||
}, | ||
{ | ||
name: "template.HTMLAttr", | ||
v: template.HTMLAttr(val), | ||
want: stringGetter{val}, | ||
out: val, | ||
err: ErrValueNotSettable, | ||
tag: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := makeStringGetter(tt.v) | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("makeStringGetter() = %v, want %v", got, tt.want) | ||
} | ||
if out := got.JawsGetString(nil); out != tt.out { | ||
t.Errorf("makeStringGetter().JawsGetString() = %v, want %v", out, tt.out) | ||
} | ||
gotTag := any(got) | ||
if tg, ok := got.(TagGetter); ok { | ||
gotTag = tg.JawsGetTag(nil) | ||
} | ||
if gotTag != tt.tag { | ||
t.Errorf("makeStringGetter().tag = %v, want %v", gotTag, tt.tag) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters