2017-03-13 22:55:02 +00:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package encoding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
qt "github.com/frankban/quicktest"
|
2017-03-13 22:55:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type tstNoStringer struct{}
|
|
|
|
|
|
|
|
func TestBase64Decode(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-08-10 19:05:17 +00:00
|
|
|
c := qt.New(t)
|
2017-03-13 22:55:02 +00:00
|
|
|
|
|
|
|
ns := New()
|
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
for _, test := range []struct {
|
2017-03-13 22:55:02 +00:00
|
|
|
v interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{"YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"},
|
|
|
|
// errors
|
|
|
|
{t, false},
|
|
|
|
} {
|
|
|
|
|
|
|
|
result, err := ns.Base64Decode(test.v)
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2017-03-13 22:55:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(result, qt.Equals, test.expect)
|
2017-03-13 22:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBase64Encode(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-08-10 19:05:17 +00:00
|
|
|
c := qt.New(t)
|
2017-03-13 22:55:02 +00:00
|
|
|
|
|
|
|
ns := New()
|
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
for _, test := range []struct {
|
2017-03-13 22:55:02 +00:00
|
|
|
v interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{"YWJjMTIzIT8kKiYoKSctPUB+", "WVdKak1USXpJVDhrS2lZb0tTY3RQVUIr"},
|
|
|
|
// errors
|
|
|
|
{t, false},
|
|
|
|
} {
|
|
|
|
|
|
|
|
result, err := ns.Base64Encode(test.v)
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2017-03-13 22:55:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(result, qt.Equals, test.expect)
|
2017-03-13 22:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonify(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-08-10 19:05:17 +00:00
|
|
|
c := qt.New(t)
|
2017-03-13 22:55:02 +00:00
|
|
|
ns := New()
|
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
for _, test := range []struct {
|
2017-03-13 22:55:02 +00:00
|
|
|
v interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
2018-11-02 18:03:48 +00:00
|
|
|
{[]string{"a", "b"}, template.HTML(`["a","b"]`)},
|
2017-03-13 22:55:02 +00:00
|
|
|
{tstNoStringer{}, template.HTML("{}")},
|
|
|
|
{nil, template.HTML("null")},
|
|
|
|
// errors
|
|
|
|
{math.NaN(), false},
|
|
|
|
} {
|
|
|
|
|
|
|
|
result, err := ns.Jsonify(test.v)
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2017-03-13 22:55:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(result, qt.Equals, test.expect)
|
2017-03-13 22:55:02 +00:00
|
|
|
}
|
|
|
|
}
|