github.com/fufuok/utils@v1.0.10/xjson/jsongen/jsongen_test.go (about)

     1  // Package jsongen forked from darjun/json-gen
     2  package jsongen
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  	"testing"
     8  )
     9  
    10  var jsStr = `{"a":3,"b":[4,true]}`
    11  
    12  func TestRawString(t *testing.T) {
    13  	testCases := []struct {
    14  		value    RawString
    15  		expected string
    16  	}{
    17  		{"1234", "1234"},
    18  		{"12.34", "12.34"},
    19  	}
    20  
    21  	for _, c := range testCases {
    22  		if string(c.value.Serialize(nil)) != c.expected {
    23  			t.Errorf("actual(%s) != expected(%s)", string(c.value.Serialize(nil)), c.expected)
    24  		}
    25  	}
    26  }
    27  
    28  func TestRawBytes(t *testing.T) {
    29  	testCases := []struct {
    30  		value    RawBytes
    31  		expected string
    32  	}{
    33  		{[]byte("1234"), "1234"},
    34  		{[]byte("12.34"), "12.34"},
    35  	}
    36  
    37  	for _, c := range testCases {
    38  		if string(c.value.Serialize(nil)) != c.expected {
    39  			t.Errorf("actual(%s) != expected(%s)", string(c.value.Serialize(nil)), c.expected)
    40  		}
    41  	}
    42  }
    43  
    44  func TestUnquotedValue(t *testing.T) {
    45  	testCases := []struct {
    46  		value    UnquotedValue
    47  		expected string
    48  	}{
    49  		{"1234", "1234"},
    50  		{"12.34", "12.34"},
    51  	}
    52  
    53  	for _, c := range testCases {
    54  		if string(c.value.Serialize(nil)) != c.expected {
    55  			t.Errorf("actual(%s) != expected(%s)", string(c.value.Serialize(nil)), c.expected)
    56  		}
    57  	}
    58  }
    59  
    60  func TestQuotedValue(t *testing.T) {
    61  	testCases := []struct {
    62  		value    QuotedValue
    63  		expected string
    64  	}{
    65  		{QuotedValue("string"), `"string"`},
    66  		{QuotedValue(`string with \`), `"string with \"`},
    67  		{QuotedValue(`string with "`), `"string with ""`},
    68  	}
    69  
    70  	for _, c := range testCases {
    71  		if string(c.value.Serialize(nil)) != c.expected {
    72  			t.Errorf("actual(%s) != expected(%s)", string(c.value.Serialize(nil)), c.expected)
    73  		}
    74  	}
    75  }
    76  
    77  func TestGenJSON(t *testing.T) {
    78  	js := NewMap()
    79  	js.PutRawString("raw", jsStr)
    80  	js.PutString("multiline", "x  \n  y  \n   \t\n  ")
    81  	bs := js.Serialize(nil)
    82  	var v map[string]interface{}
    83  	if err := json.Unmarshal(bs, &v); err != nil {
    84  		t.Fatal("Invalid JSON")
    85  	}
    86  }
    87  
    88  func array1() (*Array, string) {
    89  	a1 := NewArray()
    90  	a1.AppendUint(123)
    91  	a1.AppendInt(-45)
    92  	a1.AppendFloat(12.34)
    93  	a1.AppendBool(true)
    94  	a1.AppendString("test string")
    95  	a1.AppendString(`string with \`)
    96  	a1.AppendString(`string with "`)
    97  	a1.AppendRawString(jsStr)
    98  	a1.AppendRawBytes([]byte(jsStr))
    99  	a1.AppendRawStringArray([]string{jsStr, jsStr})
   100  	a1.AppendRawBytesArray([][]byte{[]byte(jsStr), []byte(jsStr)})
   101  	expected1 := `[123,-45,12.34,true,"test string","string with \\","string with \"",` +
   102  		`{"a":3,"b":[4,true]},{"a":3,"b":[4,true]},` +
   103  		`[{"a":3,"b":[4,true]},{"a":3,"b":[4,true]}],[{"a":3,"b":[4,true]},{"a":3,"b":[4,true]}]]`
   104  
   105  	return a1, expected1
   106  }
   107  
   108  func array2() (*Array, string) {
   109  	a2 := NewArray()
   110  	a2.AppendUintArray([]uint64{123, 456, 789})
   111  	a2.AppendIntArray([]int64{-12, -45, -78})
   112  	a2.AppendFloatArray([]float64{12.34, -56.78, 9.0})
   113  	a2.AppendBoolArray([]bool{true, false, true})
   114  	a2.AppendStringArray([]string{"test string", `string with \`, `string with "`})
   115  	expected2 := `[[123,456,789],[-12,-45,-78],[12.34,-56.78,9],[true,false,true],["test string","string with \\","string with \""]]`
   116  
   117  	return a2, expected2
   118  }
   119  
   120  func array3() (*Array, string) {
   121  	a3 := NewArray()
   122  	m1 := NewMap()
   123  	m1.PutUint("uintkey", 123)
   124  	m1.PutInt("intkey", -456)
   125  	m1.PutFloat("floatkey", 12.34)
   126  	m1.PutBool("boolkey", true)
   127  	m1.PutString("stringkey", "test string")
   128  	a3.AppendMap(m1)
   129  
   130  	m2 := NewMap()
   131  	m2.PutUint("uintkey", 456)
   132  	m2.PutInt("intkey", -789)
   133  	m2.PutFloat("floatkey", 56.78)
   134  	m2.PutBool("boolkey", false)
   135  	m2.PutString("stringkey", `string with \`)
   136  	a3.AppendMap(m2)
   137  	expected3 := `[{"uintkey":123,"intkey":-456,"floatkey":12.34,"boolkey":true,"stringkey":"test string"},{"uintkey":456,"intkey":-789,"floatkey":56.78,"boolkey":false,"stringkey":"string with \\"}]`
   138  	return a3, expected3
   139  }
   140  
   141  func array4() (*Array, string) {
   142  	a4 := NewArray()
   143  	a1, expected1 := array1()
   144  	a2, expected2 := array2()
   145  	a3, expected3 := array3()
   146  	a4.AppendArray(*a1)
   147  	a4.AppendArray(*a2)
   148  	a4.AppendArray(*a3)
   149  	expected4 := fmt.Sprintf("[%s,%s,%s]", expected1, expected2, expected3)
   150  
   151  	return a4, expected4
   152  }
   153  
   154  func TestArrayValue(t *testing.T) {
   155  	a1, expected1 := array1()
   156  	a2, expected2 := array2()
   157  	a3, expected3 := array3()
   158  	a4, expected4 := array4()
   159  
   160  	testCases := []struct {
   161  		name     string
   162  		value    *Array
   163  		expected string
   164  	}{
   165  		{"basic", a1, expected1},
   166  		{"primitive array", a2, expected2},
   167  		{"map array", a3, expected3},
   168  		{"nested general array", a4, expected4},
   169  	}
   170  
   171  	for _, c := range testCases {
   172  		data := c.value.Serialize(nil)
   173  		if string(data) != c.expected {
   174  			t.Errorf("array name:%s actual:%s != expected:%s", c.name, string(data), c.expected)
   175  		}
   176  
   177  		if len(data) != c.value.Size() {
   178  			t.Errorf("array name:%s buf size error, actual:%d, expected:%d", c.name, len(data), c.value.Size())
   179  		}
   180  
   181  		var obj []interface{}
   182  		if err := json.Unmarshal(data, &obj); err != nil {
   183  			t.Errorf("array name:%s unmarshal error:%v", c.name, err)
   184  		}
   185  	}
   186  }
   187  
   188  func map1() (*Map, string) {
   189  	m1 := NewMap()
   190  	m1.PutUint("uintkey", 123)
   191  	m1.PutInt("intkey", -45)
   192  	m1.PutFloat("floatkey", 12.34)
   193  	m1.PutBool("boolkey", true)
   194  	m1.PutString("stringkey1", "teststring")
   195  	m1.PutString("stringkey2", `string with \`)
   196  	m1.PutString("stringkey3", `string with "`)
   197  	m1.PutRawString("raw_string", jsStr)
   198  	m1.PutRawBytes("raw_bytes", []byte(jsStr))
   199  	m1.PutRawStringArray("raw_sarr", []string{jsStr, jsStr})
   200  	m1.PutRawBytesArray("raw_barr", [][]byte{[]byte(jsStr), []byte(jsStr)})
   201  	expected1 := `{"uintkey":123,"intkey":-45,"floatkey":12.34,"boolkey":true,"stringkey1":"teststring",` +
   202  		`"stringkey2":"string with \\","stringkey3":"string with \"",` +
   203  		`"raw_string":{"a":3,"b":[4,true]},"raw_bytes":{"a":3,"b":[4,true]},` +
   204  		`"raw_sarr":[{"a":3,"b":[4,true]},{"a":3,"b":[4,true]}],"raw_barr":[{"a":3,"b":[4,true]},{"a":3,"b":[4,true]}]}`
   205  
   206  	return m1, expected1
   207  }
   208  
   209  func map2() (*Map, string) {
   210  	m2 := NewMap()
   211  	m2.PutUintArray("uintarray", []uint64{123, 456, 789})
   212  	m2.PutIntArray("intarray", []int64{-23, -45, -89})
   213  	m2.PutFloatArray("floatarray", []float64{12.34, -56.78, 90})
   214  	m2.PutBoolArray("boolarray", []bool{true, false, true})
   215  	m2.PutStringArray("stringarray", []string{"test string", `string with \`, `string with "`})
   216  	expected2 := `{"uintarray":[123,456,789],"intarray":[-23,-45,-89],"floatarray":[12.34,-56.78,90],"boolarray":[true,false,true],"stringarray":["test string","string with \\","string with \""]}`
   217  
   218  	return m2, expected2
   219  }
   220  
   221  func map3() (*Map, string) {
   222  	m3 := NewMap()
   223  
   224  	a1, expected1 := array1()
   225  	a2, expected2 := array2()
   226  	a3, expected3 := array3()
   227  	a4, expected4 := array4()
   228  
   229  	m3.PutArray("array1", a1)
   230  	m3.PutArray("array2", a2)
   231  	m3.PutArray("array3", a3)
   232  	m3.PutArray("array4", a4)
   233  
   234  	expected := fmt.Sprintf(`{"array1":%s,"array2":%s,"array3":%s,"array4":%s}`, expected1, expected2, expected3, expected4)
   235  
   236  	return m3, expected
   237  }
   238  
   239  func map4() (*Map, string) {
   240  	m4 := NewMap()
   241  
   242  	m1, expected1 := map1()
   243  	m2, expected2 := map2()
   244  	m3, expected3 := map3()
   245  
   246  	m4.PutMap("map1", m1)
   247  	m4.PutMap("map2", m2)
   248  	m4.PutMap("map3", m3)
   249  
   250  	expected := fmt.Sprintf(`{"map1":%s,"map2":%s,"map3":%s}`, expected1, expected2, expected3)
   251  
   252  	return m4, expected
   253  }
   254  
   255  func TestMapValue(t *testing.T) {
   256  	m1, expected1 := map1()
   257  	m2, expected2 := map2()
   258  	m3, expected3 := map3()
   259  	m4, expected4 := map4()
   260  
   261  	testCases := []struct {
   262  		name     string
   263  		value    *Map
   264  		expected string
   265  	}{
   266  		{"basic", m1, expected1},
   267  		{"primitive array", m2, expected2},
   268  		{"general array", m3, expected3},
   269  		{"nested map", m4, expected4},
   270  	}
   271  
   272  	for _, c := range testCases {
   273  		data := c.value.Serialize(nil)
   274  		if string(data) != c.expected {
   275  			t.Errorf("map name:%s actual:%s != expected:%s", c.name, string(data), c.expected)
   276  		}
   277  
   278  		if len(data) != c.value.Size() {
   279  			t.Errorf("map name:%s buf size error, actual:%d, expected:%d", c.name, len(data), c.value.Size())
   280  		}
   281  
   282  		var obj map[string]interface{}
   283  		if err := json.Unmarshal(data, &obj); err != nil {
   284  			t.Errorf("map name:%s unmarshal error:%v", c.name, err)
   285  		}
   286  	}
   287  }