Skip to content

Commit 8f5688f

Browse files
committed
improve test coverage
1 parent 3599a5d commit 8f5688f

File tree

90 files changed

+890
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+890
-56
lines changed

.github/workflows/test.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Test & Coverage
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
# a single job to run tests and coverage for a Golang project; report coverage to Coveralls
11+
jobs:
12+
test:
13+
name: Test
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
- name: Setup Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: 1.21
22+
- name: Install dependencies
23+
run: go mod download
24+
- name: Run tests
25+
run: go test -v -vet=all -coverprofile=profile.cov ./...
26+
- name: Install goveralls
27+
run: go install github.com/mattn/goveralls@latest
28+
- name: Report coverage to Coveralls
29+
env:
30+
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
run: goveralls -coverprofile=covprofile -service=github

compiler/source_map_test.go

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package compiler
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSourceMap_Add(t *testing.T) {
9+
type args struct {
10+
t token
11+
destRange Range
12+
}
13+
tests := map[string]struct {
14+
args args
15+
toSource map[int]map[int]Position
16+
toTarget map[int]map[int]Position
17+
}{
18+
"first line": {
19+
args: args{
20+
t: token{
21+
lit: "foo",
22+
line: 1,
23+
col: 1,
24+
},
25+
destRange: Range{
26+
From: Position{Line: 1, Col: 1},
27+
To: Position{Line: 1, Col: 3},
28+
},
29+
},
30+
toSource: map[int]map[int]Position{
31+
0: {
32+
0: {Line: 0, Col: 0},
33+
1: {Line: 0, Col: 1},
34+
2: {Line: 0, Col: 2},
35+
3: {Line: 0, Col: 3},
36+
},
37+
},
38+
toTarget: map[int]map[int]Position{
39+
0: {
40+
0: {Line: 0, Col: 0},
41+
1: {Line: 0, Col: 1},
42+
2: {Line: 0, Col: 2},
43+
3: {Line: 0, Col: 3},
44+
},
45+
},
46+
},
47+
"non-first line": {
48+
args: args{
49+
t: token{
50+
lit: "foo",
51+
line: 2,
52+
col: 10,
53+
},
54+
destRange: Range{
55+
From: Position{Line: 5, Col: 3},
56+
To: Position{Line: 5, Col: 5},
57+
},
58+
},
59+
toSource: map[int]map[int]Position{
60+
1: {
61+
9: {Line: 4, Col: 2},
62+
10: {Line: 4, Col: 3},
63+
11: {Line: 4, Col: 4},
64+
12: {Line: 4, Col: 5},
65+
},
66+
},
67+
toTarget: map[int]map[int]Position{
68+
4: {
69+
2: {Line: 1, Col: 9},
70+
3: {Line: 1, Col: 10},
71+
4: {Line: 1, Col: 11},
72+
5: {Line: 1, Col: 12},
73+
},
74+
},
75+
},
76+
}
77+
for name, tt := range tests {
78+
t.Run(name, func(t *testing.T) {
79+
sm := &SourceMap{
80+
SourceLinesToTarget: make(map[int]map[int]Position),
81+
TargetLinesToSource: make(map[int]map[int]Position),
82+
}
83+
sm.Add(tt.args.t, tt.args.destRange)
84+
if !reflect.DeepEqual(tt.toSource, sm.SourceLinesToTarget) {
85+
t.Errorf("expected source lines to target to be %v, got %v", tt.toSource, sm.SourceLinesToTarget)
86+
}
87+
if !reflect.DeepEqual(tt.toTarget, sm.TargetLinesToSource) {
88+
t.Errorf("expected target lines to source to be %v, got %v", tt.toTarget, sm.TargetLinesToSource)
89+
}
90+
})
91+
}
92+
}
93+
94+
func TestSourceMap_SourcePositionFromTarget(t *testing.T) {
95+
sm := &SourceMap{
96+
SourceLinesToTarget: map[int]map[int]Position{
97+
0: {
98+
0: {Line: 0, Col: 0},
99+
1: {Line: 0, Col: 1},
100+
2: {Line: 0, Col: 2},
101+
3: {Line: 0, Col: 3},
102+
},
103+
4: {
104+
2: {Line: 1, Col: 9},
105+
3: {Line: 1, Col: 10},
106+
4: {Line: 1, Col: 11},
107+
5: {Line: 1, Col: 12},
108+
},
109+
},
110+
TargetLinesToSource: map[int]map[int]Position{
111+
0: {
112+
0: {Line: 0, Col: 0},
113+
1: {Line: 0, Col: 1},
114+
2: {Line: 0, Col: 2},
115+
3: {Line: 0, Col: 3},
116+
},
117+
1: {
118+
9: {Line: 4, Col: 2},
119+
10: {Line: 4, Col: 3},
120+
11: {Line: 4, Col: 4},
121+
12: {Line: 4, Col: 5},
122+
},
123+
},
124+
}
125+
type args struct {
126+
line int
127+
col int
128+
}
129+
tests := map[string]struct {
130+
args args
131+
expected Position
132+
ok bool
133+
}{
134+
"first line": {
135+
args: args{line: 0, col: 1},
136+
expected: Position{Line: 0, Col: 1},
137+
ok: true,
138+
},
139+
"not found": {
140+
args: args{line: 1, col: 1},
141+
},
142+
"non-first line": {
143+
args: args{line: 1, col: 10},
144+
expected: Position{Line: 4, Col: 3},
145+
ok: true,
146+
},
147+
}
148+
for name, tt := range tests {
149+
t.Run(name, func(t *testing.T) {
150+
got, ok := sm.SourcePositionFromTarget(tt.args.line, tt.args.col)
151+
if !reflect.DeepEqual(tt.expected, got) {
152+
t.Errorf("expected source position to be %v, got %v", tt.expected, got)
153+
}
154+
if tt.ok != ok {
155+
t.Errorf("expected ok to be %v, got %v", tt.ok, ok)
156+
}
157+
})
158+
}
159+
}
160+
161+
func TestSourceMap_TargetPositionFromSource(t *testing.T) {
162+
sm := &SourceMap{
163+
SourceLinesToTarget: map[int]map[int]Position{
164+
0: {
165+
0: {Line: 0, Col: 0},
166+
1: {Line: 0, Col: 1},
167+
2: {Line: 0, Col: 2},
168+
3: {Line: 0, Col: 3},
169+
},
170+
4: {
171+
2: {Line: 1, Col: 9},
172+
3: {Line: 1, Col: 10},
173+
4: {Line: 1, Col: 11},
174+
5: {Line: 1, Col: 12},
175+
},
176+
},
177+
TargetLinesToSource: map[int]map[int]Position{
178+
0: {
179+
0: {Line: 0, Col: 0},
180+
1: {Line: 0, Col: 1},
181+
2: {Line: 0, Col: 2},
182+
3: {Line: 0, Col: 3},
183+
},
184+
1: {
185+
9: {Line: 4, Col: 2},
186+
10: {Line: 4, Col: 3},
187+
11: {Line: 4, Col: 4},
188+
12: {Line: 4, Col: 5},
189+
},
190+
},
191+
}
192+
type args struct {
193+
line int
194+
col int
195+
}
196+
tests := map[string]struct {
197+
args args
198+
expected Position
199+
ok bool
200+
}{
201+
"first line": {
202+
args: args{line: 0, col: 1},
203+
expected: Position{Line: 0, Col: 1},
204+
ok: true,
205+
},
206+
"not found": {
207+
args: args{line: 1, col: 1},
208+
},
209+
"non-first line": {
210+
args: args{line: 4, col: 3},
211+
expected: Position{Line: 1, Col: 10},
212+
ok: true,
213+
},
214+
}
215+
for name, tt := range tests {
216+
t.Run(name, func(t *testing.T) {
217+
got, ok := sm.TargetPositionFromSource(tt.args.line, tt.args.col)
218+
if !reflect.DeepEqual(tt.expected, got) {
219+
t.Errorf("expected target position to be %v, got %v", tt.expected, got)
220+
}
221+
if tt.ok != ok {
222+
t.Errorf("expected ok to be %v, got %v", tt.ok, ok)
223+
}
224+
})
225+
}
226+
}

