Skip to content

Commit

Permalink
Added the new functionality to the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMcKenzieSmarty committed Sep 17, 2024
1 parent 681f4fa commit 6935d87
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Handler struct {
}

func (this *Handler) Handle(args []string) error {
if this.calculator == nil {
return fmt.Errorf("%w: calculator is required", ErrNilCalculator)
}
if len(args) != 2 {
return fmt.Errorf("%w: two args req (you provided %d", ErrTooFewArgs, len(args))
}
Expand All @@ -23,6 +26,7 @@ func (this *Handler) Handle(args []string) error {
if err != nil {
return fmt.Errorf("%w: first arg (%s) %w", ErrMalformedArgs, a, err)
}

b, err := strconv.Atoi(args[1])
if err != nil {
return fmt.Errorf("%w: first arg (%s) %w", ErrMalformedArgs, b, err)
Expand All @@ -37,7 +41,7 @@ func (this *Handler) Handle(args []string) error {
return nil
}

func NewHandler(output io.Writer, calculator calc.Addition) *Handler {
func NewHandler(output io.Writer, calculator calc.Calculator) *Handler {
return &Handler{
calculator: calculator,
output: output,
Expand All @@ -48,4 +52,5 @@ var (
ErrTooFewArgs = errors.New("\"Usage: <a> <b>\"")
ErrMalformedArgs = errors.New("invalid argument")
ErrOutputWriter = errors.New("output write failed")
ErrNilCalculator = errors.New("nil calculator")
)
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"log"
"os"

Expand All @@ -9,8 +10,18 @@ import (
)

func main() {
err := handler.NewHandler(os.Stdout, calc.Addition{}).Handle(os.Args[1:])
var op string
flag.StringVar(&op, "op", "+", "One of + - * /")
flag.Parse()
err := handler.NewHandler(os.Stdout, calculators[op]).Handle(flag.Args())
if err != nil {
log.Fatal(err)
}
}

var calculators = map[string]calc.Calculator{
"+": calc.Addition{},
"-": calc.Subtraction{},
"*": calc.Multiplication{},
"/": calc.Division{},
}
10 changes: 8 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"reflect"
"testing"

calc "calc-lib/calc"
handler "calc-lib/handler"
"github.com/JacobMcKenzieSmarty/calc-lib/calc"
"github.com/JacobMcKenzieSmarty/calc-lib/handler"
)

func assertEqual(t *testing.T, expected, actual any) {
Expand Down Expand Up @@ -51,6 +51,12 @@ func TestOutputWriterError(t *testing.T) {
assertError(t, taco, err)
}

func TestNilCalculator(t *testing.T) {
myHandler := handler.NewHandler(&bytes.Buffer{}, nil)
err := myHandler.Handle([]string{"1", "2"})
assertError(t, handler.ErrNilCalculator, err)
}

func TestHappyPath(t *testing.T) {
myBuffer := new(bytes.Buffer)
myHandler := handler.NewHandler(myBuffer, calc.Addition{})
Expand Down

0 comments on commit 6935d87

Please sign in to comment.