...

Source file src/github.com/pelletier/go-toml/v2/fast_test.go

Documentation: github.com/pelletier/go-toml/v2

     1  package toml_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/pelletier/go-toml/v2"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestFastSimpleInt(t *testing.T) {
    11  	m := map[string]int64{}
    12  	err := toml.Unmarshal([]byte(`a = 42`), &m)
    13  	require.NoError(t, err)
    14  	require.Equal(t, map[string]int64{"a": 42}, m)
    15  }
    16  
    17  func TestFastSimpleFloat(t *testing.T) {
    18  	m := map[string]float64{}
    19  	err := toml.Unmarshal([]byte("a = 42\nb = 1.1\nc = 12341234123412341234123412341234"), &m)
    20  	require.NoError(t, err)
    21  	require.Equal(t, map[string]float64{"a": 42, "b": 1.1, "c": 1.2341234123412342e+31}, m)
    22  }
    23  
    24  func TestFastSimpleString(t *testing.T) {
    25  	m := map[string]string{}
    26  	err := toml.Unmarshal([]byte(`a = "hello"`), &m)
    27  	require.NoError(t, err)
    28  	require.Equal(t, map[string]string{"a": "hello"}, m)
    29  }
    30  
    31  func TestFastSimpleInterface(t *testing.T) {
    32  	m := map[string]interface{}{}
    33  	err := toml.Unmarshal([]byte(`
    34  	a = "hello"
    35  	b = 42`), &m)
    36  	require.NoError(t, err)
    37  	require.Equal(t, map[string]interface{}{
    38  		"a": "hello",
    39  		"b": int64(42),
    40  	}, m)
    41  }
    42  
    43  func TestFastMultipartKeyInterface(t *testing.T) {
    44  	m := map[string]interface{}{}
    45  	err := toml.Unmarshal([]byte(`
    46  	a.interim = "test"
    47  	a.b.c = "hello"
    48  	b = 42`), &m)
    49  	require.NoError(t, err)
    50  	require.Equal(t, map[string]interface{}{
    51  		"a": map[string]interface{}{
    52  			"interim": "test",
    53  			"b": map[string]interface{}{
    54  				"c": "hello",
    55  			},
    56  		},
    57  		"b": int64(42),
    58  	}, m)
    59  }
    60  
    61  func TestFastExistingMap(t *testing.T) {
    62  	m := map[string]interface{}{
    63  		"ints": map[string]int{},
    64  	}
    65  	err := toml.Unmarshal([]byte(`
    66  	ints.one = 1
    67  	ints.two = 2
    68  	strings.yo = "hello"`), &m)
    69  	require.NoError(t, err)
    70  	require.Equal(t, map[string]interface{}{
    71  		"ints": map[string]interface{}{
    72  			"one": int64(1),
    73  			"two": int64(2),
    74  		},
    75  		"strings": map[string]interface{}{
    76  			"yo": "hello",
    77  		},
    78  	}, m)
    79  }
    80  
    81  func TestFastArrayTable(t *testing.T) {
    82  	b := []byte(`
    83  	[root]
    84  	[[root.nested]]
    85  	name = 'Bob'
    86  	[[root.nested]]
    87  	name = 'Alice'
    88  	`)
    89  
    90  	m := map[string]interface{}{}
    91  
    92  	err := toml.Unmarshal(b, &m)
    93  	require.NoError(t, err)
    94  
    95  	require.Equal(t, map[string]interface{}{
    96  		"root": map[string]interface{}{
    97  			"nested": []interface{}{
    98  				map[string]interface{}{
    99  					"name": "Bob",
   100  				},
   101  				map[string]interface{}{
   102  					"name": "Alice",
   103  				},
   104  			},
   105  		},
   106  	}, m)
   107  }
   108  

View as plain text