1
0
textedit/indent_test.go

405 lines
13 KiB
Go
Raw Normal View History

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("", ""))
if _, err := w.Write(nil); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
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("", ""))
if _, err := w.Write([]byte("\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
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("", ""))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nbaz\nqux quux" {
t.Fatal(b.String())
}
})
t.Run("closing new line", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", ""))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nbaz\nqux quux\n" {
t.Fatal(b.String())
}
})
t.Run("word mid line", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", ""))
if _, err := w.Write([]byte("foo bar bar foo\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar bar foo\nbaz\nqux quux\n" {
t.Fatal(b.String())
}
})
t.Run("indent last word", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("xxxx", "xxxx"))
if _, err := w.Write([]byte("foo bar bar foo\n baz\nqux quux\nfoo")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxxxfoo bar bar foo\nxxxxbaz\nxxxxqux quux\nxxxxfoo" {
t.Fatal(b.String())
}
})
})
t.Run("indent no wrap", func(t *testing.T) {
t.Run("first and rest same", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("xx", "xx"))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxfoo bar\nxxbaz\nxxqux quux\n" {
t.Fatal(b.String())
}
})
t.Run("first out", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", "xx"))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nxxbaz\nxxqux quux\n" {
t.Fatal(b.String())
}
})
t.Run("first in", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("xx", ""))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxfoo bar\nbaz\nqux quux\n" {
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"))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxfoo bar\nxxxxbaz\nxxxxqux quux\n" {
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"))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxxxfoo bar\nxxbaz\nxxqux quux\n" {
t.Fatal(b.String())
}
})
})
t.Run("wrap", func(t *testing.T) {
t.Run("same first and rest", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Wrap(9, 9))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nbaz qux\nquux" {
t.Fatal(b.String())
}
})
t.Run("shorter first", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Wrap(5, 9))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo\nbar baz\nqux quux" {
t.Fatal(b.String())
}
})
t.Run("longer first", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Wrap(9, 5))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nbaz\nqux\nquux" {
t.Fatal(b.String())
}
})
t.Run("word longer than width", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Wrap(2, 2))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo\nbar\nbaz\nqux\nquux" {
t.Fatal(b.String())
}
})
})
t.Run("indent and wrap", func(t *testing.T) {
const text = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant
presence of Scots pine (Pinus sylvestris) interspersed with sessile oak (Quercus petraea) and
silver birch (Betula pendula), their canopies creating a mosaic of light and shadow on the
forest floor. The sandy, acidic soils typical of this region support a ground layer rich in
ericaceous plants, particularly bilberry (Vaccinium myrtillus) with its dark-green oval leaves
now tinged with burgundy, and the occasional patch of heather (Calluna vulgaris) persisting in
sunnier clearings. Closer inspection of the understory reveals common wood sorrel (Oxalis
acetosella) thriving in moister pockets, while various moss speciesincluding the feathery
fronds of Hypnum cupressiformecarpet fallen logs in varying stages of decay. The drier sections
host wavy hair-grass (Deschampsia flexuosa) in delicate tufts, and where old pines have been
felled, pioneering stands of downy birch and rowan (Sorbus aucuparia) compete for space, their
growth marking the forest's continuous cycle of regeneration in this characteristically glacial
landscape of the North European Plain.
`
t.Run("uniform", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent(" ", " ", 120, 120))
if _, err := w.Write([]byte(text)); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
const expect = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant presence of Scots pine
(Pinus sylvestris) interspersed with sessile oak (Quercus petraea) and silver birch (Betula pendula), their canopies
creating a mosaic of light and shadow on the forest floor. The sandy, acidic soils typical of this region support a
ground layer rich in ericaceous plants, particularly bilberry (Vaccinium myrtillus) with its dark-green oval leaves
now tinged with burgundy, and the occasional patch of heather (Calluna vulgaris) persisting in sunnier clearings.
Closer inspection of the understory reveals common wood sorrel (Oxalis acetosella) thriving in moister pockets,
while various moss speciesincluding the feathery fronds of Hypnum cupressiformecarpet fallen logs in varying
stages of decay. The drier sections host wavy hair-grass (Deschampsia flexuosa) in delicate tufts, and where old
pines have been felled, pioneering stands of downy birch and rowan (Sorbus aucuparia) compete for space, their
growth marking the forest's continuous cycle of regeneration in this characteristically glacial landscape of the
North European Plain.`
if "\n"+b.String() != expect {
t.Fatal("\n" + b.String())
}
})
t.Run("classic", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent(" ", "", 120, 120))
if _, err := w.Write([]byte(text)); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
const expect = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant presence of Scots pine
(Pinus sylvestris) interspersed with sessile oak (Quercus petraea) and silver birch (Betula pendula), their canopies
creating a mosaic of light and shadow on the forest floor. The sandy, acidic soils typical of this region support a
ground layer rich in ericaceous plants, particularly bilberry (Vaccinium myrtillus) with its dark-green oval leaves now
tinged with burgundy, and the occasional patch of heather (Calluna vulgaris) persisting in sunnier clearings. Closer
inspection of the understory reveals common wood sorrel (Oxalis acetosella) thriving in moister pockets, while various
moss speciesincluding the feathery fronds of Hypnum cupressiformecarpet fallen logs in varying stages of decay. The
drier sections host wavy hair-grass (Deschampsia flexuosa) in delicate tufts, and where old pines have been felled,
pioneering stands of downy birch and rowan (Sorbus aucuparia) compete for space, their growth marking the forest's
continuous cycle of regeneration in this characteristically glacial landscape of the North European Plain.`
if "\n"+b.String() != expect {
t.Fatal("\n" + b.String())
}
})
t.Run("indent out", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent("", " ", 120, 120))
if _, err := w.Write([]byte(text)); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
const expect = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant presence of Scots pine (Pinus
sylvestris) interspersed with sessile oak (Quercus petraea) and silver birch (Betula pendula), their canopies
creating a mosaic of light and shadow on the forest floor. The sandy, acidic soils typical of this region support a
ground layer rich in ericaceous plants, particularly bilberry (Vaccinium myrtillus) with its dark-green oval leaves
now tinged with burgundy, and the occasional patch of heather (Calluna vulgaris) persisting in sunnier clearings.
Closer inspection of the understory reveals common wood sorrel (Oxalis acetosella) thriving in moister pockets,
while various moss speciesincluding the feathery fronds of Hypnum cupressiformecarpet fallen logs in varying
stages of decay. The drier sections host wavy hair-grass (Deschampsia flexuosa) in delicate tufts, and where old
pines have been felled, pioneering stands of downy birch and rowan (Sorbus aucuparia) compete for space, their
growth marking the forest's continuous cycle of regeneration in this characteristically glacial landscape of the
North European Plain.`
if "\n"+b.String() != expect {
t.Fatal("\n" + b.String())
}
})
t.Run("indent out with same width", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent("", " ", 116, 120))
if _, err := w.Write([]byte(text)); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
const expect = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant presence of Scots pine
(Pinus sylvestris) interspersed with sessile oak (Quercus petraea) and silver birch (Betula pendula), their canopies
creating a mosaic of light and shadow on the forest floor. The sandy, acidic soils typical of this region support a
ground layer rich in ericaceous plants, particularly bilberry (Vaccinium myrtillus) with its dark-green oval leaves
now tinged with burgundy, and the occasional patch of heather (Calluna vulgaris) persisting in sunnier clearings.
Closer inspection of the understory reveals common wood sorrel (Oxalis acetosella) thriving in moister pockets,
while various moss speciesincluding the feathery fronds of Hypnum cupressiformecarpet fallen logs in varying
stages of decay. The drier sections host wavy hair-grass (Deschampsia flexuosa) in delicate tufts, and where old
pines have been felled, pioneering stands of downy birch and rowan (Sorbus aucuparia) compete for space, their
growth marking the forest's continuous cycle of regeneration in this characteristically glacial landscape of the
North European Plain.`
if "\n"+b.String() != expect {
t.Fatal("\n" + b.String())
}
})
t.Run("tab is 8", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent("\t", "", 12, 12))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "\tfoo\nbar baz qux\nquux" {
t.Fatal(b.String())
}
})
})
}