game_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. func TestGame_scoreWordsByCommonLetterLocations(t *testing.T) {
  8. type args struct {
  9. words []string
  10. }
  11. tests := []struct {
  12. name string
  13. args args
  14. want map[string]int
  15. }{
  16. {
  17. name: "no letters in common",
  18. args: args{words: []string{"aaa", "bbb"}},
  19. want: map[string]int{"aaa": 3, "bbb": 3},
  20. },
  21. {
  22. name: "one letter in common",
  23. args: args{words: []string{"aaa", "abb"}},
  24. want: map[string]int{"aaa": 4, "abb": 4},
  25. },
  26. {
  27. name: "three Words with some letters in common",
  28. args: args{words: []string{"aaa", "abb", "aab"}},
  29. want: map[string]int{"aaa": 6, "abb": 6, "aab": 7},
  30. },
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. game := NewGame()
  35. for _, word := range tt.args.words {
  36. game.Words[word] = &Word{Word: word}
  37. }
  38. err := game.scoreWordsByCommonLetterLocations()
  39. if err != nil {
  40. t.Errorf("Error scoring Words: %s\n", err)
  41. t.FailNow()
  42. }
  43. for word, score := range tt.want {
  44. if game.Words[word].Score != score {
  45. t.Errorf("Word %s has score %d, expected %d", word, game.Words[word].Score, score)
  46. }
  47. }
  48. })
  49. }
  50. }
  51. func TestGame_FilterWords(t *testing.T) {
  52. type fields struct {
  53. Words map[string]*Word
  54. Length int
  55. }
  56. type args struct {
  57. guess string
  58. score int
  59. }
  60. tests := []struct {
  61. name string
  62. fields fields
  63. args args
  64. want map[string]*Word
  65. }{
  66. {
  67. name: "no words filtered with score = 0",
  68. fields: fields{
  69. Words: map[string]*Word{
  70. "aaa": {Word: "aaa"},
  71. "bbb": {Word: "bbb"},
  72. },
  73. },
  74. args: args{guess: "ccc", score: 0},
  75. want: map[string]*Word{
  76. "aaa": {Word: "aaa"},
  77. "bbb": {Word: "bbb"},
  78. },
  79. },
  80. {
  81. name: "no words filtered with score = 1",
  82. fields: fields{
  83. Words: map[string]*Word{
  84. "aac": {Word: "aac"},
  85. "bbc": {Word: "bbc"},
  86. },
  87. },
  88. args: args{guess: "ccc", score: 1},
  89. want: map[string]*Word{
  90. "aac": {Word: "aac"},
  91. "bbc": {Word: "bbc"},
  92. },
  93. },
  94. {
  95. name: "some words filtered",
  96. fields: fields{
  97. Words: map[string]*Word{
  98. "aac": {Word: "aac"},
  99. "bbc": {Word: "bbc"},
  100. "ccc": {Word: "ccc"},
  101. },
  102. },
  103. args: args{guess: "ccc", score: 1},
  104. want: map[string]*Word{
  105. "aac": {Word: "aac"},
  106. "bbc": {Word: "bbc"},
  107. },
  108. },
  109. {
  110. name: "real world case",
  111. fields: fields{
  112. Words: map[string]*Word{
  113. "pleads": {Word: "pleads", Score: 13},
  114. "crimes": {Word: "crimes", Score: 16},
  115. "fierce": {Word: "fierce", Score: 12},
  116. "wagons": {Word: "wagons", Score: 14},
  117. "shiner": {Word: "shiner", Score: 14},
  118. "wastes": {Word: "wastes", Score: 20},
  119. "tables": {Word: "tables", Score: 17},
  120. "ripped": {Word: "ripped", Score: 14},
  121. "silver": {Word: "silver", Score: 16},
  122. "insane": {Word: "insane", Score: 13},
  123. "visage": {Word: "visage", Score: 15},
  124. },
  125. },
  126. args: args{guess: "wastes", score: 1},
  127. want: map[string]*Word{
  128. "pleads": {Word: "pleads", Score: 13},
  129. "insane": {Word: "insane", Score: 13},
  130. "shiner": {Word: "shiner", Score: 14},
  131. "visage": {Word: "visage", Score: 15},
  132. "silver": {Word: "silver", Score: 16},
  133. "ripped": {Word: "ripped", Score: 14},
  134. },
  135. },
  136. }
  137. for _, tt := range tests {
  138. t.Run(tt.name, func(t *testing.T) {
  139. g := Game{
  140. Words: tt.fields.Words,
  141. Length: tt.fields.Length,
  142. }
  143. g.FilterWords(tt.args.guess, tt.args.score)
  144. fmt.Printf("Want: %v\n", tt.want)
  145. fmt.Printf("Got: %v\n", g.Words)
  146. for word := range tt.want {
  147. if _, ok := g.Words[word]; !ok {
  148. t.Errorf("Word %s was removed, expected it to remain", word)
  149. }
  150. }
  151. assert.Equal(t, len(tt.want), len(g.Words), "Wrong number of matches")
  152. })
  153. }
  154. }
  155. func TestGame_getBestGuess(t *testing.T) {
  156. type fields struct {
  157. Words map[string]*Word
  158. Length int
  159. }
  160. tests := []struct {
  161. name string
  162. fields fields
  163. want string
  164. wantErr bool
  165. }{
  166. {
  167. name: "simple case",
  168. fields: fields{
  169. Words: map[string]*Word{
  170. "aaa": {Word: "aaa", Score: 3},
  171. "aab": {Word: "aab", Score: 2},
  172. "caa": {Word: "caa", Score: 2},
  173. },
  174. },
  175. want: "aaa",
  176. },
  177. {
  178. name: "real-world case",
  179. fields: fields{
  180. Words: map[string]*Word{
  181. "pleads": {Word: "pleads", Score: 13},
  182. "crimes": {Word: "crimes", Score: 16},
  183. "fierce": {Word: "fierce", Score: 12},
  184. "wagons": {Word: "wagons", Score: 14},
  185. "shiner": {Word: "shiner", Score: 14},
  186. "wastes": {Word: "wastes", Score: 20},
  187. "tables": {Word: "tables", Score: 17},
  188. "ripped": {Word: "ripped", Score: 14},
  189. "silver": {Word: "silver", Score: 16},
  190. "visage": {Word: "visage", Score: 15},
  191. "insane": {Word: "insane", Score: 13},
  192. },
  193. },
  194. want: "wastes",
  195. },
  196. }
  197. for _, tt := range tests {
  198. t.Run(tt.name, func(t *testing.T) {
  199. g := Game{
  200. Words: tt.fields.Words,
  201. Length: tt.fields.Length,
  202. }
  203. got, err := g.getBestGuess()
  204. if (err != nil) != tt.wantErr {
  205. t.Errorf("getBestGuess() error = %v, wantErr %v", err, tt.wantErr)
  206. return
  207. }
  208. if got != tt.want {
  209. t.Errorf("getBestGuess() got = %v, want %v", got, tt.want)
  210. }
  211. })
  212. }
  213. }
  214. func TestGame_getSortedScores(t *testing.T) {
  215. type fields struct {
  216. Words map[string]*Word
  217. }
  218. tests := []struct {
  219. name string
  220. fields fields
  221. want []string
  222. }{
  223. {
  224. name: "sort words by scores",
  225. fields: fields{
  226. Words: map[string]*Word{
  227. "pleads": {Word: "pleads", Score: 13},
  228. "crimes": {Word: "crimes", Score: 16},
  229. "fierce": {Word: "fierce", Score: 12},
  230. "shiner": {Word: "shiner", Score: 14},
  231. "wastes": {Word: "wastes", Score: 20},
  232. "tables": {Word: "tables", Score: 17},
  233. "visage": {Word: "visage", Score: 15},
  234. },
  235. },
  236. want: []string{"wastes", "tables", "crimes", "visage", "shiner", "pleads", "fierce"},
  237. },
  238. }
  239. for _, tt := range tests {
  240. t.Run(tt.name, func(t *testing.T) {
  241. g := Game{
  242. Words: tt.fields.Words,
  243. }
  244. assert.Equalf(t, tt.want, g.getSortedScores(), "getSortedScores()")
  245. })
  246. }
  247. }