Example #1
0
File: main.go Project: cnaize/quiz
func loadWords(path string) (words.WordList, error) {
	var wordList words.WordList

	inFile, err := os.Open(path)
	if err != nil {
		return wordList, err
	}
	defer inFile.Close()

	scanner := bufio.NewScanner(inFile)
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		wordList.AddWord(scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		return wordList, err
	}

	return wordList, nil
}
Example #2
0
func TestQuiz(t *testing.T) {
	var wordList words.WordList
	wordList.AddWord("a")
	wordList.AddWord("b")
	wordList.AddWord("c")
	wordList.AddWord("df")
	wordList.AddWord("bdfc")
	wordList.AddWord("acdfe")

	res := words.HandleWord("df", wordList).(words.Result)
	if len(res) != 0 {
		t.Errorf("failed: df")
	}

	res = words.HandleWord("acdfe", wordList).(words.Result)
	if len(res) != 0 {
		t.Errorf("failed: acdfe")
	}

	res = words.HandleWord("bdfc", wordList).(words.Result)
	if len(res) != 1 || res[0] != "bdfc" {
		t.Errorf("failed: bdfc")
	}
}