chunkOfSize_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. expectedChunks := 1
  51. chunk, count, err := checkChunks(testText, size)
  52. if err != nil {
  53. t.Fatal(err)
  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. }