formatter_test.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package explain
  14. import (
  15. "bytes"
  16. "testing"
  17. )
  18. func TestFormatterWrite(t *testing.T) {
  19. buf := bytes.Buffer{}
  20. f := Formatter{
  21. Writer: &buf,
  22. }
  23. f.Write("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
  24. // Indent creates a new Formatter
  25. f.Indent(5).Write("Morbi at turpis faucibus, gravida dolor ut, fringilla velit.")
  26. // So Indent(2) doesn't indent to 7 here.
  27. f.Indent(2).Write("Etiam maximus urna at tellus faucibus mattis.")
  28. want := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  29. Morbi at turpis faucibus, gravida dolor ut, fringilla velit.
  30. Etiam maximus urna at tellus faucibus mattis.
  31. `
  32. if buf.String() != want {
  33. t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), want)
  34. }
  35. }
  36. func TestFormatterWrappedWrite(t *testing.T) {
  37. buf := bytes.Buffer{}
  38. f := Formatter{
  39. Writer: &buf,
  40. Wrap: 50,
  41. }
  42. f.WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at turpis faucibus, gravida dolor ut, fringilla velit.")
  43. f.Indent(10).WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at turpis faucibus, gravida dolor ut, fringilla velit.")
  44. // Test long words (especially urls) on their own line.
  45. f.Indent(20).WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit. ThisIsAVeryLongWordThatDoesn'tFitOnALineOnItsOwn. Morbi at turpis faucibus, gravida dolor ut, fringilla velit.")
  46. want := `Lorem ipsum dolor sit amet, consectetur adipiscing
  47. elit. Morbi at turpis faucibus, gravida dolor ut,
  48. fringilla velit.
  49. Lorem ipsum dolor sit amet, consectetur
  50. adipiscing elit. Morbi at turpis
  51. faucibus, gravida dolor ut, fringilla
  52. velit.
  53. Lorem ipsum dolor sit amet,
  54. consectetur adipiscing elit.
  55. ThisIsAVeryLongWordThatDoesn'tFitOnALineOnItsOwn.
  56. Morbi at turpis faucibus,
  57. gravida dolor ut, fringilla
  58. velit.
  59. `
  60. if buf.String() != want {
  61. t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), want)
  62. }
  63. }
  64. func TestDefaultWrap(t *testing.T) {
  65. buf := bytes.Buffer{}
  66. f := Formatter{
  67. Writer: &buf,
  68. // Wrap is not set
  69. }
  70. f.WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at turpis faucibus, gravida dolor ut, fringilla velit. Etiam maximus urna at tellus faucibus mattis.")
  71. want := `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at turpis faucibus, gravida dolor ut, fringilla velit. Etiam maximus urna at tellus faucibus mattis.
  72. `
  73. if buf.String() != want {
  74. t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), want)
  75. }
  76. }