1
0

remove unnecessary error handling

This commit is contained in:
Arpad Ryszka 2025-11-01 05:14:20 +01:00
parent 71640ab28f
commit 3a1be0d6de
5 changed files with 92 additions and 367 deletions

View File

@ -10,14 +10,8 @@ func TestEscape(t *testing.T) {
t.Run("basic", func(t *testing.T) { t.Run("basic", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Escape('\\', '\n', '\t', '"')) w := textedit.New(&b, textedit.Escape('\\', '\n', '\t', '"'))
if _, err := w.Write([]byte("foo\nbar\t\"baz\"")); err != nil { w.Write([]byte("foo\nbar\t\"baz\""))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo\\\nbar\\\t\\\"baz\\\"" { if b.String() != "foo\\\nbar\\\t\\\"baz\\\"" {
t.Fatal(b.String()) t.Fatal(b.String())
} }

View File

@ -11,14 +11,8 @@ func TestIndent(t *testing.T) {
t.Run("empty", func(t *testing.T) { t.Run("empty", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", "")) w := textedit.New(&b, textedit.Indent("", ""))
if _, err := w.Write(nil); err != nil { w.Write(nil)
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "" { if b.String() != "" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -27,14 +21,8 @@ func TestIndent(t *testing.T) {
t.Run("new line", func(t *testing.T) { t.Run("new line", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", "")) w := textedit.New(&b, textedit.Indent("", ""))
if _, err := w.Write([]byte("\n")); err != nil { w.Write([]byte("\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "\n" { if b.String() != "\n" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -43,14 +31,8 @@ func TestIndent(t *testing.T) {
t.Run("basic", func(t *testing.T) { t.Run("basic", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", "")) w := textedit.New(&b, textedit.Indent("", ""))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nbaz\nqux quux" { if b.String() != "foo bar\nbaz\nqux quux" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -59,14 +41,8 @@ func TestIndent(t *testing.T) {
t.Run("closing new line", func(t *testing.T) { t.Run("closing new line", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", "")) w := textedit.New(&b, textedit.Indent("", ""))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nbaz\nqux quux\n" { if b.String() != "foo bar\nbaz\nqux quux\n" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -75,14 +51,8 @@ func TestIndent(t *testing.T) {
t.Run("word mid line", func(t *testing.T) { t.Run("word mid line", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", "")) w := textedit.New(&b, textedit.Indent("", ""))
if _, err := w.Write([]byte("foo bar bar foo\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar bar foo\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar bar foo\nbaz\nqux quux\n" { if b.String() != "foo bar bar foo\nbaz\nqux quux\n" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -91,14 +61,8 @@ func TestIndent(t *testing.T) {
t.Run("indent last word", func(t *testing.T) { t.Run("indent last word", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("xxxx", "xxxx")) w := textedit.New(&b, textedit.Indent("xxxx", "xxxx"))
if _, err := w.Write([]byte("foo bar bar foo\n baz\nqux quux\nfoo")); err != nil { w.Write([]byte("foo bar bar foo\n baz\nqux quux\nfoo"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxxxfoo bar bar foo\nxxxxbaz\nxxxxqux quux\nxxxxfoo" { if b.String() != "xxxxfoo bar bar foo\nxxxxbaz\nxxxxqux quux\nxxxxfoo" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -109,14 +73,8 @@ func TestIndent(t *testing.T) {
t.Run("first and rest same", func(t *testing.T) { t.Run("first and rest same", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("xx", "xx")) w := textedit.New(&b, textedit.Indent("xx", "xx"))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxfoo bar\nxxbaz\nxxqux quux\n" { if b.String() != "xxfoo bar\nxxbaz\nxxqux quux\n" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -125,14 +83,8 @@ func TestIndent(t *testing.T) {
t.Run("first out", func(t *testing.T) { t.Run("first out", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("", "xx")) w := textedit.New(&b, textedit.Indent("", "xx"))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nxxbaz\nxxqux quux\n" { if b.String() != "foo bar\nxxbaz\nxxqux quux\n" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -141,14 +93,8 @@ func TestIndent(t *testing.T) {
t.Run("first in", func(t *testing.T) { t.Run("first in", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("xx", "")) w := textedit.New(&b, textedit.Indent("xx", ""))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxfoo bar\nbaz\nqux quux\n" { if b.String() != "xxfoo bar\nbaz\nqux quux\n" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -157,14 +103,8 @@ func TestIndent(t *testing.T) {
t.Run("first out offset", func(t *testing.T) { t.Run("first out offset", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("xx", "xxxx")) w := textedit.New(&b, textedit.Indent("xx", "xxxx"))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxfoo bar\nxxxxbaz\nxxxxqux quux\n" { if b.String() != "xxfoo bar\nxxxxbaz\nxxxxqux quux\n" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -173,14 +113,8 @@ func TestIndent(t *testing.T) {
t.Run("first in offset", func(t *testing.T) { t.Run("first in offset", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Indent("xxxx", "xx")) w := textedit.New(&b, textedit.Indent("xxxx", "xx"))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "xxxxfoo bar\nxxbaz\nxxqux quux\n" { if b.String() != "xxxxfoo bar\nxxbaz\nxxqux quux\n" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -191,14 +125,8 @@ func TestIndent(t *testing.T) {
t.Run("same first and rest", func(t *testing.T) { t.Run("same first and rest", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Wrap(9, 9)) w := textedit.New(&b, textedit.Wrap(9, 9))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nbaz qux\nquux" { if b.String() != "foo bar\nbaz qux\nquux" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -207,14 +135,8 @@ func TestIndent(t *testing.T) {
t.Run("shorter first", func(t *testing.T) { t.Run("shorter first", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Wrap(5, 9)) w := textedit.New(&b, textedit.Wrap(5, 9))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo\nbar baz\nqux quux" { if b.String() != "foo\nbar baz\nqux quux" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -223,14 +145,8 @@ func TestIndent(t *testing.T) {
t.Run("longer first", func(t *testing.T) { t.Run("longer first", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Wrap(9, 5)) w := textedit.New(&b, textedit.Wrap(9, 5))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar\nbaz\nqux\nquux" { if b.String() != "foo bar\nbaz\nqux\nquux" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -239,14 +155,8 @@ func TestIndent(t *testing.T) {
t.Run("word longer than width", func(t *testing.T) { t.Run("word longer than width", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Wrap(2, 2)) w := textedit.New(&b, textedit.Wrap(2, 2))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo\nbar\nbaz\nqux\nquux" { if b.String() != "foo\nbar\nbaz\nqux\nquux" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -273,14 +183,8 @@ func TestIndent(t *testing.T) {
t.Run("uniform", func(t *testing.T) { t.Run("uniform", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent(" ", " ", 120, 120)) w := textedit.New(&b, textedit.WrapIndent(" ", " ", 120, 120))
if _, err := w.Write([]byte(text)); err != nil { w.Write([]byte(text))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
const expect = ` const expect = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant presence of Scots pine 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 (Pinus sylvestris) interspersed with sessile oak (Quercus petraea) and silver birch (Betula pendula), their canopies
@ -302,14 +206,8 @@ func TestIndent(t *testing.T) {
t.Run("classic", func(t *testing.T) { t.Run("classic", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent(" ", "", 120, 120)) w := textedit.New(&b, textedit.WrapIndent(" ", "", 120, 120))
if _, err := w.Write([]byte(text)); err != nil { w.Write([]byte(text))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
const expect = ` const expect = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant presence of Scots pine 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 (Pinus sylvestris) interspersed with sessile oak (Quercus petraea) and silver birch (Betula pendula), their canopies
@ -330,14 +228,8 @@ continuous cycle of regeneration in this characteristically glacial landscape of
t.Run("indent out", func(t *testing.T) { t.Run("indent out", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent("", " ", 120, 120)) w := textedit.New(&b, textedit.WrapIndent("", " ", 120, 120))
if _, err := w.Write([]byte(text)); err != nil { w.Write([]byte(text))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
const expect = ` const expect = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant presence of Scots pine (Pinus 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 sylvestris) interspersed with sessile oak (Quercus petraea) and silver birch (Betula pendula), their canopies
@ -359,14 +251,8 @@ Walking through the mixed forests of Brandenburg in early autumn, one notices th
t.Run("indent out with same width", func(t *testing.T) { t.Run("indent out with same width", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent("", " ", 116, 120)) w := textedit.New(&b, textedit.WrapIndent("", " ", 116, 120))
if _, err := w.Write([]byte(text)); err != nil { w.Write([]byte(text))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
const expect = ` const expect = `
Walking through the mixed forests of Brandenburg in early autumn, one notices the dominant presence of Scots pine 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 (Pinus sylvestris) interspersed with sessile oak (Quercus petraea) and silver birch (Betula pendula), their canopies
@ -388,14 +274,8 @@ Walking through the mixed forests of Brandenburg in early autumn, one notices th
t.Run("tab is 8", func(t *testing.T) { t.Run("tab is 8", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.WrapIndent("\t", "", 12, 12)) w := textedit.New(&b, textedit.WrapIndent("\t", "", 12, 12))
if _, err := w.Write([]byte("foo bar\n baz\nqux quux\n")); err != nil { w.Write([]byte("foo bar\n baz\nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "\tfoo\nbar baz qux\nquux" { if b.String() != "\tfoo\nbar baz qux\nquux" {
t.Fatal(b.String()) t.Fatal(b.String())
} }

View File

@ -27,14 +27,8 @@ func TestNoop(t *testing.T) {
t.Run("editor", func(t *testing.T) { t.Run("editor", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Func(nil, func(int) []rune { return nil })) w := textedit.New(&b, textedit.Func(nil, func(int) []rune { return nil }))
if _, err := w.Write([]byte("foo bar baz")); err != nil { w.Write([]byte("foo bar baz"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar baz" { if b.String() != "foo bar baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -43,14 +37,8 @@ func TestNoop(t *testing.T) {
t.Run("release", func(t *testing.T) { t.Run("release", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Func(func(r rune, s int) ([]rune, int) { return []rune{r}, 9 }, nil)) w := textedit.New(&b, textedit.Func(func(r rune, s int) ([]rune, int) { return []rune{r}, 9 }, nil))
if _, err := w.Write([]byte("foo bar baz")); err != nil { w.Write([]byte("foo bar baz"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar baz" { if b.String() != "foo bar baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -61,15 +49,10 @@ func TestWriteRune(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("o", "e")) w := textedit.New(&b, textedit.Replace("o", "e"))
for _, r := range []rune("foo bar baz") { for _, r := range []rune("foo bar baz") {
if _, err := w.WriteRune(r); err != nil { w.WriteRune(r)
t.Fatal(err)
}
}
if err := w.Flush(); err != nil {
t.Fatal(err)
} }
w.Flush()
if b.String() != "fee bar baz" { if b.String() != "fee bar baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -78,14 +61,8 @@ func TestWriteRune(t *testing.T) {
func TestBrokenUnicode(t *testing.T) { func TestBrokenUnicode(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b) w := textedit.New(&b)
if _, err := w.Write([]byte("foo \xc2bar baz")); err != nil { w.Write([]byte("foo \xc2bar baz"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar baz" { if b.String() != "foo bar baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }

View File

@ -10,14 +10,8 @@ func TestReplace(t *testing.T) {
t.Run("empty and no match", func(t *testing.T) { t.Run("empty and no match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace()) w := textedit.New(&b, textedit.Replace())
if _, err := w.Write(nil); err != nil { w.Write(nil)
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "" { if b.String() != "" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -26,14 +20,8 @@ func TestReplace(t *testing.T) {
t.Run("empty and empty match", func(t *testing.T) { t.Run("empty and empty match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r")) w := textedit.New(&b, textedit.Replace("r"))
if _, err := w.Write(nil); err != nil { w.Write(nil)
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "" { if b.String() != "" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -42,14 +30,8 @@ func TestReplace(t *testing.T) {
t.Run("empty one single char match", func(t *testing.T) { t.Run("empty one single char match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r", "z")) w := textedit.New(&b, textedit.Replace("r", "z"))
if _, err := w.Write(nil); err != nil { w.Write(nil)
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "" { if b.String() != "" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -58,14 +40,8 @@ func TestReplace(t *testing.T) {
t.Run("empty multiple single char matches", func(t *testing.T) { t.Run("empty multiple single char matches", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("o", "e", "r", "z")) w := textedit.New(&b, textedit.Replace("o", "e", "r", "z"))
if _, err := w.Write(nil); err != nil { w.Write(nil)
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "" { if b.String() != "" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -74,14 +50,8 @@ func TestReplace(t *testing.T) {
t.Run("empty one multi-char match", func(t *testing.T) { t.Run("empty one multi-char match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("oo", "ee")) w := textedit.New(&b, textedit.Replace("oo", "ee"))
if _, err := w.Write(nil); err != nil { w.Write(nil)
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "" { if b.String() != "" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -90,14 +60,8 @@ func TestReplace(t *testing.T) {
t.Run("empty multiple multi-char matches", func(t *testing.T) { t.Run("empty multiple multi-char matches", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("oo", "ee", "ar", "az")) w := textedit.New(&b, textedit.Replace("oo", "ee", "ar", "az"))
if _, err := w.Write(nil); err != nil { w.Write(nil)
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "" { if b.String() != "" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -106,14 +70,8 @@ func TestReplace(t *testing.T) {
t.Run("no match empty match", func(t *testing.T) { t.Run("no match empty match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r")) w := textedit.New(&b, textedit.Replace("r"))
if _, err := w.Write([]byte("foo")); err != nil { w.Write([]byte("foo"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo" { if b.String() != "foo" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -122,14 +80,8 @@ func TestReplace(t *testing.T) {
t.Run("no match one single char match", func(t *testing.T) { t.Run("no match one single char match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r", "z")) w := textedit.New(&b, textedit.Replace("r", "z"))
if _, err := w.Write([]byte("foo")); err != nil { w.Write([]byte("foo"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo" { if b.String() != "foo" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -138,14 +90,8 @@ func TestReplace(t *testing.T) {
t.Run("no match multiple single char matches", func(t *testing.T) { t.Run("no match multiple single char matches", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("b", "f", "r", "z")) w := textedit.New(&b, textedit.Replace("b", "f", "r", "z"))
if _, err := w.Write([]byte("foo")); err != nil { w.Write([]byte("foo"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo" { if b.String() != "foo" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -154,14 +100,8 @@ func TestReplace(t *testing.T) {
t.Run("no match one multi-char match", func(t *testing.T) { t.Run("no match one multi-char match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("bar", "baz")) w := textedit.New(&b, textedit.Replace("bar", "baz"))
if _, err := w.Write([]byte("foo")); err != nil { w.Write([]byte("foo"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo" { if b.String() != "foo" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -170,14 +110,8 @@ func TestReplace(t *testing.T) {
t.Run("no match multiple multi-char matches", func(t *testing.T) { t.Run("no match multiple multi-char matches", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("bar", "baz", "qux", "quuz")) w := textedit.New(&b, textedit.Replace("bar", "baz", "qux", "quuz"))
if _, err := w.Write([]byte("foo")); err != nil { w.Write([]byte("foo"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo" { if b.String() != "foo" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -186,14 +120,8 @@ func TestReplace(t *testing.T) {
t.Run("match empty match", func(t *testing.T) { t.Run("match empty match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("a")) w := textedit.New(&b, textedit.Replace("a"))
if _, err := w.Write([]byte("bar")); err != nil { w.Write([]byte("bar"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "br" { if b.String() != "br" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -202,14 +130,8 @@ func TestReplace(t *testing.T) {
t.Run("match one single char match", func(t *testing.T) { t.Run("match one single char match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r", "z")) w := textedit.New(&b, textedit.Replace("r", "z"))
if _, err := w.Write([]byte("bar")); err != nil { w.Write([]byte("bar"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "baz" { if b.String() != "baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -218,14 +140,8 @@ func TestReplace(t *testing.T) {
t.Run("match multiple single char matches", func(t *testing.T) { t.Run("match multiple single char matches", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("o", "e", "r", "z")) w := textedit.New(&b, textedit.Replace("o", "e", "r", "z"))
if _, err := w.Write([]byte("foo bar")); err != nil { w.Write([]byte("foo bar"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "fee baz" { if b.String() != "fee baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -234,14 +150,8 @@ func TestReplace(t *testing.T) {
t.Run("match one multi-char match", func(t *testing.T) { t.Run("match one multi-char match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("oo", "ee")) w := textedit.New(&b, textedit.Replace("oo", "ee"))
if _, err := w.Write([]byte("foo bar")); err != nil { w.Write([]byte("foo bar"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "fee bar" { if b.String() != "fee bar" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -250,14 +160,8 @@ func TestReplace(t *testing.T) {
t.Run("match multiple multi-char matches", func(t *testing.T) { t.Run("match multiple multi-char matches", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("oo", "ee", "ar", "az")) w := textedit.New(&b, textedit.Replace("oo", "ee", "ar", "az"))
if _, err := w.Write([]byte("foo bar")); err != nil { w.Write([]byte("foo bar"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "fee baz" { if b.String() != "fee baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -266,14 +170,8 @@ func TestReplace(t *testing.T) {
t.Run("inverse order", func(t *testing.T) { t.Run("inverse order", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("ar", "az", "oo", "ee")) w := textedit.New(&b, textedit.Replace("ar", "az", "oo", "ee"))
if _, err := w.Write([]byte("foo bar")); err != nil { w.Write([]byte("foo bar"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "fee baz" { if b.String() != "fee baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -282,14 +180,8 @@ func TestReplace(t *testing.T) {
t.Run("mismatch after partial match", func(t *testing.T) { t.Run("mismatch after partial match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("foo", "bar")) w := textedit.New(&b, textedit.Replace("foo", "bar"))
if _, err := w.Write([]byte("fox baz")); err != nil { w.Write([]byte("fox baz"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "fox baz" { if b.String() != "fox baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -298,14 +190,8 @@ func TestReplace(t *testing.T) {
t.Run("empty match", func(t *testing.T) { t.Run("empty match", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("", "bar")) w := textedit.New(&b, textedit.Replace("", "bar"))
if _, err := w.Write([]byte("foo baz")); err != nil { w.Write([]byte("foo baz"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo baz" { if b.String() != "foo baz" {
t.Fatal(b.String()) t.Fatal(b.String())
} }
@ -314,14 +200,8 @@ func TestReplace(t *testing.T) {
t.Run("delete", func(t *testing.T) { t.Run("delete", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("bar", "")) w := textedit.New(&b, textedit.Replace("bar", ""))
if _, err := w.Write([]byte("foo bar")); err != nil { w.Write([]byte("foo bar"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo " { if b.String() != "foo " {
t.Fatal(b.String()) t.Fatal(b.String())
} }

View File

@ -10,14 +10,8 @@ func TestSingleLine(t *testing.T) {
t.Run("basic", func(t *testing.T) { t.Run("basic", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
w := textedit.New(&b, textedit.SingleLine()) w := textedit.New(&b, textedit.SingleLine())
if _, err := w.Write([]byte("\nfoo bar\n\nbaz \nqux quux\n")); err != nil { w.Write([]byte("\nfoo bar\n\nbaz \nqux quux\n"))
t.Fatal(err) w.Flush()
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
if b.String() != "foo bar baz qux quux" { if b.String() != "foo bar baz qux quux" {
t.Fatal(b.String()) t.Fatal(b.String())
} }