2015-03-18 16:59:14 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2016-09-28 16:06:35 +00:00
|
|
|
|
2016-10-24 00:44:56 +00:00
|
|
|
"github.com/junegunn/fzf/src/tui"
|
2015-03-18 16:59:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExtractColor(t *testing.T) {
|
2016-10-24 00:44:56 +00:00
|
|
|
assert := func(offset ansiOffset, b int32, e int32, fg tui.Color, bg tui.Color, bold bool) {
|
|
|
|
var attr tui.Attr
|
2016-09-28 16:06:35 +00:00
|
|
|
if bold {
|
2016-10-24 00:44:56 +00:00
|
|
|
attr = tui.Bold
|
2016-09-28 16:06:35 +00:00
|
|
|
}
|
2015-03-18 16:59:14 +00:00
|
|
|
if offset.offset[0] != b || offset.offset[1] != e ||
|
2016-09-28 16:06:35 +00:00
|
|
|
offset.color.fg != fg || offset.color.bg != bg || offset.color.attr != attr {
|
|
|
|
t.Error(offset, b, e, fg, bg, attr)
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
src := "hello world"
|
2015-07-22 05:19:45 +00:00
|
|
|
var state *ansiState
|
2015-03-18 16:59:14 +00:00
|
|
|
clean := "\x1b[0m"
|
2016-08-18 17:39:32 +00:00
|
|
|
check := func(assertion func(ansiOffsets *[]ansiOffset, state *ansiState)) {
|
2016-06-14 12:52:47 +00:00
|
|
|
output, ansiOffsets, newState := extractColor(src, state, nil)
|
2015-07-22 05:19:45 +00:00
|
|
|
state = newState
|
2015-08-02 05:00:18 +00:00
|
|
|
if output != "hello world" {
|
2016-11-13 17:13:16 +00:00
|
|
|
t.Errorf("Invalid output: %s %s", output, []rune(output))
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
fmt.Println(src, ansiOffsets, clean)
|
2015-07-22 05:19:45 +00:00
|
|
|
assertion(ansiOffsets, state)
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if offsets != nil {
|
2015-03-18 16:59:14 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2015-03-18 16:59:14 +00:00
|
|
|
src = "\x1b[0mhello world"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if offsets != nil {
|
2015-03-18 16:59:14 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2015-03-18 16:59:14 +00:00
|
|
|
src = "\x1b[1mhello world"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 1 {
|
2015-03-18 16:59:14 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 0, 11, -1, -1, true)
|
2015-03-18 16:59:14 +00:00
|
|
|
})
|
|
|
|
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2016-11-13 17:13:16 +00:00
|
|
|
src = "\x1b[1mhello \x1b[mw\x1b7o\x1b8r\x1b(Bl\x1b[2@d"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 1 {
|
2015-03-22 16:24:31 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 0, 6, -1, -1, true)
|
2015-03-22 16:24:31 +00:00
|
|
|
})
|
|
|
|
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2015-03-27 03:35:06 +00:00
|
|
|
src = "\x1b[1mhello \x1b[Kworld"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 1 {
|
2015-03-27 03:35:06 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 0, 11, -1, -1, true)
|
2015-03-27 03:35:06 +00:00
|
|
|
})
|
|
|
|
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2015-03-18 16:59:14 +00:00
|
|
|
src = "hello \x1b[34;45;1mworld"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 1 {
|
2015-03-18 16:59:14 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 6, 11, 4, 5, true)
|
2015-03-18 16:59:14 +00:00
|
|
|
})
|
|
|
|
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2015-03-18 16:59:14 +00:00
|
|
|
src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 1 {
|
2015-03-18 16:59:14 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 6, 11, 4, 5, true)
|
2015-03-18 16:59:14 +00:00
|
|
|
})
|
|
|
|
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2015-03-18 16:59:14 +00:00
|
|
|
src = "hello \x1b[34;45;1mwor\x1b[0mld"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 1 {
|
2015-03-18 16:59:14 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 6, 9, 4, 5, true)
|
2015-03-18 16:59:14 +00:00
|
|
|
})
|
|
|
|
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2015-03-18 16:59:14 +00:00
|
|
|
src = "hello \x1b[34;48;5;233;1mwo\x1b[38;5;161mr\x1b[0ml\x1b[38;5;161md"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 3 {
|
2015-03-18 16:59:14 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 6, 8, 4, 233, true)
|
|
|
|
assert((*offsets)[1], 8, 9, 161, 233, true)
|
|
|
|
assert((*offsets)[2], 10, 11, 161, -1, false)
|
2015-03-18 16:59:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// {38,48};5;{38,48}
|
2015-07-22 05:19:45 +00:00
|
|
|
state = nil
|
2015-03-18 16:59:14 +00:00
|
|
|
src = "hello \x1b[38;5;38;48;5;48;1mwor\x1b[38;5;48;48;5;38ml\x1b[0md"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 2 {
|
2015-03-18 16:59:14 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 6, 9, 38, 48, true)
|
|
|
|
assert((*offsets)[1], 9, 10, 48, 38, true)
|
2015-03-18 16:59:14 +00:00
|
|
|
})
|
2015-07-22 05:19:45 +00:00
|
|
|
|
|
|
|
src = "hello \x1b[32;1mworld"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 1 {
|
2015-07-22 05:19:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-09-28 16:06:35 +00:00
|
|
|
if state.fg != 2 || state.bg != -1 || state.attr == 0 {
|
2015-07-22 05:19:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 6, 11, 2, -1, true)
|
2015-07-22 05:19:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
src = "hello world"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 1 {
|
2015-07-22 05:19:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-09-28 16:06:35 +00:00
|
|
|
if state.fg != 2 || state.bg != -1 || state.attr == 0 {
|
2015-07-22 05:19:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 0, 11, 2, -1, true)
|
2015-07-22 05:19:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
src = "hello \x1b[0;38;5;200;48;5;100mworld"
|
2016-08-18 17:39:32 +00:00
|
|
|
check(func(offsets *[]ansiOffset, state *ansiState) {
|
|
|
|
if len(*offsets) != 2 {
|
2015-07-22 05:19:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-09-28 16:06:35 +00:00
|
|
|
if state.fg != 200 || state.bg != 100 || state.attr > 0 {
|
2015-07-22 05:19:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
assert((*offsets)[0], 0, 6, 2, -1, true)
|
|
|
|
assert((*offsets)[1], 6, 11, 200, 100, false)
|
2015-07-22 05:19:45 +00:00
|
|
|
})
|
2015-03-18 16:59:14 +00:00
|
|
|
}
|