]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/strings: Improve type checking
authorJoe Mooring <joe.mooring@veriphor.com>
Thu, 4 Apr 2024 15:22:33 +0000 (08:22 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 4 Apr 2024 16:34:55 +0000 (18:34 +0200)
tpl/strings/strings.go
tpl/strings/strings_test.go

index e6f7aed80d05b2e888de3821386fae22821d85a5..02f9a2b1e4ae233c6346b031e1f34d73062562a1 100644 (file)
@@ -162,24 +162,32 @@ func (ns *Namespace) ContainsAny(s, chars any) (bool, error) {
 // ContainsNonSpace reports whether s contains any non-space characters as defined
 // by Unicode's White Space property,
 // <docsmeta>{"newIn": "0.111.0" }</docsmeta>
-func (ns *Namespace) ContainsNonSpace(s any) bool {
-       ss := cast.ToString(s)
+func (ns *Namespace) ContainsNonSpace(s any) (bool, error) {
+       ss, err := cast.ToStringE(s)
+       if err != nil {
+               return false, err
+       }
 
        for _, r := range ss {
                if !unicode.IsSpace(r) {
-                       return true
+                       return true, nil
                }
        }
-       return false
+       return false, nil
 }
 
 // Diff returns an anchored diff of the two texts old and new in the “unified
 // diff” format. If old and new are identical, Diff returns an empty string.
-func (ns *Namespace) Diff(oldname string, old any, newname string, new any) string {
-       oldb := []byte(cast.ToString(old))
-       newb := []byte(cast.ToString(new))
-
-       return string(diff.Diff(oldname, oldb, newname, newb))
+func (ns *Namespace) Diff(oldname string, old any, newname string, new any) (string, error) {
+       olds, err := cast.ToStringE(old)
+       if err != nil {
+               return "", err
+       }
+       news, err := cast.ToStringE(new)
+       if err != nil {
+               return "", err
+       }
+       return string(diff.Diff(oldname, []byte(olds), newname, []byte(news))), nil
 }
 
 // HasPrefix tests whether the input s begins with prefix.
index 92767925adecae82c645bb4a4da3366b15f5130c..4fcd3b59ab1bc70d780abc1f8c93382292c0a826 100644 (file)
@@ -153,17 +153,30 @@ func TestContainsNonSpace(t *testing.T) {
        for _, test := range []struct {
                s      any
                expect bool
+               isErr  bool
        }{
-               {"", false},
-               {" ", false},
-               {"        ", false},
-               {"\t", false},
-               {"\r", false},
-               {"a", true},
-               {"    a", true},
-               {"a\n", true},
+               {"", false, false},
+               {" ", false, false},
+               {"        ", false, false},
+               {"\t", false, false},
+               {"\r", false, false},
+               {"a", true, false},
+               {"    a", true, false},
+               {"a\n", true, false},
+               // error
+               {tstNoStringer{}, false, true},
        } {
-               c.Assert(ns.ContainsNonSpace(test.s), qt.Equals, test.expect)
+
+               result, err := ns.ContainsNonSpace(test.s)
+
+               if test.isErr {
+                       c.Assert(err, qt.IsNotNil)
+                       continue
+               }
+
+               c.Assert(err, qt.IsNil)
+               c.Assert(result, qt.Equals, test.expect)
+
        }
 }
 
@@ -812,21 +825,32 @@ func TestDiff(t *testing.T) {
        t.Parallel()
        c := qt.New(t)
 
-       for _, tt := range []struct {
+       for _, test := range []struct {
                oldname string
                old     any
                newname string
                new     any
-               expect  string
+               expect  any
        }{
                {"old", "foo\n", "new", "bar\n", "diff old new\n--- old\n+++ new\n@@ -1,1 +1,1 @@\n-foo\n+bar\n"},
                {"old", "foo\n", "new", "foo\n", ""},
                {"old", "foo\n", "new", "", "diff old new\n--- old\n+++ new\n@@ -1,1 +0,0 @@\n-foo\n"},
                {"old", "foo\n", "new", nil, "diff old new\n--- old\n+++ new\n@@ -1,1 +0,0 @@\n-foo\n"},
                {"old", "", "new", "", ""},
+               // errors
+               {"old", tstNoStringer{}, "new", "foo", false},
+               {"old", "foo", "new", tstNoStringer{}, false},
        } {
 
-               result := ns.Diff(tt.oldname, tt.old, tt.newname, tt.new)
-               c.Assert(result, qt.Equals, tt.expect)
+               result, err := ns.Diff(test.oldname, test.old, test.newname, test.new)
+
+               if b, ok := test.expect.(bool); ok && !b {
+                       c.Assert(err, qt.Not(qt.IsNil))
+                       continue
+               }
+
+               c.Assert(err, qt.IsNil)
+               c.Assert(result, qt.Equals, test.expect)
+
        }
 }