chunkOfSize_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package cos
  2. import (
  3. "strings"
  4. "testing"
  5. "unicode/utf8"
  6. )
  7. func Test4ChunksOf100(t *testing.T) {
  8. size := 100
  9. expectedChunks := 4
  10. chunk := NewChunkOfSize(testText, size)
  11. count := 0
  12. for {
  13. text := chunk.Next()
  14. if text == "" {
  15. break
  16. }
  17. if utf8.RuneCountInString(text) > size {
  18. t.Fatal(text, "\nis longer than", size)
  19. }
  20. count++
  21. }
  22. if count != expectedChunks {
  23. t.Fatal("There should be", expectedChunks, "chunks, but have", count)
  24. }
  25. if !chunk.Success() {
  26. t.Fatal("There were errors:\n", strings.Join(chunk.GetErrors(), "\n"))
  27. }
  28. }
  29. func TestWordBiggerThanLimit(t *testing.T) {
  30. size := 4
  31. chunk := NewChunkOfSize(testText, size)
  32. text := chunk.Next()
  33. if text != "" {
  34. t.Fatal("Chunk should be empty, but is:", text)
  35. }
  36. if chunk.Success() || len(chunk.GetErrors()) == 0 {
  37. t.Fatal("There should be errors!")
  38. }
  39. }
  40. func TestTextShorterThanLimit(t *testing.T) {
  41. size := 400
  42. expectedChunks := 1
  43. chunk := NewChunkOfSize(testText, size)
  44. count := 0
  45. for {
  46. text := chunk.Next()
  47. if text == "" {
  48. break
  49. }
  50. if utf8.RuneCountInString(text) > size {
  51. t.Fatal(text, "\nis longer than", size)
  52. }
  53. count++
  54. }
  55. if count != expectedChunks {
  56. t.Fatal("There should be", expectedChunks, "chunks, but have", count)
  57. }
  58. if !chunk.Success() {
  59. t.Fatal("There were errors:\n", strings.Join(chunk.GetErrors(), "\n"))
  60. }
  61. }