1
0
textedit/replace_test.go

210 lines
4.6 KiB
Go

package textedit_test
import (
"bytes"
"code.squareroundforest.org/arpio/textedit"
"testing"
)
func TestReplace(t *testing.T) {
t.Run("empty and no match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace())
w.Write(nil)
w.Flush()
if b.String() != "" {
t.Fatal(b.String())
}
})
t.Run("empty and empty match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r"))
w.Write(nil)
w.Flush()
if b.String() != "" {
t.Fatal(b.String())
}
})
t.Run("empty one single char match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r", "z"))
w.Write(nil)
w.Flush()
if b.String() != "" {
t.Fatal(b.String())
}
})
t.Run("empty multiple single char matches", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("o", "e", "r", "z"))
w.Write(nil)
w.Flush()
if b.String() != "" {
t.Fatal(b.String())
}
})
t.Run("empty one multi-char match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("oo", "ee"))
w.Write(nil)
w.Flush()
if b.String() != "" {
t.Fatal(b.String())
}
})
t.Run("empty multiple multi-char matches", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("oo", "ee", "ar", "az"))
w.Write(nil)
w.Flush()
if b.String() != "" {
t.Fatal(b.String())
}
})
t.Run("no match empty match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r"))
w.Write([]byte("foo"))
w.Flush()
if b.String() != "foo" {
t.Fatal(b.String())
}
})
t.Run("no match one single char match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r", "z"))
w.Write([]byte("foo"))
w.Flush()
if b.String() != "foo" {
t.Fatal(b.String())
}
})
t.Run("no match multiple single char matches", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("b", "f", "r", "z"))
w.Write([]byte("foo"))
w.Flush()
if b.String() != "foo" {
t.Fatal(b.String())
}
})
t.Run("no match one multi-char match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("bar", "baz"))
w.Write([]byte("foo"))
w.Flush()
if b.String() != "foo" {
t.Fatal(b.String())
}
})
t.Run("no match multiple multi-char matches", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("bar", "baz", "qux", "quuz"))
w.Write([]byte("foo"))
w.Flush()
if b.String() != "foo" {
t.Fatal(b.String())
}
})
t.Run("match empty match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("a"))
w.Write([]byte("bar"))
w.Flush()
if b.String() != "br" {
t.Fatal(b.String())
}
})
t.Run("match one single char match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("r", "z"))
w.Write([]byte("bar"))
w.Flush()
if b.String() != "baz" {
t.Fatal(b.String())
}
})
t.Run("match multiple single char matches", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("o", "e", "r", "z"))
w.Write([]byte("foo bar"))
w.Flush()
if b.String() != "fee baz" {
t.Fatal(b.String())
}
})
t.Run("match one multi-char match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("oo", "ee"))
w.Write([]byte("foo bar"))
w.Flush()
if b.String() != "fee bar" {
t.Fatal(b.String())
}
})
t.Run("match multiple multi-char matches", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("oo", "ee", "ar", "az"))
w.Write([]byte("foo bar"))
w.Flush()
if b.String() != "fee baz" {
t.Fatal(b.String())
}
})
t.Run("inverse order", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("ar", "az", "oo", "ee"))
w.Write([]byte("foo bar"))
w.Flush()
if b.String() != "fee baz" {
t.Fatal(b.String())
}
})
t.Run("mismatch after partial match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("foo", "bar"))
w.Write([]byte("fox baz"))
w.Flush()
if b.String() != "fox baz" {
t.Fatal(b.String())
}
})
t.Run("empty match", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("", "bar"))
w.Write([]byte("foo baz"))
w.Flush()
if b.String() != "foo baz" {
t.Fatal(b.String())
}
})
t.Run("delete", func(t *testing.T) {
var b bytes.Buffer
w := textedit.New(&b, textedit.Replace("bar", ""))
w.Write([]byte("foo bar"))
w.Flush()
if b.String() != "foo " {
t.Fatal(b.String())
}
})
}