wa-lang.org/wazero@v1.0.2/imports/proxywasm/internal/_serde_test.go (about)

     1  // Copyright 2021 Tetrate
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internal
    16  
    17  import (
    18  	"strconv"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  var mapSerdeTestCases = []struct {
    25  	maps  [][2]string
    26  	bytes []byte
    27  }{
    28  	{
    29  		maps: [][2]string{{"a", "A"}},
    30  		bytes: []byte{
    31  			1, 0, 0, 0,
    32  			1, 0, 0, 0,
    33  			1, 0, 0, 0,
    34  			97, 0, 65, 0,
    35  		},
    36  	},
    37  	{
    38  		maps: [][2]string{{"a", "A"}, {"b", "B"}},
    39  		bytes: []byte{
    40  			2, 0, 0, 0,
    41  			1, 0, 0, 0,
    42  			1, 0, 0, 0,
    43  			1, 0, 0, 0,
    44  			1, 0, 0, 0,
    45  			97, 0, 65, 0,
    46  			98, 0, 66, 0,
    47  		},
    48  	},
    49  	{
    50  		maps: [][2]string{{"a", "ABCDEFG"}, {"@AB", "<1234"}},
    51  		bytes: []byte{
    52  			2, 0, 0, 0,
    53  			1, 0, 0, 0,
    54  			7, 0, 0, 0,
    55  			3, 0, 0, 0,
    56  			5, 0, 0, 0,
    57  			97, 0,
    58  			65, 66, 67, 68, 69, 70, 71, 0,
    59  			64, 65, 66, 0,
    60  			60, 49, 50, 51, 52, 0,
    61  		},
    62  	},
    63  }
    64  
    65  func TestDeserializeMap(t *testing.T) {
    66  	for i, c := range mapSerdeTestCases {
    67  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    68  			require.Equal(t, c.maps, DeserializeMap(c.bytes))
    69  		})
    70  	}
    71  }
    72  
    73  func TestSerializeMap(t *testing.T) {
    74  	for i, c := range mapSerdeTestCases {
    75  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    76  			require.Equal(t, c.bytes, SerializeMap(c.maps))
    77  		})
    78  	}
    79  }
    80  
    81  func TestSerializePropertyPath(t *testing.T) {
    82  	for i, c := range []struct {
    83  		path []string
    84  		exp  []byte
    85  	}{
    86  		{
    87  			path: []string{"path", "to", "a"},
    88  			exp: []byte{
    89  				'p', 'a', 't', 'h', 0,
    90  				't', 'o', 0,
    91  				'a',
    92  			},
    93  		},
    94  		{
    95  			path: []string{"a", "b"},
    96  			exp:  []byte{'a', 0, 'b'},
    97  		},
    98  		{
    99  			exp: []byte{},
   100  		},
   101  	} {
   102  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   103  			require.Equal(t, c.exp, SerializePropertyPath(c.path))
   104  		})
   105  	}
   106  }