...

Source file src/github.com/pelletier/go-toml/v2/cmd/tomljson/main_test.go

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

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestConvert(t *testing.T) {
    15  	examples := []struct {
    16  		name     string
    17  		input    io.Reader
    18  		expected string
    19  		errors   bool
    20  	}{
    21  		{
    22  			name: "valid toml",
    23  			input: strings.NewReader(`
    24  [mytoml]
    25  a = 42`),
    26  			expected: `{
    27    "mytoml": {
    28      "a": 42
    29    }
    30  }
    31  `,
    32  		},
    33  		{
    34  			name:   "invalid toml",
    35  			input:  strings.NewReader(`bad = []]`),
    36  			errors: true,
    37  		},
    38  		{
    39  			name:   "bad reader",
    40  			input:  &badReader{},
    41  			errors: true,
    42  		},
    43  	}
    44  
    45  	for _, e := range examples {
    46  		b := new(bytes.Buffer)
    47  		err := convert(e.input, b)
    48  		if e.errors {
    49  			require.Error(t, err)
    50  		} else {
    51  			assert.NoError(t, err)
    52  			assert.Equal(t, e.expected, b.String())
    53  		}
    54  	}
    55  }
    56  
    57  type badReader struct{}
    58  
    59  func (r *badReader) Read([]byte) (int, error) {
    60  	return 0, fmt.Errorf("reader failed on purpose")
    61  }
    62  

View as plain text