github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/expressions/parse_object_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  )
    11  
    12  func TestParseObject(t *testing.T) {
    13  	tests := expTestsT{
    14  		symbol: symbols.ObjectBegin,
    15  		tests: []expTestT{
    16  			{
    17  				input: `%{foo:}`,
    18  				error: true,
    19  			},
    20  			{
    21  				input:    `%{foo: bar}`,
    22  				expected: `{"foo":"bar"}`,
    23  				pos:      9,
    24  			},
    25  			{
    26  				input:    `%{a:b}`,
    27  				expected: `{"a":"b"}`,
    28  				pos:      4,
    29  			},
    30  			{
    31  				input:    `%{a:[1,2]}`,
    32  				expected: `{"a":[1,2]}`,
    33  				pos:      8,
    34  			},
    35  			{
    36  				input:    `%{a:%[1,2]}`,
    37  				expected: `{"a":[1,2]}`,
    38  				pos:      9,
    39  			},
    40  			{
    41  				input:    `%{neg:-1}`,
    42  				expected: `{"neg":-1}`,
    43  				pos:      7,
    44  			},
    45  			{
    46  				input:    `%{-2:-2,1:1,0:0,3.4:3.4}`,
    47  				expected: `{"-2":-2,"0":0,"1":1,"3.4":3.4}`,
    48  				pos:      22,
    49  			},
    50  			{
    51  				input:    `%{'foo':"bar"}`,
    52  				expected: `{"foo":"bar"}`,
    53  				pos:      12,
    54  			},
    55  			{
    56  				input:    `%{'foo':"bar",a:{1:a,2:b,3:c}}`,
    57  				expected: `{"a":{"1":"a","2":"b","3":"c"},"foo":"bar"}`,
    58  				pos:      28,
    59  			},
    60  			{
    61  				input:    `%{'foo':"bar",a:%{1:a,2:b,3:c}}`,
    62  				expected: `{"a":{"1":"a","2":"b","3":"c"},"foo":"bar"}`,
    63  				pos:      29,
    64  			},
    65  			{
    66  				input:    `%{a:$a,b:@b}`,
    67  				expected: `{"a":null,"b":null}`,
    68  				pos:      10,
    69  			},
    70  			{
    71  				input:    `%{a:$a,b:[@b]}`,
    72  				expected: `{"a":null,"b":null}`,
    73  				pos:      12,
    74  			},
    75  			{
    76  				input:    `%{a:$a,b:%[@b]}`,
    77  				expected: `{"a":null,"b":null}`,
    78  				pos:      13,
    79  			},
    80  			{
    81  				input:    `%{nan:-}`,
    82  				expected: `{"NaN":"-"}`,
    83  				pos:      6,
    84  			},
    85  			{
    86  				input:    `%{nan:-one}`,
    87  				expected: `{"NaN":"-one"}`,
    88  				pos:      9,
    89  			},
    90  		},
    91  	}
    92  
    93  	testParserObject(t, tests)
    94  }
    95  
    96  func TestParseObjectBadGrammar(t *testing.T) {
    97  	tests := expTestsT{
    98  		symbol: symbols.ObjectBegin,
    99  		tests: []expTestT{
   100  			{
   101  				input: `%{foo}`,
   102  				error: true,
   103  			},
   104  			{
   105  				input: `%{foo::bar}`,
   106  				error: true,
   107  			},
   108  			{
   109  				input: `%{foo:bar,,}`,
   110  				//error: true,
   111  				expected: `{"foo":"bar"}`,
   112  				pos:      10,
   113  			},
   114  			{
   115  				input: `%{foo:bar`,
   116  				error: true,
   117  			},
   118  		},
   119  	}
   120  
   121  	testParserObject(t, tests)
   122  }
   123  
   124  func TestParseObjectLf(t *testing.T) {
   125  	tests := expTestsT{
   126  		symbol: symbols.ObjectBegin,
   127  		tests: []expTestT{
   128  			{
   129  				input:    "%{\nfoo:bar}",
   130  				expected: `{"foo":"bar"}`,
   131  				pos:      9,
   132  			},
   133  			{
   134  				input:    "%{\n foo:bar}",
   135  				expected: `{"foo":"bar"}`,
   136  				pos:      10,
   137  			},
   138  		},
   139  	}
   140  
   141  	testParserObject(t, tests)
   142  }
   143  
   144  func TestParseObjectBool(t *testing.T) {
   145  	tests := expTestsT{
   146  		symbol: symbols.ObjectBegin,
   147  		tests: []expTestT{
   148  			{
   149  				input:    "%{foo:true}",
   150  				expected: `{"foo":true}`,
   151  				pos:      9,
   152  			},
   153  			{
   154  				input:    "%{foo:false}",
   155  				expected: `{"foo":false}`,
   156  				pos:      10,
   157  			},
   158  		},
   159  	}
   160  
   161  	testParserObject(t, tests)
   162  }
   163  
   164  // https://github.com/lmorg/murex/issues/781
   165  func TestParseObjectNull(t *testing.T) {
   166  	tests := expTestsT{
   167  		symbol: symbols.ObjectBegin,
   168  		tests: []expTestT{
   169  			{
   170  				input:    `%{foo:null}`,
   171  				expected: `{"foo":null}`,
   172  				pos:      9,
   173  			},
   174  			{
   175  				input:    `%{foo:"null"}`,
   176  				expected: `{"foo":"null"}`,
   177  				pos:      11,
   178  			},
   179  		},
   180  	}
   181  
   182  	testParserObject(t, tests)
   183  }
   184  
   185  // https://github.com/lmorg/murex/issues/781
   186  func TestParseObjectZeroLengthString(t *testing.T) {
   187  	tests := expTestsT{
   188  		symbol: symbols.ObjectBegin,
   189  		tests: []expTestT{
   190  			{
   191  				input:    `%{foo:''}`,
   192  				expected: `{"foo":""}`,
   193  				pos:      7,
   194  			},
   195  			{
   196  				input:    `%{foo:""}`,
   197  				expected: `{"foo":""}`,
   198  				pos:      7,
   199  			},
   200  		},
   201  	}
   202  
   203  	testParserObject(t, tests)
   204  }
   205  
   206  func TestParseObjectNestedString(t *testing.T) {
   207  	tests := expTestsT{
   208  		symbol: symbols.ObjectBegin,
   209  		tests: []expTestT{
   210  			{
   211  				input:    `%{foo:%(hello world)}`,
   212  				expected: `{"foo":"hello world"}`,
   213  				pos:      19,
   214  			},
   215  			{
   216  				input: `
   217  %{foo: %(
   218  	hello world
   219  )}`,
   220  				expected: `{"foo":"\n\thello world\n"}`,
   221  				pos:      24,
   222  			},
   223  		},
   224  	}
   225  
   226  	testParserObject(t, tests)
   227  }