examples/attributes/additional.goht

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package attributes
12

23
// Additional attributes may be added to an element using the `@attributes`
34
// command. This command accepts a list of additional attributes in the

examples/attributes/additional.goht.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/attributes/classes.goht

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package attributes
12

23
// The class attribute is a bit special. You will often find yourself
34
// working with not one, but several classes. This is why repeating

examples/attributes/classes.goht.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/attributes/general.goht

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package attributes
2+
13
// Goht supports the Ruby 1.9 hash style of attributes. The other styles
24
// such as HTML style, or Ruby rocket style are not supported. This should
35
// not be a problem as the Ruby 1.9 style is very similar to the style used
@@ -23,7 +25,7 @@ var myDynamicValue = "foo"
2325
// You may include a comma after the last attribute if you wish but it is
2426
// not required.
2527

26-
@goht MultiLineAttrs() {
28+
@goht MultilineAttrs() {
2729
%p{
2830
class: #{myDynamicValue},
2931
id: "bar",

examples/attributes/general.goht.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/attributes/names.goht

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package attributes
2+
13
// For most attribute names you can include the name in the list
24
// of attributes just as you expect it to appear in the HTML. Names
35
// that contain alphanumeric characters, dashes (-), and

examples/attributes/names.goht.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/attributes/optional.goht

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package attributes
12

23
// Attributes may conditionally appear in the output. For example, you may
34
// want to add the `disabled` attribute to a button if a variable is true.

examples/attributes/optional.goht.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/commands/children.goht

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package commands
2+
13
// Any template can be included into another template; assuming that
24
// you have not created a circular reference, this will not cause the
35
// compiler to loop but will instead cause the generated code to

examples/commands/children.goht.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/commands/render.goht

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package commands
2+
13
// You include other templates using the `@render` command. It takes
24
// the name of the template to render.
35
// The `@render` command is used in combination with the rendering
@@ -15,5 +17,5 @@
1517
@goht RenderWithChildrenExample() {
1618
%p The other template will be rendered below.
1719
= @render ChildrenExample()
18-
%p this content will be rendered by the other template.
20+
%span this content will be rendered by the other template.
1921
}

0 commit comments

Comments
 (0)