File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed
kadai4/hioki-daichi/person Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments