gitee.com/quant1x/gox@v1.7.6/fastjson/handy_test.go (about)

     1  package fastjson
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestGetStringConcurrent(t *testing.T) {
    10  	const concurrency = 4
    11  	data := []byte(largeFixture)
    12  
    13  	ch := make(chan error, concurrency)
    14  
    15  	for i := 0; i < concurrency; i++ {
    16  		go func() {
    17  			s := GetString(data, "non-existing-key")
    18  			if s != "" {
    19  				ch <- fmt.Errorf("unexpected non-empty string got: %q", s)
    20  			}
    21  			ch <- nil
    22  		}()
    23  	}
    24  
    25  	for i := 0; i < concurrency; i++ {
    26  		select {
    27  		case <-time.After(time.Second * 5):
    28  			t.Fatalf("timeout")
    29  		case err := <-ch:
    30  			if err != nil {
    31  				t.Fatalf("unexpected error: %s", err)
    32  			}
    33  		}
    34  	}
    35  }
    36  
    37  func TestGetBytesConcurrent(t *testing.T) {
    38  	const concurrency = 4
    39  	data := []byte(largeFixture)
    40  
    41  	ch := make(chan error, concurrency)
    42  
    43  	for i := 0; i < concurrency; i++ {
    44  		go func() {
    45  			b := GetBytes(data, "non-existing-key")
    46  			if b != nil {
    47  				ch <- fmt.Errorf("unexpected non-empty string got: %q", b)
    48  			}
    49  			ch <- nil
    50  		}()
    51  	}
    52  
    53  	for i := 0; i < concurrency; i++ {
    54  		select {
    55  		case <-time.After(time.Second * 5):
    56  			t.Fatalf("timeout")
    57  		case err := <-ch:
    58  			if err != nil {
    59  				t.Fatalf("unexpected error: %s", err)
    60  			}
    61  		}
    62  	}
    63  }
    64  
    65  func TestGetString(t *testing.T) {
    66  	data := []byte(`{"foo":"bar", "baz": 1234}`)
    67  
    68  	// normal path
    69  	s := GetString(data, "foo")
    70  	if s != "bar" {
    71  		t.Fatalf("unexpected value obtained; got %q; want %q", s, "bar")
    72  	}
    73  
    74  	// non-existing path
    75  	s = GetString(data, "foo", "zzz")
    76  	if s != "" {
    77  		t.Fatalf("unexpected non-empty value obtained: %q", s)
    78  	}
    79  
    80  	// invalid type
    81  	s = GetString(data, "baz")
    82  	if s != "" {
    83  		t.Fatalf("unexpected non-empty value obtained: %q", s)
    84  	}
    85  
    86  	// invalid fastjson
    87  	s = GetString([]byte("invalid fastjson"), "foobar", "baz")
    88  	if s != "" {
    89  		t.Fatalf("unexpected non-empty value obtained: %q", s)
    90  	}
    91  }
    92  
    93  func TestGetBytes(t *testing.T) {
    94  	data := []byte(`{"foo":"bar", "baz": 1234}`)
    95  
    96  	// normal path
    97  	b := GetBytes(data, "foo")
    98  	if string(b) != "bar" {
    99  		t.Fatalf("unexpected value obtained; got %q; want %q", b, "bar")
   100  	}
   101  
   102  	// non-existing path
   103  	b = GetBytes(data, "foo", "zzz")
   104  	if b != nil {
   105  		t.Fatalf("unexpected non-empty value obtained: %q", b)
   106  	}
   107  
   108  	// invalid type
   109  	b = GetBytes(data, "baz")
   110  	if b != nil {
   111  		t.Fatalf("unexpected non-empty value obtained: %q", b)
   112  	}
   113  
   114  	// invalid fastjson
   115  	b = GetBytes([]byte("invalid fastjson"), "foobar", "baz")
   116  	if b != nil {
   117  		t.Fatalf("unexpected non-empty value obtained: %q", b)
   118  	}
   119  }
   120  
   121  func TestGetInt(t *testing.T) {
   122  	data := []byte(`{"foo":"bar", "baz": 1234}`)
   123  
   124  	// normal path
   125  	n := GetInt(data, "baz")
   126  	if n != 1234 {
   127  		t.Fatalf("unexpected value obtained; got %d; want %d", n, 1234)
   128  	}
   129  
   130  	// non-existing path
   131  	n = GetInt(data, "foo", "zzz")
   132  	if n != 0 {
   133  		t.Fatalf("unexpected non-zero value obtained: %d", n)
   134  	}
   135  
   136  	// invalid type
   137  	n = GetInt(data, "foo")
   138  	if n != 0 {
   139  		t.Fatalf("unexpected non-zero value obtained: %d", n)
   140  	}
   141  
   142  	// invalid fastjson
   143  	n = GetInt([]byte("invalid fastjson"), "foobar", "baz")
   144  	if n != 0 {
   145  		t.Fatalf("unexpected non-empty value obtained: %d", n)
   146  	}
   147  }
   148  
   149  func TestGetFloat64(t *testing.T) {
   150  	data := []byte(`{"foo":"bar", "baz": 12.34}`)
   151  
   152  	// normal path
   153  	f := GetFloat64(data, "baz")
   154  	if f != 12.34 {
   155  		t.Fatalf("unexpected value obtained; got %f; want %f", f, 12.34)
   156  	}
   157  
   158  	// non-existing path
   159  	f = GetFloat64(data, "foo", "zzz")
   160  	if f != 0 {
   161  		t.Fatalf("unexpected non-zero value obtained: %f", f)
   162  	}
   163  
   164  	// invalid type
   165  	f = GetFloat64(data, "foo")
   166  	if f != 0 {
   167  		t.Fatalf("unexpected non-zero value obtained: %f", f)
   168  	}
   169  
   170  	// invalid fastjson
   171  	f = GetFloat64([]byte("invalid fastjson"), "foobar", "baz")
   172  	if f != 0 {
   173  		t.Fatalf("unexpected non-empty value obtained: %f", f)
   174  	}
   175  }
   176  
   177  func TestGetBool(t *testing.T) {
   178  	data := []byte(`{"foo":"bar", "baz": true}`)
   179  
   180  	// normal path
   181  	b := GetBool(data, "baz")
   182  	if !b {
   183  		t.Fatalf("unexpected value obtained; got %v; want %v", b, true)
   184  	}
   185  
   186  	// non-existing path
   187  	b = GetBool(data, "foo", "zzz")
   188  	if b {
   189  		t.Fatalf("unexpected true value obtained")
   190  	}
   191  
   192  	// invalid type
   193  	b = GetBool(data, "foo")
   194  	if b {
   195  		t.Fatalf("unexpected true value obtained")
   196  	}
   197  
   198  	// invalid fastjson
   199  	b = GetBool([]byte("invalid fastjson"), "foobar", "baz")
   200  	if b {
   201  		t.Fatalf("unexpected true value obtained")
   202  	}
   203  }
   204  
   205  func TestExists(t *testing.T) {
   206  	data := []byte(`{"foo": [{"bar": 1234, "baz": 0}]}`)
   207  
   208  	if !Exists(data, "foo") {
   209  		t.Fatalf("cannot find foo")
   210  	}
   211  	if !Exists(data, "foo", "0") {
   212  		t.Fatalf("cannot find foo[0]")
   213  	}
   214  	if !Exists(data, "foo", "0", "baz") {
   215  		t.Fatalf("cannot find foo[0].baz")
   216  	}
   217  
   218  	if Exists(data, "foobar") {
   219  		t.Fatalf("found unexpected foobar")
   220  	}
   221  	if Exists(data, "foo", "1") {
   222  		t.Fatalf("found unexpected foo[1]")
   223  	}
   224  	if Exists(data, "foo", "0", "234") {
   225  		t.Fatalf("found unexpected foo[0][234]")
   226  	}
   227  	if Exists(data, "foo", "bar") {
   228  		t.Fatalf("found unexpected foo.bar")
   229  	}
   230  
   231  	if Exists([]byte(`invalid JSON`), "foo", "bar") {
   232  		t.Fatalf("Exists returned true on invalid fastjson")
   233  	}
   234  }
   235  
   236  func TestParse(t *testing.T) {
   237  	v, err := Parse(`{"foo": "bar"}`)
   238  	if err != nil {
   239  		t.Fatalf("unexpected error: %s", err)
   240  	}
   241  	str := v.String()
   242  	if str != `{"foo":"bar"}` {
   243  		t.Fatalf("unexpected value parsed: %q; want %q", str, `{"foo":"bar"}`)
   244  	}
   245  }
   246  
   247  func TestParseBytes(t *testing.T) {
   248  	v, err := ParseBytes([]byte(`{"foo": "bar"}`))
   249  	if err != nil {
   250  		t.Fatalf("unexpected error: %s", err)
   251  	}
   252  	str := v.String()
   253  	if str != `{"foo":"bar"}` {
   254  		t.Fatalf("unexpected value parsed: %q; want %q", str, `{"foo":"bar"}`)
   255  	}
   256  }
   257  
   258  func TestMustParse(t *testing.T) {
   259  	s := `{"foo":"bar"}`
   260  	v := MustParse(s)
   261  	str := v.String()
   262  	if str != s {
   263  		t.Fatalf("unexpected value parsed; %q; want %q", str, s)
   264  	}
   265  
   266  	v = MustParseBytes([]byte(s))
   267  	if str != s {
   268  		t.Fatalf("unexpected value parsed; %q; want %q", str, s)
   269  	}
   270  }