fallout_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package main
  2. import (
  3. "testing"
  4. )
  5. func Test_scoreWordsByCommonLetterLocations(t *testing.T) {
  6. type args struct {
  7. words []string
  8. }
  9. tests := []struct {
  10. name string
  11. args args
  12. want map[string]int
  13. }{
  14. {
  15. name: "no letters in common",
  16. args: args{words: []string{"aaa", "bbb"}},
  17. want: map[string]int{"aaa": 3, "bbb": 3},
  18. },
  19. {
  20. name: "one letter in common",
  21. args: args{words: []string{"aaa", "abb"}},
  22. want: map[string]int{"aaa": 4, "abb": 4},
  23. },
  24. {
  25. name: "three Words with some letters in common",
  26. args: args{words: []string{"aaa", "abb", "aab"}},
  27. want: map[string]int{"aaa": 6, "abb": 6, "aab": 7},
  28. },
  29. }
  30. for _, tt := range tests {
  31. t.Run(tt.name, func(t *testing.T) {
  32. game := NewGame()
  33. for _, word := range tt.args.words {
  34. game.Words[word] = &Word{Word: word}
  35. }
  36. err := game.scoreWordsByCommonLetterLocations()
  37. if err != nil {
  38. t.Errorf("Error scoring Words: %s\n", err)
  39. t.FailNow()
  40. }
  41. for word, score := range tt.want {
  42. if game.Words[word].Score != score {
  43. t.Errorf("Word %s has score %d, expected %d", word, game.Words[word].Score, score)
  44. }
  45. }
  46. })
  47. }
  48. }
  49. func TestWord_MatchesGuess(t *testing.T) {
  50. type fields struct {
  51. Word string
  52. Score int
  53. }
  54. type args struct {
  55. guess string
  56. numMatchingChars int
  57. }
  58. tests := []struct {
  59. name string
  60. fields fields
  61. args args
  62. want bool
  63. }{
  64. {
  65. name: "no letters in common",
  66. fields: fields{Word: "aaa"},
  67. args: args{guess: "bbb", numMatchingChars: 0},
  68. want: true,
  69. },
  70. {
  71. name: "one letter in common",
  72. fields: fields{Word: "aaa"},
  73. args: args{guess: "abb", numMatchingChars: 1},
  74. want: true,
  75. },
  76. {
  77. name: "one letter in common where expecting 2",
  78. fields: fields{Word: "aaa"},
  79. args: args{guess: "aba", numMatchingChars: 1},
  80. want: false,
  81. },
  82. }
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. w := Word{
  86. Word: tt.fields.Word,
  87. Score: tt.fields.Score,
  88. }
  89. if got := w.MatchesGuess(tt.args.guess, tt.args.numMatchingChars); got != tt.want {
  90. t.Errorf("MatchesGuess() = %v, want %v", got, tt.want)
  91. }
  92. })
  93. }
  94. }
  95. func TestGame_FilterWords(t *testing.T) {
  96. type fields struct {
  97. Words map[string]*Word
  98. Length int
  99. }
  100. type args struct {
  101. guess string
  102. score int
  103. }
  104. tests := []struct {
  105. name string
  106. fields fields
  107. args args
  108. want map[string]*Word
  109. }{
  110. {
  111. name: "no words filtered with score = 0",
  112. fields: fields{
  113. Words: map[string]*Word{
  114. "aaa": {Word: "aaa"},
  115. "bbb": {Word: "bbb"},
  116. },
  117. },
  118. args: args{guess: "ccc", score: 0},
  119. want: map[string]*Word{
  120. "aaa": {Word: "aaa"},
  121. "bbb": {Word: "bbb"},
  122. },
  123. },
  124. {
  125. name: "no words filtered with score = 1",
  126. fields: fields{
  127. Words: map[string]*Word{
  128. "aac": {Word: "aac"},
  129. "bbc": {Word: "bbc"},
  130. },
  131. },
  132. args: args{guess: "ccc", score: 1},
  133. want: map[string]*Word{
  134. "aac": {Word: "aac"},
  135. "bbc": {Word: "bbc"},
  136. },
  137. },
  138. {
  139. name: "some words filtered",
  140. fields: fields{
  141. Words: map[string]*Word{
  142. "aac": {Word: "aac"},
  143. "bbc": {Word: "bbc"},
  144. "ccc": {Word: "ccc"},
  145. },
  146. },
  147. args: args{guess: "ccc", score: 1},
  148. want: map[string]*Word{
  149. "aac": {Word: "aac"},
  150. "bbc": {Word: "bbc"},
  151. },
  152. },
  153. }
  154. for _, tt := range tests {
  155. t.Run(tt.name, func(t *testing.T) {
  156. g := Game{
  157. Words: tt.fields.Words,
  158. Length: tt.fields.Length,
  159. }
  160. g.FilterWords(tt.args.guess, tt.args.score)
  161. if len(g.Words) != len(tt.want) {
  162. t.Errorf("Expected %d Words, got %d", len(tt.want), len(g.Words))
  163. }
  164. for word := range tt.want {
  165. if _, ok := g.Words[word]; !ok {
  166. t.Errorf("Word %s was removed, expected it to remain", word)
  167. }
  168. }
  169. })
  170. }
  171. }