github.com/m3db/m3@v1.5.0/src/query/util/json/writer_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package json
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"math"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestWriteValues(t *testing.T) {
    34  	testWrite(t, "true", func(w Writer) { w.WriteBool(true) })
    35  	testWrite(t, "false", func(w Writer) { w.WriteBool(false) })
    36  	testWrite(t, "3.145000", func(w Writer) { w.WriteFloat64(3.145) })
    37  	testWrite(t, "null", func(w Writer) { w.WriteFloat64(math.NaN()) })
    38  	testWrite(t, "null", func(w Writer) { w.WriteFloat64(math.Inf(1)) })
    39  	testWrite(t, "null", func(w Writer) { w.WriteFloat64(math.Inf(-1)) })
    40  	testWrite(t, "26756", func(w Writer) { w.WriteInt(26756) })
    41  	testWrite(t, "null", func(w Writer) { w.WriteNull() })
    42  	testWrite(t, "\"Hello\\t \\r \\\" World\"", func(w Writer) {
    43  		w.WriteString("Hello\t \r \" World")
    44  	})
    45  	testWrite(t, "\"Hello\\t \\r \\\" World\"", func(w Writer) {
    46  		w.WriteBytesString([]byte("Hello\t \r \" World"))
    47  	})
    48  	maxTestUTF8Value := 1032
    49  	type utf8FnTest func(w Writer, s string)
    50  	for _, fn := range []utf8FnTest{
    51  		utf8FnTest(func(w Writer, s string) { w.WriteString(s) }),
    52  		utf8FnTest(func(w Writer, s string) { w.WriteBytesString([]byte(s)) }),
    53  	} {
    54  		fn := fn // Capture for lambdas.
    55  		for i := 0; i <= maxTestUTF8Value; i++ {
    56  			i := i
    57  			switch {
    58  			case i == int('"') || i == int('\\'):
    59  				testWrite(t, fmt.Sprintf("\"\\%c\"", rune(i)), func(w Writer) { fn(w, fmt.Sprintf("%c", rune(i))) })
    60  			case i == int('\n'):
    61  				testWrite(t, "\"\\n\"", func(w Writer) { fn(w, fmt.Sprintf("%c", rune(i))) })
    62  			case i == int('\r'):
    63  				testWrite(t, "\"\\r\"", func(w Writer) { fn(w, fmt.Sprintf("%c", rune(i))) })
    64  			case i == int('\t'):
    65  				testWrite(t, "\"\\t\"", func(w Writer) { fn(w, fmt.Sprintf("%c", rune(i))) })
    66  			case i <= 31:
    67  				testWrite(t,
    68  					fmt.Sprintf("\"\\u%s\"", fmt.Sprintf("%U", i)[2:]),
    69  					func(w Writer) { fn(w, fmt.Sprintf("%c", rune(i))) })
    70  			default:
    71  				testWrite(t, fmt.Sprintf("\"%c\"", rune(i)), func(w Writer) { fn(w, fmt.Sprintf("%c", rune(i))) })
    72  			}
    73  		}
    74  	}
    75  }
    76  
    77  func TestWriteObject(t *testing.T) {
    78  	testWrite(t,
    79  		"{\"foo\":null,\"bar\":3.145000,\"zed\":\"Hello World\",\"nan\":null,\"infinity\":null,\"bad\\u0006\":null}",
    80  		func(w Writer) {
    81  			w.BeginObject()
    82  			w.BeginObjectField("foo")
    83  			w.WriteNull()
    84  			w.BeginObjectBytesField([]byte("bar"))
    85  			w.WriteFloat64(3.145)
    86  			w.BeginObjectField("zed")
    87  			w.WriteString("Hello World")
    88  			w.BeginObjectField("nan")
    89  			w.WriteFloat64(math.NaN())
    90  			w.BeginObjectField("infinity")
    91  			w.WriteFloat64(math.Inf(-1))
    92  			w.BeginObjectField("bad\x06")
    93  			w.WriteNull()
    94  			w.EndObject()
    95  		})
    96  }
    97  
    98  func TestWriteArray(t *testing.T) {
    99  	testWrite(t, "[\"Hello World\",3.145000,null,24,false,null,null]", func(w Writer) {
   100  		w.BeginArray()
   101  		w.WriteString("Hello World")
   102  		w.WriteFloat64(3.145)
   103  		w.WriteNull()
   104  		w.WriteInt(24)
   105  		w.WriteBool(false)
   106  		w.WriteFloat64(math.NaN())
   107  		w.WriteFloat64(math.Inf(1))
   108  		w.EndArray()
   109  	})
   110  }
   111  
   112  func TestWriteComplexObject(t *testing.T) {
   113  	testWrite(t, "{\"foo\":{\"bar\":{\"elements\":[\"Hello World\",3.145000,null,24,false],\"empty\":null}}}",
   114  		func(w Writer) {
   115  			w.BeginObject()
   116  			w.BeginObjectField("foo")
   117  			w.BeginObject()
   118  			w.BeginObjectField("bar")
   119  			w.BeginObject()
   120  			w.BeginObjectField("elements")
   121  			w.BeginArray()
   122  			w.WriteString("Hello World")
   123  			w.WriteFloat64(3.145)
   124  			w.WriteNull()
   125  			w.WriteInt(24)
   126  			w.WriteBool(false)
   127  			w.EndArray()
   128  			w.BeginObjectField("empty")
   129  			w.WriteNull()
   130  			w.EndObject()
   131  			w.EndObject()
   132  			w.EndObject()
   133  		})
   134  }
   135  
   136  func TestWriteErrors(t *testing.T) {
   137  	testWriteError(t, "value not allowed", func(w Writer) {
   138  		w.BeginObject()
   139  		w.BeginArray()
   140  	})
   141  	testWriteError(t, "container mismatch", func(w Writer) {
   142  		w.BeginObject()
   143  		w.EndArray()
   144  	})
   145  	testWriteError(t, "container still open", func(w Writer) {
   146  		w.BeginObject()
   147  	})
   148  	testWriteError(t, "not in container", func(w Writer) {
   149  		w.EndObject()
   150  	})
   151  	testWriteError(t, "field not allowed", func(w Writer) {
   152  		w.BeginObjectField("foo")
   153  	})
   154  	testWriteError(t, "field not allowed", func(w Writer) {
   155  		w.BeginObject()
   156  		w.BeginObjectField("foo")
   157  		w.BeginObjectField("bar")
   158  	})
   159  	testWriteError(t, "field not allowed", func(w Writer) {
   160  		w.BeginArray()
   161  		w.BeginObjectField("foo")
   162  	})
   163  	testWriteError(t, "value not allowed", func(w Writer) {
   164  		w.WriteBool(true)
   165  		w.BeginObject()
   166  	})
   167  	testWriteError(t, "value not allowed", func(w Writer) {
   168  		w.BeginObject()
   169  		w.WriteFloat64(100)
   170  	})
   171  }
   172  
   173  func testWriteError(t *testing.T, expected string, f func(w Writer)) {
   174  	t.Helper()
   175  	var buf bytes.Buffer
   176  	w := NewWriter(&buf)
   177  	f(w)
   178  
   179  	err := w.Close()
   180  	require.Error(t, err)
   181  	assert.Equal(t, expected, err.Error())
   182  }
   183  
   184  func testWrite(t *testing.T, expected string, f func(w Writer)) {
   185  	t.Helper()
   186  	var buf bytes.Buffer
   187  	w := NewWriter(&buf)
   188  	f(w)
   189  	require.NoError(t, w.Close(), "error writing %s", expected)
   190  	assert.Equal(t, expected, buf.String())
   191  }