~wrycode/DWCHelper

d0ea99005dee0a88f4c9d9e88b9b9e7df396bd90 — Nick Econopouly 1 year, 2 days ago 96bf79a
Finish removeTerm, Remove and TestRemove
3 files changed, 20 insertions(+), 14 deletions(-)

M DWCHelper.go
M lib.go
M lib_test.go
M DWCHelper.go => DWCHelper.go +1 -10
@@ 150,16 150,7 @@ func importDB(filename string) database {
func removeTerm(term string, db database) database {
	if Include(db.terms, term) {
		fmt.Println("Removing",term)
		
		i := Index(db.terms, term)
		length := len(db.terms)
		if i == 0 {
			db.terms = db.terms[1:]
		} else if i == length - 1 {
			db.terms = db.terms[:i-1]
		} else {
			db.terms = append(db.terms[:i - 1], db.terms[i + 1:]...)
		}
		db.terms = Remove(db.terms, term)
	}
	return db
}

M lib.go => lib.go +12 -3
@@ 20,8 20,17 @@ func Include(terms []string, term string) bool {
// func Replace( terms []string, oldTerm, newTerm) []string {
// }

// Remove returns a []string with term removed, or unchanged if the
// term isn't in the slice
// Remove returns a []string with all instances of term removed, or
// unchanged if the term isn't in the slice
func Remove(terms []string, term string) []string {
	return []string{"b","c"}
	var result []string
	for _, t := range terms {
		if t != term {
			result = append(result, t)
		}
	}
	if result != nil {
		return result
	}
	return []string{}
}

M lib_test.go => lib_test.go +7 -1
@@ 14,13 14,19 @@ func TestRemove (t *testing.T) {
		out []string // returned slice
	}{
		{[]string{"a","b","c"}, "a", []string{"b","c"}},
		{[]string{"a","b","c"}, "b", []string{"a","c"}},
		{[]string{"a","b","c"}, "c", []string{"a","b"}},
		{[]string{"a","b","b","c"}, "b", []string{"a","c"}},
		{[]string{"a"}, "a", []string{}},
		{[]string{}, "a", []string{}},
		{[]string{"a","b","c"}, "x", []string{"a","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)
			t.Errorf("Remove(%v, %v): expected %v, got %v", tt.terms, tt.term, string(expected), string(result))
		}
	}
}