replacingwriter_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package osutil
  16. import (
  17. "bytes"
  18. "fmt"
  19. "testing"
  20. )
  21. var testcases = []struct {
  22. from byte
  23. to []byte
  24. a, b string
  25. }{
  26. {'\n', []byte{'\r', '\n'}, "", ""},
  27. {'\n', []byte{'\r', '\n'}, "foo", "foo"},
  28. {'\n', []byte{'\r', '\n'}, "foo\n", "foo\r\n"},
  29. {'\n', []byte{'\r', '\n'}, "foo\nbar", "foo\r\nbar"},
  30. {'\n', []byte{'\r', '\n'}, "foo\nbar\nbaz", "foo\r\nbar\r\nbaz"},
  31. {'\n', []byte{'\r', '\n'}, "\nbar", "\r\nbar"},
  32. {'o', []byte{'x', 'l', 'r'}, "\nfoo", "\nfxlrxlr"},
  33. {'o', nil, "\nfoo", "\nf"},
  34. {'f', []byte{}, "\nfoo", "\noo"},
  35. }
  36. func TestReplacingWriter(t *testing.T) {
  37. for _, tc := range testcases {
  38. var buf bytes.Buffer
  39. w := ReplacingWriter{
  40. Writer: &buf,
  41. From: tc.from,
  42. To: tc.to,
  43. }
  44. fmt.Fprint(w, tc.a)
  45. if buf.String() != tc.b {
  46. t.Errorf("%q != %q", buf.String(), tc.b)
  47. }
  48. }
  49. }