mirror of
https://github.com/mum4k/termdash.git
synced 2025-05-01 22:17:51 +08:00

Each set of options applies to a range of characters equal to the length of the text added. E.g. on write calls like: Write("text", opts1) Write("long text", opts2) The two passed options will be stored as: opts1 for byte range low:0, high:4 opts2 for byte range low:4, high:13 The text itself will be stored in a bytes.Buffer.
201 lines
3.5 KiB
Go
201 lines
3.5 KiB
Go
package text
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
"github.com/mum4k/termdash/cell"
|
|
)
|
|
|
|
func TestGivenWOpts(t *testing.T) {
|
|
tests := []struct {
|
|
desc string
|
|
given givenWOpts
|
|
pos int
|
|
want *optsRange
|
|
}{
|
|
{
|
|
desc: "no write options given results in defaults",
|
|
given: nil,
|
|
pos: 1,
|
|
want: &optsRange{
|
|
low: 0,
|
|
high: 0,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
desc: "multiple given options, position falls before them",
|
|
given: givenWOpts{
|
|
2: &optsRange{
|
|
low: 2,
|
|
high: 5,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorBlue,
|
|
},
|
|
},
|
|
},
|
|
5: &optsRange{
|
|
low: 5,
|
|
high: 10,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorRed,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pos: 1,
|
|
want: &optsRange{
|
|
low: 0,
|
|
high: 0,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
desc: "multiple given options, position falls on the lower",
|
|
given: givenWOpts{
|
|
2: &optsRange{
|
|
low: 2,
|
|
high: 5,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorBlue,
|
|
},
|
|
},
|
|
},
|
|
5: &optsRange{
|
|
low: 5,
|
|
high: 10,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorRed,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pos: 2,
|
|
want: &optsRange{
|
|
low: 2,
|
|
high: 5,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorBlue,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
desc: "multiple given options, position falls between them",
|
|
given: givenWOpts{
|
|
2: &optsRange{
|
|
low: 2,
|
|
high: 5,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorBlue,
|
|
},
|
|
},
|
|
},
|
|
5: &optsRange{
|
|
low: 5,
|
|
high: 10,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorRed,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pos: 4,
|
|
want: &optsRange{
|
|
low: 2,
|
|
high: 5,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorBlue,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
desc: "multiple given options, position falls on the higher",
|
|
given: givenWOpts{
|
|
2: &optsRange{
|
|
low: 2,
|
|
high: 5,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorBlue,
|
|
},
|
|
},
|
|
},
|
|
5: &optsRange{
|
|
low: 5,
|
|
high: 10,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorRed,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pos: 5,
|
|
want: &optsRange{
|
|
low: 5,
|
|
high: 10,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorRed,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
desc: "multiple given options, position falls after them",
|
|
given: givenWOpts{
|
|
2: &optsRange{
|
|
low: 2,
|
|
high: 5,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorBlue,
|
|
},
|
|
},
|
|
},
|
|
5: &optsRange{
|
|
low: 5,
|
|
high: 10,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{
|
|
FgColor: cell.ColorRed,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pos: 10,
|
|
want: &optsRange{
|
|
low: 0,
|
|
high: 0,
|
|
opts: &writeOptions{
|
|
cellOpts: &cell.Options{},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
got := tc.given.forPosition(tc.pos)
|
|
if diff := pretty.Compare(tc.want, got); diff != "" {
|
|
t.Errorf("forPosition => unexpected diff (-want, +got):\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|