package amber import ( "bytes" "strings" "testing" ) func Test_Doctype(t *testing.T) { res, err := run(`!!! 5`, nil) if err != nil { t.Fatal(err.Error()) } else { expect(res, ``, t) } } func Test_Nesting(t *testing.T) { res, err := run(`html head title body`, nil) if err != nil { t.Fatal(err.Error()) } else { expect(res, `
1
`, t) } } func Test_Mixin_NoArguments(t *testing.T) { res, err := run(` mixin a() p Testing +a()`, nil) if err != nil { t.Fatal(err.Error()) } else { expect(res, `Testing
`, t) } } func Test_Mixin_MultiArguments(t *testing.T) { res, err := run(` mixin a($a, $b, $c, $d) p #{$a} #{$b} #{$c} #{$d} +a("a", "b", "c", A)`, map[string]int{"A": 2}) if err != nil { t.Fatal(err.Error()) } else { expect(res, `a b c 2
`, t) } } func Test_ClassName(t *testing.T) { res, err := run(`div.test p.test1.test2 [class=$] .test3`, "test4") if err != nil { t.Fatal(err.Error()) } else { expect(res, `This is C
", t) } func Failing_Test_CompileDir(t *testing.T) { tmpl, err := CompileDir("samples/", DefaultDirOptions, DefaultOptions) // Test Compilation if err != nil { t.Fatal(err.Error()) } // Make sure files are added to map correctly val1, ok := tmpl["basic"] if ok != true || val1 == nil { t.Fatal("CompileDir, template not found.") } val2, ok := tmpl["inherit"] if ok != true || val2 == nil { t.Fatal("CompileDir, template not found.") } val3, ok := tmpl["compiledir_test/basic"] if ok != true || val3 == nil { t.Fatal("CompileDir, template not found.") } val4, ok := tmpl["compiledir_test/compiledir_test/basic"] if ok != true || val4 == nil { t.Fatal("CompileDir, template not found.") } // Make sure file parsing is the same var doc1, doc2 bytes.Buffer val1.Execute(&doc1, nil) val4.Execute(&doc2, nil) expect(doc1.String(), doc2.String(), t) // Check against CompileFile compilefile, err := CompileFile("samples/basic.amber", DefaultOptions) if err != nil { t.Fatal(err.Error()) } var doc3 bytes.Buffer compilefile.Execute(&doc3, nil) expect(doc1.String(), doc3.String(), t) expect(doc2.String(), doc3.String(), t) } func Benchmark_Parse(b *testing.B) { code := ` !!! 5 html head title Test Title body nav#mainNav[data-foo="bar"] div#content div.left div.center block center p Main Content .long ? somevar && someothervar div.right` for i := 0; i < b.N; i++ { cmp := New() cmp.Parse(code) } } func Benchmark_Compile(b *testing.B) { b.StopTimer() code := ` !!! 5 html head title Test Title body nav#mainNav[data-foo="bar"] div#content div.left div.center block center p Main Content .long ? somevar && someothervar div.right` cmp := New() cmp.Parse(code) b.StartTimer() for i := 0; i < b.N; i++ { cmp.CompileString() } } func expect(cur, expected string, t *testing.T) { if cur != expected { t.Fatalf("Expected {%s} got {%s}.", expected, cur) } } func run(tpl string, data interface{}) (string, error) { t, err := Compile(tpl, Options{false, false}) if err != nil { return "", err } var buf bytes.Buffer if err = t.Execute(&buf, data); err != nil { return "", err } return strings.TrimSpace(buf.String()), nil }