| 1 |
// Copyright 2020 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package encoding |
| 15 |
|
| 16 |
import ( |
| 17 |
"html/template" |
| 18 |
"math" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
) |
| 23 |
|
| 24 |
type tstNoStringer struct{} |
| 25 |
|
| 26 |
func TestBase64Decode(t *testing.T) { |
| 27 |
t.Parallel() |
| 28 |
c := qt.New(t) |
| 29 |
|
| 30 |
ns := New() |
| 31 |
|
| 32 |
for _, test := range []struct { |
| 33 |
v any |
| 34 |
expect any |
| 35 |
}{ |
| 36 |
{"YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"}, |
| 37 |
// errors |
| 38 |
{t, false}, |
| 39 |
} { |
| 40 |
|
| 41 |
result, err := ns.Base64Decode(test.v) |
| 42 |
|
| 43 |
if b, ok := test.expect.(bool); ok && !b { |
| 44 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 45 |
continue |
| 46 |
} |
| 47 |
|
| 48 |
c.Assert(err, qt.IsNil) |
| 49 |
c.Assert(result, qt.Equals, test.expect) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestBase64Encode(t *testing.T) { |
| 54 |
t.Parallel() |
| 55 |
c := qt.New(t) |
| 56 |
|
| 57 |
ns := New() |
| 58 |
|
| 59 |
for _, test := range []struct { |
| 60 |
v any |
| 61 |
expect any |
| 62 |
}{ |
| 63 |
{"YWJjMTIzIT8kKiYoKSctPUB+", "WVdKak1USXpJVDhrS2lZb0tTY3RQVUIr"}, |
| 64 |
// errors |
| 65 |
{t, false}, |
| 66 |
} { |
| 67 |
|
| 68 |
result, err := ns.Base64Encode(test.v) |
| 69 |
|
| 70 |
if b, ok := test.expect.(bool); ok && !b { |
| 71 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 72 |
continue |
| 73 |
} |
| 74 |
|
| 75 |
c.Assert(err, qt.IsNil) |
| 76 |
c.Assert(result, qt.Equals, test.expect) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestHexDecode(t *testing.T) { |
| 81 |
t.Parallel() |
| 82 |
c := qt.New(t) |
| 83 |
|
| 84 |
ns := New() |
| 85 |
|
| 86 |
for _, test := range []struct { |
| 87 |
v any |
| 88 |
expect any |
| 89 |
}{ |
| 90 |
{"616263313233213f242a2628292d3d407e", "abc123!?$*&()-=@~"}, |
| 91 |
// errors |
| 92 |
{t, false}, |
| 93 |
} { |
| 94 |
|
| 95 |
result, err := ns.HexDecode(test.v) |
| 96 |
|
| 97 |
if b, ok := test.expect.(bool); ok && !b { |
| 98 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 99 |
continue |
| 100 |
} |
| 101 |
|
| 102 |
c.Assert(err, qt.IsNil) |
| 103 |
c.Assert(result, qt.Equals, test.expect) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestHexEncode(t *testing.T) { |
| 108 |
t.Parallel() |
| 109 |
c := qt.New(t) |
| 110 |
|
| 111 |
ns := New() |
| 112 |
|
| 113 |
for _, test := range []struct { |
| 114 |
v any |
| 115 |
expect any |
| 116 |
}{ |
| 117 |
{"abc123!?$*&()-=@~", "616263313233213f242a2628292d3d407e"}, |
| 118 |
// errors |
| 119 |
{t, false}, |
| 120 |
} { |
| 121 |
|
| 122 |
result, err := ns.HexEncode(test.v) |
| 123 |
|
| 124 |
if b, ok := test.expect.(bool); ok && !b { |
| 125 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 126 |
continue |
| 127 |
} |
| 128 |
|
| 129 |
c.Assert(err, qt.IsNil) |
| 130 |
c.Assert(result, qt.Equals, test.expect) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestJsonify(t *testing.T) { |
| 135 |
t.Parallel() |
| 136 |
c := qt.New(t) |
| 137 |
ns := New() |
| 138 |
|
| 139 |
for i, test := range []struct { |
| 140 |
opts any |
| 141 |
v any |
| 142 |
expect any |
| 143 |
}{ |
| 144 |
{nil, []string{"a", "b"}, template.HTML(`["a","b"]`)}, |
| 145 |
{map[string]string{"indent": "<i>"}, []string{"a", "b"}, template.HTML("[\n<i>\"a\",\n<i>\"b\"\n]")}, |
| 146 |
{map[string]string{"prefix": "<p>"}, []string{"a", "b"}, template.HTML("[\n<p>\"a\",\n<p>\"b\"\n<p>]")}, |
| 147 |
{map[string]string{"prefix": "<p>", "indent": "<i>"}, []string{"a", "b"}, template.HTML("[\n<p><i>\"a\",\n<p><i>\"b\"\n<p>]")}, |
| 148 |
{map[string]string{"indent": "<i>"}, []string{"a", "b"}, template.HTML("[\n<i>\"a\",\n<i>\"b\"\n]")}, |
| 149 |
{map[string]any{"noHTMLEscape": false}, []string{"<a>", "<b>"}, template.HTML("[\"\\u003ca\\u003e\",\"\\u003cb\\u003e\"]")}, |
| 150 |
{map[string]any{"noHTMLEscape": true}, []string{"<a>", "<b>"}, template.HTML("[\"<a>\",\"<b>\"]")}, |
| 151 |
{nil, tstNoStringer{}, template.HTML("{}")}, |
| 152 |
{nil, nil, template.HTML("null")}, |
| 153 |
// errors |
| 154 |
{nil, math.NaN(), false}, |
| 155 |
{tstNoStringer{}, []string{"a", "b"}, false}, |
| 156 |
} { |
| 157 |
args := []any{} |
| 158 |
|
| 159 |
if test.opts != nil { |
| 160 |
args = append(args, test.opts) |
| 161 |
} |
| 162 |
|
| 163 |
args = append(args, test.v) |
| 164 |
|
| 165 |
result, err := ns.Jsonify(args...) |
| 166 |
|
| 167 |
if b, ok := test.expect.(bool); ok && !b { |
| 168 |
c.Assert(err, qt.Not(qt.IsNil), qt.Commentf("#%d", i)) |
| 169 |
continue |
| 170 |
} |
| 171 |
|
| 172 |
c.Assert(err, qt.IsNil) |
| 173 |
c.Assert(result, qt.Equals, test.expect, qt.Commentf("#%d", i)) |
| 174 |
} |
| 175 |
} |