diff --git a/strategy.go b/strategy/strategy.go similarity index 65% rename from strategy.go rename to strategy/strategy.go index 9ef0c6e..09a5d81 100644 --- a/strategy.go +++ b/strategy/strategy.go @@ -1,6 +1,4 @@ -package main - -import "fmt" +package strategy type Operator interface { Apply(int, int) int @@ -25,15 +23,3 @@ type Addition struct{} func (Addition) Apply(lval, rval int) int { return lval + rval } - -func main() { - mult := Operation{Multiplication{}} - - // Outputs 15 - fmt.Println(mult.Operate(3, 5)) - - pow := Operation{Addition{}} - - // Outputs 8 - fmt.Println(pow.Operate(3, 5)) -} diff --git a/strategy/strategy_test.go b/strategy/strategy_test.go new file mode 100644 index 0000000..deadd4f --- /dev/null +++ b/strategy/strategy_test.go @@ -0,0 +1,18 @@ +package strategy + +import "testing" + +func TestMultiplicationStrategy(t *testing.T) { + mult := Operation{Multiplication{}} + + if res := mult.Operate(3, 5); res != 15 { + t.Errorf("Multiplication.Operate(3, 5) expected 15 got %q", res) + } +} + +func TestAdditionStrategy(t *testing.T) { + add := Operation{Addition{}} + if res := add.Operate(3, 5); res != 8 { + t.Errorf("Addition.Operate(3, 5) expected 8 got %q", res) + } +}