2013-09-29 02:05:16 -04:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const HTML_WITH_NAV = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head></head>
|
|
|
|
<body>
|
|
|
|
<nav>
|
|
|
|
<ul class="nav navbar-nav">
|
2013-10-01 15:51:00 -04:00
|
|
|
<li hugo-nav="section_1"><a href="#">Section 1</a></li>
|
|
|
|
<li hugo-nav="section_2"><a href="#">Section 2</a></li>
|
2013-09-29 02:05:16 -04:00
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
const EXPECTED_HTML_WITH_NAV_1 = `<!DOCTYPE html><html><head></head>
|
|
|
|
<body>
|
|
|
|
<nav>
|
|
|
|
<ul class="nav navbar-nav">
|
2013-10-01 15:51:00 -04:00
|
|
|
<li hugo-nav="section_1"><a href="#">Section 1</a></li>
|
|
|
|
<li hugo-nav="section_2" class="active"><a href="#">Section 2</a></li>
|
2013-09-29 02:05:16 -04:00
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
|
|
|
|
</body></html>`
|
|
|
|
|
|
|
|
func TestSetNav(t *testing.T) {
|
2013-11-01 01:14:11 -04:00
|
|
|
trs := NavActive("section_2", "hugo-nav")
|
|
|
|
chain := NewChain(trs...)
|
2013-09-29 02:05:16 -04:00
|
|
|
out := new(bytes.Buffer)
|
2013-11-01 01:14:11 -04:00
|
|
|
if err := chain.Apply(out, strings.NewReader(HTML_WITH_NAV)); err != nil {
|
2013-09-29 02:05:16 -04:00
|
|
|
t.Errorf("Unexpected error in Apply() for NavActive: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := EXPECTED_HTML_WITH_NAV_1
|
|
|
|
if out.String() != expected {
|
|
|
|
t.Errorf("NavActive.Apply output expected and got:\n%q\n%q", expected, out.String())
|
|
|
|
}
|
|
|
|
}
|
2013-10-29 23:24:29 -04:00
|
|
|
|
|
|
|
func BenchmarkTransform(b *testing.B) {
|
2013-11-01 01:14:11 -04:00
|
|
|
tr := NavActive("section_2", "hugo-nav")
|
|
|
|
chain := NewChain(tr...)
|
|
|
|
out := new(bytes.Buffer)
|
2013-10-29 23:24:29 -04:00
|
|
|
for i := 0; i < b.N; i++ {
|
2013-11-01 01:14:11 -04:00
|
|
|
if err := chain.Apply(out, strings.NewReader(HTML_WITH_NAV)); err != nil {
|
2013-10-29 23:24:29 -04:00
|
|
|
b.Errorf("Unexpected error in Apply() for NavActive: %s", err)
|
|
|
|
}
|
2013-11-01 01:14:11 -04:00
|
|
|
out.Reset()
|
2013-10-29 23:24:29 -04:00
|
|
|
}
|
|
|
|
}
|