github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/util/json/to_json_test.go (about)

     1  // Copyright 2019 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  	"bytes"
     9  	"testing"
    10  
    11  	"github.com/attic-labs/noms/go/chunks"
    12  	"github.com/attic-labs/noms/go/types"
    13  	"github.com/stretchr/testify/suite"
    14  )
    15  
    16  func TestToJSONSuite(t *testing.T) {
    17  	suite.Run(t, &ToJSONSuite{})
    18  }
    19  
    20  type ToJSONSuite struct {
    21  	suite.Suite
    22  	vs *types.ValueStore
    23  }
    24  
    25  func (suite *ToJSONSuite) SetupTest() {
    26  	st := &chunks.TestStorage{}
    27  	suite.vs = types.NewValueStore(st.NewView())
    28  }
    29  
    30  func (suite *ToJSONSuite) TearDownTest() {
    31  	suite.vs.Close()
    32  }
    33  
    34  func (suite *ToJSONSuite) TestToJSON() {
    35  	tc := []struct {
    36  		desc     string
    37  		in       types.Value
    38  		opts     ToOptions
    39  		exp      string
    40  		expError string
    41  	}{
    42  		{"true", types.Bool(true), ToOptions{}, "true", ""},
    43  		{"false", types.Bool(false), ToOptions{}, "false", ""},
    44  		{"42", types.Number(42), ToOptions{}, "42", ""},
    45  		{"88.8", types.Number(88.8), ToOptions{}, "88.8", ""},
    46  		{"empty string", types.String(""), ToOptions{}, `""`, ""},
    47  		{"foobar", types.String("foobar"), ToOptions{}, `"foobar"`, ""},
    48  		{"strings with newlines", types.String(`"\nmonkey`), ToOptions{}, `"\"\\nmonkey"`, ""},
    49  		{"structs when not enabled", types.NewStruct("", types.StructData{}), ToOptions{}, "", "Struct marshaling not enabled"},
    50  		{"named struct", types.NewStruct("Person", types.StructData{}), ToOptions{Structs: true}, "", "Named struct marshaling not supported"},
    51  		{"struct nested errors", types.NewStruct("", types.StructData{"foo": types.NewList(suite.vs)}), ToOptions{Structs: true}, "", "List marshaling not enabled"},
    52  		{"empty struct", types.NewStruct("", types.StructData{}), ToOptions{Structs: true}, "{}", ""},
    53  		{"non-empty struct", types.NewStruct("", types.StructData{"str": types.String("bar"), "num": types.Number(42)}), ToOptions{Structs: true}, `{"num":42,"str":"bar"}`, ""},
    54  		{"list when not enabled", types.NewList(suite.vs), ToOptions{}, "", "List marshaling not enabled"},
    55  		{"list nested errors", types.NewList(suite.vs, types.NewSet(suite.vs)), ToOptions{Lists: true}, "", "Set marshaling not enabled"},
    56  		{"empty list", types.NewList(suite.vs), ToOptions{Lists: true}, "[]", ""},
    57  		{"non-empty list", types.NewList(suite.vs, types.Number(42), types.String("foo")), ToOptions{Lists: true}, `[42,"foo"]`, ""},
    58  		{"sets when not enabled", types.NewSet(suite.vs), ToOptions{}, "", "Set marshaling not enabled"},
    59  		{"set nested errors", types.NewSet(suite.vs, types.NewList(suite.vs)), ToOptions{Sets: true}, "", "List marshaling not enabled"},
    60  		{"empty set", types.NewSet(suite.vs), ToOptions{Sets: true}, "[]", ""},
    61  		{"non-empty set", types.NewSet(suite.vs, types.Number(42), types.String("foo")), ToOptions{Sets: true}, `[42,"foo"]`, ""},
    62  		{"maps when not enabled", types.NewMap(suite.vs), ToOptions{}, "", "Map marshaling not enabled"},
    63  		{"map nested errors", types.NewMap(suite.vs, types.String("foo"), types.NewSet(suite.vs)), ToOptions{Maps: true}, "", "Set marshaling not enabled"},
    64  		{"map non-string key", types.NewMap(suite.vs, types.Number(42), types.Number(42)), ToOptions{Maps: true}, "", "Map key kind Number not supported"},
    65  		{"empty map", types.NewMap(suite.vs), ToOptions{Maps: true}, "{}", ""},
    66  		{"non-empty map", types.NewMap(suite.vs, types.String("foo"), types.String("bar"), types.String("baz"), types.Number(42)), ToOptions{Maps: true}, `{"baz":42,"foo":"bar"}`, ""},
    67  		{"complex value", types.NewStruct("", types.StructData{
    68  			"list": types.NewList(suite.vs,
    69  				types.NewSet(suite.vs,
    70  					types.NewMap(suite.vs, types.String("foo"), types.String("bar"), types.String("hot"), types.Number(42))))}), ToOptions{Structs: true, Lists: true, Sets: true, Maps: true}, `{"list":[[{"foo":"bar","hot":42}]]}`, ""},
    71  	}
    72  
    73  	for _, t := range tc {
    74  		buf := &bytes.Buffer{}
    75  		err := ToJSON(t.in, buf, t.opts)
    76  		if t.expError != "" {
    77  			suite.EqualError(err, t.expError, t.desc)
    78  			suite.Equal("", string(buf.Bytes()), t.desc)
    79  		} else {
    80  			suite.NoError(err)
    81  			suite.Equal(t.exp+"\n", string(buf.Bytes()), t.desc)
    82  		}
    83  	}
    84  }