github.com/DominikUrban/viper@v0.0.0-20220730150717-aaf74638bd32/internal/encoding/ini/codec_test.go (about)

     1  package ini
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  // original form of the data
     9  const original = `; key-value pair
    10  key=value ; key-value pair
    11  
    12  # map
    13  [map] # map
    14  key=%(key)s
    15  
    16  `
    17  
    18  // encoded form of the data
    19  const encoded = `key=value
    20  
    21  [map]
    22  key=value
    23  
    24  `
    25  
    26  // decoded form of the data
    27  //
    28  // in case of INI it's slightly different from Viper's internal representation
    29  // (eg. top level keys land in a section called default)
    30  var decoded = map[string]interface{}{
    31  	"DEFAULT": map[string]interface{}{
    32  		"key": "value",
    33  	},
    34  	"map": map[string]interface{}{
    35  		"key": "value",
    36  	},
    37  }
    38  
    39  // Viper's internal representation
    40  var data = map[string]interface{}{
    41  	"key": "value",
    42  	"map": map[string]interface{}{
    43  		"key": "value",
    44  	},
    45  }
    46  
    47  func TestCodec_Encode(t *testing.T) {
    48  	t.Run("OK", func(t *testing.T) {
    49  		codec := Codec{}
    50  
    51  		b, err := codec.Encode(data)
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  
    56  		if encoded != string(b) {
    57  			t.Fatalf("decoded value does not match the expected one\nactual:   %#v\nexpected: %#v", string(b), encoded)
    58  		}
    59  	})
    60  
    61  	t.Run("Default", func(t *testing.T) {
    62  		codec := Codec{}
    63  
    64  		data := map[string]interface{}{
    65  			"default": map[string]interface{}{
    66  				"key": "value",
    67  			},
    68  			"map": map[string]interface{}{
    69  				"key": "value",
    70  			},
    71  		}
    72  
    73  		b, err := codec.Encode(data)
    74  		if err != nil {
    75  			t.Fatal(err)
    76  		}
    77  
    78  		if encoded != string(b) {
    79  			t.Fatalf("decoded value does not match the expected one\nactual:   %#v\nexpected: %#v", string(b), encoded)
    80  		}
    81  	})
    82  }
    83  
    84  func TestCodec_Decode(t *testing.T) {
    85  	t.Run("OK", func(t *testing.T) {
    86  		codec := Codec{}
    87  
    88  		v := map[string]interface{}{}
    89  
    90  		err := codec.Decode([]byte(original), v)
    91  		if err != nil {
    92  			t.Fatal(err)
    93  		}
    94  
    95  		if !reflect.DeepEqual(decoded, v) {
    96  			t.Fatalf("decoded value does not match the expected one\nactual:   %#v\nexpected: %#v", v, decoded)
    97  		}
    98  	})
    99  
   100  	t.Run("InvalidData", func(t *testing.T) {
   101  		codec := Codec{}
   102  
   103  		v := map[string]interface{}{}
   104  
   105  		err := codec.Decode([]byte(`invalid data`), v)
   106  		if err == nil {
   107  			t.Fatal("expected decoding to fail")
   108  		}
   109  
   110  		t.Logf("decoding failed as expected: %s", err)
   111  	})
   112  }