chunkOfSize_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package cos
  2. import (
  3. "strings"
  4. "testing"
  5. "unicode/utf8"
  6. "fmt"
  7. )
  8. func checkChunks(text string, size int) (*chunkOfSize, int, error) {
  9. chunk := NewChunkOfSize(testText, size)
  10. count := 0
  11. for {
  12. text := chunk.Next()
  13. if text == "" {
  14. break
  15. }
  16. if utf8.RuneCountInString(text) > size {
  17. return chunk, -1, fmt.Errorf("'%s'\nis longer than %d", text, size)
  18. }
  19. count++
  20. }
  21. return chunk, count, nil
  22. }
  23. func Test4ChunksOf100(t *testing.T) {
  24. size := 100
  25. expectedChunks := 4
  26. chunk, count, err := checkChunks(testText, size)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if count != expectedChunks {
  31. t.Fatal("There should be", expectedChunks, "chunks, but have", count)
  32. }
  33. if !chunk.Success() {
  34. t.Fatal("There were errors:\n", strings.Join(chunk.GetErrors(), "\n"))
  35. }
  36. }
  37. func TestWordBiggerThanLimit(t *testing.T) {
  38. size := 4
  39. chunk := NewChunkOfSize(testText, size)
  40. text := chunk.Next()
  41. if text != "" {
  42. t.Fatal("Chunk should be empty, but is:", text)
  43. }
  44. if chunk.Success() || len(chunk.GetErrors()) == 0 {
  45. t.Fatal("There should be errors!")
  46. }
  47. }
  48. func TestTextShorterThanLimit(t *testing.T) {
  49. size := 400
  50. chunk := NewChunkOfSize(shorterText, size)
  51. if chunk != nil {
  52. t.Fatal("Chunk should be nil!")
  53. }
  54. }
  55. func checkWord(word string) bool {
  56. for i := range validWords {
  57. if word == validWords[i] {
  58. return true
  59. }
  60. }
  61. return false
  62. }
  63. func TestWordsNotCut(t *testing.T){
  64. size := 12
  65. chunk := NewChunkOfSize(shorterText, size)
  66. for {
  67. text := chunk.Next()
  68. if text == "" {
  69. break
  70. }
  71. words := strings.Split(text, " ")
  72. for i := range words {
  73. if !checkWord(words[i]) {
  74. t.Fatal(words[i], "isn't a valid word!")
  75. }
  76. }
  77. }
  78. }