github.com/ndau/noms@v1.0.5/go/types/validating_decoder_test.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package types
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/ndau/noms/go/chunks"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestValidatingBatchingSinkDecode(t *testing.T) {
    15  	v := Number(42)
    16  	c := EncodeValue(v)
    17  	storage := &chunks.TestStorage{}
    18  	vdc := NewValidatingDecoder(storage.NewView())
    19  
    20  	dc := vdc.Decode(&c)
    21  	assert.True(t, v.Equals(*dc.Value))
    22  }
    23  
    24  func assertPanicsOnInvalidChunk(t *testing.T, data []interface{}) {
    25  	storage := &chunks.TestStorage{}
    26  	vs := NewValueStore(storage.NewView())
    27  	dataAsByteSlice := toBinaryNomsReaderData(data)
    28  	dec := newValueDecoder(dataAsByteSlice, vs)
    29  	v := dec.readValue()
    30  
    31  	c := EncodeValue(v)
    32  	vdc := NewValidatingDecoder(storage.NewView())
    33  
    34  	assert.Panics(t, func() {
    35  		vdc.Decode(&c)
    36  	})
    37  }
    38  
    39  func TestValidatingBatchingSinkDecodeInvalidUnion(t *testing.T) {
    40  	data := []interface{}{
    41  		uint8(TypeKind),
    42  		uint8(UnionKind), uint64(2) /* len */, uint8(NumberKind), uint8(BoolKind),
    43  	}
    44  	assertPanicsOnInvalidChunk(t, data)
    45  }
    46  
    47  func TestValidatingBatchingSinkDecodeInvalidStructFieldOrder(t *testing.T) {
    48  	data := []interface{}{
    49  		uint8(TypeKind),
    50  		uint8(StructKind), "S", uint64(2), /* len */
    51  		"b", "a",
    52  		uint8(NumberKind), uint8(NumberKind),
    53  		false, false,
    54  	}
    55  	assertPanicsOnInvalidChunk(t, data)
    56  }
    57  
    58  func TestValidatingBatchingSinkDecodeInvalidStructName(t *testing.T) {
    59  	data := []interface{}{
    60  		uint8(TypeKind),
    61  		uint8(StructKind), "S ", uint64(0), /* len */
    62  	}
    63  	assertPanicsOnInvalidChunk(t, data)
    64  }
    65  
    66  func TestValidatingBatchingSinkDecodeInvalidStructFieldName(t *testing.T) {
    67  	data := []interface{}{
    68  		uint8(TypeKind),
    69  		uint8(StructKind), "S", uint64(1), /* len */
    70  		"b ", uint8(NumberKind), false,
    71  	}
    72  	assertPanicsOnInvalidChunk(t, data)
    73  }