Skip to content

Commit 215584f

Browse files
committed
Add package: person
1 parent 200b373 commit 215584f

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

kadai4/hioki-daichi/person/person.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Package person is a package that manages processing around person.
3+
*/
4+
package person
5+
6+
import "github.com/gopherdojo/dojo3/kadai4/hioki-daichi/fortune"
7+
8+
// Person has Name and Fortune.
9+
type Person struct {
10+
Name string `json:"name"`
11+
Fortune fortune.Fortune `json:"fortune"`
12+
Errors []string `json:"-"`
13+
}
14+
15+
// NewPerson generates a new person.
16+
func NewPerson(n string, f fortune.Fortune) *Person {
17+
return &Person{
18+
Name: n,
19+
Fortune: f,
20+
}
21+
}
22+
23+
// Validate validates its own fields.
24+
func (p *Person) Validate() {
25+
if len(p.Name) > 32 {
26+
p.Errors = append(p.Errors, "Name is too long (maximum is 32 characters)")
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package person
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/gopherdojo/dojo3/kadai4/hioki-daichi/fortune"
8+
)
9+
10+
func TestPerson_NewPerson(t *testing.T) {
11+
expected := "*person.Person"
12+
13+
p := NewPerson("Gopher", fortune.Daikichi)
14+
15+
actual := reflect.TypeOf(p).String()
16+
if actual != expected {
17+
t.Errorf(`unexpected : expected: "%s" actual: "%s"`, expected, actual)
18+
}
19+
}
20+
21+
func TestPerson_Validate(t *testing.T) {
22+
expected := "Name is too long (maximum is 32 characters)"
23+
24+
p := NewPerson("123456789012345678901234567890123", fortune.Daikichi)
25+
p.Validate()
26+
27+
actual := p.Errors[0]
28+
if actual != expected {
29+
t.Errorf(`unexpected : expected: "%s" actual: "%s"`, expected, actual)
30+
}
31+
}

0 commit comments

Comments
 (0)