game_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. game.scoreWordsByCommonLetterLocations()
  39. for word, score := range tt.want {
  40. if game.Words[word].Score != score {
  41. t.Errorf("Word %s has score %d, expected %d", word, game.Words[word].Score, score)
  42. }
  43. }
  44. })
  45. }
  46. }
  47. func TestGame_FilterWords(t *testing.T) {
  48. type fields struct {
  49. Words map[string]*Word
  50. Length int
  51. }
  52. type args struct {
  53. guess string
  54. score int
  55. }
  56. tests := []struct {
  57. name string
  58. fields fields
  59. args args
  60. want map[string]*Word
  61. }{
  62. {
  63. name: "no words filtered with score = 0",
  64. fields: fields{
  65. Words: map[string]*Word{
  66. "aaa": {Word: "aaa"},
  67. "bbb": {Word: "bbb"},
  68. },
  69. },
  70. args: args{guess: "ccc", score: 0},
  71. want: map[string]*Word{
  72. "aaa": {Word: "aaa"},
  73. "bbb": {Word: "bbb"},
  74. },
  75. },
  76. {
  77. name: "no words filtered with score = 1",
  78. fields: fields{
  79. Words: map[string]*Word{
  80. "aac": {Word: "aac"},
  81. "bbc": {Word: "bbc"},
  82. },
  83. },
  84. args: args{guess: "ccc", score: 1},
  85. want: map[string]*Word{
  86. "aac": {Word: "aac"},
  87. "bbc": {Word: "bbc"},
  88. },
  89. },
  90. {
  91. name: "some words filtered",
  92. fields: fields{
  93. Words: map[string]*Word{
  94. "aac": {Word: "aac"},
  95. "bbc": {Word: "bbc"},
  96. "ccc": {Word: "ccc"},
  97. },
  98. },
  99. args: args{guess: "ccc", score: 1},
  100. want: map[string]*Word{
  101. "aac": {Word: "aac"},
  102. "bbc": {Word: "bbc"},
  103. },
  104. },
  105. {
  106. name: "real world case",
  107. fields: fields{
  108. Words: map[string]*Word{
  109. "pleads": {Word: "pleads", Score: 13},
  110. "crimes": {Word: "crimes", Score: 16},
  111. "fierce": {Word: "fierce", Score: 12},
  112. "wagons": {Word: "wagons", Score: 14},
  113. "shiner": {Word: "shiner", Score: 14},
  114. "wastes": {Word: "wastes", Score: 20},
  115. "tables": {Word: "tables", Score: 17},
  116. "ripped": {Word: "ripped", Score: 14},
  117. "silver": {Word: "silver", Score: 16},
  118. "insane": {Word: "insane", Score: 13},
  119. "visage": {Word: "visage", Score: 15},
  120. },
  121. },
  122. args: args{guess: "wastes", score: 1},
  123. want: map[string]*Word{
  124. "pleads": {Word: "pleads", Score: 13},
  125. "insane": {Word: "insane", Score: 13},
  126. "shiner": {Word: "shiner", Score: 14},
  127. "visage": {Word: "visage", Score: 15},
  128. "silver": {Word: "silver", Score: 16},
  129. "ripped": {Word: "ripped", Score: 14},
  130. },
  131. },
  132. }
  133. for _, tt := range tests {
  134. t.Run(tt.name, func(t *testing.T) {
  135. g := Game{
  136. Words: tt.fields.Words,
  137. Length: tt.fields.Length,
  138. }
  139. g.FilterWords(tt.args.guess, tt.args.score)
  140. fmt.Printf("Want: %v\n", tt.want)
  141. fmt.Printf("Got: %v\n", g.Words)
  142. for word := range tt.want {
  143. if _, ok := g.Words[word]; !ok {
  144. t.Errorf("Word %s was removed, expected it to remain", word)
  145. }
  146. }
  147. assert.Equal(t, len(tt.want), len(g.Words), "Wrong number of matches")
  148. })
  149. }
  150. }
  151. func TestGame_getBestGuess(t *testing.T) {
  152. type fields struct {
  153. Words map[string]*Word
  154. Length int
  155. }
  156. tests := []struct {
  157. name string
  158. fields fields
  159. want string
  160. wantErr bool
  161. }{
  162. {
  163. name: "simple case",
  164. fields: fields{
  165. Words: map[string]*Word{
  166. "aaa": {Word: "aaa", Score: 3},
  167. "aab": {Word: "aab", Score: 2},
  168. "caa": {Word: "caa", Score: 2},
  169. },
  170. },
  171. want: "aaa",
  172. },
  173. {
  174. name: "real-world case",
  175. fields: fields{
  176. Words: map[string]*Word{
  177. "pleads": {Word: "pleads", Score: 13},
  178. "crimes": {Word: "crimes", Score: 16},
  179. "fierce": {Word: "fierce", Score: 12},
  180. "wagons": {Word: "wagons", Score: 14},
  181. "shiner": {Word: "shiner", Score: 14},
  182. "wastes": {Word: "wastes", Score: 20},
  183. "tables": {Word: "tables", Score: 17},
  184. "ripped": {Word: "ripped", Score: 14},
  185. "silver": {Word: "silver", Score: 16},
  186. "visage": {Word: "visage", Score: 15},
  187. "insane": {Word: "insane", Score: 13},
  188. },
  189. },
  190. want: "wastes",
  191. },
  192. }
  193. for _, tt := range tests {
  194. t.Run(tt.name, func(t *testing.T) {
  195. g := Game{
  196. Words: tt.fields.Words,
  197. Length: tt.fields.Length,
  198. }
  199. got := g.getBestGuess()
  200. if got != tt.want {
  201. t.Errorf("getBestGuess() got = %v, wantedLossCount %v", got, tt.want)
  202. }
  203. })
  204. }
  205. }
  206. func TestGame_getSortedScores(t *testing.T) {
  207. type fields struct {
  208. Words map[string]*Word
  209. }
  210. tests := []struct {
  211. name string
  212. fields fields
  213. want []string
  214. }{
  215. {
  216. name: "sort words by scores",
  217. fields: fields{
  218. Words: map[string]*Word{
  219. "pleads": {Word: "pleads", Score: 13},
  220. "crimes": {Word: "crimes", Score: 16},
  221. "fierce": {Word: "fierce", Score: 12},
  222. "shiner": {Word: "shiner", Score: 14},
  223. "wastes": {Word: "wastes", Score: 20},
  224. "tables": {Word: "tables", Score: 17},
  225. "visage": {Word: "visage", Score: 15},
  226. },
  227. },
  228. want: []string{"wastes", "tables", "crimes", "visage", "shiner", "pleads", "fierce"},
  229. },
  230. }
  231. for _, tt := range tests {
  232. t.Run(tt.name, func(t *testing.T) {
  233. g := Game{
  234. Words: tt.fields.Words,
  235. }
  236. assert.Equalf(t, tt.want, g.getSortedScores(), "getSortedScores()")
  237. })
  238. }
  239. }
  240. func TestGame_printSortedScores(t *testing.T) {
  241. type fields struct {
  242. Words map[string]*Word
  243. }
  244. tests := []struct {
  245. name string
  246. fields fields
  247. want []string
  248. }{
  249. {
  250. name: "sort words by scores",
  251. fields: fields{
  252. Words: map[string]*Word{
  253. "pleads": {Word: "pleads", Score: 13},
  254. "crimes": {Word: "crimes", Score: 16},
  255. "fierce": {Word: "fierce", Score: 12},
  256. "shiner": {Word: "shiner", Score: 14},
  257. "wastes": {Word: "wastes", Score: 20},
  258. "tables": {Word: "tables", Score: 17},
  259. "visage": {Word: "visage", Score: 15},
  260. },
  261. },
  262. want: []string{"wastes", "tables", "crimes", "visage", "shiner", "pleads", "fierce"},
  263. },
  264. }
  265. for _, tt := range tests {
  266. t.Run(tt.name, func(t *testing.T) {
  267. g := Game{
  268. Words: tt.fields.Words,
  269. }
  270. assert.Equalf(t, tt.want, g.printSortedScores(), "getSortedScores()")
  271. })
  272. }
  273. }