replacingwriter_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package osutil
  7. import (
  8. "bytes"
  9. "fmt"
  10. "testing"
  11. )
  12. var testcases = []struct {
  13. from byte
  14. to []byte
  15. a, b string
  16. }{
  17. {'\n', []byte{'\r', '\n'}, "", ""},
  18. {'\n', []byte{'\r', '\n'}, "foo", "foo"},
  19. {'\n', []byte{'\r', '\n'}, "foo\n", "foo\r\n"},
  20. {'\n', []byte{'\r', '\n'}, "foo\nbar", "foo\r\nbar"},
  21. {'\n', []byte{'\r', '\n'}, "foo\nbar\nbaz", "foo\r\nbar\r\nbaz"},
  22. {'\n', []byte{'\r', '\n'}, "\nbar", "\r\nbar"},
  23. {'o', []byte{'x', 'l', 'r'}, "\nfoo", "\nfxlrxlr"},
  24. {'o', nil, "\nfoo", "\nf"},
  25. {'f', []byte{}, "\nfoo", "\noo"},
  26. }
  27. func TestReplacingWriter(t *testing.T) {
  28. for _, tc := range testcases {
  29. var buf bytes.Buffer
  30. w := ReplacingWriter{
  31. Writer: &buf,
  32. From: tc.from,
  33. To: tc.to,
  34. }
  35. fmt.Fprint(w, tc.a)
  36. if buf.String() != tc.b {
  37. t.Errorf("%q != %q", buf.String(), tc.b)
  38. }
  39. }
  40. }