k_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package kata_test
  2. import (
  3. "strings"
  4. "testing"
  5. . "github.com/onsi/ginkgo"
  6. . "github.com/onsi/gomega"
  7. . "code.osinet.fr/fgm/codewars/kyu5/second_variation_on_caesar_cipher"
  8. )
  9. func doTest1(s string, shift int, exp []string) {
  10. var ans = Encode(s, shift)
  11. Expect(ans).To(Equal(exp))
  12. }
  13. func doTest2(arr []string, exp string) {
  14. var ans = Decode(arr)
  15. Expect(ans).To(Equal(exp))
  16. }
  17. var _ = Describe("Tests", func() {
  18. It("should handle basic cases Encode", func() {
  19. var u = "I should have known that you would have a perfect answer for me!!!"
  20. var v = []string{"ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!"}
  21. doTest1(u, 1, v)
  22. u = "abcdefghjuty12"
  23. v = []string{"abbc", "defg", "hikv", "uz12"}
  24. doTest1(u, 1, v)
  25. })
  26. It("should handle basic cases Decode", func() {
  27. var u = "I should have known that you would have a perfect answer for me!!!"
  28. var v = []string{"ijJ tipvme ibw", "f lopxo uibu z", "pv xpvme ibwf ", "b qfsgfdu botx", "fs gps nf!!!"}
  29. doTest2(v, u)
  30. })
  31. })
  32. func TestChunk(t *testing.T) {
  33. checks := [...]struct{ len, four, last int }{
  34. {45, 9, 9,},
  35. {46, 10, 6},
  36. {47, 10, 7},
  37. {48, 10, 8},
  38. {49, 10, 9},
  39. {50, 10, 10},
  40. {51, 11, 7},
  41. {52, 11, 8},
  42. {68, 14, 12},
  43. }
  44. for _, check := range checks {
  45. s := strings.Repeat(" ", check.len)
  46. actualFour := Chunk(s, 0)
  47. actualLast := Chunk(s, 4)
  48. if len(actualFour) != check.four {
  49. t.Errorf("len %d four expected %d got %d", check.len, check.four, len(actualFour))
  50. }
  51. if len(actualLast) != check.last {
  52. t.Errorf("len %d last expected %d got %d", check.len, check.last, len(actualLast))
  53. }
  54. }
  55. }
  56. func TestPrefix(t *testing.T) {
  57. checks := [...]struct {
  58. s string
  59. shift int
  60. expected string
  61. }{
  62. {"John", 1, "jk"},
  63. {"Yann", 2, "ya"},
  64. {"war", 4, "wa"},
  65. }
  66. for _, check := range checks {
  67. actual := string(Prefix(check.s, check.shift))
  68. if actual != check.expected {
  69. t.Errorf("%v by %d: expected %s, got %s", check.s, check.shift, check.expected, actual)
  70. }
  71. }
  72. }