...

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

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

     1  package dotenv
     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  `
    14  
    15  // encoded form of the data.
    16  const encoded = `KEY=value
    17  `
    18  
    19  // data is Viper's internal representation.
    20  var data = map[string]any{
    21  	"KEY": "value",
    22  }
    23  
    24  func TestCodec_Encode(t *testing.T) {
    25  	codec := Codec{}
    26  
    27  	b, err := codec.Encode(data)
    28  	require.NoError(t, err)
    29  
    30  	assert.Equal(t, encoded, string(b))
    31  }
    32  
    33  func TestCodec_Decode(t *testing.T) {
    34  	t.Run("OK", func(t *testing.T) {
    35  		codec := Codec{}
    36  
    37  		v := map[string]any{}
    38  
    39  		err := codec.Decode([]byte(original), v)
    40  		require.NoError(t, err)
    41  
    42  		assert.Equal(t, data, v)
    43  	})
    44  
    45  	t.Run("InvalidData", func(t *testing.T) {
    46  		codec := Codec{}
    47  
    48  		v := map[string]any{}
    49  
    50  		err := codec.Decode([]byte(`invalid data`), v)
    51  		require.Error(t, err)
    52  
    53  		t.Logf("decoding failed as expected: %s", err)
    54  	})
    55  }
    56  

View as plain text