github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/mxjson/valid_test.go (about)

     1  package mxjson_test
     2  
     3  import "testing"
     4  
     5  func TestBasicMapsBoolean(t *testing.T) {
     6  	tests := []testCase{
     7  		{
     8  			Json:     `{"foo": true}`,
     9  			Expected: `{"foo":true}`,
    10  		},
    11  		{
    12  			Json:     `{'foo': false}`,
    13  			Expected: `{"foo":false}`,
    14  		},
    15  		{
    16  			Json:     `{"foo":true}`,
    17  			Expected: `{"foo":true}`,
    18  		},
    19  
    20  		{
    21  			Json:     `{"1foo": true, "2foo": false}`,
    22  			Expected: `{"1foo":true,"2foo":false}`,
    23  		},
    24  		{
    25  			Json:     `{"1foo": false, "2foo": true, "3foo": false}`,
    26  			Expected: `{"1foo":false,"2foo":true,"3foo":false}`,
    27  		},
    28  
    29  		{
    30  			Json:     `{"1": {"11foo": true} }`,
    31  			Expected: `{"1":{"11foo":true}}`,
    32  		},
    33  		{
    34  			Json:     `{"1": {"11foo": true, "12foo": false} }`,
    35  			Expected: `{"1":{"11foo":true,"12foo":false}}`,
    36  		},
    37  		{
    38  			Json:     `{"1": {"11foo": false, "12foo": true}, "2": {"21foo": true, "22foo": false} }`,
    39  			Expected: `{"1":{"11foo":false,"12foo":true},"2":{"21foo":true,"22foo":false}}`,
    40  		},
    41  	}
    42  
    43  	runTestCases(t, tests)
    44  }
    45  
    46  func TestBasicMapsNumbers(t *testing.T) {
    47  	tests := []testCase{
    48  		{
    49  			Json:     `{"foo": 1}`,
    50  			Expected: `{"foo":1}`,
    51  		},
    52  		{
    53  			Json:     `{'foo': 1 }`,
    54  			Expected: `{"foo":1}`,
    55  		},
    56  		{
    57  			Json:     `{"foo":1}`,
    58  			Expected: `{"foo":1}`,
    59  		},
    60  
    61  		{
    62  			Json:     `{"1foo": 1, "2foo": 2}`,
    63  			Expected: `{"1foo":1,"2foo":2}`,
    64  		},
    65  		{
    66  			Json:     `{"1foo": 1, "2foo": 2, "3foo": 3}`,
    67  			Expected: `{"1foo":1,"2foo":2,"3foo":3}`,
    68  		},
    69  
    70  		{
    71  			Json:     `{"1": {"11foo": 11} }`,
    72  			Expected: `{"1":{"11foo":11}}`,
    73  		},
    74  		{
    75  			Json:     `{"1": {"11foo": 11, "12foo": 12} }`,
    76  			Expected: `{"1":{"11foo":11,"12foo":12}}`,
    77  		},
    78  		{
    79  			Json:     `{"1": {"11foo": 11, "12foo": 12}, "2": {"21foo": 21, "22foo": 22} }`,
    80  			Expected: `{"1":{"11foo":11,"12foo":12},"2":{"21foo":21,"22foo":22}}`,
    81  		},
    82  
    83  		{
    84  			Json:     `{"1": {"11foo": 1.1, "12foo": 1.2}, "2": {"21foo": 2.1, "22foo": 2.2} }`,
    85  			Expected: `{"1":{"11foo":1.1,"12foo":1.2},"2":{"21foo":2.1,"22foo":2.2}}`,
    86  		},
    87  	}
    88  
    89  	runTestCases(t, tests)
    90  }
    91  
    92  func TestBasicMaps(t *testing.T) {
    93  	tests := []testCase{
    94  		{
    95  			Json:     `{"foo": "bar"}`,
    96  			Expected: `{"foo":"bar"}`,
    97  		},
    98  		{
    99  			Json:     `{'foo': 'bar'}`,
   100  			Expected: `{"foo":"bar"}`,
   101  		},
   102  		{
   103  			Json:     `{"foo": (bar)}`,
   104  			Expected: `{"foo":"bar"}`,
   105  		},
   106  
   107  		{
   108  			Json:     `{"1foo": "1bar", "2foo": "2bar"}`,
   109  			Expected: `{"1foo":"1bar","2foo":"2bar"}`,
   110  		},
   111  		{
   112  			Json:     `{"1foo": "1bar", "2foo": "2bar", "3foo": "3bar"}`,
   113  			Expected: `{"1foo":"1bar","2foo":"2bar","3foo":"3bar"}`,
   114  		},
   115  
   116  		{
   117  			Json:     `{"1": {"11foo": "11bar"} }`,
   118  			Expected: `{"1":{"11foo":"11bar"}}`,
   119  		},
   120  		{
   121  			Json:     `{"1": {"11foo": "11bar", "12foo": "12bar"} }`,
   122  			Expected: `{"1":{"11foo":"11bar","12foo":"12bar"}}`,
   123  		},
   124  		{
   125  			Json:     `{"1": {"11foo": "11bar", "12foo": "12bar"}, "2": {"21foo": "21bar", "22foo": "22bar"} }`,
   126  			Expected: `{"1":{"11foo":"11bar","12foo":"12bar"},"2":{"21foo":"21bar","22foo":"22bar"}}`,
   127  		},
   128  	}
   129  
   130  	runTestCases(t, tests)
   131  }
   132  
   133  func TestBasicArrayBoolean(t *testing.T) {
   134  	tests := []testCase{
   135  		{
   136  			Json:     `[true, false, false, true]`,
   137  			Expected: `[true,false,false,true]`,
   138  		},
   139  	}
   140  
   141  	runTestCases(t, tests)
   142  }
   143  
   144  func TestBasicArrayNumber(t *testing.T) {
   145  	tests := []testCase{
   146  		{
   147  			Json:     `[1, 3, 2, 4]`,
   148  			Expected: `[1,3,2,4]`,
   149  		},
   150  	}
   151  
   152  	runTestCases(t, tests)
   153  }
   154  
   155  func TestBasicArrayString(t *testing.T) {
   156  	tests := []testCase{
   157  		{
   158  			Json:     `["1one", "2two", "3three", "4four"]`,
   159  			Expected: `["1one","2two","3three","4four"]`,
   160  		},
   161  	}
   162  
   163  	runTestCases(t, tests)
   164  }
   165  
   166  func TestQuotedColon(t *testing.T) {
   167  	tests := []testCase{
   168  		{
   169  			Json: `{
   170  						"DynamicDesc": 'out: foobar',
   171  						"Optional": true
   172  					}`,
   173  			Expected: `{"DynamicDesc":"out: foobar","Optional":true}`,
   174  		},
   175  		{
   176  			Json: `{
   177  						"DynamicDesc": "out: foobar",
   178  						"Optional": true
   179  					}`,
   180  			Expected: `{"DynamicDesc":"out: foobar","Optional":true}`,
   181  		},
   182  		{
   183  			Json: `{
   184  						"DynamicDesc": ({ out: foobar }),
   185  						"Optional": true
   186  					}`,
   187  			Expected: `{"DynamicDesc":"{ out: foobar }","Optional":true}`,
   188  		},
   189  	}
   190  
   191  	runTestCases(t, tests)
   192  }
   193  
   194  func TestHungProcess(t *testing.T) {
   195  	tests := []testCase{
   196  		{
   197  			Json: `[
   198  						{
   199  							#"DynamicDesc": ({
   200  							#    systemctl: --help -> @[..Unit Commands:]s -> tabulate: --column-wraps --map --key-inc-hint --split-space
   201  							#}),
   202  							#"Optional": true,
   203  							#"AllowMultiple": false
   204  						}
   205  						#{
   206  							#"DynamicDesc": ({
   207  							#    systemctl: --help -> @[Unit Commands:..]s -> tabulate: --column-wraps --map --key-inc-hint
   208  							#}),
   209  							#"Optional": false,
   210  							#"AllowMultiple": false,
   211  							#"FlagValues": {
   212  							#    ${ autocomplete.systemctl.flags }
   213  							#}
   214  						#}
   215  					]`,
   216  			Expected: `[{}]`,
   217  		},
   218  		{
   219  			Json: `{[
   220  						{
   221  							#"DynamicDesc": ({
   222  							#    systemctl: --help -> @[..Unit Commands:]s -> tabulate: --column-wraps --map --key-inc-hint --split-space
   223  							#}),
   224  							#"Optional": true,
   225  							#"AllowMultiple": false
   226  						}
   227  						#{
   228  							#"DynamicDesc": ({
   229  							#    systemctl: --help -> @[Unit Commands:..]s -> tabulate: --column-wraps --map --key-inc-hint
   230  							#}),
   231  							#"Optional": false,
   232  							#"AllowMultiple": false,
   233  							#"FlagValues": {
   234  							#    ${ autocomplete.systemctl.flags }
   235  							#}
   236  						#}
   237  					]}`,
   238  			Expected: `{"":[{}]}`,
   239  		},
   240  	}
   241  
   242  	runTestCases(t, tests)
   243  }
   244  
   245  func TestComments(t *testing.T) {
   246  	tests := []testCase{
   247  		{
   248  			Json:     "{ \"foo\": \"bar\"\n# \\ \n}",
   249  			Expected: `{"foo":"bar"}`,
   250  		},
   251  	}
   252  
   253  	runTestCases(t, tests)
   254  }