2018-08-05 05:13:49 -04:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2013-10-01 17:26:21 -04:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-02-12 06:17:59 -05:00
|
|
|
"strings"
|
2013-10-01 17:26:21 -04:00
|
|
|
"testing"
|
2016-05-07 17:34:53 -04:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2013-10-01 17:26:21 -04:00
|
|
|
)
|
|
|
|
|
2013-11-05 17:28:06 -05:00
|
|
|
func TestChainZeroTransformers(t *testing.T) {
|
2018-08-05 05:13:49 -04:00
|
|
|
tr := New()
|
2013-11-05 17:28:06 -05:00
|
|
|
in := new(bytes.Buffer)
|
|
|
|
out := new(bytes.Buffer)
|
2018-08-05 05:13:49 -04:00
|
|
|
if err := tr.Apply(in, out); err != nil {
|
2013-11-05 17:28:06 -05:00
|
|
|
t.Errorf("A zero transformer chain returned an error.")
|
|
|
|
}
|
2013-10-29 23:24:29 -04:00
|
|
|
}
|
|
|
|
|
2015-03-17 19:36:48 -04:00
|
|
|
func TestChaingMultipleTransformers(t *testing.T) {
|
2018-08-05 05:13:49 -04:00
|
|
|
f1 := func(ct FromTo) error {
|
|
|
|
_, err := ct.To().Write(bytes.Replace(ct.From().Bytes(), []byte("f1"), []byte("f1r"), -1))
|
|
|
|
return err
|
2015-03-17 19:36:48 -04:00
|
|
|
}
|
2018-08-05 05:13:49 -04:00
|
|
|
f2 := func(ct FromTo) error {
|
|
|
|
_, err := ct.To().Write(bytes.Replace(ct.From().Bytes(), []byte("f2"), []byte("f2r"), -1))
|
|
|
|
return err
|
2015-03-17 19:36:48 -04:00
|
|
|
}
|
2018-08-05 05:13:49 -04:00
|
|
|
f3 := func(ct FromTo) error {
|
|
|
|
_, err := ct.To().Write(bytes.Replace(ct.From().Bytes(), []byte("f3"), []byte("f3r"), -1))
|
|
|
|
return err
|
2015-03-17 19:36:48 -04:00
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
f4 := func(ct FromTo) error {
|
|
|
|
_, err := ct.To().Write(bytes.Replace(ct.From().Bytes(), []byte("f4"), []byte("f4r"), -1))
|
|
|
|
return err
|
2015-03-17 19:36:48 -04:00
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
tr := New(f1, f2, f3, f4)
|
2015-03-17 19:36:48 -04:00
|
|
|
|
|
|
|
out := new(bytes.Buffer)
|
2018-08-05 05:13:49 -04:00
|
|
|
if err := tr.Apply(out, strings.NewReader("Test: f4 f3 f1 f2 f1 The End.")); err != nil {
|
2015-03-17 19:36:48 -04:00
|
|
|
t.Errorf("Multi transformer chain returned an error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "Test: f4r f3r f1r f2r f1r The End."
|
|
|
|
|
2019-03-24 05:11:16 -04:00
|
|
|
if out.String() != expected {
|
|
|
|
t.Errorf("Expected %s got %s", expected, out.String())
|
2015-03-17 19:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 12:17:48 -05:00
|
|
|
func TestNewEmptyTransforms(t *testing.T) {
|
2018-08-05 05:13:49 -04:00
|
|
|
transforms := NewEmpty()
|
2016-02-06 12:17:48 -05:00
|
|
|
assert.Equal(t, 20, cap(transforms))
|
|
|
|
}
|