diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-12-18 16:50:34 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-12-18 20:05:22 +0100 |
commit | d20ca3700512d661247b44d953515b9455e57ed6 (patch) | |
tree | 2db72cf0bc6246e1c28106942af6b1e6f679e4e1 /tpl/compare | |
parent | 3e316155c5d4fbf166d38e997a41101b6aa501d5 (diff) |
tpl: Get rid of the custom template truth logic
Fixes #6615
Diffstat (limited to 'tpl/compare')
-rw-r--r-- | tpl/compare/init.go | 21 | ||||
-rw-r--r-- | tpl/compare/truth.go | 73 | ||||
-rw-r--r-- | tpl/compare/truth_test.go | 60 |
3 files changed, 0 insertions, 154 deletions
diff --git a/tpl/compare/init.go b/tpl/compare/init.go index 2e536ff04..3b9dc6856 100644 --- a/tpl/compare/init.go +++ b/tpl/compare/init.go @@ -71,27 +71,6 @@ func init() { [][2]string{}, ) - ns.AddMethodMapping(ctx.And, - []string{"and"}, - [][2]string{}, - ) - - ns.AddMethodMapping(ctx.Or, - []string{"or"}, - [][2]string{}, - ) - - // getif is used internally by Hugo. Do not document. - ns.AddMethodMapping(ctx.getIf, - []string{"getif"}, - [][2]string{}, - ) - - ns.AddMethodMapping(ctx.Not, - []string{"not"}, - [][2]string{}, - ) - ns.AddMethodMapping(ctx.Conditional, []string{"cond"}, [][2]string{ diff --git a/tpl/compare/truth.go b/tpl/compare/truth.go deleted file mode 100644 index 85ee22121..000000000 --- a/tpl/compare/truth.go +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2019 The Hugo Authors. All rights reserved. -// The functions in this file is based on the Go source code, copyright -// The Go Authors and governed by a BSD-style license. -// -// 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 compare provides template functions for comparing values. -package compare - -import ( - "reflect" - - "github.com/gohugoio/hugo/common/hreflect" -) - -// Boolean logic, based on: -// https://github.com/golang/go/blob/178a2c42254166cffed1b25fb1d3c7a5727cada6/src/text/template/funcs.go#L302 - -func truth(arg reflect.Value) bool { - return hreflect.IsTruthfulValue(arg) -} - -// getIf will return the given arg if it is considered truthful, else an empty string. -func (*Namespace) getIf(arg reflect.Value) reflect.Value { - if truth(arg) { - return arg - } - return reflect.ValueOf("") -} - -// And computes the Boolean AND of its arguments, returning -// the first false argument it encounters, or the last argument. -func (*Namespace) And(arg0 reflect.Value, args ...reflect.Value) reflect.Value { - if !truth(arg0) { - return arg0 - } - for i := range args { - arg0 = args[i] - if !truth(arg0) { - break - } - } - return arg0 -} - -// Or computes the Boolean OR of its arguments, returning -// the first true argument it encounters, or the last argument. -func (*Namespace) Or(arg0 reflect.Value, args ...reflect.Value) reflect.Value { - if truth(arg0) { - return arg0 - } - for i := range args { - arg0 = args[i] - if truth(arg0) { - break - } - } - return arg0 -} - -// Not returns the Boolean negation of its argument. -func (*Namespace) Not(arg reflect.Value) bool { - return !truth(arg) -} diff --git a/tpl/compare/truth_test.go b/tpl/compare/truth_test.go deleted file mode 100644 index 4c83e8b0a..000000000 --- a/tpl/compare/truth_test.go +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2019 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 compare - -import ( - "reflect" - "testing" - "time" - - qt "github.com/frankban/quicktest" - "github.com/gohugoio/hugo/common/hreflect" -) - -func TestTruth(t *testing.T) { - n := New(false) - - truthv, falsev := reflect.ValueOf(time.Now()), reflect.ValueOf(false) - - assertTruth := func(t *testing.T, v reflect.Value, expected bool) { - if hreflect.IsTruthfulValue(v) != expected { - t.Fatal("truth mismatch") - } - } - - t.Run("And", func(t *testing.T) { - assertTruth(t, n.And(truthv, truthv), true) - assertTruth(t, n.And(truthv, falsev), false) - - }) - - t.Run("Or", func(t *testing.T) { - assertTruth(t, n.Or(truthv, truthv), true) - assertTruth(t, n.Or(falsev, truthv, falsev), true) - assertTruth(t, n.Or(falsev, falsev), false) - }) - - t.Run("Not", func(t *testing.T) { - c := qt.New(t) - c.Assert(n.Not(falsev), qt.Equals, true) - c.Assert(n.Not(truthv), qt.Equals, false) - }) - - t.Run("getIf", func(t *testing.T) { - c := qt.New(t) - assertTruth(t, n.getIf(reflect.ValueOf(nil)), false) - s := reflect.ValueOf("Hugo") - c.Assert(n.getIf(s), qt.Equals, s) - }) -} |