| 1 | package rgbterm |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func bufsForTest(s string) (inputBuf *bytes.Buffer, outputBuf *bytes.Buffer) { |
| 9 | inputBuf = bytes.NewBufferString(s) |
| 10 | outputBuf = &bytes.Buffer{} |
| 11 | return |
| 12 | } |
| 13 | |
| 14 | func TestInterpretNoEscapes(t *testing.T) { |
| 15 | s := "No escapes here." |
| 16 | input, output := bufsForTest("No escapes here.") |
| 17 | |
| 18 | cnt := 0 |
| 19 | |
| 20 | subst := func(s string) []byte { |
| 21 | cnt++ |
| 22 | return nil |
| 23 | } |
| 24 | |
| 25 | err := interpret(input, output, subst) |
| 26 | |
| 27 | if err != nil { |
| 28 | t.FailNow() |
| 29 | } |
| 30 | |
| 31 | if cnt != 0 { |
| 32 | t.FailNow() |
| 33 | } |
| 34 | |
| 35 | if output.String() != s { |
| 36 | t.FailNow() |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestInterpretOutputBrace(t *testing.T) { |
| 41 | sin := "We have {{no} escapes here." |
| 42 | sout := "We have {no} escapes here." |
| 43 | input, output := bufsForTest(sin) |
| 44 | |
| 45 | cnt := 0 |
| 46 | |
| 47 | subst := func(s string) []byte { |
| 48 | cnt++ |
| 49 | return []byte{} |
| 50 | } |
| 51 | |
| 52 | err := interpret(input, output, subst) |
| 53 | |
| 54 | if err != nil { |
| 55 | t.FailNow() |
| 56 | } |
| 57 | |
| 58 | if cnt != 0 { |
| 59 | t.FailNow() |
| 60 | } |
| 61 | |
| 62 | if output.String() != sout { |
| 63 | t.FailNow() |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestInterpretEmptyReplace(t *testing.T) { |
| 68 | sin := "We have {some} escapes here." |
| 69 | sout := "We have escapes here." |
| 70 | input, output := bufsForTest(sin) |
| 71 | |
| 72 | cnt := 0 |
| 73 | |
| 74 | subst := func(s string) []byte { |
| 75 | cnt++ |
| 76 | return []byte{} |
| 77 | } |
| 78 | |
| 79 | err := interpret(input, output, subst) |
| 80 | if err != nil { |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | if cnt != 1 { |
| 85 | t.FailNow() |
| 86 | } |
| 87 | |
| 88 | if output.String() != sout { |
| 89 | t.FailNow() |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestInterpretNilReplace(t *testing.T) { |
| 94 | sin := "We have {some} escapes here." |
| 95 | sout := "We have escapes here." |
| 96 | input, output := bufsForTest(sin) |
| 97 | |
| 98 | cnt := 0 |
| 99 | |
| 100 | subst := func(s string) []byte { |
| 101 | if s != "some" { |
| 102 | t.FailNow() |
| 103 | } |
| 104 | cnt++ |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | err := interpret(input, output, subst) |
| 109 | |
| 110 | if err != nil { |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | if cnt != 1 { |
| 115 | t.FailNow() |
| 116 | } |
| 117 | |
| 118 | if output.String() != sout { |
| 119 | t.FailNow() |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestInterpretSomeReplaces(t *testing.T) { |
| 124 | sin := "We have {some} escapes {more} here." |
| 125 | sout := "We have more escapes more here." |
| 126 | input, output := bufsForTest(sin) |
| 127 | |
| 128 | cnt := 0 |
| 129 | |
| 130 | subst := func(s string) []byte { |
| 131 | if s != "some" && s != "more" { |
| 132 | t.FailNow() |
| 133 | } |
| 134 | cnt++ |
| 135 | return []byte("more") |
| 136 | } |
| 137 | |
| 138 | err := interpret(input, output, subst) |
| 139 | |
| 140 | if err != nil { |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | if cnt != 2 { |
| 145 | t.FailNow() |
| 146 | } |
| 147 | |
| 148 | if output.String() != sout { |
| 149 | t.FailNow() |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestInterpretReplaceTooLong(t *testing.T) { |
| 154 | sin := "We have {#0a0a0a,#0a0a0a,}" |
| 155 | input, output := bufsForTest(sin) |
| 156 | |
| 157 | cnt := 0 |
| 158 | |
| 159 | subst := func(s string) []byte { |
| 160 | cnt++ |
| 161 | return []byte{} |
| 162 | } |
| 163 | |
| 164 | err := interpret(input, output, subst) |
| 165 | |
| 166 | if err != nil { |
| 167 | return |
| 168 | } |
| 169 | |
| 170 | if cnt != 0 { |
| 171 | t.Log("Count is", cnt) |
| 172 | t.FailNow() |
| 173 | } |
| 174 | |
| 175 | if output.String() != sin { |
| 176 | t.Log("Str is", output.String()) |
| 177 | t.FailNow() |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func TestParseEmptyEscape(t *testing.T) { |
| 182 | fg, bg := parseEscape("") |
| 183 | |
| 184 | if fg != nil || bg != nil { |
| 185 | t.FailNow() |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestParseInvalidEscape(t *testing.T) { |
| 190 | fg, bg := parseEscape("invl") |
| 191 | |
| 192 | if fg != nil || bg != nil { |
| 193 | t.FailNow() |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestParseFgEscape(t *testing.T) { |
| 198 | fg, bg := parseEscape("#0a0501") |
| 199 | |
| 200 | if fg[0] != 0xa || fg[1] != 0x5 || fg[2] != 0x1 { |
| 201 | t.FailNow() |
| 202 | } |
| 203 | |
| 204 | if bg != nil { |
| 205 | t.FailNow() |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | func TestParseFgBgEscape(t *testing.T) { |
| 210 | fg, bg := parseEscape("#0a0501,#fffefd") |
| 211 | |
| 212 | if fg[0] != 0xa || fg[1] != 0x5 || fg[2] != 0x1 { |
| 213 | t.FailNow() |
| 214 | } |
| 215 | |
| 216 | if bg[0] != 0xff || bg[1] != 0xfe || bg[2] != 0xfd { |
| 217 | t.FailNow() |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func TestParseBgEscape(t *testing.T) { |
| 222 | fg, bg := parseEscape(",#fffefd") |
| 223 | |
| 224 | if fg != nil { |
| 225 | t.FailNow() |
| 226 | } |
| 227 | |
| 228 | if bg[0] != 0xff || bg[1] != 0xfe || bg[2] != 0xfd { |
| 229 | t.FailNow() |
| 230 | } |
| 231 | } |