49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package people
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestPersonModel tests the Unmarshalling and properties.
|
|
func TestPersonModel(t *testing.T) {
|
|
p, err := Unmarshal(`{"name": "batman","identity": {"firstName": "bruce","lastName": "wayne"},"birthday": "1915-04-17"}`)
|
|
if err != nil {
|
|
t.Fatalf("Unmarshal() error = %v", err)
|
|
}
|
|
|
|
if p.Name != "batman" ||
|
|
p.Identity.FirstName != "bruce" ||
|
|
p.Identity.LastName != "wayne" ||
|
|
p.Birthday.Year() != 1915 {
|
|
t.Fatalf("Unmarshal() error")
|
|
}
|
|
|
|
if p.IsSuperHero() {
|
|
t.Fatalf("Batman has no superpowers")
|
|
}
|
|
|
|
js, err := p.Marshal()
|
|
if err != nil {
|
|
t.Fatalf("Marshal() error = %v", err)
|
|
}
|
|
|
|
if !strings.Contains(string(js), `"birthday":"1915-04-17"`) {
|
|
t.Fatalf("Marshal() birthday error")
|
|
}
|
|
|
|
// Batman with superpowers
|
|
p, err = Unmarshal(`{"name": "batman","identity": {"firstName": "bruce","lastName": "wayne"},"birthday": "1915-04-17","superpowers": ["money","intelligence"]}`)
|
|
if err != nil {
|
|
t.Fatalf("Unmarshal() error = %v", err)
|
|
}
|
|
|
|
if !p.IsSuperHero() || !p.Has("money") || !p.Has("intelligence") {
|
|
t.Fatalf("Batman has some superpowers as well")
|
|
}
|
|
|
|
if p.Has("flight") {
|
|
t.Fatalf("Batman has no superpowers")
|
|
}
|
|
}
|