github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/util/json/from_json_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 json
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/attic-labs/noms/go/chunks"
    11  	"github.com/attic-labs/noms/go/types"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  func TestLibTestSuite(t *testing.T) {
    16  	suite.Run(t, &LibTestSuite{})
    17  }
    18  
    19  type LibTestSuite struct {
    20  	suite.Suite
    21  	vs *types.ValueStore
    22  }
    23  
    24  func (suite *LibTestSuite) SetupTest() {
    25  	st := &chunks.TestStorage{}
    26  	suite.vs = types.NewValueStore(st.NewView())
    27  }
    28  
    29  func (suite *LibTestSuite) TearDownTest() {
    30  	suite.vs.Close()
    31  }
    32  
    33  func (suite *LibTestSuite) TestPrimitiveTypes() {
    34  	vs := suite.vs
    35  	suite.EqualValues(types.String("expected"), NomsValueFromDecodedJSON(vs, "expected", false))
    36  	suite.EqualValues(types.Bool(false), NomsValueFromDecodedJSON(vs, false, false))
    37  	suite.EqualValues(types.Number(1.7), NomsValueFromDecodedJSON(vs, 1.7, false))
    38  	suite.False(NomsValueFromDecodedJSON(vs, 1.7, false).Equals(types.Bool(true)))
    39  }
    40  
    41  func (suite *LibTestSuite) TestCompositeTypes() {
    42  	vs := suite.vs
    43  
    44  	// [false true]
    45  	suite.EqualValues(
    46  		types.NewList(vs).Edit().Append(types.Bool(false)).Append(types.Bool(true)).List(),
    47  		NomsValueFromDecodedJSON(vs, []interface{}{false, true}, false))
    48  
    49  	// [[false true]]
    50  	suite.EqualValues(
    51  		types.NewList(vs).Edit().Append(
    52  			types.NewList(vs).Edit().Append(types.Bool(false)).Append(types.Bool(true)).List()).List(),
    53  		NomsValueFromDecodedJSON(vs, []interface{}{[]interface{}{false, true}}, false))
    54  
    55  	// {"string": "string",
    56  	//  "list": [false true],
    57  	//  "map": {"nested": "string"}
    58  	// }
    59  	m := types.NewMap(
    60  		vs,
    61  		types.String("string"),
    62  		types.String("string"),
    63  		types.String("list"),
    64  		types.NewList(vs).Edit().Append(types.Bool(false)).Append(types.Bool(true)).List(),
    65  		types.String("map"),
    66  		types.NewMap(
    67  			vs,
    68  			types.String("nested"),
    69  			types.String("string")))
    70  	o := NomsValueFromDecodedJSON(vs, map[string]interface{}{
    71  		"string": "string",
    72  		"list":   []interface{}{false, true},
    73  		"map":    map[string]interface{}{"nested": "string"},
    74  	}, false)
    75  
    76  	suite.True(m.Equals(o))
    77  }
    78  
    79  func (suite *LibTestSuite) TestCompositeTypeWithStruct() {
    80  	vs := suite.vs
    81  
    82  	// {"string": "string",
    83  	//  "list": [false true],
    84  	//  "struct": {"nested": "string"}
    85  	// }
    86  	tstruct := types.NewStruct("", types.StructData{
    87  		"string": types.String("string"),
    88  		"list":   types.NewList(vs).Edit().Append(types.Bool(false)).Append(types.Bool(true)).List(),
    89  		"struct": types.NewStruct("", types.StructData{
    90  			"nested": types.String("string"),
    91  		}),
    92  	})
    93  	o := NomsValueFromDecodedJSON(vs, map[string]interface{}{
    94  		"string": "string",
    95  		"list":   []interface{}{false, true},
    96  		"struct": map[string]interface{}{"nested": "string"},
    97  	}, true)
    98  
    99  	suite.True(tstruct.Equals(o))
   100  }
   101  
   102  func (suite *LibTestSuite) TestPanicOnUnsupportedType() {
   103  	vs := suite.vs
   104  	suite.Panics(func() { NomsValueFromDecodedJSON(vs, map[int]string{1: "one"}, false) }, "Should panic on map[int]string!")
   105  }