1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-18 14:47:03 +00:00
Files
go-chasquid-smtp/internal/protoio/protoio_test.go
Alberto Bertogli f4fb2153b8 protoio: Use keyed fields in composite literal
In the protoio tests, we were using unkeyed fields in some composite
literals. This can cause confusion and makes the code more brittle wrt.
future changes. go vet also complains about this.

This patch fixes the issue by adding the field names to the struct
initializations.
2018-03-02 19:37:37 +00:00

90 lines
2.0 KiB
Go

package protoio
import (
"os"
"testing"
"blitiri.com.ar/go/chasquid/internal/protoio/testpb"
"blitiri.com.ar/go/chasquid/internal/testlib"
)
func TestBin(t *testing.T) {
dir := testlib.MustTempDir(t)
defer testlib.RemoveIfOk(t, dir)
pb := &testpb.M{Content: "hola"}
if err := WriteMessage("f", pb, 0600); err != nil {
t.Error(err)
}
pb2 := &testpb.M{}
if err := ReadMessage("f", pb2); err != nil {
t.Error(err)
}
if pb.Content != pb2.Content {
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
}
}
func TestText(t *testing.T) {
dir := testlib.MustTempDir(t)
defer testlib.RemoveIfOk(t, dir)
pb := &testpb.M{Content: "hola"}
if err := WriteTextMessage("f", pb, 0600); err != nil {
t.Error(err)
}
pb2 := &testpb.M{}
if err := ReadTextMessage("f", pb2); err != nil {
t.Error(err)
}
if pb.Content != pb2.Content {
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
}
}
func TestStore(t *testing.T) {
dir := testlib.MustTempDir(t)
defer testlib.RemoveIfOk(t, dir)
st, err := NewStore(dir + "/store")
if err != nil {
t.Fatalf("failed to create store: %v", err)
}
if ids, err := st.ListIDs(); len(ids) != 0 || err != nil {
t.Errorf("expected no ids, got %v - %v", ids, err)
}
pb := &testpb.M{Content: "hola"}
if err := st.Put("f", pb); err != nil {
t.Error(err)
}
pb2 := &testpb.M{}
if ok, err := st.Get("f", pb2); err != nil || !ok {
t.Errorf("Get(f): %v - %v", ok, err)
}
if pb.Content != pb2.Content {
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
}
if ok, err := st.Get("notexists", pb2); err != nil || ok {
t.Errorf("Get(notexists): %v - %v", ok, err)
}
// Add an extraneous file, which ListIDs should ignore.
fd, err := os.Create(dir + "/store/" + "somefile")
if fd != nil {
fd.Close()
}
if err != nil {
t.Errorf("failed to create extraneous file: %v", err)
}
if ids, err := st.ListIDs(); len(ids) != 1 || ids[0] != "f" || err != nil {
t.Errorf("expected [f], got %v - %v", ids, err)
}
}