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

     1  package dasel_test
     2  
     3  import (
     4  	"github.com/tomwright/dasel"
     5  	"testing"
     6  )
     7  
     8  func testFormatNode(value interface{}, format string, exp string) func(t *testing.T) {
     9  	return func(t *testing.T) {
    10  		node := dasel.New(value)
    11  		buf, err := dasel.FormatNode(node, format)
    12  		if err != nil {
    13  			t.Errorf("unexpected error: %v", err)
    14  		}
    15  		got := buf.String()
    16  		if exp != got {
    17  			t.Errorf("expected %s, got %s", exp, got)
    18  		}
    19  	}
    20  }
    21  
    22  func testFormatNodes(values []interface{}, format string, exp string) func(t *testing.T) {
    23  	return func(t *testing.T) {
    24  		nodes := make([]*dasel.Node, len(values))
    25  		for k, v := range values {
    26  			nodes[k] = dasel.New(v)
    27  		}
    28  		buf, err := dasel.FormatNodes(nodes, format)
    29  		if err != nil {
    30  			t.Errorf("unexpected error: %v", err)
    31  		}
    32  		got := buf.String()
    33  		if exp != got {
    34  			t.Errorf("expected %s, got %s", exp, got)
    35  		}
    36  	}
    37  }
    38  
    39  func TestFormatNode(t *testing.T) {
    40  	t.Run("InvalidFormatTemplate", func(t *testing.T) {
    41  		_, err := dasel.FormatNode(nil, "{{")
    42  		if err == nil {
    43  			t.Errorf("expected error but got none")
    44  		}
    45  	})
    46  	t.Run("PropertyAccess", testFormatNode(
    47  		map[string]interface{}{
    48  			"name":  "Tom",
    49  			"email": "contact@tomwright.me",
    50  		},
    51  		`{{ .name }}, {{ .email }}`,
    52  		`Tom, contact@tomwright.me`,
    53  	))
    54  	t.Run("QueryAccess", testFormatNode(
    55  		map[string]interface{}{
    56  			"name":  "Tom",
    57  			"email": "contact@tomwright.me",
    58  		},
    59  		`{{ query ".name" }}, {{ query ".email" }}`,
    60  		`Tom, contact@tomwright.me`,
    61  	))
    62  	t.Run("SelectAccess", testFormatNode(
    63  		map[string]interface{}{
    64  			"name":  "Tom",
    65  			"email": "contact@tomwright.me",
    66  		},
    67  		`{{ select ".name" }}, {{ select ".email" }}`,
    68  		`Tom, contact@tomwright.me`,
    69  	))
    70  	t.Run("Format", testFormatNode(
    71  		map[string]interface{}{
    72  			"name":  "Tom",
    73  			"email": "contact@tomwright.me",
    74  		},
    75  		`{{ query ".name" | format "{{ . }}" }}, {{ query ".email" | format "{{ . }}" }}`,
    76  		`Tom, contact@tomwright.me`,
    77  	))
    78  	t.Run("QueryAccessInvalidSelector", testFormatNode(
    79  		map[string]interface{}{
    80  			"name":  "Tom",
    81  			"email": "contact@tomwright.me",
    82  		},
    83  		`{{ query ".bad" }}`,
    84  		`<nil>`,
    85  	))
    86  	t.Run("QueryMultipleCommaSeparated", testFormatNode(
    87  		map[string]interface{}{
    88  			"users": []map[string]interface{}{
    89  				{
    90  					"name": "Tom",
    91  				},
    92  				{
    93  					"name": "Jim",
    94  				},
    95  				{
    96  					"name": "Frank",
    97  				},
    98  			},
    99  		},
   100  		`{{ queryMultiple ".users.[*]" | format "{{ .name }}{{ if not isLast }},{{ end }}" }}`,
   101  		`Tom,Jim,Frank`,
   102  	))
   103  	t.Run("QueryMultipleLineSeparated", testFormatNode(
   104  		map[string]interface{}{
   105  			"users": []map[string]interface{}{
   106  				{
   107  					"name": "Tom",
   108  				},
   109  				{
   110  					"name": "Jim",
   111  				},
   112  				{
   113  					"name": "Frank",
   114  				},
   115  			},
   116  		},
   117  		`{{ queryMultiple ".users.[*]" | format "{{ .name }}{{ if not isLast }}{{ newline }}{{ end }}" }}`,
   118  		`Tom
   119  Jim
   120  Frank`,
   121  	))
   122  	t.Run("QueryMultipleDashSeparated", testFormatNode(
   123  		map[string]interface{}{
   124  			"users": []map[string]interface{}{
   125  				{
   126  					"name": "Tom",
   127  				},
   128  				{
   129  					"name": "Jim",
   130  				},
   131  				{
   132  					"name": "Frank",
   133  				},
   134  			},
   135  		},
   136  		`{{ queryMultiple ".users.[*]" | format "{{ if not isFirst }}---{{ newline }}{{ end }}{{ .name }}{{ if not isLast }}{{ newline }}{{ end }}" }}`,
   137  		`Tom
   138  ---
   139  Jim
   140  ---
   141  Frank`,
   142  	))
   143  	t.Run("QueryMultipleBadSelector", testFormatNode(
   144  		map[string]interface{}{
   145  			"users": []map[string]interface{}{
   146  				{
   147  					"name": "Tom",
   148  				},
   149  				{
   150  					"name": "Jim",
   151  				},
   152  				{
   153  					"name": "Frank",
   154  				},
   155  			},
   156  		},
   157  		`{{ queryMultiple ".users.[*].names" | format "{{ . }}{{ if not isLast }}{{ newline }}{{ end }}" }}`,
   158  		``,
   159  	))
   160  }
   161  
   162  func TestFormatNodes(t *testing.T) {
   163  	t.Run("InvalidFormatTemplate", func(t *testing.T) {
   164  		_, err := dasel.FormatNodes([]*dasel.Node{dasel.New("")}, "{{")
   165  		if err == nil {
   166  			t.Errorf("expected error but got none")
   167  		}
   168  	})
   169  	t.Run("PropertyAccess", testFormatNodes(
   170  		[]interface{}{
   171  			map[string]interface{}{
   172  				"name":  "Tom",
   173  				"email": "contact@tomwright.me",
   174  			},
   175  			map[string]interface{}{
   176  				"name":  "Jim",
   177  				"email": "jim@gmail.com",
   178  			},
   179  		},
   180  		"{{ .name }}, {{ .email }}{{ if not isLast }}{{ newline }}{{ end }}",
   181  		`Tom, contact@tomwright.me
   182  Jim, jim@gmail.com`,
   183  	))
   184  	t.Run("QueryAccess", testFormatNodes(
   185  		[]interface{}{
   186  			map[string]interface{}{
   187  				"name":  "Tom",
   188  				"email": "contact@tomwright.me",
   189  			},
   190  			map[string]interface{}{
   191  				"name":  "Jim",
   192  				"email": "jim@gmail.com",
   193  			},
   194  		},
   195  		`{{ query ".name" }}, {{ query ".email" }}{{ if not isLast }}{{ newline }}{{ end }}`,
   196  		`Tom, contact@tomwright.me
   197  Jim, jim@gmail.com`))
   198  	t.Run("SelectAccess", testFormatNodes(
   199  		[]interface{}{
   200  			map[string]interface{}{
   201  				"name":  "Tom",
   202  				"email": "contact@tomwright.me",
   203  			},
   204  			map[string]interface{}{
   205  				"name":  "Jim",
   206  				"email": "jim@gmail.com",
   207  			},
   208  		},
   209  		`{{ select ".name" }}, {{ select ".email" }}{{ if not isLast }}{{ newline }}{{ end }}`,
   210  		`Tom, contact@tomwright.me
   211  Jim, jim@gmail.com`))
   212  	t.Run("QueryAccessInvalidSelector", testFormatNodes(
   213  		[]interface{}{
   214  			map[string]interface{}{
   215  				"name":  "Tom",
   216  				"email": "contact@tomwright.me",
   217  			},
   218  			map[string]interface{}{
   219  				"name":  "Jim",
   220  				"email": "jim@gmail.com",
   221  			},
   222  		},
   223  		`{{ query ".bad" }}{{ newline }}`,
   224  		`<nil>
   225  <nil>
   226  `,
   227  	))
   228  }