2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2018-05-29 04:24:09 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-09-11 02:39:20 +08:00
|
|
|
"src.elv.sh/pkg/eval"
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
2018-05-29 04:24:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStyledSegment(t *testing.T) {
|
|
|
|
Test(t,
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled-segment abc &fg-color=cyan) bold)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[;1;36mabc\033[m"),
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled-segment (styled-segment abc &fg-color=magenta) &dim=$true) cyan)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[;2;36mabc\033[m"),
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled-segment abc &inverse=$true) inverse)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[;7mabc\033[m"),
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled-segment abc) toggle-inverse)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[;7mabc\033[m"),
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled-segment abc &inverse=$true) no-inverse)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[mabc"),
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled-segment abc &inverse=$true) toggle-inverse)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[mabc"),
|
2021-09-11 02:39:20 +08:00
|
|
|
|
|
|
|
That("styled-segment []").Throws(ErrorWithMessage(
|
|
|
|
"argument to styled-segment must be a string or a styled segment")),
|
|
|
|
That("styled-segment text &foo=bar").
|
|
|
|
Throws(ErrorWithMessage("unrecognized option 'foo'")),
|
2018-05-29 04:24:09 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-11 02:39:20 +08:00
|
|
|
func TestStyled(t *testing.T) {
|
2018-05-29 04:24:09 +08:00
|
|
|
Test(t,
|
2021-09-11 02:39:20 +08:00
|
|
|
// Transform string
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled abc bold)").Prints("\033[;1mabc\033[m"),
|
|
|
|
That("print (styled abc red cyan)").Prints("\033[;36mabc\033[m"),
|
|
|
|
That("print (styled abc bg-green)").Prints("\033[;42mabc\033[m"),
|
|
|
|
That("print (styled abc no-dim)").Prints("\033[mabc"),
|
2021-09-11 02:39:20 +08:00
|
|
|
|
|
|
|
// Transform already styled text
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled abc red) blue)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[;34mabc\033[m"),
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled abc italic) red)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[;3;31mabc\033[m"),
|
2018-07-06 08:32:42 +08:00
|
|
|
That("print (styled (styled abc inverse) inverse)").
|
2022-07-24 17:26:42 +08:00
|
|
|
Prints("\033[;7mabc\033[m"),
|
|
|
|
That("print (styled (styled abc inverse) no-inverse)").
|
|
|
|
Prints("\033[mabc"),
|
|
|
|
That("print (styled (styled abc inverse) toggle-inverse)").
|
|
|
|
Prints("\033[mabc"),
|
|
|
|
That("print (styled (styled abc inverse) toggle-inverse toggle-inverse)").
|
|
|
|
Prints("\033[;7mabc\033[m"),
|
2021-09-11 02:39:20 +08:00
|
|
|
|
|
|
|
// Function as transformer
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled abc {|s| put $s })").Prints("\033[mabc"),
|
|
|
|
That("print (styled abc {|s| styled-segment $s &bold=$true &italic=$false })").Prints("\033[;1mabc\033[m"),
|
|
|
|
That("print (styled abc italic {|s| styled-segment $s &bold=$true &italic=$false })").Prints("\033[;1mabc\033[m"),
|
2021-09-11 02:39:20 +08:00
|
|
|
|
2021-11-28 11:24:42 +08:00
|
|
|
That("styled abc {|_| fail bad }").Throws(eval.FailError{"bad"}),
|
|
|
|
That("styled abc {|_| put a b }").Throws(ErrorWithMessage(
|
2021-09-11 02:39:20 +08:00
|
|
|
"styling function must return a single segment; got 2 values")),
|
2021-11-28 11:24:42 +08:00
|
|
|
That("styled abc {|_| put [] }").Throws(ErrorWithMessage(
|
2021-09-11 02:39:20 +08:00
|
|
|
"styling function must return a segment; got list")),
|
|
|
|
|
|
|
|
// Bad usage
|
|
|
|
That("styled abc hopefully-never-exists").Throws(ErrorWithMessage(
|
|
|
|
"hopefully-never-exists is not a valid style transformer")),
|
|
|
|
That("styled []").Throws(ErrorWithMessage(
|
|
|
|
"expected string, styled segment or styled text; got list")),
|
|
|
|
That("styled abc []").Throws(ErrorWithMessage(
|
|
|
|
"need string or callable; got list")),
|
2018-05-29 04:24:09 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-07-15 21:46:40 +08:00
|
|
|
func TestStyled_DoesNotModifyArgument(t *testing.T) {
|
|
|
|
Test(t,
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var x = (styled text); var y = (styled $x red); put $x[0][fg-color]").
|
2018-07-15 21:46:40 +08:00
|
|
|
Puts("default"),
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var x = (styled-segment text); var y = (styled $x red); put $x[fg-color]").
|
2018-07-15 21:46:40 +08:00
|
|
|
Puts("default"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-29 04:24:09 +08:00
|
|
|
func TestStyledConcat(t *testing.T) {
|
|
|
|
Test(t,
|
2022-11-23 08:40:38 +08:00
|
|
|
// segment + string
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled-segment abc &fg-color=red)abc").Prints("\033[;31mabc\033[mabc"),
|
2022-11-23 08:40:38 +08:00
|
|
|
// segment + segment
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled-segment abc &bg-color=red)(styled-segment abc &fg-color=red)").Prints("\033[;41mabc\033[;31mabc\033[m"),
|
2022-11-23 08:40:38 +08:00
|
|
|
// segment + text
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled-segment abc &underlined=$true)(styled abc bright-cyan)").Prints("\033[;4mabc\033[;96mabc\033[m"),
|
2022-11-23 08:40:38 +08:00
|
|
|
// segment + num
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled-segment abc &blink)(num 44/3)").Prints("\033[;5mabc\033[m44/3"),
|
|
|
|
That("print (styled-segment abc &blink)(num 42)").Prints("\033[;5mabc\033[m42"),
|
2022-11-23 08:40:38 +08:00
|
|
|
// segment + unsupported
|
|
|
|
That("print (styled-segment abc){ }").Throws(ErrorWithMessage("cannot concatenate ui:text-segment and fn")),
|
|
|
|
// string + segment
|
|
|
|
That("print abc(styled-segment abc &fg-color=red)").Prints("\033[mabc\033[31mabc\033[m"),
|
|
|
|
// num + segment
|
|
|
|
That("print (num 99.0)(styled-segment abc &blink)").Prints("\033[m99.0\033[5mabc\033[m"),
|
|
|
|
That("print (num 66)(styled-segment abc &blink)").Prints("\033[m66\033[5mabc\033[m"),
|
|
|
|
That("print (num 3/2)(styled-segment abc &blink)").Prints("\033[m3/2\033[5mabc\033[m"),
|
|
|
|
// unsupported + segment
|
|
|
|
That("print { }(styled-segment abc)").Throws(ErrorWithMessage("cannot concatenate fn and ui:text-segment")),
|
|
|
|
|
|
|
|
// text + string
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled abc blink)abc").Prints("\033[;5mabc\033[mabc"),
|
2022-11-23 08:40:38 +08:00
|
|
|
// text + number
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled abc blink)(num 13)").Prints("\033[;5mabc\033[m13"),
|
|
|
|
That("print (styled abc blink)(num 3/4)").Prints("\033[;5mabc\033[m3/4"),
|
2022-11-23 08:40:38 +08:00
|
|
|
// text + segment
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled abc inverse)(styled-segment abc &bg-color=white)").Prints("\033[;7mabc\033[;47mabc\033[m"),
|
2022-11-23 08:40:38 +08:00
|
|
|
// text + text
|
2022-07-24 17:26:42 +08:00
|
|
|
That("print (styled abc bold)(styled abc dim)").Prints("\033[;1mabc\033[;2mabc\033[m"),
|
2022-11-23 08:40:38 +08:00
|
|
|
// text + unsupported
|
|
|
|
That("print (styled abc){ }").Throws(ErrorWithMessage("cannot concatenate ui:text and fn")),
|
|
|
|
// string + text
|
|
|
|
That("print abc(styled abc blink)").Prints("\033[mabc\033[5mabc\033[m"),
|
|
|
|
// number + text
|
|
|
|
That("print (num 13)(styled abc blink)").Prints("\033[m13\033[5mabc\033[m"),
|
|
|
|
That("print (num 4/3)(styled abc blink)").Prints("\033[m4/3\033[5mabc\033[m"),
|
|
|
|
// unsupported + text
|
|
|
|
That("print { }(styled abc)").Throws(ErrorWithMessage("cannot concatenate fn and ui:text")),
|
2018-05-29 04:24:09 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStyledIndexing(t *testing.T) {
|
|
|
|
Test(t,
|
|
|
|
That("put (styled-segment abc &italic=$true &fg-color=red)[bold]").Puts(false),
|
|
|
|
That("put (styled-segment abc &italic=$true &fg-color=red)[italic]").Puts(true),
|
|
|
|
That("put (styled-segment abc &italic=$true &fg-color=red)[fg-color]").Puts("red"),
|
|
|
|
)
|
|
|
|
|
|
|
|
Test(t,
|
|
|
|
That("put (styled abc red)[0][bold]").Puts(false),
|
|
|
|
That("put (styled abc red)[0][bg-color]").Puts("default"),
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var t = (styled-segment abc &underlined=$true)(styled abc bright-cyan); put $t[1][fg-color]").Puts("bright-cyan"),
|
|
|
|
That("var t = (styled-segment abc &underlined=$true)(styled abc bright-cyan); put $t[1][underlined]").Puts(false),
|
2018-05-29 04:24:09 +08:00
|
|
|
)
|
|
|
|
}
|