Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Mar 30, 2024
1 parent 9bad4db commit 817dc4e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
12 changes: 12 additions & 0 deletions _testdata/test1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package testdata

func Func1(arg1 string) error { return nil }

// Func0 has no parameters.
func Func0() {}

var (
VarFoo int
VarBar string
varPriv float64
)
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/koron-go/srcdom

go 1.21

require github.com/google/go-cmp v0.6.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
105 changes: 105 additions & 0 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"runtime"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/koron-go/srcdom"
)

Expand Down Expand Up @@ -50,3 +52,106 @@ func TestReadGOROOT(t *testing.T) {
t.Fatal(err)
}
}

func TestReadFile(t *testing.T) {
got, err := srcdom.Read(filepath.Join("_testdata", "test1.go"))
if err != nil {
t.Fatal(err)
}
want := srcdom.Package{
Name: "testdata",
Funcs: []*srcdom.Func{
{
Name: "Func1",
Params: []*srcdom.Var{{Name: "arg1", Type: "string"}},
Results: []*srcdom.Var{{Type: "error"}},
},
{Name: "Func0"},
},
Values: []*srcdom.Value{
{Name: "VarFoo", Type: "int"},
{Name: "VarBar", Type: "string"},
{Name: "varPriv", Type: "float64"},
},
}
if d := cmp.Diff(&want, got, cmpopts.IgnoreUnexported(srcdom.Package{})); d != "" {
t.Errorf("unmatch srcdom.Package: -want +got\n%s", d)
}
pkg := got

t.Run("Value", func(t *testing.T) {
for _, c := range []struct {
name string
ispub bool
want srcdom.Value
}{
{"VarFoo", true, srcdom.Value{Name: "VarFoo", Type: "int"}},
{"VarBar", true, srcdom.Value{Name: "VarBar", Type: "string"}},
{"varPriv", false, srcdom.Value{Name: "varPriv", Type: "float64"}},
} {
got, ok := pkg.Value(c.name)
if !ok {
t.Errorf("value:%s not found", c.name)
continue
}
if d := cmp.Diff(&c.want, got); d != "" {
t.Errorf("value:%s unmatch: -want +got\n%s", c.name, d)
}
gotPub := got.IsPublic()
if gotPub != c.ispub {
t.Errorf("value:%s IsPublic unmatch: want=%t got=%t", c.name, c.ispub, gotPub)
}
}
})

t.Run("ValueNames", func(t *testing.T) {
got := pkg.ValueNames()
want := []string{
"VarBar",
"VarFoo",
"varPriv",
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("unmatch ValueNames() result: -want +got\n%s", d)
}
})

t.Run("Func", func(t *testing.T) {
for _, c := range []struct {
name string
ispub bool
want srcdom.Func
}{
{"Func1", true, srcdom.Func{
Name: "Func1",
Params: []*srcdom.Var{{Name: "arg1", Type: "string"}},
Results: []*srcdom.Var{{Type: "error"}},
}},
{"Func0", true, srcdom.Func{Name: "Func0"}},
} {
got, ok := pkg.Func(c.name)
if !ok {
t.Errorf("func:%s not found", c.name)
continue
}
if d := cmp.Diff(&c.want, got); d != "" {
t.Errorf("func:%s unmatch: -want +got\n%s", c.name, d)
}
gotPub := got.IsPublic()
if gotPub != c.ispub {
t.Errorf("func:%s IsPublic unmatch: want=%t got=%t", c.name, c.ispub, gotPub)
}
}
})

t.Run("FuncNames", func(t *testing.T) {
got := pkg.FuncNames()
want := []string{
"Func0",
"Func1",
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("unmatch FuncNames() result: -want +got\n%s", d)
}
})
}

0 comments on commit 817dc4e

Please sign in to comment.