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

     1  package storage_test
     2  
     3  import (
     4  	"github.com/tomwright/dasel/storage"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  var jsonBytes = []byte(`{
    10    "name": "Tom"
    11  }
    12  `)
    13  var jsonMap = map[string]interface{}{
    14  	"name": "Tom",
    15  }
    16  
    17  func TestJSONParser_FromBytes(t *testing.T) {
    18  	t.Run("Valid", func(t *testing.T) {
    19  		got, err := (&storage.JSONParser{}).FromBytes(jsonBytes)
    20  		if err != nil {
    21  			t.Errorf("unexpected error: %s", err)
    22  			return
    23  		}
    24  		exp := &storage.BasicSingleDocument{Value: jsonMap}
    25  		if !reflect.DeepEqual(exp, got) {
    26  			t.Errorf("expected %v, got %v", exp, got)
    27  		}
    28  	})
    29  	t.Run("ValidMultiDocument", func(t *testing.T) {
    30  		got, err := (&storage.JSONParser{}).FromBytes(jsonBytesMulti)
    31  		if err != nil {
    32  			t.Errorf("unexpected error: %s", err)
    33  			return
    34  		}
    35  		exp := &storage.BasicMultiDocument{
    36  			Values: jsonMapMulti,
    37  		}
    38  		if !reflect.DeepEqual(exp, got) {
    39  			t.Errorf("expected %v, got %v", jsonMap, got)
    40  		}
    41  	})
    42  	t.Run("ValidMultiDocumentMixed", func(t *testing.T) {
    43  		got, err := (&storage.JSONParser{}).FromBytes(jsonBytesMultiMixed)
    44  		if err != nil {
    45  			t.Errorf("unexpected error: %s", err)
    46  			return
    47  		}
    48  		exp := &storage.BasicMultiDocument{
    49  			Values: jsonMapMultiMixed,
    50  		}
    51  		if !reflect.DeepEqual(exp, got) {
    52  			t.Errorf("expected %v, got %v", jsonMap, got)
    53  		}
    54  	})
    55  	t.Run("Empty", func(t *testing.T) {
    56  		got, err := (&storage.JSONParser{}).FromBytes([]byte(``))
    57  		if err != nil {
    58  			t.Errorf("unexpected error: %s", err)
    59  			return
    60  		}
    61  		if !reflect.DeepEqual(nil, got) {
    62  			t.Errorf("expected %v, got %v", nil, got)
    63  		}
    64  	})
    65  }
    66  
    67  func TestJSONParser_FromBytes_Error(t *testing.T) {
    68  	_, err := (&storage.JSONParser{}).FromBytes(yamlBytes)
    69  	if err == nil {
    70  		t.Errorf("expected error but got none")
    71  		return
    72  	}
    73  }
    74  
    75  func TestJSONParser_ToBytes(t *testing.T) {
    76  	t.Run("Valid", func(t *testing.T) {
    77  		got, err := (&storage.JSONParser{}).ToBytes(jsonMap)
    78  		if err != nil {
    79  			t.Errorf("unexpected error: %s", err)
    80  			return
    81  		}
    82  		if string(jsonBytes) != string(got) {
    83  			t.Errorf("expected %v, got %v", string(jsonBytes), string(got))
    84  		}
    85  	})
    86  
    87  	t.Run("ValidSingle", func(t *testing.T) {
    88  		got, err := (&storage.JSONParser{}).ToBytes(&storage.BasicSingleDocument{Value: jsonMap})
    89  		if err != nil {
    90  			t.Errorf("unexpected error: %s", err)
    91  			return
    92  		}
    93  		if string(jsonBytes) != string(got) {
    94  			t.Errorf("expected %v, got %v", string(jsonBytes), string(got))
    95  		}
    96  	})
    97  
    98  	t.Run("ValidSingleNoPrettyPrint", func(t *testing.T) {
    99  		res, err := (&storage.JSONParser{}).ToBytes(&storage.BasicSingleDocument{Value: jsonMap}, storage.PrettyPrintOption(false))
   100  		if err != nil {
   101  			t.Errorf("unexpected error: %s", err)
   102  			return
   103  		}
   104  		got := string(res)
   105  		exp := `{"name":"Tom"}
   106  `
   107  		if exp != got {
   108  			t.Errorf("expected %v, got %v", exp, got)
   109  		}
   110  	})
   111  
   112  	t.Run("ValidSingleColourise", func(t *testing.T) {
   113  		got, err := (&storage.JSONParser{}).ToBytes(&storage.BasicSingleDocument{Value: jsonMap}, storage.ColouriseOption(true))
   114  		if err != nil {
   115  			t.Errorf("unexpected error: %s", err)
   116  			return
   117  		}
   118  		expBuf, _ := storage.Colourise(`{
   119    "name": "Tom"
   120  }
   121  `, "json")
   122  		exp := expBuf.Bytes()
   123  		if !reflect.DeepEqual(exp, got) {
   124  			t.Errorf("expected %v, got %v", exp, got)
   125  		}
   126  	})
   127  
   128  	t.Run("ValidSingleCustomIndent", func(t *testing.T) {
   129  		res, err := (&storage.JSONParser{}).ToBytes(&storage.BasicSingleDocument{Value: jsonMap}, storage.IndentOption("   "))
   130  		if err != nil {
   131  			t.Errorf("unexpected error: %s", err)
   132  			return
   133  		}
   134  		got := string(res)
   135  		exp := `{
   136     "name": "Tom"
   137  }
   138  `
   139  		if exp != got {
   140  			t.Errorf("expected %v, got %v", exp, got)
   141  		}
   142  	})
   143  
   144  	t.Run("ValidMulti", func(t *testing.T) {
   145  		got, err := (&storage.JSONParser{}).ToBytes(&storage.BasicMultiDocument{Values: jsonMapMulti})
   146  		if err != nil {
   147  			t.Errorf("unexpected error: %s", err)
   148  			return
   149  		}
   150  		if string(jsonBytesMulti) != string(got) {
   151  			t.Errorf("expected %v, got %v", string(jsonBytesMulti), string(got))
   152  		}
   153  	})
   154  
   155  	t.Run("ValidMultiMixed", func(t *testing.T) {
   156  		got, err := (&storage.JSONParser{}).ToBytes(&storage.BasicMultiDocument{Values: jsonMapMultiMixed})
   157  		if err != nil {
   158  			t.Errorf("unexpected error: %s", err)
   159  			return
   160  		}
   161  		if string(jsonBytesMultiMixed) != string(got) {
   162  			t.Errorf("expected %v, got %v", string(jsonBytesMultiMixed), string(got))
   163  		}
   164  	})
   165  }
   166  
   167  var jsonBytesMulti = []byte(`{
   168    "name": "Tom"
   169  }
   170  {
   171    "name": "Ellis"
   172  }
   173  `)
   174  
   175  var jsonMapMulti = []interface{}{
   176  	map[string]interface{}{"name": "Tom"},
   177  	map[string]interface{}{"name": "Ellis"},
   178  }
   179  
   180  var jsonBytesMultiMixed = []byte(`{
   181    "name": "Tom",
   182    "other": true
   183  }
   184  {
   185    "name": "Ellis"
   186  }
   187  `)
   188  
   189  var jsonMapMultiMixed = []interface{}{
   190  	map[string]interface{}{"name": "Tom", "other": true},
   191  	map[string]interface{}{"name": "Ellis"},
   192  }