gitee.com/quant1x/gox@v1.21.2/encoding/binary/struc/parse_test.go (about)

     1  package struc
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func parseTest(data interface{}) error {
    10  	_, err := parseFields(reflect.ValueOf(data))
    11  	return err
    12  }
    13  
    14  type empty struct{}
    15  
    16  func TestEmptyStruc(t *testing.T) {
    17  	if err := parseTest(&empty{}); err == nil {
    18  		t.Fatal("failed to error on empty struct")
    19  	}
    20  }
    21  
    22  type chanStruct struct {
    23  	Test chan int
    24  }
    25  
    26  func TestChanError(t *testing.T) {
    27  	if err := parseTest(&chanStruct{}); err == nil {
    28  		// TODO: should probably ignore channel fields
    29  		t.Fatal("failed to error on struct containing channel")
    30  	}
    31  }
    32  
    33  type badSizeof struct {
    34  	Size int `struc:"sizeof=Bad"`
    35  }
    36  
    37  func TestBadSizeof(t *testing.T) {
    38  	if err := parseTest(&badSizeof{}); err == nil {
    39  		t.Fatal("failed to error on missing Sizeof target")
    40  	}
    41  }
    42  
    43  type missingSize struct {
    44  	Test []byte
    45  }
    46  
    47  func TestMissingSize(t *testing.T) {
    48  	if err := parseTest(&missingSize{}); err == nil {
    49  		t.Fatal("failed to error on missing field size")
    50  	}
    51  }
    52  
    53  type badNested struct {
    54  	Empty empty
    55  }
    56  
    57  func TestNestedParseError(t *testing.T) {
    58  	var buf bytes.Buffer
    59  	if err := Pack(&buf, &badNested{}); err == nil {
    60  		t.Fatal("failed to error on bad nested struct")
    61  	}
    62  }