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("", "")) w.Write(nil) w.Flush() 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("", "")) w.Write([]byte("\n")) w.Flush() 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("", "")) w.Write([]byte("foo bar\n baz\nqux quux")) w.Flush() 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("", "")) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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("", "")) w.Write([]byte("foo bar bar foo\n baz\nqux quux\n")) w.Flush() 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")) w.Write([]byte("foo bar bar foo\n baz\nqux quux\nfoo")) w.Flush() 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")) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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")) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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", "")) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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")) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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")) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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)) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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)) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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)) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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)) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() 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 species—including the feathery fronds of Hypnum cupressiforme—carpet 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)) w.Write([]byte(text)) w.Flush() 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 species—including the feathery fronds of Hypnum cupressiforme—carpet 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)) w.Write([]byte(text)) w.Flush() 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 species—including the feathery fronds of Hypnum cupressiforme—carpet 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)) w.Write([]byte(text)) w.Flush() 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 species—including the feathery fronds of Hypnum cupressiforme—carpet 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)) w.Write([]byte(text)) w.Flush() 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 species—including the feathery fronds of Hypnum cupressiforme—carpet 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)) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() if b.String() != "\tfoo\nbar baz qux\nquux" { t.Fatal(b.String()) } }) }) }