proceed to learn go!
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package creditcards
|
||||
|
||||
import "errors"
|
||||
|
||||
|
||||
type card struct {
|
||||
number string
|
||||
}
|
||||
|
||||
func New(number string) (card,error) {
|
||||
if number == "" {
|
||||
return card{}, errors.New("number must not be empty")
|
||||
}
|
||||
return card{number},nil
|
||||
}
|
||||
|
||||
func (c *card) Number() string {
|
||||
return c.number
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package creditcards_test
|
||||
|
||||
import (
|
||||
"creditcards"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
t.Parallel()
|
||||
want := "1234567890"
|
||||
cc, err := creditcards.New(want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := cc.Number()
|
||||
if want != got {
|
||||
t.Errorf("want %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
func TestNewInvalidReturnsError(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := creditcards.New("")
|
||||
if err == nil {
|
||||
t.Fatal("want error for invalid card number, got nil")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module creditcards
|
||||
|
||||
go 1.24.4
|
||||
Reference in New Issue
Block a user