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