...

Source file src/github.com/spf13/viper/internal/encoding/javaproperties/codec_test.go

Documentation: github.com/spf13/viper/internal/encoding/javaproperties

     1  package javaproperties
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  // original form of the data.
    11  const original = `#key-value pair
    12  key = value
    13  map.key = value
    14  `
    15  
    16  // encoded form of the data.
    17  const encoded = `key = value
    18  map.key = value
    19  `
    20  
    21  // data is Viper's internal representation.
    22  var data = map[string]any{
    23  	"key": "value",
    24  	"map": map[string]any{
    25  		"key": "value",
    26  	},
    27  }
    28  
    29  func TestCodec_Encode(t *testing.T) {
    30  	codec := Codec{}
    31  
    32  	b, err := codec.Encode(data)
    33  	require.NoError(t, err)
    34  
    35  	assert.Equal(t, encoded, string(b))
    36  }
    37  
    38  func TestCodec_Decode(t *testing.T) {
    39  	t.Run("OK", func(t *testing.T) {
    40  		codec := Codec{}
    41  
    42  		v := map[string]any{}
    43  
    44  		err := codec.Decode([]byte(original), v)
    45  		require.NoError(t, err)
    46  
    47  		assert.Equal(t, data, v)
    48  	})
    49  
    50  	t.Run("InvalidData", func(t *testing.T) {
    51  		t.Skip("TODO: needs invalid data example")
    52  
    53  		codec := Codec{}
    54  
    55  		v := map[string]any{}
    56  
    57  		codec.Decode([]byte(``), v)
    58  
    59  		assert.Empty(t, v)
    60  	})
    61  }
    62  
    63  func TestCodec_DecodeEncode(t *testing.T) {
    64  	codec := Codec{}
    65  
    66  	v := map[string]any{}
    67  
    68  	err := codec.Decode([]byte(original), v)
    69  	require.NoError(t, err)
    70  
    71  	b, err := codec.Encode(data)
    72  	require.NoError(t, err)
    73  
    74  	assert.Equal(t, original, string(b))
    75  }
    76  

View as plain text