2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
2016-08-13 15:39:44 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
func TestParseRange(t *testing.T) {
|
|
|
|
{
|
|
|
|
i := ".."
|
|
|
|
r, _ := ParseRange(&i)
|
2015-01-11 18:01:24 +00:00
|
|
|
if r.begin != rangeEllipsis || r.end != rangeEllipsis {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", r)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
i := "3.."
|
|
|
|
r, _ := ParseRange(&i)
|
2015-01-11 18:01:24 +00:00
|
|
|
if r.begin != 3 || r.end != rangeEllipsis {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", r)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
i := "3..5"
|
|
|
|
r, _ := ParseRange(&i)
|
|
|
|
if r.begin != 3 || r.end != 5 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", r)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
i := "-3..-5"
|
|
|
|
r, _ := ParseRange(&i)
|
|
|
|
if r.begin != -3 || r.end != -5 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", r)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
i := "3"
|
|
|
|
r, _ := ParseRange(&i)
|
|
|
|
if r.begin != 3 || r.end != 3 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", r)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokenize(t *testing.T) {
|
|
|
|
// AWK-style
|
|
|
|
input := " abc: def: ghi "
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens := Tokenize(input, Delimiter{})
|
2017-06-02 04:25:35 +00:00
|
|
|
if tokens[0].text.ToString() != "abc: " || tokens[0].prefixLength != 2 {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.Errorf("%s", tokens)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With delimiter
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens = Tokenize(input, delimiterRegexp(":"))
|
2017-06-02 04:25:35 +00:00
|
|
|
if tokens[0].text.ToString() != " abc:" || tokens[0].prefixLength != 0 {
|
2017-07-19 17:44:30 +00:00
|
|
|
t.Error(tokens[0].text.ToString(), tokens[0].prefixLength)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-08-10 14:47:03 +00:00
|
|
|
|
|
|
|
// With delimiter regex
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens = Tokenize(input, delimiterRegexp("\\s+"))
|
2017-06-02 04:25:35 +00:00
|
|
|
if tokens[0].text.ToString() != " " || tokens[0].prefixLength != 0 ||
|
|
|
|
tokens[1].text.ToString() != "abc: " || tokens[1].prefixLength != 2 ||
|
|
|
|
tokens[2].text.ToString() != "def: " || tokens[2].prefixLength != 8 ||
|
|
|
|
tokens[3].text.ToString() != "ghi " || tokens[3].prefixLength != 14 {
|
2015-08-10 14:47:03 +00:00
|
|
|
t.Errorf("%s", tokens)
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransform(t *testing.T) {
|
|
|
|
input := " abc: def: ghi: jkl"
|
|
|
|
{
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens := Tokenize(input, Delimiter{})
|
2015-01-01 19:49:30 +00:00
|
|
|
{
|
|
|
|
ranges := splitNth("1,2,3")
|
|
|
|
tx := Transform(tokens, ranges)
|
2019-07-19 04:23:18 +00:00
|
|
|
if joinTokens(tx) != "abc: def: ghi: " {
|
2015-08-02 05:00:18 +00:00
|
|
|
t.Errorf("%s", tx)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ranges := splitNth("1..2,3,2..,1")
|
|
|
|
tx := Transform(tokens, ranges)
|
2015-08-02 05:00:18 +00:00
|
|
|
if string(joinTokens(tx)) != "abc: def: ghi: def: ghi: jklabc: " ||
|
|
|
|
len(tx) != 4 ||
|
2016-08-13 15:39:44 +00:00
|
|
|
tx[0].text.ToString() != "abc: def: " || tx[0].prefixLength != 2 ||
|
|
|
|
tx[1].text.ToString() != "ghi: " || tx[1].prefixLength != 14 ||
|
|
|
|
tx[2].text.ToString() != "def: ghi: jkl" || tx[2].prefixLength != 8 ||
|
|
|
|
tx[3].text.ToString() != "abc: " || tx[3].prefixLength != 2 {
|
2015-08-02 05:00:18 +00:00
|
|
|
t.Errorf("%s", tx)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens := Tokenize(input, delimiterRegexp(":"))
|
2015-01-01 19:49:30 +00:00
|
|
|
{
|
|
|
|
ranges := splitNth("1..2,3,2..,1")
|
|
|
|
tx := Transform(tokens, ranges)
|
2019-07-19 04:23:18 +00:00
|
|
|
if joinTokens(tx) != " abc: def: ghi: def: ghi: jkl abc:" ||
|
2015-08-02 05:00:18 +00:00
|
|
|
len(tx) != 4 ||
|
2016-08-13 15:39:44 +00:00
|
|
|
tx[0].text.ToString() != " abc: def:" || tx[0].prefixLength != 0 ||
|
|
|
|
tx[1].text.ToString() != " ghi:" || tx[1].prefixLength != 12 ||
|
|
|
|
tx[2].text.ToString() != " def: ghi: jkl" || tx[2].prefixLength != 6 ||
|
|
|
|
tx[3].text.ToString() != " abc:" || tx[3].prefixLength != 0 {
|
2015-08-02 05:00:18 +00:00
|
|
|
t.Errorf("%s", tx)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 10:32:44 +00:00
|
|
|
|
|
|
|
func TestTransformIndexOutOfBounds(t *testing.T) {
|
|
|
|
Transform([]Token{}, splitNth("1"))
|
|
|
|
}
|