diff --git a/CHANGELOG.md b/CHANGELOG.md index b48ffbb..a99335e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.2.4 + +- Deleted `go.dkinom.dev/baseconv/characters` +- Added tests in `go.dkinom.dev/baseconv/internal/characters` + ## 0.2.3 - Retracted `go.dkinom.dev/baseconv/characters` diff --git a/characters/doc.go b/characters/doc.go deleted file mode 100644 index b703a42..0000000 --- a/characters/doc.go +++ /dev/null @@ -1 +0,0 @@ -package characters diff --git a/characters/go.mod b/characters/go.mod deleted file mode 100644 index 773751a..0000000 --- a/characters/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module go.dkinom.dev/baseconv/characters - -go 1.18 - -retract [v0.0.0-00000000000000-000000000000, v0.2.3] diff --git a/internal/characters/ascii_test.go b/internal/characters/ascii_test.go new file mode 100644 index 0000000..d75cb7e --- /dev/null +++ b/internal/characters/ascii_test.go @@ -0,0 +1,54 @@ +package characters + +import ( + "testing" +) + +func TestIsASCII(t *testing.T) { + t.Run("ASCII string", func(t *testing.T) { + if !isASCII("asdfgh") { + t.Fatal("'asdfgh' contains only ascii characters") + } + }) + + t.Run("Empty string", func(t *testing.T) { + if !isASCII("") { + t.Fatal() + } + }) + + t.Run("Non-ASCII string", func(t *testing.T) { + if isASCII("jklπoi") { + t.Fatal() + } + }) +} + +func TestASCIICharacterIterator(t *testing.T) { + t.Run("ASCII string", func(t *testing.T) { + str := "qwerty" + bytes := []byte(str) + itr := newASCII(str) + + length := 0 + for itr.Next() { + length++ + + if itr.Str() != string(bytes[length-1]) { + t.Fatal() + } + } + + if length != 6 { + t.Fatal("'qwerty' has 6 characters") + } + }) + + t.Run("Empty string", func(t *testing.T) { + itr := newASCII("") + + if itr.Next() { + t.Fatal() + } + }) +} diff --git a/internal/characters/characters_test.go b/internal/characters/characters_test.go new file mode 100644 index 0000000..0d4346f --- /dev/null +++ b/internal/characters/characters_test.go @@ -0,0 +1,62 @@ +package characters + +import ( + "testing" + + "github.com/rivo/uniseg" +) + +func TestCharacterIterator(t *testing.T) { + t.Run("ASCII string", func(t *testing.T) { + str := "zxcvbnm" + bytes := []byte(str) + itr := NewCharacters(str) + + if _, ok := itr.(*ascii); !ok { + t.Fatal("expected ascii iterator") + } + + length := 0 + for itr.Next() { + length++ + + if itr.Str() != string(bytes[length-1]) { + t.Fatal() + } + } + + if length != 7 { + t.Fatal("'zxcvbnm' has 7 characters") + } + }) + + t.Run("Empty string", func(t *testing.T) { + itr := NewCharacters("") + + if _, ok := itr.(*ascii); !ok { + t.Fatal("expected ascii iterator") + } + + if itr.Next() { + t.Fatal() + } + }) + + t.Run("Non-ASCII string", func(t *testing.T) { + str := "0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣" + itr := NewCharacters(str) + + if _, ok := itr.(*uniseg.Graphemes); !ok { + t.Fatal("expected graphemes iterator") + } + + length := 0 + for itr.Next() { + length++ + } + + if length != 10 { + t.Fatal("'0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣' has 10 characters") + } + }) +}