term_writer_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2016 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package term
  27. import (
  28. "bytes"
  29. "strings"
  30. "testing"
  31. )
  32. const test = "Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube"
  33. func TestWordWrapWriter(t *testing.T) {
  34. testcases := map[string]struct {
  35. input string
  36. maxWidth uint
  37. }{
  38. "max 10": {input: test, maxWidth: 10},
  39. "max 80": {input: test, maxWidth: 80},
  40. "max 120": {input: test, maxWidth: 120},
  41. "max 5000": {input: test, maxWidth: 5000},
  42. }
  43. for k, tc := range testcases {
  44. b := bytes.NewBufferString("")
  45. w := NewWordWrapWriter(b, tc.maxWidth)
  46. _, err := w.Write([]byte(tc.input))
  47. if err != nil {
  48. t.Errorf("%s: Unexpected error: %v", k, err)
  49. }
  50. result := b.String()
  51. if !strings.Contains(result, "Kube") {
  52. t.Errorf("%s: Expected to contain \"Kube\"", k)
  53. }
  54. if len(result) < len(tc.input) {
  55. t.Errorf("%s: Unexpectedly short string, got %d wanted at least %d chars: %q", k, len(result), len(tc.input), result)
  56. }
  57. for _, line := range strings.Split(result, "\n") {
  58. if len(line) > int(tc.maxWidth) {
  59. t.Errorf("%s: Every line must be at most %d chars long, got %d: %q", k, tc.maxWidth, len(line), line)
  60. }
  61. }
  62. for _, word := range strings.Split(result, " ") {
  63. if !strings.Contains(word, "Kube") {
  64. t.Errorf("%s: Unexpected broken word: %q", k, word)
  65. }
  66. }
  67. }
  68. }
  69. func TestMaxWidthWriter(t *testing.T) {
  70. testcases := map[string]struct {
  71. input string
  72. maxWidth uint
  73. }{
  74. "max 10": {input: test, maxWidth: 10},
  75. "max 80": {input: test, maxWidth: 80},
  76. "max 120": {input: test, maxWidth: 120},
  77. "max 5000": {input: test, maxWidth: 5000},
  78. }
  79. for k, tc := range testcases {
  80. b := bytes.NewBufferString("")
  81. w := NewMaxWidthWriter(b, tc.maxWidth)
  82. _, err := w.Write([]byte(tc.input))
  83. if err != nil {
  84. t.Errorf("%s: Unexpected error: %v", k, err)
  85. }
  86. result := b.String()
  87. if !strings.Contains(result, "Kube") {
  88. t.Errorf("%s: Expected to contain \"Kube\"", k)
  89. }
  90. if len(result) < len(tc.input) {
  91. t.Errorf("%s: Unexpectedly short string, got %d wanted at least %d chars: %q", k, len(result), len(tc.input), result)
  92. }
  93. lines := strings.Split(result, "\n")
  94. for i, line := range lines {
  95. if len(line) > int(tc.maxWidth) {
  96. t.Errorf("%s: Every line must be at most %d chars long, got %d: %q", k, tc.maxWidth, len(line), line)
  97. }
  98. if i < len(lines)-1 && len(line) != int(tc.maxWidth) {
  99. t.Errorf("%s: Lines except the last one are expected to be exactly %d chars long, got %d: %q", k, tc.maxWidth, len(line), line)
  100. }
  101. }
  102. }
  103. }