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

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