Skip to content

Commit

Permalink
test for map
Browse files Browse the repository at this point in the history
  • Loading branch information
fzerorubigd committed Mar 9, 2018
1 parent 7db68fd commit 2bcfec6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ func (m *MapType) String() string {
return fmt.Sprintf("map[%s]%s", m.Key.String(), m.Value.String())
}

// Package return the map package
func (m *MapType) Package() *Package {
return m.pkg
}

// Equal check if the type is equal?
func (m *MapType) Equal(t Type) bool {
v, ok := t.(*MapType)
if !ok {
Expand All @@ -28,7 +30,6 @@ func (m *MapType) Equal(t Type) bool {
if !m.pkg.Equal(v.pkg) {
return false
}

return m.Key.Equal(v.Key) && m.Value.Equal(m.Value)
}

Expand Down
49 changes: 49 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package humanize

import (
"fmt"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

var mp = `
package maptest
type (
MAP map[int]string
)
var m map[string]int
var n map[string]int
`

func TestMap(t *testing.T) {
Convey("Map parser test", t, func() {
var p = &Package{}
f, err := ParseFile(mp, p)
So(err, ShouldBeNil)

p.Files = append(p.Files, f)
So(p.Bind(), ShouldBeNil)
Convey("facts about MAP", func() {
mp, err := p.FindType("MAP")
So(err, ShouldBeNil)
So(mp.Name, ShouldEqual, "MAP")
So(mp.Type, ShouldHaveSameTypeAs, &MapType{})
So(mp.String(), ShouldEqual, "MAP map[int]string")
mpt := mp.Type.(*MapType)
So(mpt.Key, ShouldHaveSameTypeAs, &IdentType{})
So(mpt.Value, ShouldHaveSameTypeAs, &IdentType{})
So(mpt.Package().Equal(p), ShouldBeTrue)

m, err := p.FindVariable("m")
So(err, ShouldBeNil)
n, err := p.FindVariable("n")
So(err, ShouldBeNil)
fmt.Printf("%T", m.Type)
So(m.Type.Equal(n.Type), ShouldBeTrue)
})
})
}

0 comments on commit 2bcfec6

Please sign in to comment.