chunkOfSize_test.go 1.8 KB

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