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

     1  package lang_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/lang"
     7  	"github.com/lmorg/murex/test/count"
     8  	"github.com/lmorg/murex/utils/json"
     9  )
    10  
    11  type testElementLookupT struct {
    12  	Object   interface{}
    13  	Path     string
    14  	Expected interface{}
    15  	Error    bool
    16  }
    17  
    18  func TestElementLookup(t *testing.T) {
    19  	tests := []testElementLookupT{
    20  		{
    21  			Object: []string{
    22  				"foo", "bar",
    23  			},
    24  			Path:     "/1",
    25  			Expected: "bar",
    26  		},
    27  		{
    28  			Object: []string{
    29  				"foo", "bar",
    30  			},
    31  			Path:     ".1",
    32  			Expected: "bar",
    33  		},
    34  		{
    35  			Object: []string{
    36  				"foo", "bar",
    37  			},
    38  			Path:  ".-1",
    39  			Error: true,
    40  		},
    41  
    42  		/////
    43  
    44  		{
    45  			Object: []interface{}{
    46  				"foo", "bar",
    47  			},
    48  			Path:     "/1",
    49  			Expected: "bar",
    50  		},
    51  		{
    52  			Object: []interface{}{
    53  				"foo", "bar",
    54  			},
    55  			Path:     ".1",
    56  			Expected: "bar",
    57  		},
    58  		{
    59  			Object: []interface{}{
    60  				"foo", "bar",
    61  			},
    62  			Path:  ".-1",
    63  			Error: true,
    64  		},
    65  
    66  		/////
    67  
    68  		{
    69  			Object: map[string]string{
    70  				"Simpsons":   "Homer",
    71  				"Futurama":   "Bender",
    72  				"South Park": "Cartman",
    73  			},
    74  			Path:     "/Simpsons",
    75  			Expected: "Homer",
    76  		},
    77  		{
    78  			Object: map[string]string{
    79  				"Simpsons":   "Homer",
    80  				"Futurama":   "Bender",
    81  				"South Park": "Cartman",
    82  			},
    83  			Path:     ",futurama",
    84  			Expected: "Bender",
    85  		},
    86  		{
    87  			Object: map[string]string{
    88  				"Simpsons":   "Homer",
    89  				"Futurama":   "Bender",
    90  				"SOUTH PARK": "Cartman",
    91  			},
    92  			Path:     ".South Park",
    93  			Expected: "Cartman",
    94  		},
    95  		{
    96  			Object: map[string]string{
    97  				"Simpsons":   "Homer",
    98  				"Futurama":   "Bender",
    99  				"south park": "Cartman",
   100  			},
   101  			Path:     ".South Park",
   102  			Expected: "Cartman",
   103  		},
   104  
   105  		/////
   106  
   107  		{
   108  			Object: map[string]interface{}{
   109  				"Simpsons":   "Homer",
   110  				"Futurama":   "Bender",
   111  				"South Park": "Cartman",
   112  			},
   113  			Path:     "/Simpsons",
   114  			Expected: "Homer",
   115  		},
   116  		{
   117  			Object: map[string]interface{}{
   118  				"Simpsons":   "Homer",
   119  				"Futurama":   "Bender",
   120  				"South Park": "Cartman",
   121  			},
   122  			Path:     ",futurama",
   123  			Expected: "Bender",
   124  		},
   125  		{
   126  			Object: map[string]interface{}{
   127  				"Simpsons":   "Homer",
   128  				"Futurama":   "Bender",
   129  				"SOUTH PARK": "Cartman",
   130  			},
   131  			Path:     ".South Park",
   132  			Expected: "Cartman",
   133  		},
   134  		{
   135  			Object: map[string]interface{}{
   136  				"Simpsons":   "Homer",
   137  				"Futurama":   "Bender",
   138  				"south park": "Cartman",
   139  			},
   140  			Path:     ".South Park",
   141  			Expected: "Cartman",
   142  		},
   143  
   144  		/////
   145  
   146  		{
   147  			Object: map[interface{}]interface{}{
   148  				"Simpsons":   "Homer",
   149  				"Futurama":   "Bender",
   150  				"South Park": "Cartman",
   151  			},
   152  			Path:     "/Simpsons",
   153  			Expected: "Homer",
   154  		},
   155  		{
   156  			Object: map[interface{}]interface{}{
   157  				"Simpsons":   "Homer",
   158  				"Futurama":   "Bender",
   159  				"South Park": "Cartman",
   160  			},
   161  			Path:     ",futurama",
   162  			Expected: "Bender",
   163  		},
   164  		{
   165  			Object: map[interface{}]interface{}{
   166  				"Simpsons":   "Homer",
   167  				"Futurama":   "Bender",
   168  				"SOUTH PARK": "Cartman",
   169  			},
   170  			Path:     ".South Park",
   171  			Expected: "Cartman",
   172  		},
   173  		{
   174  			Object: map[interface{}]interface{}{
   175  				"Simpsons":   "Homer",
   176  				"Futurama":   "Bender",
   177  				"south park": "Cartman",
   178  			},
   179  			Path:     ".South Park",
   180  			Expected: "Cartman",
   181  		},
   182  	}
   183  
   184  	count.Tests(t, len(tests))
   185  
   186  	for i, test := range tests {
   187  		expected := json.LazyLogging(test.Expected)
   188  
   189  		v, err := lang.ElementLookup(test.Object, test.Path)
   190  		actual := json.LazyLogging(v)
   191  
   192  		if (err != nil) != test.Error || actual != expected {
   193  			t.Errorf("Test %d failed", i)
   194  			t.Logf("  Object:   %s", json.LazyLogging(test.Object))
   195  			t.Logf("  Path:     '%s'", test.Path)
   196  			t.Logf("  Expected: %s", expected)
   197  			t.Logf("  Actual:   %s", actual)
   198  			t.Logf("  err exp:  %v", test.Error)
   199  			t.Logf("  err act:  %v", err)
   200  		}
   201  	}
   202  }