Skip to content

Commit 2ba575d

Browse files
committed
Add unit tests
1 parent 5fe6715 commit 2ba575d

File tree

6 files changed

+142
-1
lines changed

6 files changed

+142
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ build: prepare
5151

5252
.PHONY: test
5353
test: prepare
54-
$(GOTEST) -race -v ./...
54+
$(GOTEST) -race -coverprofile /tmp/try-golang-cover $(shell go list ./... | grep -v /examples/ | grep -v /cmd/)
5555

5656
.PHONY: clean
5757
clean: prepare

builder/builder_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package builder_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/devlights/try-golang/builder"
7+
)
8+
9+
func TestMappingIsNotZero(t *testing.T) {
10+
cases := []struct {
11+
name string
12+
out interface{}
13+
}{
14+
{"sizeIsNotZero", 0},
15+
}
16+
17+
for _, c := range cases {
18+
t.Run(c.name, func(t *testing.T) {
19+
m := builder.BuildMappings()
20+
if len(m) == c.out {
21+
t.Errorf("[want] not zero\t[got] zero")
22+
}
23+
})
24+
}
25+
}

mapping/types_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package mapping_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/devlights/try-golang/mapping"
7+
)
8+
9+
type (
10+
onlyOneRegister struct{}
11+
)
12+
13+
func (me *onlyOneRegister) Regist(m mapping.ExampleMapping) {
14+
m["this_is_a_test"] = func() error { return nil }
15+
}
16+
17+
func TestMapping(t *testing.T) {
18+
m := mapping.NewSampleMapping()
19+
m.MakeMapping(&onlyOneRegister{})
20+
21+
_, ok := m["this_is_a_test"]
22+
if !ok {
23+
t.Errorf("no key.. [want] false\t[got] true")
24+
}
25+
}
26+
27+
func TestAllExampleNames(t *testing.T) {
28+
m := mapping.NewSampleMapping()
29+
m.MakeMapping(&onlyOneRegister{})
30+
31+
names := m.AllExampleNames()
32+
if len(names) != 1 {
33+
t.Errorf("mismatch example count..\t[want] 1\t[got] %d", len(names))
34+
}
35+
36+
name := names[0]
37+
if name != "this_is_a_test" {
38+
t.Errorf("mismatch example name..\t[want] this_is_a_test\t[got] %s", name)
39+
}
40+
}

runner/exec_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package runner_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/devlights/try-golang/mapping"
7+
"github.com/devlights/try-golang/runner"
8+
)
9+
10+
func TestExec(t *testing.T) {
11+
r := new(runner.SilentRegister)
12+
13+
m := mapping.NewSampleMapping()
14+
m.MakeMapping(r)
15+
16+
a := runner.NewExecArgs("silent", m)
17+
e := runner.NewExec(a)
18+
19+
if err := e.Run(); err != nil {
20+
t.Errorf("should not raise error (%s)", err)
21+
}
22+
23+
if !r.Target.Called {
24+
t.Errorf("never called the target example")
25+
}
26+
}

runner/export_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package runner
2+
3+
import "github.com/devlights/try-golang/mapping"
4+
5+
type (
6+
SilentExample struct {
7+
Called bool
8+
}
9+
SilentRegister struct {
10+
Target *SilentExample
11+
}
12+
)
13+
14+
func (me *SilentExample) Run() error {
15+
me.Called = true
16+
return nil
17+
}
18+
19+
func (me *SilentRegister) Regist(m mapping.ExampleMapping) {
20+
e := &SilentExample{false}
21+
m["silent"] = e.Run
22+
23+
me.Target = e
24+
}

runner/once_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package runner_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/devlights/try-golang/mapping"
7+
"github.com/devlights/try-golang/runner"
8+
)
9+
10+
func TestOnce(t *testing.T) {
11+
r := new(runner.SilentRegister)
12+
13+
m := mapping.NewSampleMapping()
14+
m.MakeMapping(r)
15+
16+
a := runner.NewOnceArgs("silent", m)
17+
e := runner.NewOnce(a)
18+
19+
if err := e.Run(); err != nil {
20+
t.Errorf("should not raise error (%s)", err)
21+
}
22+
23+
if !r.Target.Called {
24+
t.Errorf("never called the target example")
25+
}
26+
}

0 commit comments

Comments
 (0)