Files
selfcare/proxy_go/public/go-oi@v1.0.0/longwrite_test.go
2025-03-03 11:40:37 +08:00

196 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package oi
import (
"github.com/reiver/go-oi/test"
"errors"
"testing"
)
func TestLongWrite(t *testing.T) {
tests := []struct{
String string
}{
{
String: "",
},
{
String: "apple",
},
{
String: "banana",
},
{
String: "cherry",
},
{
String: "Hello world!",
},
{
String: "😁😂😃😄😅😆😉😊😋😌😍😏😒😓😔😖😘😚😜😝😞😠😡😢😣😤😥😨😩😪😫😭😰😱😲😳😵😷",
},
{
String: "0123456789",
},
{
String: "٠١٢٣٤٥٦٧٨٩", // Arabic-Indic Digits
},
{
String: "۰۱۲۳۴۵۶۷۸۹", // Extended Arabic-Indic Digits
},
{
String: " Ⅱ Ⅲ Ⅳ Ⅵ Ⅶ Ⅷ Ⅸ Ⅺ Ⅻ ",
},
{
String: " ⅱ ⅲ ⅳ ⅵ ⅶ ⅷ ⅸ ⅺ ⅻ ⅿ",
},
{
String: "ↀ ↁ ↂ Ↄ ↄ ↅ ↆ ↇ ↈ",
},
}
for testNumber, test := range tests {
p := []byte(test.String)
var writer oitest.ShortWriter
n, err := LongWrite(&writer, p)
if nil != err {
t.Errorf("For test #%d, did not expect an error, but actually got one: (%T) %q; for %q.", testNumber, err, err.Error(), test.String)
continue
}
if expected, actual := int64(len([]byte(test.String))), n; expected != actual {
t.Errorf("For test #%d, expected %d, but actually got %d; for %q.", testNumber, expected, actual, test.String)
continue
}
if expected, actual := test.String, writer.String(); expected != actual {
t.Errorf("For test #%d, expected %q, but actually got %q", testNumber, expected, actual)
continue
}
}
}
func TestLongWriteExpectError(t *testing.T) {
tests := []struct{
String string
Expected string
Writes []int
Err error
}{
{
String: "apple",
Expected: "appl",
Writes: []int{2,2},
Err: errors.New("Crabapple!"),
},
{
String: "apple",
Expected: "appl",
Writes: []int{2,2,0},
Err: errors.New("Crabapple!!"),
},
{
String: "banana",
Expected: "banan",
Writes: []int{2,3},
Err: errors.New("bananananananana!"),
},
{
String: "banana",
Expected: "banan",
Writes: []int{2,3,0},
Err: errors.New("bananananananananananananana!!!"),
},
{
String: "cherry",
Expected: "cher",
Writes: []int{1,1,1,1},
Err: errors.New("C.H.E.R.R.Y."),
},
{
String: "cherry",
Expected: "cher",
Writes: []int{1,1,1,1,0},
Err: errors.New("C_H_E_R_R_Y"),
},
{
String: "Hello world!",
Expected: "Hello world",
Writes: []int{1,2,3,5},
Err: errors.New("Welcome!"),
},
{
String: "Hello world!",
Expected: "Hello world",
Writes: []int{1,2,3,5,0},
Err: errors.New("WeLcOmE!!!"),
},
{
String: " ",
Expected: " ",
Writes: []int{1,2,3,5,8,13},
Err: errors.New("Space, the final frontier"),
},
{
String: " ",
Expected: " ",
Writes: []int{1,2,3,5,8,13,0},
Err: errors.New("Space, the final frontier"),
},
}
for testNumber, test := range tests {
p := []byte(test.String)
writer := oitest.NewWritesThenErrorWriter(test.Err, test.Writes...)
n, err := LongWrite(writer, p)
if nil == err {
t.Errorf("For test #%d, expected to get an error, but actually did not get one: %v; for %q.", testNumber, err, test.String)
continue
}
if expected, actual := test.Err, err; expected != actual {
t.Errorf("For test #%d, expected to get error (%T) %q, but actually got (%T) %q; for %q.", testNumber, expected, expected.Error(), actual, actual.Error(), test.String)
continue
}
if expected, actual := int64(len(test.Expected)), n; expected != actual {
t.Errorf("For test #%d, expected number of bytes written to be %d = len(%q), but actually was %d = len(%q); for %q.", testNumber, expected, test.Expected, actual, writer.String(), test.String)
continue
}
}
}