github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/expressions/parse_array_test.go (about)

     1  package expressions
     2  
     3  import (
     4  	"testing"
     5  
     6  	_ "github.com/lmorg/murex/builtins/core/mkarray"
     7  	_ "github.com/lmorg/murex/builtins/types/generic"
     8  	_ "github.com/lmorg/murex/builtins/types/json"
     9  	"github.com/lmorg/murex/lang/expressions/symbols"
    10  	"github.com/lmorg/murex/test"
    11  )
    12  
    13  func TestParseArray(t *testing.T) {
    14  	tests := expTestsT{
    15  		symbol: symbols.ArrayBegin,
    16  		tests: []expTestT{
    17  			{
    18  				input:    `%[1 2 3]`,
    19  				expected: `[1,2,3]`,
    20  				pos:      6,
    21  			},
    22  			{
    23  				input: `%[1 2 3`,
    24  				error: true,
    25  			},
    26  			{
    27  				input:    `%[1  2  3]`,
    28  				expected: `[1,2,3]`,
    29  				pos:      8,
    30  			},
    31  			{
    32  				input:    `%[1,2,3]`,
    33  				expected: `[1,2,3]`,
    34  				pos:      6,
    35  			},
    36  			{
    37  				input:    `%[1, 2, 3]`,
    38  				expected: `[1,2,3]`,
    39  				pos:      8,
    40  			},
    41  			{
    42  				input:    `%[  1  ,  2  ,  3  ]`,
    43  				expected: `[1,2,3]`,
    44  				pos:      18,
    45  			},
    46  			/////
    47  			{
    48  				input:    `%[  foo  ,  bar  ,  baz  ]`,
    49  				expected: `["foo","bar","baz"]`,
    50  				pos:      24,
    51  			},
    52  			/////
    53  			{
    54  				input: `%[  [1 2 3]  ,  [ "foo" "bar" ]`,
    55  				error: true,
    56  			},
    57  			{
    58  				input: `%[%]`,
    59  				error: true,
    60  			},
    61  			{
    62  				input:    `%[%(%)]`,
    63  				expected: `["%"]`,
    64  				pos:      5,
    65  			},
    66  			{
    67  				input:    `%[  [1 2 3]  ,  [ "foo" "bar" ] ]`,
    68  				expected: `[[1,2,3],["foo","bar"]]`,
    69  				pos:      31,
    70  			},
    71  			{
    72  				input:    `%[  %[1 2 3]  ,  %[ "foo" "bar" ] ]`,
    73  				expected: `[[1,2,3],["foo","bar"]]`,
    74  				pos:      33,
    75  			},
    76  			/////
    77  			{
    78  				input:    "%[\n\t1\n\t2\n\t3\n]",
    79  				expected: `[1,2,3]`,
    80  				pos:      11,
    81  			},
    82  			/////
    83  			{
    84  				input:    "%[$TestParseArray]",
    85  				expected: `[null]`,
    86  				pos:      16,
    87  			},
    88  			{
    89  				input:    "%[1,2,$TestParseArray]",
    90  				expected: `[1,2,null]`,
    91  				pos:      20,
    92  			},
    93  			{
    94  				input:    "%[@TestParseArray]",
    95  				expected: `null`,
    96  				pos:      16,
    97  			},
    98  			{
    99  				input:    "%[[@TestParseArray]]",
   100  				expected: `[null]`,
   101  				pos:      18,
   102  			},
   103  			/////
   104  			{
   105  				input:    "%[[mon..wed]]",
   106  				expected: `["mon","tue","wed"]`,
   107  				pos:      11,
   108  			},
   109  			/////
   110  			{
   111  				input:    "%[-2,1,0,3.4]",
   112  				expected: `[-2,1,0,3.4]`,
   113  				pos:      11,
   114  			},
   115  			{
   116  				input:    "%[-2 1 0 3.4]",
   117  				expected: `[-2,1,0,3.4]`,
   118  				pos:      11,
   119  			},
   120  			{
   121  				input:    "%[-]",
   122  				expected: `["-"]`,
   123  				pos:      2,
   124  			},
   125  			{
   126  				input:    "%[-one]",
   127  				expected: `["-one"]`,
   128  				pos:      5,
   129  			},
   130  			{
   131  				input:    "%[{a:1} {b:2}]",
   132  				expected: `[{"a":1},{"b":2}]`,
   133  				pos:      12,
   134  			},
   135  			/////
   136  			{
   137  				input:    "%[1..3]",
   138  				expected: `[1,2,3]`,
   139  				pos:      5,
   140  			},
   141  		},
   142  	}
   143  
   144  	testParserObject(t, tests)
   145  }
   146  
   147  func TestParseArrayBarewords(t *testing.T) {
   148  	tests := expTestsT{
   149  		symbol: symbols.ArrayBegin,
   150  		tests: []expTestT{
   151  			{
   152  				input:    `%[false,null,true]`,
   153  				expected: `[false,null,true]`,
   154  				pos:      16,
   155  			},
   156  			{
   157  				input:    `%[false null true]`,
   158  				expected: `[false,null,true]`,
   159  				pos:      16,
   160  			},
   161  			{
   162  				input:    "%[\nfalse\nnull\ntrue\n]",
   163  				expected: `[false,null,true]`,
   164  				pos:      18,
   165  			},
   166  			{
   167  				input:    `%["false" "null" "true"]`,
   168  				expected: `["false","null","true"]`,
   169  				pos:      22,
   170  			},
   171  		},
   172  	}
   173  
   174  	testParserObject(t, tests)
   175  }
   176  
   177  func TestParseArrayWithArrayVar(t *testing.T) {
   178  	tests := []test.MurexTest{
   179  		{
   180  			Block:  `bob = %{a:1, b:2, c:[a b c]}; %[x y z @bob[c] ]`,
   181  			Stdout: `["x","y","z","a","b","c"]`,
   182  		},
   183  	}
   184  
   185  	test.RunMurexTests(tests, t)
   186  }
   187  
   188  func TestParseArrayObjects(t *testing.T) {
   189  	tests := expTestsT{
   190  		symbol: symbols.ArrayBegin,
   191  		tests: []expTestT{
   192  			{
   193  				input:    `%[{a:1},{b:2},{c:3}]`,
   194  				expected: `[{"a":1},{"b":2},{"c":3}]`,
   195  				pos:      18,
   196  			},
   197  			{
   198  				input: `%[
   199  					{
   200  						a:1
   201  					},
   202  					{
   203  						b:2
   204  					},
   205  					{
   206  						c:3
   207  					}
   208  				]`,
   209  				expected: `[{"a":1},{"b":2},{"c":3}]`,
   210  				pos:      80,
   211  			},
   212  		},
   213  	}
   214  
   215  	testParserObject(t, tests)
   216  }