File: compile_test.go

package info (click to toggle)
golang-github-yosssi-ace 0.0.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 484 kB
  • sloc: makefile: 21; sh: 1
file content (83 lines) | stat: -rw-r--r-- 2,402 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ace

import (
	"testing"
)

func TestLineCompile(t *testing.T) {
	for i, this := range []struct {
		template string
		expect   string
	}{
		{
			template: `a href=./foo foo`,
			expect:   `<a href="./foo">foo</a>`,
		},
		{
			template: `span.color-red red`,
			expect:   `<span class="color-red">red</span>`,
		},
		{
			template: `span#ref1 text`,
			expect:   `<span id="ref1">text</span>`,
		},
		{
			template: `span#ref1.color-red.text-big text`,
			expect:   `<span id="ref1" class="color-red text-big">text</span>`,
		},
		{
			template: `span.color-red#ref1.text-big text`,
			expect:   `<span id="ref1" class="color-red text-big">text</span>`,
		},
		{
			template: `#ref1 text`,
			expect:   `<div id="ref1">text</div>`,
		},
		{
			template: `#ref1.color-red.text-big text`,
			expect:   `<div id="ref1" class="color-red text-big">text</div>`,
		},
		{
			template: `.color-red#ref1.text-big text`,
			expect:   `<div id="ref1" class="color-red text-big">text</div>`,
		},
		{
			template: "div class=\"dialog {{ if eq .Attr `important` }}color-red{{end}}\" text",
			expect:   "<div class=\"dialog {{if eq .Attr `important`}}color-red{{end}}\">text</div>",
		},
		{
			template: "div class=\"dialog {{ if eq .Attr `important` }}color-red text-big{{end}}\" text",
			expect:   "<div class=\"dialog {{if eq .Attr `important`}}color-red text-big{{end}}\">text</div>",
		},
		{
			template: "div class=\"dialog {{ if eq .Attr \"important\" }}color-red{{end}}\" text",
			expect:   "<div class=\"dialog {{if eq .Attr \"important\"}}color-red{{end}}\">text</div>",
		},
		{
			template: "div class=\"dialog {{ if eq .Attr \"important\" }}color-red text-big{{end}}\" text",
			expect:   "<div class=\"dialog {{if eq .Attr \"important\"}}color-red text-big{{end}}\">text</div>",
		},
	} {
		name, filepath := "dummy", "dummy.ace"
		base := NewFile(filepath, []byte(this.template))
		inner := NewFile("", []byte{})

		src := NewSource(base, inner, []*File{})
		rslt, err := ParseSource(src, nil)
		if err != nil {
			t.Errorf("[%d] failed: %s", i, err)
			continue
		}

		tpl, err := CompileResult(name, rslt, nil)
		if err != nil {
			t.Errorf("[%d] failed: %s", i, err)
			continue
		}

		compiled := tpl.Lookup(name).Tree.Root.String()
		if compiled != this.expect {
			t.Errorf("[%d] Compiler didn't return an expected value, got %v but expected %v", i, compiled, this.expect)
		}
	}
}