~wrycode/DWCHelper

96bf79a6174390e5420a28b3061a743341cdd9ac — Nick Econopouly 1 year, 5 days ago 98626ea
Begin writing unit tests.

Since hard research may rest on this project, we'll need to take
testing seriously. I began by starting a rudementary lib_test.go file
which will help me learn Go's testing style before I tackle the main
file; it's unfortunate that I didn't start with TDD at the beginning,
but DWCHelper.go is pretty functional and modular so writing tests
shouldn't be too difficult.
2 files changed, 36 insertions(+), 0 deletions(-)

M lib.go
A lib_test.go
M lib.go => lib.go +10 -0
@@ 15,3 15,13 @@ func Index(terms []string, s string) int {
func Include(terms []string, term string) bool {
	return Index(terms, term) >= 0
}

// Replace returns a []string with oldTerm replaced by newTerm
// func Replace( terms []string, oldTerm, newTerm) []string {
// }

// Remove returns a []string with term removed, or unchanged if the
// term isn't in the slice
func Remove(terms []string, term string) []string {
	return []string{"b","c"}
}

A lib_test.go => lib_test.go +26 -0
@@ 0,0 1,26 @@
package main

import (

	"testing"
	"encoding/json"
)

func TestRemove (t *testing.T) {

	var removeTests = []struct {
		terms []string // original slice
		term string // term to remove
		out []string // returned slice
	}{
		{[]string{"a","b","c"}, "a", []string{"b","c"}},
	}
	
	for _, tt := range removeTests {
		result, _ := json.Marshal(Remove(tt.terms, tt.term))
		expected, _ := json.Marshal(tt.out)
		if string(result) != string(expected) {
			t.Errorf("Remove(%v, %v): expected %v, got %v", tt.terms, tt.term, expected, result)
		}
	}
}