github.com/tomwright/dasel@v1.27.3/storage/toml_test.go (about)

     1  package storage_test
     2  
     3  import (
     4  	"github.com/tomwright/dasel/storage"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  var tomlBytes = []byte(`names = ["John", "Frank"]
    12  
    13  [person]
    14    name = "Tom"
    15  `)
    16  var tomlMap = map[string]interface{}{
    17  	"person": map[string]interface{}{
    18  		"name": "Tom",
    19  	},
    20  	"names": []interface{}{"John", "Frank"},
    21  }
    22  
    23  func TestTOMLParser_FromBytes(t *testing.T) {
    24  	t.Run("Valid", func(t *testing.T) {
    25  		got, err := (&storage.TOMLParser{}).FromBytes(tomlBytes)
    26  		if err != nil {
    27  			t.Errorf("unexpected error: %s", err)
    28  			return
    29  		}
    30  		if !reflect.DeepEqual(&storage.BasicSingleDocument{Value: tomlMap}, got) {
    31  			t.Errorf("expected %v, got %v", tomlMap, got)
    32  		}
    33  	})
    34  	t.Run("Invalid", func(t *testing.T) {
    35  		_, err := (&storage.TOMLParser{}).FromBytes([]byte(`x:x`))
    36  		if err == nil || !strings.Contains(err.Error(), "could not unmarshal data") {
    37  			t.Errorf("unexpected error: %v", err)
    38  			return
    39  		}
    40  	})
    41  }
    42  
    43  func TestTOMLParser_ToBytes(t *testing.T) {
    44  	t.Run("Default", func(t *testing.T) {
    45  		got, err := (&storage.TOMLParser{}).ToBytes(tomlMap)
    46  		if err != nil {
    47  			t.Errorf("unexpected error: %s", err)
    48  			return
    49  		}
    50  		if string(tomlBytes) != string(got) {
    51  			t.Errorf("expected:\n%s\ngot:\n%s", tomlBytes, got)
    52  		}
    53  	})
    54  	t.Run("SingleDocument", func(t *testing.T) {
    55  		got, err := (&storage.TOMLParser{}).ToBytes(&storage.BasicSingleDocument{Value: tomlMap})
    56  		if err != nil {
    57  			t.Errorf("unexpected error: %s", err)
    58  			return
    59  		}
    60  		if string(tomlBytes) != string(got) {
    61  			t.Errorf("expected:\n%s\ngot:\n%s", tomlBytes, got)
    62  		}
    63  	})
    64  	t.Run("SingleDocumentColourise", func(t *testing.T) {
    65  		got, err := (&storage.TOMLParser{}).ToBytes(&storage.BasicSingleDocument{Value: tomlMap}, storage.ColouriseOption(true))
    66  		if err != nil {
    67  			t.Errorf("unexpected error: %s", err)
    68  			return
    69  		}
    70  
    71  		expBuf, _ := storage.Colourise(string(tomlBytes), "toml")
    72  		exp := expBuf.Bytes()
    73  		if !reflect.DeepEqual(exp, got) {
    74  			t.Errorf("expected %v, got %v", exp, got)
    75  		}
    76  	})
    77  	t.Run("SingleDocumentCustomIndent", func(t *testing.T) {
    78  		res, err := (&storage.TOMLParser{}).ToBytes(&storage.BasicSingleDocument{Value: tomlMap}, storage.IndentOption("   "))
    79  		if err != nil {
    80  			t.Errorf("unexpected error: %s", err)
    81  			return
    82  		}
    83  		got := string(res)
    84  		exp := `names = ["John", "Frank"]
    85  
    86  [person]
    87     name = "Tom"
    88  `
    89  		if exp != got {
    90  			t.Errorf("expected:\n%s\ngot:\n%s", exp, got)
    91  		}
    92  	})
    93  	t.Run("MultiDocument", func(t *testing.T) {
    94  		got, err := (&storage.TOMLParser{}).ToBytes(&storage.BasicMultiDocument{Values: []interface{}{tomlMap, tomlMap}})
    95  		if err != nil {
    96  			t.Errorf("unexpected error: %s", err)
    97  			return
    98  		}
    99  		exp := append([]byte{}, tomlBytes...)
   100  		exp = append(exp, tomlBytes...)
   101  		if string(exp) != string(got) {
   102  			t.Errorf("expected:\n%s\ngot:\n%s", exp, got)
   103  		}
   104  	})
   105  	t.Run("SingleDocumentValue", func(t *testing.T) {
   106  		got, err := (&storage.TOMLParser{}).ToBytes(&storage.BasicSingleDocument{Value: "asd"})
   107  		if err != nil {
   108  			t.Errorf("unexpected error: %s", err)
   109  			return
   110  		}
   111  		exp := `asd
   112  `
   113  		if exp != string(got) {
   114  			t.Errorf("expected:\n%s\ngot:\n%s", exp, got)
   115  		}
   116  	})
   117  	t.Run("DefaultValue", func(t *testing.T) {
   118  		got, err := (&storage.TOMLParser{}).ToBytes("asd")
   119  		if err != nil {
   120  			t.Errorf("unexpected error: %s", err)
   121  			return
   122  		}
   123  		exp := `asd
   124  `
   125  		if exp != string(got) {
   126  			t.Errorf("expected:\n%s\ngot:\n%s", exp, got)
   127  		}
   128  	})
   129  	t.Run("MultiDocumentValue", func(t *testing.T) {
   130  		got, err := (&storage.TOMLParser{}).ToBytes(&storage.BasicMultiDocument{Values: []interface{}{"asd", "123"}})
   131  		if err != nil {
   132  			t.Errorf("unexpected error: %s", err)
   133  			return
   134  		}
   135  		exp := `asd
   136  123
   137  `
   138  		if exp != string(got) {
   139  			t.Errorf("expected:\n%s\ngot:\n%s", exp, got)
   140  		}
   141  	})
   142  	t.Run("time.Time", func(t *testing.T) {
   143  		v, _ := time.Parse(time.RFC3339, "2022-01-02T12:34:56Z")
   144  		got, err := (&storage.TOMLParser{}).ToBytes(v)
   145  		if err != nil {
   146  			t.Errorf("unexpected error: %s", err)
   147  			return
   148  		}
   149  		exp := `2022-01-02T12:34:56Z
   150  `
   151  		if exp != string(got) {
   152  			t.Errorf("expected:\n%s\ngot:\n%s", exp, got)
   153  		}
   154  	})
   155  }