git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/uuid/json_test.go (about)

     1  // Copyright 2016 Google Inc.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package uuid
     6  
     7  import (
     8  	"encoding/json"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  var testUUID = Must(Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479"))
    14  
    15  func TestJSON(t *testing.T) {
    16  	type S struct {
    17  		ID1 UUID
    18  		ID2 UUID
    19  	}
    20  	s1 := S{ID1: testUUID}
    21  	data, err := json.Marshal(&s1)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	var s2 S
    26  	if err := json.Unmarshal(data, &s2); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if !reflect.DeepEqual(&s1, &s2) {
    30  		t.Errorf("got %#v, want %#v", s2, s1)
    31  	}
    32  }
    33  
    34  func TestJSONUnmarshal(t *testing.T) {
    35  	type S struct {
    36  		ID1 UUID
    37  		ID2 UUID `json:"ID2,omitempty"`
    38  	}
    39  
    40  	testCases := map[string]struct {
    41  		data           []byte
    42  		expectedError  error
    43  		expectedResult UUID
    44  	}{
    45  		"success": {
    46  			data:           []byte(`{"ID1": "f47ac10b-58cc-0372-8567-0e02b2c3d479"}`),
    47  			expectedError:  nil,
    48  			expectedResult: testUUID,
    49  		},
    50  		"zero": {
    51  			data:           []byte(`{"ID1": "00000000-0000-0000-0000-000000000000"}`),
    52  			expectedError:  nil,
    53  			expectedResult: Nil,
    54  		},
    55  		"null": {
    56  			data:           []byte(`{"ID1": null}`),
    57  			expectedError:  nil,
    58  			expectedResult: Nil,
    59  		},
    60  		"empty": {
    61  			data:           []byte(`{"ID1": ""}`),
    62  			expectedError:  invalidLengthError{len: 0},
    63  			expectedResult: Nil,
    64  		},
    65  		"omitempty": {
    66  			data:           []byte(`{"ID2": ""}`),
    67  			expectedError:  invalidLengthError{len: 0},
    68  			expectedResult: Nil,
    69  		},
    70  	}
    71  
    72  	for name, tc := range testCases {
    73  		t.Run(name, func(t *testing.T) {
    74  			var s S
    75  			if err := json.Unmarshal(tc.data, &s); err != tc.expectedError {
    76  				t.Errorf("unexpected error: got %v, want %v", err, tc.expectedError)
    77  			}
    78  			if !reflect.DeepEqual(s.ID1, tc.expectedResult) {
    79  				t.Errorf("got %#v, want %#v", s.ID1, tc.expectedResult)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func BenchmarkUUID_MarshalJSON(b *testing.B) {
    86  	x := &struct {
    87  		UUID UUID `json:"uuid"`
    88  	}{}
    89  	var err error
    90  	x.UUID, err = Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
    91  	if err != nil {
    92  		b.Fatal(err)
    93  	}
    94  	for i := 0; i < b.N; i++ {
    95  		js, err := json.Marshal(x)
    96  		if err != nil {
    97  			b.Fatalf("marshal json: %#v (%v)", js, err)
    98  		}
    99  	}
   100  }
   101  
   102  func BenchmarkUUID_UnmarshalJSON(b *testing.B) {
   103  	js := []byte(`{"uuid":"f47ac10b-58cc-0372-8567-0e02b2c3d479"}`)
   104  	var x *struct {
   105  		UUID UUID `json:"uuid"`
   106  	}
   107  	for i := 0; i < b.N; i++ {
   108  		err := json.Unmarshal(js, &x)
   109  		if err != nil {
   110  			b.Fatalf("marshal json: %#v (%v)", js, err)
   111  		}
   112  	}
   113  }