2025-11-01 03:49:02 +01:00
|
|
|
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())
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write(nil)
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write(nil)
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write(nil)
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write(nil)
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write(nil)
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write(nil)
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("bar"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("bar"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("fox baz"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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"))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo baz"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
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", ""))
|
2025-11-01 05:14:20 +01:00
|
|
|
w.Write([]byte("foo bar"))
|
|
|
|
|
w.Flush()
|
2025-11-01 03:49:02 +01:00
|
|
|
if b.String() != "foo " {
|
|
|
|
|
t.Fatal(b.String())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|