strings_test.go 652 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package utils
  3. import (
  4. "bufio"
  5. "fmt"
  6. "strings"
  7. "testing"
  8. "github.com/google/go-cmp/cmp"
  9. )
  10. var _ = fmt.Print
  11. func TestStringScanner(t *testing.T) {
  12. for _, text := range []string{
  13. "a\nb\nc",
  14. "a\nb\nc\r",
  15. "a\n\n\nb\nc",
  16. "a\r\r\nb\r\nc\n",
  17. "\n1",
  18. "",
  19. "\n",
  20. } {
  21. actual := Splitlines(text)
  22. expected := make([]string, 0, len(actual))
  23. s := bufio.NewScanner(strings.NewReader(text))
  24. for s.Scan() {
  25. expected = append(expected, s.Text())
  26. }
  27. if diff := cmp.Diff(expected, actual); diff != "" {
  28. t.Fatalf("Failed for: %#v\n%s", text, diff)
  29. }
  30. }
  31. }