github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/lists/append_test.go (about)

     1  package lists
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	_ "github.com/lmorg/murex/builtins/types/generic"
     8  	_ "github.com/lmorg/murex/builtins/types/json"
     9  	_ "github.com/lmorg/murex/builtins/types/jsonlines"
    10  	"github.com/lmorg/murex/lang/types"
    11  	"github.com/lmorg/murex/test"
    12  )
    13  
    14  /*
    15  	PREPEND
    16  */
    17  
    18  // TestPrependJsonStr tests the p with a JSON array of strings
    19  func TestPrependJsonStr(t *testing.T) {
    20  	test.RunMethodTest(t,
    21  		cmdPrepend, "prepend",
    22  		`["a","b","c"]`,
    23  		types.Json,
    24  		[]string{"new"},
    25  		`["new","a","b","c"]`,
    26  		nil,
    27  	)
    28  }
    29  
    30  // TestPrependJsonInt tests the prepend method with a JSON array of integers
    31  func TestPrependJsonInt(t *testing.T) {
    32  	test.RunMethodTest(t,
    33  		cmdPrepend, "prepend",
    34  		`[1,2,3]`,
    35  		types.Json,
    36  		[]string{"9"},
    37  		`[9,1,2,3]`,
    38  		nil,
    39  	)
    40  }
    41  
    42  // TestPrependJsonMixed tests the prepend method with a JSON array of mixed data types
    43  func TestPrependJsonMixed(t *testing.T) {
    44  	test.RunMethodTest(t,
    45  		cmdPrepend, "prepend",
    46  		`[1,2,3]`,
    47  		types.Json,
    48  		[]string{"new"},
    49  		"", //`["new","1","2","3"]`,
    50  		errors.New("cannot convert 'new' to a floating point number: strconv.ParseFloat: parsing \"new\": invalid syntax"),
    51  	)
    52  }
    53  
    54  // TestPrependGenericStr tests the prepend method with a generic list of strings
    55  func TestPrependGenericStr(t *testing.T) {
    56  	test.RunMethodTest(t,
    57  		cmdPrepend, "prepend",
    58  		"a\nb\nc",
    59  		types.Generic,
    60  		[]string{"new"},
    61  		"new\na\nb\nc\n",
    62  		nil,
    63  	)
    64  }
    65  
    66  // TestPrependGenericInt tests the prepend method with a generic list of integers
    67  func TestPrependGenericInt(t *testing.T) {
    68  	test.RunMethodTest(t,
    69  		cmdPrepend, "prepend",
    70  		"1\n2\n3",
    71  		types.Generic,
    72  		[]string{"9"},
    73  		"9\n1\n2\n3\n",
    74  		nil,
    75  	)
    76  }
    77  
    78  // TestPrependGenericMixed tests the prepend method with a generic list of mixed data types
    79  func TestPrependGenericMixed(t *testing.T) {
    80  	test.RunMethodTest(t,
    81  		cmdPrepend, "prepend",
    82  		"1\n2\n3",
    83  		types.Generic,
    84  		[]string{"new"},
    85  		"new\n1\n2\n3\n",
    86  		nil,
    87  	)
    88  }
    89  
    90  /*
    91  	APPEND
    92  */
    93  
    94  // TestAppendJsonStr tests the append method with a JSON array of strings
    95  func TestAppendJsonStr(t *testing.T) {
    96  	test.RunMethodTest(t,
    97  		cmdAppend, "append",
    98  		`["a","b","c"]`,
    99  		types.Json,
   100  		[]string{"new"},
   101  		`["a","b","c","new"]`,
   102  		nil,
   103  	)
   104  }
   105  
   106  // TestAppendJsonInt tests the append method with a JSON array of integers
   107  func TestAppendJsonInt(t *testing.T) {
   108  	test.RunMethodTest(t,
   109  		cmdAppend, "append",
   110  		`[1,2,3]`,
   111  		types.Json,
   112  		[]string{"9"},
   113  		`[1,2,3,9]`,
   114  		nil,
   115  	)
   116  }
   117  
   118  // TestAppendJsonMixed tests the append method with a JSON array of mixed data types
   119  func TestAppendJsonMixed(t *testing.T) {
   120  	test.RunMethodTest(t,
   121  		cmdAppend, "append",
   122  		`[1,2,3]`,
   123  		types.Json,
   124  		[]string{"new"},
   125  		``,
   126  		errors.New(`cannot convert 'new' to a floating point number: strconv.ParseFloat: parsing "new": invalid syntax`),
   127  	)
   128  }
   129  
   130  // TestAppendGenericStr tests the append method with a generic list of strings
   131  func TestAppendGenericStr(t *testing.T) {
   132  	test.RunMethodTest(t,
   133  		cmdAppend, "append",
   134  		"a\nb\nc",
   135  		types.Generic,
   136  		[]string{"new"},
   137  		"a\nb\nc\nnew\n",
   138  		nil,
   139  	)
   140  }
   141  
   142  // TestAppendGenericInt tests the append method with a generic list of integers
   143  func TestAppendGenericInt(t *testing.T) {
   144  	test.RunMethodTest(t,
   145  		cmdAppend, "append",
   146  		"1\n2\n3",
   147  		types.Generic,
   148  		[]string{"9"},
   149  		"1\n2\n3\n9\n",
   150  		nil,
   151  	)
   152  }
   153  
   154  // TestAppendGenericMixed tests the append method with a generic list of mixed data types
   155  func TestAppendGenericMixed(t *testing.T) {
   156  	test.RunMethodTest(t,
   157  		cmdAppend, "append",
   158  		"1\n2\n3",
   159  		types.Generic,
   160  		[]string{"new"},
   161  		"1\n2\n3\nnew\n",
   162  		nil,
   163  	)
   164  }