Beispiel #1
0
func TestChaingMultipleTransformers(t *testing.T) {
	f1 := func(ct contentTransformer) {
		ct.Write(bytes.Replace(ct.Content(), []byte("f1"), []byte("f1r"), -1))
	}
	f2 := func(ct contentTransformer) {
		ct.Write(bytes.Replace(ct.Content(), []byte("f2"), []byte("f2r"), -1))
	}
	f3 := func(ct contentTransformer) {
		ct.Write(bytes.Replace(ct.Content(), []byte("f3"), []byte("f3r"), -1))
	}

	f4 := func(ct contentTransformer) {
		ct.Write(bytes.Replace(ct.Content(), []byte("f4"), []byte("f4r"), -1))
	}

	tr := NewChain(f1, f2, f3, f4)

	out := new(bytes.Buffer)
	if err := tr.Apply(out, helpers.StringToReader("Test: f4 f3 f1 f2 f1 The End."), []byte("")); err != nil {
		t.Errorf("Multi transformer chain returned an error: %s", err)
	}

	expected := "Test: f4r f3r f1r f2r f1r The End."

	if string(out.Bytes()) != expected {
		t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
	}
}
Beispiel #2
0
func TestLiveReloadInject(t *testing.T) {
	out := new(bytes.Buffer)
	in := helpers.StringToReader("</body>")

	tr := NewChain(LiveReloadInject)
	tr.Apply(out, in, []byte("path"))

	expected := `<script data-no-instant>document.write('<script src="/livereload.js?mindelay=10"></' + 'script>')</script></body>`
	if string(out.Bytes()) != expected {
		t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
	}
}
func TestHugoGeneratorInject(t *testing.T) {
	hugoGeneratorTag = "META"
	for i, this := range []struct {
		in     string
		expect string
	}{
		{`<head>
	<foo />
</head>`, `<head>
	META
	<foo />
</head>`},
		{`<HEAD>
	<foo />
</HEAD>`, `<HEAD>
	META
	<foo />
</HEAD>`},
		{`<head><meta name="generator" content="Jekyll" /></head>`, `<head><meta name="generator" content="Jekyll" /></head>`},
		{`<head><meta name='generator' content='Jekyll' /></head>`, `<head><meta name='generator' content='Jekyll' /></head>`},
		{`<head><meta name=generator content=Jekyll /></head>`, `<head><meta name=generator content=Jekyll /></head>`},
		{`<head><META     NAME="GENERATOR" content="Jekyll" /></head>`, `<head><META     NAME="GENERATOR" content="Jekyll" /></head>`},
		{"", ""},
		{"</head>", "</head>"},
		{"<head>", "<head>\n\tMETA"},
	} {
		in := helpers.StringToReader(this.in)
		out := new(bytes.Buffer)

		tr := NewChain(HugoGeneratorInject)
		tr.Apply(out, in, []byte(""))

		if out.String() != this.expect {
			t.Errorf("[%d] Expected \n%q got \n%q", i, this.expect, out.String())
		}
	}

}
Beispiel #4
0
func TestFileBytes(t *testing.T) {
	assert.Equal(t, []byte("abc"), NewFileWithContents("a", helpers.StringToReader("abc")).Bytes())
	assert.Equal(t, []byte(""), NewFile("a").Bytes())
}
Beispiel #5
0
func TestFileString(t *testing.T) {
	assert.Equal(t, "abc", NewFileWithContents("a", helpers.StringToReader("abc")).String())
	assert.Equal(t, "", NewFile("a").String())
}