gitea源码

event_test.go 950B

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