2015-12-10 17:19:38 -05:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
package tpl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-09-12 15:45:12 -04:00
|
|
|
"encoding/base64"
|
2015-04-05 15:03:16 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-11-20 05:53:25 -05:00
|
|
|
"github.com/spf13/cast"
|
2016-02-05 01:05:04 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-04-05 15:03:16 -04:00
|
|
|
"html/template"
|
2016-02-06 06:29:13 -05:00
|
|
|
"math/rand"
|
2015-04-05 15:03:16 -04:00
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2016-02-07 09:15:27 -05:00
|
|
|
"strings"
|
2015-04-05 15:03:16 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tstNoStringer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type tstCompareType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
tstEq tstCompareType = iota
|
|
|
|
tstNe
|
|
|
|
tstGt
|
|
|
|
tstGe
|
|
|
|
tstLt
|
|
|
|
tstLe
|
|
|
|
)
|
|
|
|
|
|
|
|
func tstIsEq(tp tstCompareType) bool {
|
|
|
|
return tp == tstEq || tp == tstGe || tp == tstLe
|
|
|
|
}
|
|
|
|
|
|
|
|
func tstIsGt(tp tstCompareType) bool {
|
|
|
|
return tp == tstGt || tp == tstGe
|
|
|
|
}
|
|
|
|
|
|
|
|
func tstIsLt(tp tstCompareType) bool {
|
|
|
|
return tp == tstLt || tp == tstLe
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompare(t *testing.T) {
|
|
|
|
for _, this := range []struct {
|
|
|
|
tstCompareType
|
|
|
|
funcUnderTest func(a, b interface{}) bool
|
|
|
|
}{
|
2016-02-06 09:53:39 -05:00
|
|
|
{tstGt, gt},
|
|
|
|
{tstLt, lt},
|
|
|
|
{tstGe, ge},
|
|
|
|
{tstLe, le},
|
|
|
|
{tstEq, eq},
|
|
|
|
{tstNe, ne},
|
2015-04-05 15:03:16 -04:00
|
|
|
} {
|
|
|
|
doTestCompare(t, this.tstCompareType, this.funcUnderTest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b interface{}) bool) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
left interface{}
|
|
|
|
right interface{}
|
|
|
|
expectIndicator int
|
|
|
|
}{
|
|
|
|
{5, 8, -1},
|
|
|
|
{8, 5, 1},
|
|
|
|
{5, 5, 0},
|
|
|
|
{int(5), int64(5), 0},
|
|
|
|
{int32(5), int(5), 0},
|
|
|
|
{int16(4), int(5), -1},
|
|
|
|
{uint(15), uint64(15), 0},
|
|
|
|
{-2, 1, -1},
|
|
|
|
{2, -5, 1},
|
|
|
|
{0.0, 1.23, -1},
|
|
|
|
{1.1, 1.1, 0},
|
|
|
|
{float32(1.0), float64(1.0), 0},
|
|
|
|
{1.23, 0.0, 1},
|
|
|
|
{"5", "5", 0},
|
|
|
|
{"8", "5", 1},
|
|
|
|
{"5", "0001", 1},
|
|
|
|
{[]int{100, 99}, []int{1, 2, 3, 4}, -1},
|
2015-11-20 05:53:25 -05:00
|
|
|
{cast.ToTime("2015-11-20"), cast.ToTime("2015-11-20"), 0},
|
|
|
|
{cast.ToTime("2015-11-19"), cast.ToTime("2015-11-20"), -1},
|
|
|
|
{cast.ToTime("2015-11-20"), cast.ToTime("2015-11-19"), 1},
|
2015-04-05 15:03:16 -04:00
|
|
|
} {
|
|
|
|
result := funcUnderTest(this.left, this.right)
|
|
|
|
success := false
|
|
|
|
|
|
|
|
if this.expectIndicator == 0 {
|
|
|
|
if tstIsEq(tp) {
|
|
|
|
success = result
|
|
|
|
} else {
|
|
|
|
success = !result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if this.expectIndicator < 0 {
|
|
|
|
success = result && (tstIsLt(tp) || tp == tstNe)
|
|
|
|
success = success || (!result && !tstIsLt(tp))
|
|
|
|
}
|
|
|
|
|
|
|
|
if this.expectIndicator > 0 {
|
|
|
|
success = result && (tstIsGt(tp) || tp == tstNe)
|
|
|
|
success = success || (!result && (!tstIsGt(tp) || tp != tstNe))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
t.Errorf("[%d][%s] %v compared to %v: %t", i, path.Base(runtime.FuncForPC(reflect.ValueOf(funcUnderTest).Pointer()).Name()), this.left, this.right, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArethmic(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
a interface{}
|
|
|
|
b interface{}
|
|
|
|
op rune
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{1, 2, '+', int64(3)},
|
|
|
|
{1, 2, '-', int64(-1)},
|
|
|
|
{2, 2, '*', int64(4)},
|
|
|
|
{4, 2, '/', int64(2)},
|
|
|
|
{uint8(1), uint8(3), '+', uint64(4)},
|
|
|
|
{uint8(3), uint8(2), '-', uint64(1)},
|
|
|
|
{uint8(2), uint8(2), '*', uint64(4)},
|
|
|
|
{uint16(4), uint8(2), '/', uint64(2)},
|
|
|
|
{4, 2, '¤', false},
|
|
|
|
{4, 0, '/', false},
|
2015-08-05 12:55:25 -04:00
|
|
|
{float64(2.3), float64(2.3), '+', float64(4.6)},
|
|
|
|
{float64(2.3), int(2), '*', float64(4.6)},
|
2016-02-07 08:20:25 -05:00
|
|
|
{int(1), float64(2), '+', float64(3)},
|
|
|
|
{int(1), uint(2), '+', uint64(3)},
|
|
|
|
{1, "do", '+', false},
|
|
|
|
{float64(1), uint(2), '+', float64(3)},
|
|
|
|
{float64(1), "do", '+', false},
|
|
|
|
{uint(1), int(2), '+', uint64(3)},
|
|
|
|
{uint(1), float64(2), '+', float64(3)},
|
|
|
|
{uint(1), "do", '+', false},
|
|
|
|
{"do ", "be", '+', "do be"},
|
|
|
|
{"do ", "be", '*', false},
|
|
|
|
{t, t, '+', false},
|
2015-04-05 15:03:16 -04:00
|
|
|
} {
|
|
|
|
// TODO(bep): Take precision into account.
|
|
|
|
result, err := doArithmetic(this.a, this.b, this.op)
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] doArethmic didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
|
|
|
t.Errorf("[%d] doArethmic got %v (%T) but expected %v (%T)", i, result, result, this.expect, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMod(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
a interface{}
|
|
|
|
b interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{3, 2, int64(1)},
|
|
|
|
{3, 1, int64(0)},
|
|
|
|
{3, 0, false},
|
|
|
|
{0, 3, int64(0)},
|
|
|
|
{3.1, 2, false},
|
|
|
|
{3, 2.1, false},
|
|
|
|
{3.1, 2.1, false},
|
|
|
|
{int8(3), int8(2), int64(1)},
|
|
|
|
{int16(3), int16(2), int64(1)},
|
|
|
|
{int32(3), int32(2), int64(1)},
|
|
|
|
{int64(3), int64(2), int64(1)},
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err := mod(this.a, this.b)
|
2015-04-05 15:03:16 -04:00
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] modulo didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
|
|
|
t.Errorf("[%d] modulo got %v but expected %v", i, result, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModBool(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
a interface{}
|
|
|
|
b interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{3, 3, true},
|
|
|
|
{3, 2, false},
|
|
|
|
{3, 1, true},
|
|
|
|
{3, 0, nil},
|
|
|
|
{0, 3, true},
|
|
|
|
{3.1, 2, nil},
|
|
|
|
{3, 2.1, nil},
|
|
|
|
{3.1, 2.1, nil},
|
|
|
|
{int8(3), int8(3), true},
|
|
|
|
{int8(3), int8(2), false},
|
|
|
|
{int16(3), int16(3), true},
|
|
|
|
{int16(3), int16(2), false},
|
|
|
|
{int32(3), int32(3), true},
|
|
|
|
{int32(3), int32(2), false},
|
|
|
|
{int64(3), int64(3), true},
|
|
|
|
{int64(3), int64(2), false},
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err := modBool(this.a, this.b)
|
2015-04-05 15:03:16 -04:00
|
|
|
if this.expect == nil {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] modulo didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
|
|
|
t.Errorf("[%d] modulo got %v but expected %v", i, result, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFirst(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
count interface{}
|
|
|
|
sequence interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{int(2), []string{"a", "b", "c"}, []string{"a", "b"}},
|
|
|
|
{int32(3), []string{"a", "b"}, []string{"a", "b"}},
|
|
|
|
{int64(2), []int{100, 200, 300}, []int{100, 200}},
|
|
|
|
{100, []int{100, 200}, []int{100, 200}},
|
|
|
|
{"1", []int{100, 200, 300}, []int{100}},
|
|
|
|
{int64(-1), []int{100, 200, 300}, false},
|
|
|
|
{"noint", []int{100, 200, 300}, false},
|
|
|
|
{1, nil, false},
|
|
|
|
{nil, []int{100}, false},
|
|
|
|
{1, t, false},
|
2016-02-07 15:16:43 -05:00
|
|
|
{1, (*string)(nil), false},
|
2015-04-05 15:03:16 -04:00
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
results, err := first(this.count, this.sequence)
|
2015-04-05 15:03:16 -04:00
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] First didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(results, this.expect) {
|
|
|
|
t.Errorf("[%d] First %d items, got %v but expected %v", i, this.count, results, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 18:11:47 -04:00
|
|
|
func TestLast(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
count interface{}
|
|
|
|
sequence interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{int(2), []string{"a", "b", "c"}, []string{"b", "c"}},
|
|
|
|
{int32(3), []string{"a", "b"}, []string{"a", "b"}},
|
|
|
|
{int64(2), []int{100, 200, 300}, []int{200, 300}},
|
|
|
|
{100, []int{100, 200}, []int{100, 200}},
|
|
|
|
{"1", []int{100, 200, 300}, []int{300}},
|
|
|
|
{int64(-1), []int{100, 200, 300}, false},
|
|
|
|
{"noint", []int{100, 200, 300}, false},
|
|
|
|
{1, nil, false},
|
|
|
|
{nil, []int{100}, false},
|
|
|
|
{1, t, false},
|
2016-02-07 15:16:43 -05:00
|
|
|
{1, (*string)(nil), false},
|
2015-06-10 18:11:47 -04:00
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
results, err := last(this.count, this.sequence)
|
2015-06-10 18:11:47 -04:00
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] First didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(results, this.expect) {
|
|
|
|
t.Errorf("[%d] First %d items, got %v but expected %v", i, this.count, results, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:49:55 -04:00
|
|
|
func TestAfter(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
count interface{}
|
|
|
|
sequence interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{int(2), []string{"a", "b", "c", "d"}, []string{"c", "d"}},
|
|
|
|
{int32(3), []string{"a", "b"}, false},
|
|
|
|
{int64(2), []int{100, 200, 300}, []int{300}},
|
|
|
|
{100, []int{100, 200}, false},
|
|
|
|
{"1", []int{100, 200, 300}, []int{200, 300}},
|
|
|
|
{int64(-1), []int{100, 200, 300}, false},
|
|
|
|
{"noint", []int{100, 200, 300}, false},
|
|
|
|
{1, nil, false},
|
|
|
|
{nil, []int{100}, false},
|
|
|
|
{1, t, false},
|
2016-02-07 15:16:43 -05:00
|
|
|
{1, (*string)(nil), false},
|
2015-06-10 17:49:55 -04:00
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
results, err := after(this.count, this.sequence)
|
2015-06-10 17:49:55 -04:00
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] First didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(results, this.expect) {
|
|
|
|
t.Errorf("[%d] First %d items, got %v but expected %v", i, this.count, results, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 00:27:37 -05:00
|
|
|
func TestShuffleInputAndOutputFormat(t *testing.T) {
|
2015-12-06 05:28:03 -05:00
|
|
|
for i, this := range []struct {
|
|
|
|
sequence interface{}
|
2016-02-06 06:29:13 -05:00
|
|
|
success bool
|
2015-12-06 05:28:03 -05:00
|
|
|
}{
|
2015-12-07 00:27:37 -05:00
|
|
|
{[]string{"a", "b", "c", "d"}, true},
|
|
|
|
{[]int{100, 200, 300}, true},
|
|
|
|
{[]int{100, 200, 300}, true},
|
|
|
|
{[]int{100, 200}, true},
|
|
|
|
{[]string{"a", "b"}, true},
|
|
|
|
{[]int{100, 200, 300}, true},
|
|
|
|
{[]int{100, 200, 300}, true},
|
|
|
|
{[]int{100}, true},
|
|
|
|
{nil, false},
|
|
|
|
{t, false},
|
2016-02-07 15:16:43 -05:00
|
|
|
{(*string)(nil), false},
|
2015-12-06 05:28:03 -05:00
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
results, err := shuffle(this.sequence)
|
2015-12-07 00:27:37 -05:00
|
|
|
if !this.success {
|
2015-12-06 05:28:03 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] First didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resultsv := reflect.ValueOf(results)
|
2015-12-07 00:27:37 -05:00
|
|
|
sequencev := reflect.ValueOf(this.sequence)
|
|
|
|
|
2015-12-06 05:28:03 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-12-07 00:27:37 -05:00
|
|
|
if resultsv.Len() != sequencev.Len() {
|
|
|
|
t.Errorf("Expected %d items, got %d items", sequencev.Len(), resultsv.Len())
|
2015-12-06 05:28:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 00:27:37 -05:00
|
|
|
func TestShuffleRandomising(t *testing.T) {
|
|
|
|
// Note that this test can fail with false negative result if the shuffle
|
|
|
|
// of the sequence happens to be the same as the original sequence. However
|
|
|
|
// the propability of the event is 10^-158 which is negligible.
|
|
|
|
sequenceLength := 100
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
|
|
|
for _, this := range []struct {
|
|
|
|
sequence []int
|
|
|
|
}{
|
|
|
|
{rand.Perm(sequenceLength)},
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
results, _ := shuffle(this.sequence)
|
2015-12-07 00:27:37 -05:00
|
|
|
|
|
|
|
resultsv := reflect.ValueOf(results)
|
|
|
|
|
|
|
|
allSame := true
|
|
|
|
for index, value := range this.sequence {
|
|
|
|
allSame = allSame && (resultsv.Index(index).Interface() == value)
|
|
|
|
}
|
|
|
|
|
|
|
|
if allSame {
|
|
|
|
t.Error("Expected sequence to be shuffled but was in the same order")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 11:30:21 -04:00
|
|
|
func TestDictionary(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
2015-10-12 14:47:06 -04:00
|
|
|
v1 []interface{}
|
|
|
|
expecterr bool
|
|
|
|
expectedValue map[string]interface{}
|
2015-10-02 11:30:21 -04:00
|
|
|
}{
|
2015-10-12 14:47:06 -04:00
|
|
|
{[]interface{}{"a", "b"}, false, map[string]interface{}{"a": "b"}},
|
|
|
|
{[]interface{}{5, "b"}, true, nil},
|
|
|
|
{[]interface{}{"a", 12, "b", []int{4}}, false, map[string]interface{}{"a": 12, "b": []int{4}}},
|
|
|
|
{[]interface{}{"a", "b", "c"}, true, nil},
|
2015-10-02 11:30:21 -04:00
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
r, e := dictionary(this.v1...)
|
2015-10-02 11:30:21 -04:00
|
|
|
|
2015-10-12 14:47:06 -04:00
|
|
|
if (this.expecterr && e == nil) || (!this.expecterr && e != nil) {
|
2015-10-12 15:34:15 -04:00
|
|
|
t.Errorf("[%d] got an unexpected error: %s", i, e)
|
2015-10-02 11:30:21 -04:00
|
|
|
} else if !this.expecterr {
|
|
|
|
if !reflect.DeepEqual(r, this.expectedValue) {
|
|
|
|
t.Errorf("[%d] got %v but expected %v", i, r, this.expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
func TestIn(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
v1 interface{}
|
|
|
|
v2 interface{}
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{[]string{"a", "b", "c"}, "b", true},
|
2015-10-12 11:58:40 -04:00
|
|
|
{[]interface{}{"a", "b", "c"}, "b", true},
|
|
|
|
{[]interface{}{"a", "b", "c"}, "d", false},
|
2015-04-05 15:03:16 -04:00
|
|
|
{[]string{"a", "b", "c"}, "d", false},
|
|
|
|
{[]string{"a", "12", "c"}, 12, false},
|
|
|
|
{[]int{1, 2, 4}, 2, true},
|
2015-10-12 11:58:40 -04:00
|
|
|
{[]interface{}{1, 2, 4}, 2, true},
|
|
|
|
{[]interface{}{1, 2, 4}, nil, false},
|
|
|
|
{[]interface{}{nil}, nil, false},
|
2015-04-05 15:03:16 -04:00
|
|
|
{[]int{1, 2, 4}, 3, false},
|
|
|
|
{[]float64{1.23, 2.45, 4.67}, 1.23, true},
|
|
|
|
{[]float64{1.234567, 2.45, 4.67}, 1.234568, false},
|
|
|
|
{"this substring should be found", "substring", true},
|
|
|
|
{"this substring should not be found", "subseastring", false},
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
result := in(this.v1, this.v2)
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
if result != this.expect {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Errorf("[%d] got %v but expected %v", i, result, this.expect)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSlicestr(t *testing.T) {
|
2015-08-14 04:36:56 -04:00
|
|
|
var err error
|
2015-04-05 15:03:16 -04:00
|
|
|
for i, this := range []struct {
|
|
|
|
v1 interface{}
|
2015-08-14 04:36:56 -04:00
|
|
|
v2 interface{}
|
|
|
|
v3 interface{}
|
2015-04-05 15:03:16 -04:00
|
|
|
expect interface{}
|
|
|
|
}{
|
2015-08-14 04:36:56 -04:00
|
|
|
{"abc", 1, 2, "b"},
|
|
|
|
{"abc", 1, 3, "bc"},
|
|
|
|
{"abcdef", 1, int8(3), "bc"},
|
|
|
|
{"abcdef", 1, int16(3), "bc"},
|
|
|
|
{"abcdef", 1, int32(3), "bc"},
|
|
|
|
{"abcdef", 1, int64(3), "bc"},
|
|
|
|
{"abc", 0, 1, "a"},
|
|
|
|
{"abcdef", nil, nil, "abcdef"},
|
|
|
|
{"abcdef", 0, 6, "abcdef"},
|
|
|
|
{"abcdef", 0, 2, "ab"},
|
|
|
|
{"abcdef", 2, nil, "cdef"},
|
|
|
|
{"abcdef", int8(2), nil, "cdef"},
|
|
|
|
{"abcdef", int16(2), nil, "cdef"},
|
|
|
|
{"abcdef", int32(2), nil, "cdef"},
|
|
|
|
{"abcdef", int64(2), nil, "cdef"},
|
|
|
|
{123, 1, 3, "23"},
|
|
|
|
{"abcdef", 6, nil, false},
|
|
|
|
{"abcdef", 4, 7, false},
|
|
|
|
{"abcdef", -1, nil, false},
|
|
|
|
{"abcdef", -1, 7, false},
|
|
|
|
{"abcdef", 1, -1, false},
|
|
|
|
{tstNoStringer{}, 0, 1, false},
|
|
|
|
{"ĀĀĀ", 0, 1, "Ā"}, // issue #1333
|
2016-02-07 14:14:24 -05:00
|
|
|
{"a", t, nil, false},
|
|
|
|
{"a", 1, t, false},
|
2015-04-05 15:03:16 -04:00
|
|
|
} {
|
2015-08-14 04:36:56 -04:00
|
|
|
var result string
|
|
|
|
if this.v2 == nil {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = slicestr(this.v1)
|
2015-08-14 04:36:56 -04:00
|
|
|
} else if this.v3 == nil {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = slicestr(this.v1, this.v2)
|
2015-08-14 04:36:56 -04:00
|
|
|
} else {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = slicestr(this.v1, this.v2, this.v3)
|
2015-08-14 04:36:56 -04:00
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Slice didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Errorf("[%d] got %s but expected %s", i, result, this.expect)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 14:14:24 -05:00
|
|
|
|
|
|
|
// Too many arguments
|
|
|
|
_, err = slicestr("a", 1, 2, 3)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Should have errored")
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubstr(t *testing.T) {
|
2015-06-06 11:21:14 -04:00
|
|
|
var err error
|
|
|
|
var n int
|
2015-04-05 15:03:16 -04:00
|
|
|
for i, this := range []struct {
|
|
|
|
v1 interface{}
|
2015-06-06 11:21:14 -04:00
|
|
|
v2 interface{}
|
|
|
|
v3 interface{}
|
2015-04-05 15:03:16 -04:00
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{"abc", 1, 2, "bc"},
|
|
|
|
{"abc", 0, 1, "a"},
|
|
|
|
{"abcdef", -1, 2, "ef"},
|
|
|
|
{"abcdef", -3, 3, "bcd"},
|
|
|
|
{"abcdef", 0, -1, "abcde"},
|
|
|
|
{"abcdef", 2, -1, "cde"},
|
|
|
|
{"abcdef", 4, -4, false},
|
|
|
|
{"abcdef", 7, 1, false},
|
|
|
|
{"abcdef", 1, 100, "bcdef"},
|
|
|
|
{"abcdef", -100, 3, "abc"},
|
|
|
|
{"abcdef", -3, -1, "de"},
|
2015-06-06 11:21:14 -04:00
|
|
|
{"abcdef", 2, nil, "cdef"},
|
|
|
|
{"abcdef", int8(2), nil, "cdef"},
|
|
|
|
{"abcdef", int16(2), nil, "cdef"},
|
|
|
|
{"abcdef", int32(2), nil, "cdef"},
|
|
|
|
{"abcdef", int64(2), nil, "cdef"},
|
|
|
|
{"abcdef", 2, int8(3), "cde"},
|
|
|
|
{"abcdef", 2, int16(3), "cde"},
|
|
|
|
{"abcdef", 2, int32(3), "cde"},
|
|
|
|
{"abcdef", 2, int64(3), "cde"},
|
2015-04-05 15:03:16 -04:00
|
|
|
{123, 1, 3, "23"},
|
|
|
|
{1.2e3, 0, 4, "1200"},
|
|
|
|
{tstNoStringer{}, 0, 1, false},
|
2015-08-15 09:47:16 -04:00
|
|
|
{"abcdef", 2.0, nil, "cdef"},
|
|
|
|
{"abcdef", 2.0, 2, "cd"},
|
|
|
|
{"abcdef", 2, 2.0, "cd"},
|
2015-08-07 02:52:22 -04:00
|
|
|
{"ĀĀĀ", 1, 2, "ĀĀ"}, // # issue 1333
|
2016-02-07 09:22:57 -05:00
|
|
|
{"abcdef", "doo", nil, false},
|
|
|
|
{"abcdef", "doo", "doo", false},
|
|
|
|
{"abcdef", 1, "doo", false},
|
2015-04-05 15:03:16 -04:00
|
|
|
} {
|
2015-06-06 11:21:14 -04:00
|
|
|
var result string
|
|
|
|
n = i
|
|
|
|
|
|
|
|
if this.v3 == nil {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = substr(this.v1, this.v2)
|
2015-06-06 11:21:14 -04:00
|
|
|
} else {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = substr(this.v1, this.v2, this.v3)
|
2015-06-06 11:21:14 -04:00
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Substr didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Errorf("[%d] got %s but expected %s", i, result, this.expect)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-06 11:21:14 -04:00
|
|
|
|
|
|
|
n++
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = substr("abcdef")
|
2015-06-06 11:21:14 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Substr didn't return an expected error", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
n++
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = substr("abcdef", 1, 2, 3)
|
2015-06-06 11:21:14 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Substr didn't return an expected error", n)
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplit(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
v1 interface{}
|
|
|
|
v2 string
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{"a, b", ", ", []string{"a", "b"}},
|
|
|
|
{"a & b & c", " & ", []string{"a", "b", "c"}},
|
|
|
|
{"http://exmaple.com", "http://", []string{"", "exmaple.com"}},
|
|
|
|
{123, "2", []string{"1", "3"}},
|
|
|
|
{tstNoStringer{}, ",", false},
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err := split(this.v1, this.v2)
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Split didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Errorf("[%d] got %s but expected %s", i, result, this.expect)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntersect(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
sequence1 interface{}
|
|
|
|
sequence2 interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{[]string{"a", "b", "c"}, []string{"a", "b"}, []string{"a", "b"}},
|
|
|
|
{[]string{"a", "b"}, []string{"a", "b", "c"}, []string{"a", "b"}},
|
|
|
|
{[]string{"a", "b", "c"}, []string{"d", "e"}, []string{}},
|
|
|
|
{[]string{}, []string{}, []string{}},
|
|
|
|
{[]string{"a", "b"}, nil, make([]interface{}, 0)},
|
|
|
|
{nil, []string{"a", "b"}, make([]interface{}, 0)},
|
|
|
|
{nil, nil, make([]interface{}, 0)},
|
|
|
|
{[]string{"1", "2"}, []int{1, 2}, []string{}},
|
|
|
|
{[]int{1, 2}, []string{"1", "2"}, []int{}},
|
|
|
|
{[]int{1, 2, 4}, []int{2, 4}, []int{2, 4}},
|
|
|
|
{[]int{2, 4}, []int{1, 2, 4}, []int{2, 4}},
|
|
|
|
{[]int{1, 2, 4}, []int{3, 6}, []int{}},
|
|
|
|
{[]float64{2.2, 4.4}, []float64{1.1, 2.2, 4.4}, []float64{2.2, 4.4}},
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
results, err := intersect(this.sequence1, this.sequence2)
|
2015-04-05 15:03:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(results, this.expect) {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Errorf("[%d] got %v but expected %v", i, results, this.expect)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err1 := intersect("not an array or slice", []string{"a"})
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
if err1 == nil {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Error("Expected error for non array as first arg")
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err2 := intersect([]string{"a"}, "not an array or slice")
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
if err2 == nil {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Error("Expected error for non array as second arg")
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSet(t *testing.T) {
|
|
|
|
aSlice := []interface{}{1, 2, 3, 5}
|
|
|
|
aMap := map[string]interface{}{"a": 1, "b": 2}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
assert.True(t, isSet(aSlice, 2))
|
|
|
|
assert.True(t, isSet(aMap, "b"))
|
|
|
|
assert.False(t, isSet(aSlice, 22))
|
|
|
|
assert.False(t, isSet(aMap, "bc"))
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *TstX) TstRp() string {
|
|
|
|
return "r" + x.A
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x TstX) TstRv() string {
|
|
|
|
return "r" + x.B
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x TstX) unexportedMethod() string {
|
|
|
|
return x.unexported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x TstX) MethodWithArg(s string) string {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x TstX) MethodReturnNothing() {}
|
|
|
|
|
|
|
|
func (x TstX) MethodReturnErrorOnly() error {
|
|
|
|
return errors.New("something error occured")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x TstX) MethodReturnTwoValues() (string, string) {
|
|
|
|
return "foo", "bar"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x TstX) MethodReturnValueWithError() (string, error) {
|
|
|
|
return "", errors.New("something error occured")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x TstX) String() string {
|
|
|
|
return fmt.Sprintf("A: %s, B: %s", x.A, x.B)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TstX struct {
|
|
|
|
A, B string
|
|
|
|
unexported string
|
|
|
|
}
|
|
|
|
|
2015-05-26 06:33:32 -04:00
|
|
|
func TestTimeUnix(t *testing.T) {
|
|
|
|
var sec int64 = 1234567890
|
|
|
|
tv := reflect.ValueOf(time.Unix(sec, 0))
|
|
|
|
i := 1
|
|
|
|
|
|
|
|
res := timeUnix(tv)
|
|
|
|
if sec != res {
|
|
|
|
t.Errorf("[%d] timeUnix got %v but expected %v", i, res, sec)
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
func(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Errorf("[%d] timeUnix didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
iv := reflect.ValueOf(sec)
|
|
|
|
timeUnix(iv)
|
|
|
|
}(t)
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
func TestEvaluateSubElem(t *testing.T) {
|
|
|
|
tstx := TstX{A: "foo", B: "bar"}
|
|
|
|
var inner struct {
|
|
|
|
S fmt.Stringer
|
|
|
|
}
|
|
|
|
inner.S = tstx
|
|
|
|
interfaceValue := reflect.ValueOf(&inner).Elem().Field(0)
|
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
value reflect.Value
|
|
|
|
key string
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{reflect.ValueOf(tstx), "A", "foo"},
|
|
|
|
{reflect.ValueOf(&tstx), "TstRp", "rfoo"},
|
|
|
|
{reflect.ValueOf(tstx), "TstRv", "rbar"},
|
|
|
|
//{reflect.ValueOf(map[int]string{1: "foo", 2: "bar"}), 1, "foo"},
|
|
|
|
{reflect.ValueOf(map[string]string{"key1": "foo", "key2": "bar"}), "key1", "foo"},
|
|
|
|
{interfaceValue, "String", "A: foo, B: bar"},
|
|
|
|
{reflect.Value{}, "foo", false},
|
|
|
|
//{reflect.ValueOf(map[int]string{1: "foo", 2: "bar"}), 1.2, false},
|
|
|
|
{reflect.ValueOf(tstx), "unexported", false},
|
|
|
|
{reflect.ValueOf(tstx), "unexportedMethod", false},
|
|
|
|
{reflect.ValueOf(tstx), "MethodWithArg", false},
|
|
|
|
{reflect.ValueOf(tstx), "MethodReturnNothing", false},
|
|
|
|
{reflect.ValueOf(tstx), "MethodReturnErrorOnly", false},
|
|
|
|
{reflect.ValueOf(tstx), "MethodReturnTwoValues", false},
|
|
|
|
{reflect.ValueOf(tstx), "MethodReturnValueWithError", false},
|
|
|
|
{reflect.ValueOf((*TstX)(nil)), "A", false},
|
|
|
|
{reflect.ValueOf(tstx), "C", false},
|
|
|
|
{reflect.ValueOf(map[int]string{1: "foo", 2: "bar"}), "1", false},
|
|
|
|
{reflect.ValueOf([]string{"foo", "bar"}), "1", false},
|
|
|
|
} {
|
|
|
|
result, err := evaluateSubElem(this.value, this.key)
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] evaluateSubElem didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result.Kind() != reflect.String || result.String() != this.expect {
|
|
|
|
t.Errorf("[%d] evaluateSubElem with %v got %v but expected %v", i, this.key, result, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckCondition(t *testing.T) {
|
|
|
|
type expect struct {
|
|
|
|
result bool
|
|
|
|
isError bool
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
value reflect.Value
|
|
|
|
match reflect.Value
|
|
|
|
op string
|
|
|
|
expect
|
|
|
|
}{
|
|
|
|
{reflect.ValueOf(123), reflect.ValueOf(123), "", expect{true, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf("foo"), "", expect{true, false}},
|
2015-05-26 06:33:32 -04:00
|
|
|
{
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
"",
|
|
|
|
expect{true, false},
|
|
|
|
},
|
2015-12-07 15:28:36 -05:00
|
|
|
{reflect.ValueOf(true), reflect.ValueOf(true), "", expect{true, false}},
|
2015-06-27 13:15:42 -04:00
|
|
|
{reflect.ValueOf(nil), reflect.ValueOf(nil), "", expect{true, false}},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf(123), reflect.ValueOf(456), "!=", expect{true, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf("bar"), "!=", expect{true, false}},
|
2015-05-26 06:33:32 -04:00
|
|
|
{
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
"!=",
|
|
|
|
expect{true, false},
|
|
|
|
},
|
2015-12-07 15:28:36 -05:00
|
|
|
{reflect.ValueOf(true), reflect.ValueOf(false), "!=", expect{true, false}},
|
2015-06-27 13:15:42 -04:00
|
|
|
{reflect.ValueOf(123), reflect.ValueOf(nil), "!=", expect{true, false}},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf(456), reflect.ValueOf(123), ">=", expect{true, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf("bar"), ">=", expect{true, false}},
|
2015-05-26 06:33:32 -04:00
|
|
|
{
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
">=",
|
|
|
|
expect{true, false},
|
|
|
|
},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf(456), reflect.ValueOf(123), ">", expect{true, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf("bar"), ">", expect{true, false}},
|
2015-05-26 06:33:32 -04:00
|
|
|
{
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
">",
|
|
|
|
expect{true, false},
|
|
|
|
},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf(123), reflect.ValueOf(456), "<=", expect{true, false}},
|
|
|
|
{reflect.ValueOf("bar"), reflect.ValueOf("foo"), "<=", expect{true, false}},
|
2015-05-26 06:33:32 -04:00
|
|
|
{
|
|
|
|
reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
"<=",
|
|
|
|
expect{true, false},
|
|
|
|
},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf(123), reflect.ValueOf(456), "<", expect{true, false}},
|
|
|
|
{reflect.ValueOf("bar"), reflect.ValueOf("foo"), "<", expect{true, false}},
|
2015-05-26 06:33:32 -04:00
|
|
|
{
|
|
|
|
reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
"<",
|
|
|
|
expect{true, false},
|
|
|
|
},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf(123), reflect.ValueOf([]int{123, 45, 678}), "in", expect{true, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf([]string{"foo", "bar", "baz"}), "in", expect{true, false}},
|
2015-05-26 06:33:32 -04:00
|
|
|
{
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
reflect.ValueOf([]time.Time{
|
|
|
|
time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC),
|
|
|
|
time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC),
|
|
|
|
time.Date(2015, time.June, 26, 19, 18, 56, 12345, time.UTC),
|
|
|
|
}),
|
|
|
|
"in",
|
|
|
|
expect{true, false},
|
|
|
|
},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf(123), reflect.ValueOf([]int{45, 678}), "not in", expect{true, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf([]string{"bar", "baz"}), "not in", expect{true, false}},
|
2015-05-26 06:33:32 -04:00
|
|
|
{
|
|
|
|
reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
|
|
|
|
reflect.ValueOf([]time.Time{
|
|
|
|
time.Date(2015, time.February, 26, 19, 18, 56, 12345, time.UTC),
|
|
|
|
time.Date(2015, time.March, 26, 19, 18, 56, 12345, time.UTC),
|
|
|
|
time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC),
|
|
|
|
}),
|
|
|
|
"not in",
|
|
|
|
expect{true, false},
|
|
|
|
},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf("bar-foo-baz"), "in", expect{true, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf("bar--baz"), "not in", expect{true, false}},
|
|
|
|
{reflect.Value{}, reflect.ValueOf("foo"), "", expect{false, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.Value{}, "", expect{false, false}},
|
|
|
|
{reflect.ValueOf((*TstX)(nil)), reflect.ValueOf("foo"), "", expect{false, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf((*TstX)(nil)), "", expect{false, false}},
|
2015-12-07 15:28:36 -05:00
|
|
|
{reflect.ValueOf(true), reflect.ValueOf("foo"), "", expect{false, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf(true), "", expect{false, false}},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf(map[int]string{}), "", expect{false, false}},
|
|
|
|
{reflect.ValueOf("foo"), reflect.ValueOf([]int{1, 2}), "", expect{false, false}},
|
2015-12-07 15:28:36 -05:00
|
|
|
{reflect.ValueOf((*TstX)(nil)), reflect.ValueOf((*TstX)(nil)), ">", expect{false, false}},
|
|
|
|
{reflect.ValueOf(true), reflect.ValueOf(false), ">", expect{false, false}},
|
2015-04-05 15:03:16 -04:00
|
|
|
{reflect.ValueOf(123), reflect.ValueOf([]int{}), "in", expect{false, false}},
|
|
|
|
{reflect.ValueOf(123), reflect.ValueOf(123), "op", expect{false, true}},
|
|
|
|
} {
|
|
|
|
result, err := checkCondition(this.value, this.match, this.op)
|
|
|
|
if this.expect.isError {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] checkCondition didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result != this.expect.result {
|
|
|
|
t.Errorf("[%d] check condition %v %s %v, got %v but expected %v", i, this.value, this.op, this.match, result, this.expect.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhere(t *testing.T) {
|
|
|
|
// TODO(spf): Put these page tests back in
|
|
|
|
//page1 := &Page{contentType: "v", Source: Source{File: *source.NewFile("/x/y/z/source.md")}}
|
|
|
|
//page2 := &Page{contentType: "w", Source: Source{File: *source.NewFile("/y/z/a/source.md")}}
|
|
|
|
|
|
|
|
type Mid struct {
|
|
|
|
Tst TstX
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
sequence interface{}
|
|
|
|
key interface{}
|
|
|
|
op string
|
|
|
|
match interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
sequence: []map[int]string{
|
|
|
|
{1: "a", 2: "m"}, {1: "c", 2: "d"}, {1: "e", 3: "m"},
|
|
|
|
},
|
|
|
|
key: 2, match: "m",
|
|
|
|
expect: []map[int]string{
|
|
|
|
{1: "a", 2: "m"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]int{
|
|
|
|
{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
|
|
|
|
},
|
|
|
|
key: "b", match: 4,
|
|
|
|
expect: []map[string]int{
|
|
|
|
{"a": 3, "b": 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
|
|
|
|
},
|
|
|
|
key: "B", match: "f",
|
|
|
|
expect: []TstX{
|
|
|
|
{A: "e", B: "f"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []*map[int]string{
|
|
|
|
{1: "a", 2: "m"}, {1: "c", 2: "d"}, {1: "e", 3: "m"},
|
|
|
|
},
|
|
|
|
key: 2, match: "m",
|
|
|
|
expect: []*map[int]string{
|
|
|
|
{1: "a", 2: "m"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []*TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
|
|
|
|
},
|
|
|
|
key: "B", match: "f",
|
|
|
|
expect: []*TstX{
|
|
|
|
{A: "e", B: "f"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []*TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "c"},
|
|
|
|
},
|
|
|
|
key: "TstRp", match: "rc",
|
|
|
|
expect: []*TstX{
|
|
|
|
{A: "c", B: "d"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "c"},
|
|
|
|
},
|
|
|
|
key: "TstRv", match: "rc",
|
|
|
|
expect: []TstX{
|
|
|
|
{A: "e", B: "c"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]TstX{
|
|
|
|
{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
|
|
|
|
},
|
|
|
|
key: "foo.B", match: "d",
|
|
|
|
expect: []map[string]TstX{
|
|
|
|
{"foo": TstX{A: "c", B: "d"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]TstX{
|
|
|
|
{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
|
|
|
|
},
|
|
|
|
key: ".foo.B", match: "d",
|
|
|
|
expect: []map[string]TstX{
|
|
|
|
{"foo": TstX{A: "c", B: "d"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]TstX{
|
|
|
|
{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
|
|
|
|
},
|
|
|
|
key: "foo.TstRv", match: "rd",
|
|
|
|
expect: []map[string]TstX{
|
|
|
|
{"foo": TstX{A: "c", B: "d"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]*TstX{
|
|
|
|
{"foo": &TstX{A: "a", B: "b"}}, {"foo": &TstX{A: "c", B: "d"}}, {"foo": &TstX{A: "e", B: "f"}},
|
|
|
|
},
|
|
|
|
key: "foo.TstRp", match: "rc",
|
|
|
|
expect: []map[string]*TstX{
|
|
|
|
{"foo": &TstX{A: "c", B: "d"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]Mid{
|
|
|
|
{"foo": Mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": Mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": Mid{Tst: TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
key: "foo.Tst.B", match: "d",
|
|
|
|
expect: []map[string]Mid{
|
|
|
|
{"foo": Mid{Tst: TstX{A: "c", B: "d"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]Mid{
|
|
|
|
{"foo": Mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": Mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": Mid{Tst: TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
key: "foo.Tst.TstRv", match: "rd",
|
|
|
|
expect: []map[string]Mid{
|
|
|
|
{"foo": Mid{Tst: TstX{A: "c", B: "d"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]*Mid{
|
|
|
|
{"foo": &Mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": &Mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": &Mid{Tst: TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
key: "foo.Tst.TstRp", match: "rc",
|
|
|
|
expect: []map[string]*Mid{
|
|
|
|
{"foo": &Mid{Tst: TstX{A: "c", B: "d"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]int{
|
|
|
|
{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6},
|
|
|
|
},
|
|
|
|
key: "b", op: ">", match: 3,
|
|
|
|
expect: []map[string]int{
|
|
|
|
{"a": 3, "b": 4}, {"a": 5, "b": 6},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
|
|
|
|
},
|
|
|
|
key: "B", op: "!=", match: "f",
|
|
|
|
expect: []TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "c", B: "d"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]int{
|
|
|
|
{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6},
|
|
|
|
},
|
|
|
|
key: "b", op: "in", match: []int{3, 4, 5},
|
|
|
|
expect: []map[string]int{
|
|
|
|
{"a": 3, "b": 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
|
|
|
|
},
|
|
|
|
key: "B", op: "not in", match: []string{"c", "d", "e"},
|
|
|
|
expect: []TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "e", B: "f"},
|
|
|
|
},
|
|
|
|
},
|
2015-06-27 13:15:42 -04:00
|
|
|
{
|
|
|
|
sequence: []map[string]int{
|
|
|
|
{"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
|
|
|
|
},
|
|
|
|
key: "b", op: "", match: nil,
|
|
|
|
expect: []map[string]int{
|
|
|
|
{"a": 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]int{
|
|
|
|
{"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
|
|
|
|
},
|
|
|
|
key: "b", op: "!=", match: nil,
|
|
|
|
expect: []map[string]int{
|
|
|
|
{"a": 1, "b": 2}, {"a": 5, "b": 6},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]int{
|
|
|
|
{"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
|
|
|
|
},
|
|
|
|
key: "b", op: ">", match: nil,
|
|
|
|
expect: []map[string]int{},
|
|
|
|
},
|
2015-12-07 15:28:36 -05:00
|
|
|
{
|
|
|
|
sequence: []map[string]bool{
|
|
|
|
{"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
|
|
|
|
},
|
|
|
|
key: "b", op: "", match: true,
|
|
|
|
expect: []map[string]bool{
|
|
|
|
{"c": true, "b": true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]bool{
|
|
|
|
{"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
|
|
|
|
},
|
|
|
|
key: "b", op: "!=", match: true,
|
|
|
|
expect: []map[string]bool{
|
|
|
|
{"a": true, "b": false}, {"d": true, "b": false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequence: []map[string]bool{
|
|
|
|
{"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
|
|
|
|
},
|
|
|
|
key: "b", op: ">", match: false,
|
|
|
|
expect: []map[string]bool{},
|
|
|
|
},
|
2015-04-05 15:03:16 -04:00
|
|
|
{sequence: (*[]TstX)(nil), key: "A", match: "a", expect: false},
|
|
|
|
{sequence: TstX{A: "a", B: "b"}, key: "A", match: "a", expect: false},
|
|
|
|
{sequence: []map[string]*TstX{{"foo": nil}}, key: "foo.B", match: "d", expect: false},
|
|
|
|
{
|
|
|
|
sequence: []TstX{
|
|
|
|
{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
|
|
|
|
},
|
|
|
|
key: "B", op: "op", match: "f",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
//{[]*Page{page1, page2}, "Type", "v", []*Page{page1}},
|
|
|
|
//{[]*Page{page1, page2}, "Section", "y", []*Page{page2}},
|
|
|
|
} {
|
|
|
|
var results interface{}
|
|
|
|
var err error
|
|
|
|
if len(this.op) > 0 {
|
2016-02-06 09:53:39 -05:00
|
|
|
results, err = where(this.sequence, this.key, this.op, this.match)
|
2015-04-05 15:03:16 -04:00
|
|
|
} else {
|
2016-02-06 09:53:39 -05:00
|
|
|
results, err = where(this.sequence, this.key, this.match)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Where didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(results, this.expect) {
|
|
|
|
t.Errorf("[%d] Where clause matching %v with %v, got %v but expected %v", i, this.key, this.match, results, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = where(map[string]int{"a": 1, "b": 2}, "a", []byte("="), 1)
|
2015-04-05 15:03:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Where called with none string op value didn't return an expected error")
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = where(map[string]int{"a": 1, "b": 2}, "a", []byte("="), 1, 2)
|
2015-04-05 15:03:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Where called with more than two variable arguments didn't return an expected error")
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = where(map[string]int{"a": 1, "b": 2}, "a")
|
2015-04-05 15:03:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Where called with no variable arguments didn't return an expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelimit(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
sequence interface{}
|
|
|
|
delimiter interface{}
|
|
|
|
last interface{}
|
|
|
|
expect template.HTML
|
|
|
|
}{
|
|
|
|
{[]string{"class1", "class2", "class3"}, " ", nil, "class1 class2 class3"},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, ",", nil, "1,2,3,4,5"},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, ", ", nil, "1, 2, 3, 4, 5"},
|
|
|
|
{[]string{"class1", "class2", "class3"}, " ", " and ", "class1 class2 and class3"},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, ",", ",", "1,2,3,4,5"},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, ", ", ", and ", "1, 2, 3, 4, and 5"},
|
|
|
|
// test maps with and without sorting required
|
|
|
|
{map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}, "--", nil, "10--20--30--40--50"},
|
|
|
|
{map[string]int{"3": 10, "2": 20, "1": 30, "4": 40, "5": 50}, "--", nil, "30--20--10--40--50"},
|
|
|
|
{map[string]string{"1": "10", "2": "20", "3": "30", "4": "40", "5": "50"}, "--", nil, "10--20--30--40--50"},
|
|
|
|
{map[string]string{"3": "10", "2": "20", "1": "30", "4": "40", "5": "50"}, "--", nil, "30--20--10--40--50"},
|
|
|
|
{map[string]string{"one": "10", "two": "20", "three": "30", "four": "40", "five": "50"}, "--", nil, "50--40--10--30--20"},
|
|
|
|
{map[int]string{1: "10", 2: "20", 3: "30", 4: "40", 5: "50"}, "--", nil, "10--20--30--40--50"},
|
|
|
|
{map[int]string{3: "10", 2: "20", 1: "30", 4: "40", 5: "50"}, "--", nil, "30--20--10--40--50"},
|
|
|
|
{map[float64]string{3.3: "10", 2.3: "20", 1.3: "30", 4.3: "40", 5.3: "50"}, "--", nil, "30--20--10--40--50"},
|
|
|
|
// test maps with a last delimiter
|
|
|
|
{map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}, "--", "--and--", "10--20--30--40--and--50"},
|
|
|
|
{map[string]int{"3": 10, "2": 20, "1": 30, "4": 40, "5": 50}, "--", "--and--", "30--20--10--40--and--50"},
|
|
|
|
{map[string]string{"1": "10", "2": "20", "3": "30", "4": "40", "5": "50"}, "--", "--and--", "10--20--30--40--and--50"},
|
|
|
|
{map[string]string{"3": "10", "2": "20", "1": "30", "4": "40", "5": "50"}, "--", "--and--", "30--20--10--40--and--50"},
|
|
|
|
{map[string]string{"one": "10", "two": "20", "three": "30", "four": "40", "five": "50"}, "--", "--and--", "50--40--10--30--and--20"},
|
|
|
|
{map[int]string{1: "10", 2: "20", 3: "30", 4: "40", 5: "50"}, "--", "--and--", "10--20--30--40--and--50"},
|
|
|
|
{map[int]string{3: "10", 2: "20", 1: "30", 4: "40", 5: "50"}, "--", "--and--", "30--20--10--40--and--50"},
|
|
|
|
{map[float64]string{3.5: "10", 2.5: "20", 1.5: "30", 4.5: "40", 5.5: "50"}, "--", "--and--", "30--20--10--40--and--50"},
|
|
|
|
} {
|
|
|
|
var result template.HTML
|
|
|
|
var err error
|
|
|
|
if this.last == nil {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = delimit(this.sequence, this.delimiter)
|
2015-04-05 15:03:16 -04:00
|
|
|
} else {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = delimit(this.sequence, this.delimiter, this.last)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
|
|
|
t.Errorf("[%d] Delimit called on sequence: %v | delimiter: `%v` | last: `%v`, got %v but expected %v", i, this.sequence, this.delimiter, this.last, result, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSort(t *testing.T) {
|
|
|
|
type ts struct {
|
|
|
|
MyInt int
|
|
|
|
MyFloat float64
|
|
|
|
MyString string
|
|
|
|
}
|
2015-08-07 07:05:18 -04:00
|
|
|
type mid struct {
|
|
|
|
Tst TstX
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
for i, this := range []struct {
|
|
|
|
sequence interface{}
|
|
|
|
sortByField interface{}
|
|
|
|
sortAsc string
|
2015-08-06 13:27:15 -04:00
|
|
|
expect interface{}
|
2015-04-05 15:03:16 -04:00
|
|
|
}{
|
2015-08-06 13:27:15 -04:00
|
|
|
{[]string{"class1", "class2", "class3"}, nil, "asc", []string{"class1", "class2", "class3"}},
|
|
|
|
{[]string{"class3", "class1", "class2"}, nil, "asc", []string{"class1", "class2", "class3"}},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, nil, "asc", []int{1, 2, 3, 4, 5}},
|
|
|
|
{[]int{5, 4, 3, 1, 2}, nil, "asc", []int{1, 2, 3, 4, 5}},
|
2015-08-07 07:05:18 -04:00
|
|
|
// test sort key parameter is focibly set empty
|
|
|
|
{[]string{"class3", "class1", "class2"}, map[int]string{1: "a"}, "asc", []string{"class1", "class2", "class3"}},
|
2015-04-05 15:03:16 -04:00
|
|
|
// test map sorting by keys
|
2015-08-06 13:27:15 -04:00
|
|
|
{map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}, nil, "asc", []int{10, 20, 30, 40, 50}},
|
|
|
|
{map[string]int{"3": 10, "2": 20, "1": 30, "4": 40, "5": 50}, nil, "asc", []int{30, 20, 10, 40, 50}},
|
|
|
|
{map[string]string{"1": "10", "2": "20", "3": "30", "4": "40", "5": "50"}, nil, "asc", []string{"10", "20", "30", "40", "50"}},
|
|
|
|
{map[string]string{"3": "10", "2": "20", "1": "30", "4": "40", "5": "50"}, nil, "asc", []string{"30", "20", "10", "40", "50"}},
|
|
|
|
{map[string]string{"one": "10", "two": "20", "three": "30", "four": "40", "five": "50"}, nil, "asc", []string{"50", "40", "10", "30", "20"}},
|
|
|
|
{map[int]string{1: "10", 2: "20", 3: "30", 4: "40", 5: "50"}, nil, "asc", []string{"10", "20", "30", "40", "50"}},
|
|
|
|
{map[int]string{3: "10", 2: "20", 1: "30", 4: "40", 5: "50"}, nil, "asc", []string{"30", "20", "10", "40", "50"}},
|
|
|
|
{map[float64]string{3.3: "10", 2.3: "20", 1.3: "30", 4.3: "40", 5.3: "50"}, nil, "asc", []string{"30", "20", "10", "40", "50"}},
|
2015-04-05 15:03:16 -04:00
|
|
|
// test map sorting by value
|
2015-08-06 13:27:15 -04:00
|
|
|
{map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}, "value", "asc", []int{10, 20, 30, 40, 50}},
|
|
|
|
{map[string]int{"3": 10, "2": 20, "1": 30, "4": 40, "5": 50}, "value", "asc", []int{10, 20, 30, 40, 50}},
|
2015-04-05 15:03:16 -04:00
|
|
|
// test map sorting by field value
|
|
|
|
{
|
|
|
|
map[string]ts{"1": {10, 10.5, "ten"}, "2": {20, 20.5, "twenty"}, "3": {30, 30.5, "thirty"}, "4": {40, 40.5, "forty"}, "5": {50, 50.5, "fifty"}},
|
|
|
|
"MyInt",
|
|
|
|
"asc",
|
2015-08-06 13:27:15 -04:00
|
|
|
[]ts{{10, 10.5, "ten"}, {20, 20.5, "twenty"}, {30, 30.5, "thirty"}, {40, 40.5, "forty"}, {50, 50.5, "fifty"}},
|
2015-04-05 15:03:16 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]ts{"1": {10, 10.5, "ten"}, "2": {20, 20.5, "twenty"}, "3": {30, 30.5, "thirty"}, "4": {40, 40.5, "forty"}, "5": {50, 50.5, "fifty"}},
|
|
|
|
"MyFloat",
|
|
|
|
"asc",
|
2015-08-06 13:27:15 -04:00
|
|
|
[]ts{{10, 10.5, "ten"}, {20, 20.5, "twenty"}, {30, 30.5, "thirty"}, {40, 40.5, "forty"}, {50, 50.5, "fifty"}},
|
2015-04-05 15:03:16 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]ts{"1": {10, 10.5, "ten"}, "2": {20, 20.5, "twenty"}, "3": {30, 30.5, "thirty"}, "4": {40, 40.5, "forty"}, "5": {50, 50.5, "fifty"}},
|
|
|
|
"MyString",
|
|
|
|
"asc",
|
2015-08-06 13:27:15 -04:00
|
|
|
[]ts{{50, 50.5, "fifty"}, {40, 40.5, "forty"}, {10, 10.5, "ten"}, {30, 30.5, "thirty"}, {20, 20.5, "twenty"}},
|
2015-04-05 15:03:16 -04:00
|
|
|
},
|
2015-08-07 07:05:18 -04:00
|
|
|
// test sort desc
|
2015-08-06 13:27:15 -04:00
|
|
|
{[]string{"class1", "class2", "class3"}, "value", "desc", []string{"class3", "class2", "class1"}},
|
|
|
|
{[]string{"class3", "class1", "class2"}, "value", "desc", []string{"class3", "class2", "class1"}},
|
2015-08-07 07:05:18 -04:00
|
|
|
// test sort by struct's method
|
|
|
|
{
|
|
|
|
[]TstX{{A: "i", B: "j"}, {A: "e", B: "f"}, {A: "c", B: "d"}, {A: "g", B: "h"}, {A: "a", B: "b"}},
|
|
|
|
"TstRv",
|
|
|
|
"asc",
|
|
|
|
[]TstX{{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"}, {A: "g", B: "h"}, {A: "i", B: "j"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*TstX{{A: "i", B: "j"}, {A: "e", B: "f"}, {A: "c", B: "d"}, {A: "g", B: "h"}, {A: "a", B: "b"}},
|
|
|
|
"TstRp",
|
|
|
|
"asc",
|
|
|
|
[]*TstX{{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"}, {A: "g", B: "h"}, {A: "i", B: "j"}},
|
|
|
|
},
|
|
|
|
// test map sorting by struct's method
|
|
|
|
{
|
|
|
|
map[string]TstX{"1": {A: "i", B: "j"}, "2": {A: "e", B: "f"}, "3": {A: "c", B: "d"}, "4": {A: "g", B: "h"}, "5": {A: "a", B: "b"}},
|
|
|
|
"TstRv",
|
|
|
|
"asc",
|
|
|
|
[]TstX{{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"}, {A: "g", B: "h"}, {A: "i", B: "j"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]*TstX{"1": {A: "i", B: "j"}, "2": {A: "e", B: "f"}, "3": {A: "c", B: "d"}, "4": {A: "g", B: "h"}, "5": {A: "a", B: "b"}},
|
|
|
|
"TstRp",
|
|
|
|
"asc",
|
|
|
|
[]*TstX{{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"}, {A: "g", B: "h"}, {A: "i", B: "j"}},
|
|
|
|
},
|
|
|
|
// test sort by dot chaining key argument
|
|
|
|
{
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "e", B: "f"}}, {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}},
|
|
|
|
"foo.A",
|
|
|
|
"asc",
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "e", B: "f"}}, {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}},
|
|
|
|
".foo.A",
|
|
|
|
"asc",
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "e", B: "f"}}, {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}},
|
|
|
|
"foo.TstRv",
|
|
|
|
"asc",
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]map[string]*TstX{{"foo": &TstX{A: "e", B: "f"}}, {"foo": &TstX{A: "a", B: "b"}}, {"foo": &TstX{A: "c", B: "d"}}},
|
|
|
|
"foo.TstRp",
|
|
|
|
"asc",
|
|
|
|
[]map[string]*TstX{{"foo": &TstX{A: "a", B: "b"}}, {"foo": &TstX{A: "c", B: "d"}}, {"foo": &TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]map[string]mid{{"foo": mid{Tst: TstX{A: "e", B: "f"}}}, {"foo": mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": mid{Tst: TstX{A: "c", B: "d"}}}},
|
|
|
|
"foo.Tst.A",
|
|
|
|
"asc",
|
|
|
|
[]map[string]mid{{"foo": mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": mid{Tst: TstX{A: "e", B: "f"}}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]map[string]mid{{"foo": mid{Tst: TstX{A: "e", B: "f"}}}, {"foo": mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": mid{Tst: TstX{A: "c", B: "d"}}}},
|
|
|
|
"foo.Tst.TstRv",
|
|
|
|
"asc",
|
|
|
|
[]map[string]mid{{"foo": mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": mid{Tst: TstX{A: "e", B: "f"}}}},
|
|
|
|
},
|
|
|
|
// test map sorting by dot chaining key argument
|
|
|
|
{
|
|
|
|
map[string]map[string]TstX{"1": {"foo": TstX{A: "e", B: "f"}}, "2": {"foo": TstX{A: "a", B: "b"}}, "3": {"foo": TstX{A: "c", B: "d"}}},
|
|
|
|
"foo.A",
|
|
|
|
"asc",
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]map[string]TstX{"1": {"foo": TstX{A: "e", B: "f"}}, "2": {"foo": TstX{A: "a", B: "b"}}, "3": {"foo": TstX{A: "c", B: "d"}}},
|
|
|
|
".foo.A",
|
|
|
|
"asc",
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]map[string]TstX{"1": {"foo": TstX{A: "e", B: "f"}}, "2": {"foo": TstX{A: "a", B: "b"}}, "3": {"foo": TstX{A: "c", B: "d"}}},
|
|
|
|
"foo.TstRv",
|
|
|
|
"asc",
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]map[string]*TstX{"1": {"foo": &TstX{A: "e", B: "f"}}, "2": {"foo": &TstX{A: "a", B: "b"}}, "3": {"foo": &TstX{A: "c", B: "d"}}},
|
|
|
|
"foo.TstRp",
|
|
|
|
"asc",
|
|
|
|
[]map[string]*TstX{{"foo": &TstX{A: "a", B: "b"}}, {"foo": &TstX{A: "c", B: "d"}}, {"foo": &TstX{A: "e", B: "f"}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]map[string]mid{"1": {"foo": mid{Tst: TstX{A: "e", B: "f"}}}, "2": {"foo": mid{Tst: TstX{A: "a", B: "b"}}}, "3": {"foo": mid{Tst: TstX{A: "c", B: "d"}}}},
|
|
|
|
"foo.Tst.A",
|
|
|
|
"asc",
|
|
|
|
[]map[string]mid{{"foo": mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": mid{Tst: TstX{A: "e", B: "f"}}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]map[string]mid{"1": {"foo": mid{Tst: TstX{A: "e", B: "f"}}}, "2": {"foo": mid{Tst: TstX{A: "a", B: "b"}}}, "3": {"foo": mid{Tst: TstX{A: "c", B: "d"}}}},
|
|
|
|
"foo.Tst.TstRv",
|
|
|
|
"asc",
|
|
|
|
[]map[string]mid{{"foo": mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": mid{Tst: TstX{A: "e", B: "f"}}}},
|
|
|
|
},
|
|
|
|
// test error cases
|
|
|
|
{(*[]TstX)(nil), nil, "asc", false},
|
|
|
|
{TstX{A: "a", B: "b"}, nil, "asc", false},
|
|
|
|
{
|
|
|
|
[]map[string]TstX{{"foo": TstX{A: "e", B: "f"}}, {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}},
|
|
|
|
"foo.NotAvailable",
|
|
|
|
"asc",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
map[string]map[string]TstX{"1": {"foo": TstX{A: "e", B: "f"}}, "2": {"foo": TstX{A: "a", B: "b"}}, "3": {"foo": TstX{A: "c", B: "d"}}},
|
|
|
|
"foo.NotAvailable",
|
|
|
|
"asc",
|
|
|
|
false,
|
|
|
|
},
|
2015-04-05 15:03:16 -04:00
|
|
|
} {
|
2015-08-06 13:27:15 -04:00
|
|
|
var result interface{}
|
2015-04-05 15:03:16 -04:00
|
|
|
var err error
|
|
|
|
if this.sortByField == nil {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = sortSeq(this.sequence)
|
2015-04-05 15:03:16 -04:00
|
|
|
} else {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err = sortSeq(this.sequence, this.sortByField, this.sortAsc)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
2015-08-07 07:05:18 -04:00
|
|
|
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Sort didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
|
|
|
t.Errorf("[%d] Sort called on sequence: %v | sortByField: `%v` | got %v but expected %v", i, this.sequence, this.sortByField, result, this.expect)
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReturnWhenSet(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
data interface{}
|
|
|
|
key interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{[]int{1, 2, 3}, 1, int64(2)},
|
|
|
|
{[]uint{1, 2, 3}, 1, uint64(2)},
|
|
|
|
{[]float64{1.1, 2.2, 3.3}, 1, float64(2.2)},
|
|
|
|
{[]string{"foo", "bar", "baz"}, 1, "bar"},
|
|
|
|
{[]TstX{{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"}}, 1, ""},
|
|
|
|
{map[string]int{"foo": 1, "bar": 2, "baz": 3}, "bar", int64(2)},
|
|
|
|
{map[string]uint{"foo": 1, "bar": 2, "baz": 3}, "bar", uint64(2)},
|
|
|
|
{map[string]float64{"foo": 1.1, "bar": 2.2, "baz": 3.3}, "bar", float64(2.2)},
|
|
|
|
{map[string]string{"foo": "FOO", "bar": "BAR", "baz": "BAZ"}, "bar", "BAR"},
|
|
|
|
{map[string]TstX{"foo": {A: "a", B: "b"}, "bar": {A: "c", B: "d"}, "baz": {A: "e", B: "f"}}, "bar", ""},
|
|
|
|
{(*[]string)(nil), "bar", ""},
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
result := returnWhenSet(this.data, this.key)
|
2015-04-05 15:03:16 -04:00
|
|
|
if !reflect.DeepEqual(result, this.expect) {
|
|
|
|
t.Errorf("[%d] ReturnWhenSet got %v (type %v) but expected %v (type %v)", i, result, reflect.TypeOf(result), this.expect, reflect.TypeOf(this.expect))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarkdownify(t *testing.T) {
|
2016-02-06 09:53:39 -05:00
|
|
|
result := markdownify("Hello **World!**")
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
expect := template.HTML("Hello <strong>World!</strong>")
|
|
|
|
|
|
|
|
if result != expect {
|
|
|
|
t.Errorf("Markdownify: got '%s', expected '%s'", result, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApply(t *testing.T) {
|
|
|
|
strings := []interface{}{"a\n", "b\n"}
|
|
|
|
noStringers := []interface{}{tstNoStringer{}, tstNoStringer{}}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
chomped, _ := apply(strings, "chomp", ".")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, []interface{}{"a", "b"}, chomped)
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
chomped, _ = apply(strings, "chomp", "c\n")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, []interface{}{"c", "c"}, chomped)
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
chomped, _ = apply(nil, "chomp", ".")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, []interface{}{}, chomped)
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err := apply(strings, "apply", ".")
|
2015-04-05 15:03:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("apply with apply should fail")
|
|
|
|
}
|
|
|
|
|
2016-02-05 01:05:04 -05:00
|
|
|
var nilErr *error
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = apply(nilErr, "chomp", ".")
|
2015-04-05 15:03:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("apply with nil in seq should fail")
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = apply(strings, "dobedobedo", ".")
|
2015-04-05 15:03:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("apply with unknown func should fail")
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = apply(noStringers, "chomp", ".")
|
2015-04-05 15:03:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("apply when func fails should fail")
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err = apply(tstNoStringer{}, "chomp", ".")
|
2015-04-05 15:03:16 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("apply with non-sequence should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChomp(t *testing.T) {
|
|
|
|
base := "\n This is\na story "
|
|
|
|
for i, item := range []string{
|
|
|
|
"\n", "\n\n",
|
|
|
|
"\r", "\r\r",
|
|
|
|
"\r\n", "\r\n\r\n",
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
chomped, _ := chomp(base + item)
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
if chomped != base {
|
|
|
|
t.Errorf("[%d] Chomp failed, got '%v'", i, chomped)
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
_, err := chomp(tstNoStringer{})
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Chomp should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 09:15:27 -05:00
|
|
|
func TestHighlight(t *testing.T) {
|
|
|
|
code := "func boo() {}"
|
|
|
|
highlighted, err := highlight(code, "go", "")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Highlight returned error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this depends on a Pygments installation, but will always contain the function name.
|
|
|
|
if !strings.Contains(string(highlighted), "boo") {
|
|
|
|
t.Errorf("Highlight mismatch, got %v", highlighted)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = highlight(t, "go", "")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected highlight error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 08:37:04 -05:00
|
|
|
func TestInflect(t *testing.T) {
|
2016-02-06 19:45:37 -05:00
|
|
|
for i, this := range []struct {
|
2016-02-07 08:37:04 -05:00
|
|
|
inflectFunc func(i interface{}) (string, error)
|
|
|
|
in string
|
|
|
|
expected string
|
2016-02-05 01:05:04 -05:00
|
|
|
}{
|
2016-02-07 08:37:04 -05:00
|
|
|
{humanize, "MyCamel", "My camel"},
|
|
|
|
{pluralize, "cat", "cats"},
|
|
|
|
{singularize, "cats", "cat"},
|
2016-02-05 01:05:04 -05:00
|
|
|
} {
|
2016-02-06 19:45:37 -05:00
|
|
|
|
2016-02-07 08:37:04 -05:00
|
|
|
result, err := this.inflectFunc(this.in)
|
2016-02-06 19:45:37 -05:00
|
|
|
|
2016-02-07 08:37:04 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] Unexpected Inflect error: %s", i, err)
|
|
|
|
} else if result != this.expected {
|
|
|
|
t.Errorf("[%d] Inflect method error, got %v expected %v", i, result, this.expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = this.inflectFunc(t)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Expected Inflect error", i)
|
2016-02-06 19:45:37 -05:00
|
|
|
}
|
2016-02-05 01:05:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 08:51:06 -05:00
|
|
|
func TestCounterFuncs(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
countFunc func(i interface{}) (int, error)
|
|
|
|
in string
|
|
|
|
expected int
|
|
|
|
}{
|
|
|
|
{countWords, "Do Be Do Be Do", 5},
|
|
|
|
{countWords, "旁边", 2},
|
|
|
|
{countRunes, "旁边", 2},
|
|
|
|
} {
|
|
|
|
|
|
|
|
result, err := this.countFunc(this.in)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] Unexpected counter error: %s", i, err)
|
|
|
|
} else if result != this.expected {
|
|
|
|
t.Errorf("[%d] Count method error, got %v expected %v", i, result, this.expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = this.countFunc(t)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] Expected Count error", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
func TestReplace(t *testing.T) {
|
2016-02-06 09:53:39 -05:00
|
|
|
v, _ := replace("aab", "a", "b")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, "bbb", v)
|
2016-02-06 09:53:39 -05:00
|
|
|
v, _ = replace("11a11", 1, 2)
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, "22a22", v)
|
2016-02-06 09:53:39 -05:00
|
|
|
v, _ = replace(12345, 1, 2)
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, "22345", v)
|
2016-02-06 09:53:39 -05:00
|
|
|
_, e := replace(tstNoStringer{}, "a", "b")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.NotNil(t, e, "tstNoStringer isn't trimmable")
|
2016-02-06 09:53:39 -05:00
|
|
|
_, e = replace("a", tstNoStringer{}, "b")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.NotNil(t, e, "tstNoStringer cannot be converted to string")
|
2016-02-06 09:53:39 -05:00
|
|
|
_, e = replace("a", "b", tstNoStringer{})
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.NotNil(t, e, "tstNoStringer cannot be converted to string")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrim(t *testing.T) {
|
2016-02-06 09:53:39 -05:00
|
|
|
v, _ := trim("1234 my way 13", "123")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, "4 my way ", v)
|
2016-02-06 09:53:39 -05:00
|
|
|
v, _ = trim(" my way ", " ")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, "my way", v)
|
2016-02-06 09:53:39 -05:00
|
|
|
v, _ = trim(1234, "14")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.Equal(t, "23", v)
|
2016-02-06 09:53:39 -05:00
|
|
|
_, e := trim(tstNoStringer{}, " ")
|
2015-04-05 15:03:16 -04:00
|
|
|
assert.NotNil(t, e, "tstNoStringer isn't trimmable")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDateFormat(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
layout string
|
|
|
|
value interface{}
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{"Monday, Jan 2, 2006", "2015-01-21", "Wednesday, Jan 21, 2015"},
|
|
|
|
{"Monday, Jan 2, 2006", time.Date(2015, time.January, 21, 0, 0, 0, 0, time.UTC), "Wednesday, Jan 21, 2015"},
|
|
|
|
{"This isn't a date layout string", "2015-01-21", "This isn't a date layout string"},
|
|
|
|
{"Monday, Jan 2, 2006", 1421733600, false},
|
|
|
|
{"Monday, Jan 2, 2006", 1421733600.123, false},
|
|
|
|
} {
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err := dateFormat(this.layout, this.value)
|
2015-04-05 15:03:16 -04:00
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] DateFormat didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] DateFormat failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result != this.expect {
|
|
|
|
t.Errorf("[%d] DateFormat got %v but expected %v", i, result, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeHTML(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
str string
|
|
|
|
tmplStr string
|
|
|
|
expectWithoutEscape string
|
|
|
|
expectWithEscape string
|
|
|
|
}{
|
|
|
|
{`<div></div>`, `{{ . }}`, `<div></div>`, `<div></div>`},
|
|
|
|
} {
|
|
|
|
tmpl, err := template.New("test").Parse(this.tmplStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] unable to create new html template %q: %s", i, this.tmplStr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = tmpl.Execute(buf, this.str)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithoutEscape {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value, got %v but expected %v", i, buf.String(), this.expectWithoutEscape)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Reset()
|
2016-02-06 09:53:39 -05:00
|
|
|
err = tmpl.Execute(buf, safeHTML(this.str))
|
2015-04-05 15:03:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeHTML returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithEscape {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeHTML, got %v but expected %v", i, buf.String(), this.expectWithEscape)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeHTMLAttr(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
str string
|
|
|
|
tmplStr string
|
|
|
|
expectWithoutEscape string
|
|
|
|
expectWithEscape string
|
|
|
|
}{
|
|
|
|
{`href="irc://irc.freenode.net/#golang"`, `<a {{ . }}>irc</a>`, `<a ZgotmplZ>irc</a>`, `<a href="irc://irc.freenode.net/#golang">irc</a>`},
|
|
|
|
} {
|
|
|
|
tmpl, err := template.New("test").Parse(this.tmplStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] unable to create new html template %q: %s", i, this.tmplStr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = tmpl.Execute(buf, this.str)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithoutEscape {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value, got %v but expected %v", i, buf.String(), this.expectWithoutEscape)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Reset()
|
2016-02-06 09:53:39 -05:00
|
|
|
err = tmpl.Execute(buf, safeHTMLAttr(this.str))
|
2015-04-05 15:03:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeHTMLAttr returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithEscape {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeHTMLAttr, got %v but expected %v", i, buf.String(), this.expectWithEscape)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeCSS(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
str string
|
|
|
|
tmplStr string
|
|
|
|
expectWithoutEscape string
|
|
|
|
expectWithEscape string
|
|
|
|
}{
|
|
|
|
{`width: 60px;`, `<div style="{{ . }}"></div>`, `<div style="ZgotmplZ"></div>`, `<div style="width: 60px;"></div>`},
|
|
|
|
} {
|
|
|
|
tmpl, err := template.New("test").Parse(this.tmplStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] unable to create new html template %q: %s", i, this.tmplStr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = tmpl.Execute(buf, this.str)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithoutEscape {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value, got %v but expected %v", i, buf.String(), this.expectWithoutEscape)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Reset()
|
2016-02-06 09:53:39 -05:00
|
|
|
err = tmpl.Execute(buf, safeCSS(this.str))
|
2015-04-05 15:03:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeCSS returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithEscape {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeCSS, got %v but expected %v", i, buf.String(), this.expectWithEscape)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 15:30:57 -05:00
|
|
|
func TestSafeJS(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
str string
|
|
|
|
tmplStr string
|
|
|
|
expectWithoutEscape string
|
|
|
|
expectWithEscape string
|
|
|
|
}{
|
|
|
|
{`619c16f`, `<script>var x{{ . }};</script>`, `<script>var x"619c16f";</script>`, `<script>var x619c16f;</script>`},
|
|
|
|
} {
|
|
|
|
tmpl, err := template.New("test").Parse(this.tmplStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] unable to create new html template %q: %s", i, this.tmplStr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = tmpl.Execute(buf, this.str)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithoutEscape {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value, got %v but expected %v", i, buf.String(), this.expectWithoutEscape)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Reset()
|
2016-02-06 09:53:39 -05:00
|
|
|
err = tmpl.Execute(buf, safeJS(this.str))
|
2015-11-15 15:30:57 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeJS returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithEscape {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeJS, got %v but expected %v", i, buf.String(), this.expectWithEscape)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:03:16 -04:00
|
|
|
func TestSafeURL(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
str string
|
|
|
|
tmplStr string
|
|
|
|
expectWithoutEscape string
|
|
|
|
expectWithEscape string
|
|
|
|
}{
|
|
|
|
{`irc://irc.freenode.net/#golang`, `<a href="{{ . }}">IRC</a>`, `<a href="#ZgotmplZ">IRC</a>`, `<a href="irc://irc.freenode.net/#golang">IRC</a>`},
|
|
|
|
} {
|
|
|
|
tmpl, err := template.New("test").Parse(this.tmplStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] unable to create new html template %q: %s", i, this.tmplStr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = tmpl.Execute(buf, this.str)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithoutEscape {
|
|
|
|
t.Errorf("[%d] execute template with a raw string value, got %v but expected %v", i, buf.String(), this.expectWithoutEscape)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Reset()
|
2016-02-06 09:53:39 -05:00
|
|
|
err = tmpl.Execute(buf, safeURL(this.str))
|
2015-04-05 15:03:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeURL returns unexpected error: %s", i, err)
|
|
|
|
}
|
|
|
|
if buf.String() != this.expectWithEscape {
|
|
|
|
t.Errorf("[%d] execute template with an escaped string value by SafeURL, got %v but expected %v", i, buf.String(), this.expectWithEscape)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 15:45:12 -04:00
|
|
|
|
|
|
|
func TestBase64Decode(t *testing.T) {
|
|
|
|
testStr := "abc123!?$*&()'-=@~"
|
|
|
|
enc := base64.StdEncoding.EncodeToString([]byte(testStr))
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err := base64Decode(enc)
|
2015-09-12 15:45:12 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2016-02-07 08:58:23 -05:00
|
|
|
t.Error("base64Decode returned error:", err)
|
2015-09-12 15:45:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if result != testStr {
|
2016-02-07 08:58:23 -05:00
|
|
|
t.Errorf("base64Decode: got '%s', expected '%s'", result, testStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = base64Decode(t)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error from base64Decode")
|
2015-09-12 15:45:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBase64Encode(t *testing.T) {
|
|
|
|
testStr := "YWJjMTIzIT8kKiYoKSctPUB+"
|
|
|
|
dec, err := base64.StdEncoding.DecodeString(testStr)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-02-07 08:58:23 -05:00
|
|
|
t.Error("base64Encode: the DecodeString function of the base64 package returned an error:", err)
|
2015-09-12 15:45:12 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 09:53:39 -05:00
|
|
|
result, err := base64Encode(string(dec))
|
2015-09-12 15:45:12 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2016-02-07 08:58:23 -05:00
|
|
|
t.Errorf("base64Encode: Can't cast arg '%s' into a string:", testStr)
|
2015-09-12 15:45:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if result != testStr {
|
2016-02-07 08:58:23 -05:00
|
|
|
t.Errorf("base64Encode: got '%s', expected '%s'", result, testStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = base64Encode(t)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error from base64Encode")
|
2015-09-12 15:45:12 -04:00
|
|
|
}
|
|
|
|
}
|