package textedit_test import ( "bytes" "code.squareroundforest.org/arpio/textedit" "testing" ) func TestWrap(t *testing.T) { 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("zero width", func(t *testing.T) { var b bytes.Buffer w := textedit.New(&b, textedit.Wrap(0, 0)) 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("negative 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()) } }) t.Run("wrap on release", func(t *testing.T) { var b bytes.Buffer w := textedit.New(&b, textedit.WrapIndent(" ", " ", 15, 15)) w.Write([]byte("Some sample text...\n on multiple lines.")) w.Flush() const expect = ` Some sample text... on multiple lines.` if "\n"+b.String() != expect { t.Fatal("\n" + b.String()) } }) t.Run("zero width", func(t *testing.T) { var b bytes.Buffer w := textedit.New(&b, textedit.WrapIndent("xx", "xx", 0, 0)) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() if b.String() != "xxfoo\nxxbar\nxxbaz\nxxqux\nxxquux" { t.Fatal(b.String()) } }) t.Run("negative width", func(t *testing.T) { var b bytes.Buffer w := textedit.New(&b, textedit.WrapIndent("xx", "xx", -2, -2)) w.Write([]byte("foo bar\n baz\nqux quux\n")) w.Flush() if b.String() != "xxfoo\nxxbar\nxxbaz\nxxqux\nxxquux" { t.Fatal(b.String()) } }) t.Run("last word on the same line", func(t *testing.T) { var b bytes.Buffer w := textedit.New(&b, textedit.WrapIndent("xx", "xx", 9, 9)) w.Write([]byte("foo bar")) w.Flush() if b.String() != "xxfoo bar" { t.Fatal(b.String()) } }) }) }