color_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package ircclient
  2. import (
  3. "testing"
  4. )
  5. type colortext struct {
  6. Text string
  7. Color int
  8. }
  9. func TestColor(t *testing.T) {
  10. var sent colortext
  11. var expect string
  12. var got string
  13. var plain string
  14. for sent, expect = range map[colortext]string{{Text: "One", Color: 1}: "\x0301One\x0f",
  15. {"Two", 2}: "\x0302Two\x0f",
  16. {"Three", 3}: "\x0303Three\x0f"} {
  17. got = ColorString(sent.Color, sent.Text)
  18. if got != expect {
  19. t.Errorf("Got %#v, expected %#v", got, expect)
  20. }
  21. plain = Stripper(got)
  22. if plain != sent.Text {
  23. t.Errorf("Got %#v, expected %#v", plain, sent.Text)
  24. }
  25. }
  26. }
  27. // DRY - Don't repeat yourself
  28. type Attr struct {
  29. Name string
  30. Func func(string) string
  31. Code string
  32. }
  33. func DryAttributeTest(t *testing.T, attr Attr) {
  34. var sent, expect, got, plain string
  35. var text []string = []string{"One", "Two", "Three"}
  36. for _, sent = range text {
  37. expect = attr.Code + sent + attr.Code
  38. got = attr.Func(sent)
  39. if got != expect {
  40. t.Errorf("%s: Got %#v, expected %#v", attr.Name, got, expect)
  41. }
  42. plain = Stripper(got)
  43. if plain != sent {
  44. t.Errorf("%s: Got %#v, expected %#v", attr.Name, got, expect)
  45. }
  46. }
  47. }
  48. func TestAttributes(t *testing.T) {
  49. Attrs := []Attr{
  50. {"Bold", Bold, "\x02"},
  51. {"Ttalics", Italics, "\x1d"},
  52. }
  53. for _, attr := range Attrs {
  54. DryAttributeTest(t, attr)
  55. }
  56. /*
  57. var sent string
  58. var expect string
  59. var got string
  60. var plain string
  61. for sent, expect = range map[string]string{"One": "\x02One\x02",
  62. "Two": "\x02Two\x02",
  63. "Three": "\x02Three\x02"} {
  64. got = Bold(sent)
  65. if got != expect {
  66. t.Errorf("Got %#v, expected %#v", got, expect)
  67. }
  68. plain = Stripper(got)
  69. if plain != sent {
  70. t.Errorf("Got %#v, expected %#v", plain, sent)
  71. }
  72. }
  73. */
  74. }
  75. func TestItalics(t *testing.T) {
  76. var sent string
  77. var expect string
  78. var got string
  79. var plain string
  80. for sent, expect = range map[string]string{"One": "\x1dOne\x1d",
  81. "Two": "\x1dTwo\x1d",
  82. "Three": "\x1dThree\x1d"} {
  83. got = Italics(sent)
  84. if got != expect {
  85. t.Errorf("Got %#v, expected %#v", got, expect)
  86. }
  87. plain = Stripper(got)
  88. if plain != sent {
  89. t.Errorf("Got %s, expected %s", plain, sent)
  90. }
  91. }
  92. }
  93. func TestUnderline(t *testing.T) {
  94. var sent string
  95. var expect string
  96. var got string
  97. var plain string
  98. for sent, expect = range map[string]string{"One": "\x16One\x16",
  99. "Two": "\x16Two\x16",
  100. "Three": "\x16Three\x16"} {
  101. got = Underline(sent)
  102. if got != expect {
  103. t.Errorf("Got %#v, expected %#v", got, expect)
  104. }
  105. plain = Stripper(got)
  106. if plain != sent {
  107. t.Errorf("Got %#v, expected %#v", plain, sent)
  108. }
  109. }
  110. }
  111. func TestStrike(t *testing.T) {
  112. var sent string
  113. var expect string
  114. var got string
  115. var plain string
  116. for sent, expect = range map[string]string{"One": "\x1eOne\x1e",
  117. "Two": "\x1eTwo\x1e",
  118. "Three": "\x1eThree\x1e"} {
  119. got = Strike(sent)
  120. if got != expect {
  121. t.Errorf("Got %#v, expected %#v", got, expect)
  122. }
  123. plain = Stripper(got)
  124. if plain != sent {
  125. t.Errorf("Got %#v, expected %#v", plain, sent)
  126. }
  127. }
  128. }
  129. func TestMono(t *testing.T) {
  130. var sent string
  131. var expect string
  132. var got string
  133. var plain string
  134. for sent, expect = range map[string]string{"One": "\x11One\x11",
  135. "Two": "\x11Two\x11",
  136. "Three": "\x11Three\x11"} {
  137. got = Monospace(sent)
  138. if got != expect {
  139. t.Errorf("Got %#v, expected %#v", got, expect)
  140. }
  141. plain = Stripper(got)
  142. if plain != sent {
  143. t.Errorf("Got %#v, expected %#v", plain, sent)
  144. }
  145. }
  146. }