Commit 7a1de8fe authored by Boris Mühmer's avatar Boris Mühmer
Browse files

switched to interfaces



Signed-off-by: default avatarBoris Mühmer <boris@muehmer.de>
parent 17a3e3a1
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
package wator

type celler interface {
// Celler is just a simple cell.
type Celler interface {
	Name() string
	String() string
}

// Food is not water, but somthing that can be consumed.
type Food interface {
	Celler
	Age() int
	SetAge(int)
}

// Hunter is something that goes for food.
type Hunter interface {
	Food
	Ate() int
	SetAte(int)
}
+14 −1
Original line number Diff line number Diff line
package wator
package fish

// Fish is the representation for a fish.
type Fish struct {
@@ -6,9 +6,22 @@ type Fish struct {
}

const (
	name = "Fish"
	fish = "·" // "f" // "🐟"
)

// New creates a new fish with a given age.
func New(age int) *Fish {
	return &Fish{
		age: age,
	}
}

// Name returns the name.
func (f *Fish) Name() string {
	return name
}

// String return the representation.
func (f *Fish) String() string {
	return fish

cell/fish/fish_test.go

0 → 100644
+29 −0
Original line number Diff line number Diff line
package fish

import "testing"

func TestFish(t *testing.T) {
	f := New(0)

	if f == nil {
		t.Errorf("failed to create a new fish")
	}

	if fn := f.Name(); fn != name {
		t.Errorf("f.Name() is %q, expected %q", fn, name)
	}

	if fs := f.String(); fs != fish {
		t.Errorf("f.String() = %s, expected %s", fs, fish)
	}

	if fa := f.Age(); fa != 0 {
		t.Errorf("f.Age() = %d, expected 0", fa)
	}

	na := 42
	f.SetAge(na)
	if fa := f.Age(); fa != na {
		t.Errorf("f.Age() = %d, expected %d", fa, na)
	}
}
+15 −1
Original line number Diff line number Diff line
package wator
package shark

// Shark is the representation for a shark.
type Shark struct {
@@ -7,9 +7,23 @@ type Shark struct {
}

const (
	name  = "Shark"
	shark = "O" // "S" // "🦈"
)

// New creates a new shark.
func New(age, ate int) *Shark {
	return &Shark{
		age: age,
		ate: ate,
	}
}

// Name ...
func (s *Shark) Name() string {
	return name
}

// String return the representation.
func (s *Shark) String() string {
	var i string
+9 −5
Original line number Diff line number Diff line
package wator
package shark

import "testing"

func TestShark(t *testing.T) {
	s := &Shark{}
	s := New(0, 0)

	if s == nil {
		t.Errorf("failed to create a new shark")
	}

	if sn := s.Name(); sn != name {
		t.Errorf("s.Name() is %q, expected %q", sn, name)
	}

	for _, tv := range []struct {
		a int
		s string
@@ -23,21 +27,21 @@ func TestShark(t *testing.T) {
	} {
		s.SetAge(tv.a)
		if si := s.String(); si != tv.s {
			t.Errorf("s.String() = %s, expected %s", si, tv.s)
			t.Errorf("s.String() is %q, expected %q", si, tv.s)
		}
	}

	for _, ta := range []int{0, 42} {
		s.SetAge(ta)
		if a := s.Age(); a != ta {
			t.Errorf("s.Age() = %d, expected %d", a, ta)
			t.Errorf("s.Age() is %d, expected %d", a, ta)
		}
	}

	for _, ta := range []int{0, 42} {
		s.SetAte(ta)
		if a := s.Ate(); a != ta {
			t.Errorf("s.Ate() = %d, expected %d", a, ta)
			t.Errorf("s.Ate() is %d, expected %d", a, ta)
		}
	}
}
Loading