You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event_test.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package eventsource
  4. import (
  5. "bytes"
  6. "testing"
  7. )
  8. func Test_wrapNewlines(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. prefix string
  12. value string
  13. output string
  14. }{
  15. {
  16. "check no new lines",
  17. "prefix: ",
  18. "value",
  19. "prefix: value\n",
  20. },
  21. {
  22. "check simple newline",
  23. "prefix: ",
  24. "value1\nvalue2",
  25. "prefix: value1\nprefix: value2\n",
  26. },
  27. {
  28. "check pathological newlines",
  29. "p: ",
  30. "\n1\n\n2\n3\n",
  31. "p: \np: 1\np: \np: 2\np: 3\np: \n",
  32. },
  33. }
  34. for _, tt := range tests {
  35. t.Run(tt.name, func(t *testing.T) {
  36. w := &bytes.Buffer{}
  37. gotSum, err := wrapNewlines(w, []byte(tt.prefix), []byte(tt.value))
  38. if err != nil {
  39. t.Errorf("wrapNewlines() error = %v", err)
  40. return
  41. }
  42. if gotSum != int64(len(tt.output)) {
  43. t.Errorf("wrapNewlines() = %v, want %v", gotSum, int64(len(tt.output)))
  44. }
  45. if gotW := w.String(); gotW != tt.output {
  46. t.Errorf("wrapNewlines() = %v, want %v", gotW, tt.output)
  47. }
  48. })
  49. }
  50. }