chain_test.go (2031B)
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package transform
15
16 import (
17 "bytes"
18 "strings"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestChainZeroTransformers(t *testing.T) {
25 tr := New()
26 in := new(bytes.Buffer)
27 out := new(bytes.Buffer)
28 if err := tr.Apply(in, out); err != nil {
29 t.Errorf("A zero transformer chain returned an error.")
30 }
31 }
32
33 func TestChainingMultipleTransformers(t *testing.T) {
34 f1 := func(ct FromTo) error {
35 _, err := ct.To().Write(bytes.Replace(ct.From().Bytes(), []byte("f1"), []byte("f1r"), -1))
36 return err
37 }
38 f2 := func(ct FromTo) error {
39 _, err := ct.To().Write(bytes.Replace(ct.From().Bytes(), []byte("f2"), []byte("f2r"), -1))
40 return err
41 }
42 f3 := func(ct FromTo) error {
43 _, err := ct.To().Write(bytes.Replace(ct.From().Bytes(), []byte("f3"), []byte("f3r"), -1))
44 return err
45 }
46
47 f4 := func(ct FromTo) error {
48 _, err := ct.To().Write(bytes.Replace(ct.From().Bytes(), []byte("f4"), []byte("f4r"), -1))
49 return err
50 }
51
52 tr := New(f1, f2, f3, f4)
53
54 out := new(bytes.Buffer)
55 if err := tr.Apply(out, strings.NewReader("Test: f4 f3 f1 f2 f1 The End.")); err != nil {
56 t.Errorf("Multi transformer chain returned an error: %s", err)
57 }
58
59 expected := "Test: f4r f3r f1r f2r f1r The End."
60
61 if out.String() != expected {
62 t.Errorf("Expected %s got %s", expected, out.String())
63 }
64 }
65
66 func TestNewEmptyTransforms(t *testing.T) {
67 c := qt.New(t)
68 transforms := NewEmpty()
69 c.Assert(cap(transforms), qt.Equals, 20)
70 }