mirror of
https://github.com/divan/txqr.git
synced 2026-08-31 15:27:28 +08:00
Fix tests
This commit is contained in:
+27
-6
@@ -1,6 +1,7 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -10,14 +11,14 @@ func TestDecode(t *testing.T) {
|
||||
expected := strings.Repeat("s", N+1)
|
||||
dec := NewDecoder()
|
||||
var err error
|
||||
err = dec.DecodeChunk("0/65|" + strings.Repeat("s", N))
|
||||
err = dec.DecodeChunk("0/101|" + strings.Repeat("s", N))
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeChunk failed: %v", err)
|
||||
}
|
||||
if dec.IsCompleted() {
|
||||
t.Fatalf("IsCompleted expected to be false")
|
||||
}
|
||||
err = dec.DecodeChunk("64/65|" + "s")
|
||||
err = dec.DecodeChunk("100/101|" + "s")
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeChunk failed: %v", err)
|
||||
}
|
||||
@@ -26,7 +27,7 @@ func TestDecode(t *testing.T) {
|
||||
}
|
||||
|
||||
if dec.Data() != expected {
|
||||
t.Fatalf("Expected to get '%s', but got '%s'", expected, dec.Data())
|
||||
t.Fatalf("Expected to get '%s' (len %d), but got '%s' (len %d)", expected, len(expected), dec.Data(), len(dec.Data()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,14 +36,17 @@ func TestInvalidDecode(t *testing.T) {
|
||||
expected := strings.Repeat("s", N+1)
|
||||
dec := NewDecoder()
|
||||
var err error
|
||||
err = dec.DecodeChunk("0/65|" + strings.Repeat("s", 90))
|
||||
err = dec.DecodeChunk("0/101|" + strings.Repeat("s", 90))
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeChunk failed: %v", err)
|
||||
}
|
||||
if dec.IsCompleted() {
|
||||
t.Fatalf("IsCompleted expected to be false")
|
||||
}
|
||||
err = dec.DecodeChunk("64/65|" + "s")
|
||||
if dec.Progress() != 89 {
|
||||
t.Fatalf("Progress should be equal to %v, but got %v", 99, dec.Progress())
|
||||
}
|
||||
err = dec.DecodeChunk("100/101|" + "s")
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeChunk failed: %v", err)
|
||||
}
|
||||
@@ -50,7 +54,7 @@ func TestInvalidDecode(t *testing.T) {
|
||||
t.Fatalf("IsCompleted expected to be false")
|
||||
}
|
||||
// missing out-of-order chunk
|
||||
err = dec.DecodeChunk("5a/65|" + strings.Repeat("s", 10))
|
||||
err = dec.DecodeChunk("90/101|" + strings.Repeat("s", 10))
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeChunk failed: %v", err)
|
||||
}
|
||||
@@ -62,3 +66,20 @@ func TestInvalidDecode(t *testing.T) {
|
||||
t.Fatalf("Expected to get '%s', but got '%s'", expected, dec.Data())
|
||||
}
|
||||
}
|
||||
|
||||
func TestProgress(t *testing.T) {
|
||||
N := 100
|
||||
dec := NewDecoder()
|
||||
var err error
|
||||
|
||||
for i := 0; i < N; i++ {
|
||||
chunk := fmt.Sprintf("%d/%d|s", i, N)
|
||||
err = dec.DecodeChunk(chunk)
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeChunk failed: %v", err)
|
||||
}
|
||||
if dec.Progress() != i+1 {
|
||||
t.Fatalf("Progress should be equal to %v, but got %v", i+1, dec.Progress())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ func TestEncodeString(t *testing.T) {
|
||||
t.Fatalf("Expected 2 chunks")
|
||||
}
|
||||
|
||||
expected := "0/65|" + strings.Repeat("s", N)
|
||||
expected := "0/101|" + strings.Repeat("s", N)
|
||||
if chunks[0] != expected {
|
||||
t.Fatalf("First chunk is invalid: '%s'", chunks[0])
|
||||
}
|
||||
expected = "64/65|" + "s"
|
||||
expected = "100/101|" + "s"
|
||||
if chunks[1] != expected {
|
||||
t.Fatalf("Second chunk is invalid: '%s'", chunks[1])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user