github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/communication/codec_json_test.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package communication
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  type customPayload struct {
    27  	Field int
    28  }
    29  
    30  var _ Codec = &codecJSON{}
    31  
    32  func TestCodecJSONPack(t *testing.T) {
    33  	table := []struct {
    34  		payload      interface{}
    35  		expectedData string
    36  	}{
    37  		{`hello`, `"hello"`},
    38  		{`hello "name"`, `"hello \"name\""`},
    39  		{true, `true`},
    40  		{nil, `null`},
    41  		{10, `10`},
    42  		{10.20, `10.2`},
    43  		{&customPayload{123}, `{"Field":123}`},
    44  		{&customPayload{}, `{"Field":0}`},
    45  	}
    46  
    47  	codec := codecJSON{}
    48  	for _, tt := range table {
    49  		data, err := codec.Pack(tt.payload)
    50  
    51  		assert.NoError(t, err)
    52  		assert.Exactly(t, []byte(tt.expectedData), data)
    53  	}
    54  }
    55  
    56  const (
    57  	typeString = iota
    58  	typeBool
    59  	typeInt
    60  	typeFloat
    61  	typeCustom
    62  )
    63  
    64  func TestCodecJSONUnpack(t *testing.T) {
    65  	table := []struct {
    66  		data            string
    67  		payloadType     int
    68  		expectedPayload interface{}
    69  	}{
    70  		{`"hello"`, typeString, `hello`},
    71  		{`"hello \"name\""`, typeString, `hello "name"`},
    72  		{`true`, typeBool, true},
    73  		{`null`, typeBool, nil},
    74  		{`10`, typeInt, 10},
    75  		{`10.2`, typeFloat, 10.20},
    76  		{`{"Field":123}`, typeCustom, &customPayload{123}},
    77  		{`{"Field":0}`, typeCustom, &customPayload{}},
    78  	}
    79  
    80  	codec := codecJSON{}
    81  	for _, tt := range table {
    82  		switch tt.payloadType {
    83  		case typeString:
    84  			var payload string
    85  			err := codec.Unpack([]byte(tt.data), &payload)
    86  
    87  			assert.NoError(t, err)
    88  			assert.Exactly(t, tt.expectedPayload, payload)
    89  
    90  		case typeBool:
    91  			var payload interface{}
    92  			err := codec.Unpack([]byte(tt.data), &payload)
    93  
    94  			assert.NoError(t, err)
    95  			assert.Exactly(t, tt.expectedPayload, payload)
    96  
    97  		case typeInt:
    98  			var payload int
    99  			err := codec.Unpack([]byte(tt.data), &payload)
   100  
   101  			assert.NoError(t, err)
   102  			assert.Exactly(t, tt.expectedPayload, payload)
   103  
   104  		case typeFloat:
   105  			var payload float64
   106  			err := codec.Unpack([]byte(tt.data), &payload)
   107  
   108  			assert.NoError(t, err)
   109  			assert.Exactly(t, tt.expectedPayload, payload)
   110  
   111  		case typeCustom:
   112  			var payload *customPayload
   113  			err := codec.Unpack([]byte(tt.data), &payload)
   114  
   115  			assert.NoError(t, err)
   116  			assert.Exactly(t, tt.expectedPayload, payload)
   117  
   118  		default:
   119  			t.Fatalf("Unknown type: %d", tt.payloadType)
   120  		}
   121  	}
   122  }