Skip to content

Commit

Permalink
Added Coverage to local testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Jul 24, 2024
1 parent 5e84d89 commit ac02039
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
63 changes: 63 additions & 0 deletions cmd/bfg/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"io"
"testing"
)

func TestInputReader(t *testing.T) {
table := []struct {
name string
path string
dflag bool
err bool
}{
{
"stdin test",
"",
true,
false,
},
{
"no stdin test",
"",
false,
true,
},
{
"file test",
"./main.go",
true,
false,
},
{
"bad file test",
"./file_not_found",
true,
true,
},
{
"explicit stdin test",
"-",
false,
false,
},
}

for _, v := range table {
t.Run(v.name, func(t *testing.T) {
buff, err := inputReader(v.path, v.dflag)

if err != nil && !v.err {
t.Errorf("Error thrown %v", err)
} else if err == nil && v.err {
t.Error("Error was expected")
} else if !v.err {
_, ok := buff.(io.ByteReader)
if !ok {
t.Error("no reader returned")
}
}
})
}
}
8 changes: 7 additions & 1 deletion parser/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ func TestExecuteSmall(t *testing.T) {
{opAddDp, -1},
{opDupVal, -1},
{opNoop, -2},
{opAddDp, -1},
{opOut, 1},
{opAddDp, -2},
{opSetVal, 32},
{opOut, 1},
{opIn, 1},
}
startdata := make([]int, 65536)
outputBuf := bufio.NewWriter(&bufferWriter{})
inputBuf := bufio.NewReader(strings.NewReader("no input."))
data := Execute(startdata, program, inputBuf, outputBuf)[:10]
want := []int{0, 0, 0, 0, 10, 10, 0, 0, 0, 0}
want := []int{0, 0, 0, 110, 10, 10, 0, 0, 0, 0}

if !reflect.DeepEqual(data, want) {
t.Errorf("got %v want %v", data, want)
Expand Down
5 changes: 4 additions & 1 deletion parser/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ func TestPrint(t *testing.T) {
{opMove, 2},
{opJmpZ, 7},
{opIn, 1},
{opSkip, -3},
{opSetVal, 2},
{opMulVal, 2},
{opNoop, 2},
{opDupVal, 1},
{opNoop, 2},
{opJmpNz, 5},
{opAddDp, 2},
{opOut, 1},
}
var buf bytes.Buffer
outputBuf := bufio.NewWriter(&buf)
Print(program, outputBuf)
got := buf.String()
want := " \n >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n\t [->>++<<]\n\t [->+>+<<]\n ]\n >>\n"
want := " \n >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n\t [<<<]\n\t ++\n\t [->>++<<]\n\t [->+>+<<]\n ]\n >>\n .\n"

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
Expand Down

0 comments on commit ac02039

Please sign in to comment.