Skip to content

Commit 71ab1eb

Browse files
committed
First commit; Added testing; Refactored tests
0 parents  commit 71ab1eb

7 files changed

+129
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

calc/caclulator_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package calc
2+
3+
import "testing"
4+
5+
func TestAddition(t *testing.T) {
6+
assertEqual(t, 3, Addition{}.Calculate(1, 2))
7+
assertEqual(t, 4, Addition{}.Calculate(2, 2))
8+
}
9+
10+
func assertEqual(t *testing.T, expected, actual any) {
11+
if actual != expected {
12+
t.Errorf("Expected: %v -> Actual: %v", expected, actual)
13+
}
14+
}

calc/calculator.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package calc
2+
3+
type Calculator interface{ Calculate(a, b int) int }
4+
5+
type Addition struct {
6+
calculator Calculator
7+
}
8+
9+
func (Addition) Calculate(a, b int) int {
10+
return a + b
11+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/smartystreets/training-cohort/calc
2+
3+
go 1.22.5

handler.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"strconv"
7+
8+
"github.com/smartystreets/training-cohort/calc/calc"
9+
)
10+
11+
type Handler struct {
12+
output io.Writer
13+
calculator calc.Calculator
14+
}
15+
16+
func NewHandler(output io.Writer, calculator calc.Calculator) *Handler {
17+
return &Handler{output: output, calculator: calculator}
18+
}
19+
20+
func (this *Handler) Handle(values []string) (int, error) {
21+
if len(values) != 2 {
22+
return 0, errTooFewArgs
23+
}
24+
a, err := strconv.Atoi(values[0])
25+
if err != nil {
26+
return 0, errMalformedInput
27+
}
28+
29+
b, err := strconv.Atoi(values[1])
30+
if err != nil {
31+
return 0, errMalformedInput
32+
}
33+
return this.calculator.Calculate(a, b), err
34+
}
35+
36+
var (
37+
errTooFewArgs = fmt.Errorf("Too few arguments provided.")
38+
errMalformedInput = fmt.Errorf("Invalid argument type provided.")
39+
)

handler_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"os"
6+
"testing"
7+
8+
"github.com/smartystreets/training-cohort/calcy-lib/calc"
9+
)
10+
11+
func TestMissingInput(t *testing.T) {
12+
var values []string
13+
handle := NewHandler(os.Stdout, calc.Addition{})
14+
result, err := handle.Handle(values)
15+
if result != 0 {
16+
t.Error("Expected 0, got ", result)
17+
}
18+
if !errors.Is(err, errTooFewArgs) {
19+
t.Errorf("Expected %s", errTooFewArgs)
20+
}
21+
22+
}
23+
func TestMalformedInput(t *testing.T) {
24+
var values []string
25+
handle := NewHandler(os.Stdout, calc.Addition{})
26+
result, err := handle.Handle(values)
27+
if result != 0 {
28+
t.Error("Expected 0, got ", result)
29+
}
30+
if !errors.Is(err, errTooFewArgs) {
31+
t.Errorf("Expected %s", errMalformedInput)
32+
}
33+
}
34+
func TestCorrectInput(t *testing.T) {
35+
values := []string{"1", "2"}
36+
handle := NewHandler(os.Stdout, calc.Addition{})
37+
result, err := handle.Handle(values)
38+
if result != 0 {
39+
t.Error("Expected 3, got ", result)
40+
}
41+
if !errors.Is(err, nil) {
42+
t.Errorf("Expected %s", "")
43+
}
44+
}

main.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/smartystreets/training-cohort/calcy-lib/calc"
8+
)
9+
10+
func main() {
11+
handle := NewHandler(os.Stdout, calc.Addition{})
12+
result, err := handle.Handle(os.Args[1:])
13+
if err != nil {
14+
panic(err)
15+
}
16+
fmt.Printf("%d\n", result)
17+
}

0 commit comments

Comments
 (0)