2025-11-01 03:49:02 +01:00
|
|
|
package textedit_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"code.squareroundforest.org/arpio/textedit"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestIndent(t *testing.T) {
|
|
|
|
|
t.Run("no indent no wrap", func(t *testing.T) {
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("", ""))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write(nil)
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
if b.String() != "" {
|
|
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("new line", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("", ""))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("\n"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
if b.String() != "\n" {
|
|
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("basic", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("", ""))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar\n baz\nqux quux"))
|
|
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "foo bar\n baz\nqux quux" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("closing new line", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("", ""))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar\n baz\nqux quux\n"))
|
|
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "foo bar\n baz\nqux quux\n" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("word mid line", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("", ""))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar bar foo\n baz\nqux quux\n"))
|
|
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "foo bar bar foo\n baz\nqux quux\n" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
2025-11-02 01:04:54 +01:00
|
|
|
})
|
2025-11-01 03:49:02 +01:00
|
|
|
|
2025-11-02 01:04:54 +01:00
|
|
|
t.Run("indent no wrap", func(t *testing.T) {
|
|
|
|
|
t.Run("first and rest same", func(t *testing.T) {
|
2025-11-01 03:49:02 +01:00
|
|
|
var b bytes.Buffer
|
2025-11-02 01:04:54 +01:00
|
|
|
w := textedit.New(&b, textedit.Indent("xx", "xx"))
|
|
|
|
|
w.Write([]byte("foo bar\n baz\nqux quux\n"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "xxfoo bar\nxx baz\nxxqux quux\n" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-02 01:04:54 +01:00
|
|
|
t.Run("indent last word", func(t *testing.T) {
|
2025-11-01 03:49:02 +01:00
|
|
|
var b bytes.Buffer
|
2025-11-02 01:04:54 +01:00
|
|
|
w := textedit.New(&b, textedit.Indent("xxxx", "xxxx"))
|
|
|
|
|
w.Write([]byte("foo bar bar foo\n baz\nqux quux\nfoo"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "xxxxfoo bar bar foo\nxxxx baz\nxxxxqux quux\nxxxxfoo" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("first out", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("", "xx"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar\n baz\nqux quux\n"))
|
|
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "foo bar\nxx baz\nxxqux quux\n" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("first in", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("xx", ""))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar\n baz\nqux quux\n"))
|
|
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "xxfoo bar\n baz\nqux quux\n" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("first out offset", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("xx", "xxxx"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar\n baz\nqux quux\n"))
|
|
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "xxfoo bar\nxxxx baz\nxxxxqux quux\n" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("first in offset", func(t *testing.T) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := textedit.New(&b, textedit.Indent("xxxx", "xx"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar\n baz\nqux quux\n"))
|
|
|
|
|
w.Flush()
|
2025-11-02 01:04:54 +01:00
|
|
|
if b.String() != "xxxxfoo bar\nxx baz\nxxqux quux\n" {
|
2025-11-01 03:49:02 +01:00
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|