github.com/newrelic/go-agent@v3.26.0+incompatible/internal/jsonx/encode_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package jsonx
     5  
     6  import (
     7  	"bytes"
     8  	"math"
     9  	"testing"
    10  )
    11  
    12  func TestAppendFloat(t *testing.T) {
    13  	buf := &bytes.Buffer{}
    14  
    15  	err := AppendFloat(buf, math.NaN())
    16  	if err == nil {
    17  		t.Error("AppendFloat(NaN) should return an error")
    18  	}
    19  
    20  	err = AppendFloat(buf, math.Inf(1))
    21  	if err == nil {
    22  		t.Error("AppendFloat(+Inf) should return an error")
    23  	}
    24  
    25  	err = AppendFloat(buf, math.Inf(-1))
    26  	if err == nil {
    27  		t.Error("AppendFloat(-Inf) should return an error")
    28  	}
    29  }
    30  
    31  func TestAppendFloats(t *testing.T) {
    32  	buf := &bytes.Buffer{}
    33  
    34  	AppendFloatArray(buf)
    35  	if want, got := "[]", buf.String(); want != got {
    36  		t.Errorf("AppendFloatArray(buf)=%q want=%q", got, want)
    37  	}
    38  
    39  	buf.Reset()
    40  	AppendFloatArray(buf, 3.14)
    41  	if want, got := "[3.14]", buf.String(); want != got {
    42  		t.Errorf("AppendFloatArray(buf)=%q want=%q", got, want)
    43  	}
    44  
    45  	buf.Reset()
    46  	AppendFloatArray(buf, 1, 2)
    47  	if want, got := "[1,2]", buf.String(); want != got {
    48  		t.Errorf("AppendFloatArray(buf)=%q want=%q", got, want)
    49  	}
    50  }
    51  
    52  func TestAppendInt(t *testing.T) {
    53  	buf := &bytes.Buffer{}
    54  
    55  	AppendInt(buf, 42)
    56  	if got := buf.String(); got != "42" {
    57  		t.Errorf("AppendUint(42) = %#q want %#q", got, "42")
    58  	}
    59  
    60  	buf.Reset()
    61  	AppendInt(buf, -42)
    62  	if got := buf.String(); got != "-42" {
    63  		t.Errorf("AppendUint(-42) = %#q want %#q", got, "-42")
    64  	}
    65  }
    66  
    67  func TestAppendIntArray(t *testing.T) {
    68  	buf := &bytes.Buffer{}
    69  
    70  	AppendIntArray(buf)
    71  	if want, got := "[]", buf.String(); want != got {
    72  		t.Errorf("AppendIntArray(buf)=%q want=%q", got, want)
    73  	}
    74  
    75  	buf.Reset()
    76  	AppendIntArray(buf, 42)
    77  	if want, got := "[42]", buf.String(); want != got {
    78  		t.Errorf("AppendIntArray(buf)=%q want=%q", got, want)
    79  	}
    80  
    81  	buf.Reset()
    82  	AppendIntArray(buf, 1, -2)
    83  	if want, got := "[1,-2]", buf.String(); want != got {
    84  		t.Errorf("AppendIntArray(buf)=%q want=%q", got, want)
    85  	}
    86  
    87  	buf.Reset()
    88  	AppendIntArray(buf, 1, -2, 0)
    89  	if want, got := "[1,-2,0]", buf.String(); want != got {
    90  		t.Errorf("AppendIntArray(buf)=%q want=%q", got, want)
    91  	}
    92  }
    93  
    94  func TestAppendUint(t *testing.T) {
    95  	buf := &bytes.Buffer{}
    96  
    97  	AppendUint(buf, 42)
    98  	if got := buf.String(); got != "42" {
    99  		t.Errorf("AppendUint(42) = %#q want %#q", got, "42")
   100  	}
   101  }
   102  
   103  func TestAppendUintArray(t *testing.T) {
   104  	buf := &bytes.Buffer{}
   105  
   106  	AppendUintArray(buf)
   107  	if want, got := "[]", buf.String(); want != got {
   108  		t.Errorf("AppendUintArray(buf)=%q want=%q", got, want)
   109  	}
   110  
   111  	buf.Reset()
   112  	AppendUintArray(buf, 42)
   113  	if want, got := "[42]", buf.String(); want != got {
   114  		t.Errorf("AppendUintArray(buf)=%q want=%q", got, want)
   115  	}
   116  
   117  	buf.Reset()
   118  	AppendUintArray(buf, 1, 2)
   119  	if want, got := "[1,2]", buf.String(); want != got {
   120  		t.Errorf("AppendUintArray(buf)=%q want=%q", got, want)
   121  	}
   122  
   123  	buf.Reset()
   124  	AppendUintArray(buf, 1, 2, 3)
   125  	if want, got := "[1,2,3]", buf.String(); want != got {
   126  		t.Errorf("AppendUintArray(buf)=%q want=%q", got, want)
   127  	}
   128  }
   129  
   130  var encodeStringTests = []struct {
   131  	in  string
   132  	out string
   133  }{
   134  	{"\x00", `"\u0000"`},
   135  	{"\x01", `"\u0001"`},
   136  	{"\x02", `"\u0002"`},
   137  	{"\x03", `"\u0003"`},
   138  	{"\x04", `"\u0004"`},
   139  	{"\x05", `"\u0005"`},
   140  	{"\x06", `"\u0006"`},
   141  	{"\x07", `"\u0007"`},
   142  	{"\x08", `"\u0008"`},
   143  	{"\x09", `"\t"`},
   144  	{"\x0a", `"\n"`},
   145  	{"\x0b", `"\u000b"`},
   146  	{"\x0c", `"\u000c"`},
   147  	{"\x0d", `"\r"`},
   148  	{"\x0e", `"\u000e"`},
   149  	{"\x0f", `"\u000f"`},
   150  	{"\x10", `"\u0010"`},
   151  	{"\x11", `"\u0011"`},
   152  	{"\x12", `"\u0012"`},
   153  	{"\x13", `"\u0013"`},
   154  	{"\x14", `"\u0014"`},
   155  	{"\x15", `"\u0015"`},
   156  	{"\x16", `"\u0016"`},
   157  	{"\x17", `"\u0017"`},
   158  	{"\x18", `"\u0018"`},
   159  	{"\x19", `"\u0019"`},
   160  	{"\x1a", `"\u001a"`},
   161  	{"\x1b", `"\u001b"`},
   162  	{"\x1c", `"\u001c"`},
   163  	{"\x1d", `"\u001d"`},
   164  	{"\x1e", `"\u001e"`},
   165  	{"\x1f", `"\u001f"`},
   166  	{"\\", `"\\"`},
   167  	{`"`, `"\""`},
   168  	{"the\u2028quick\t\nbrown\u2029fox", `"the\u2028quick\t\nbrown\u2029fox"`},
   169  }
   170  
   171  func TestAppendString(t *testing.T) {
   172  	buf := &bytes.Buffer{}
   173  
   174  	for _, tt := range encodeStringTests {
   175  		buf.Reset()
   176  
   177  		AppendString(buf, tt.in)
   178  		if got := buf.String(); got != tt.out {
   179  			t.Errorf("AppendString(%q) = %#q, want %#q", tt.in, got, tt.out)
   180  		}
   181  	}
   182  }