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.1KB

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