github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/base/fill/path_value_test.go (about)

     1  package fill
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestFillPathValue(t *testing.T) {
     8  	c := Collection{}
     9  	err := SetPathValue("name", "Alice", &c)
    10  	if err != nil {
    11  		panic(err)
    12  	}
    13  	if c.Name != "Alice" {
    14  		t.Errorf("expected: s.Name should be \"Alice\"")
    15  	}
    16  
    17  	c = Collection{}
    18  	err = SetPathValue("age", 42, &c)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	if c.Age != 42 {
    23  		t.Errorf("expected: s.Age should be 42")
    24  	}
    25  
    26  	c = Collection{}
    27  	err = SetPathValue("age", "56", &c)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	if c.Age != 56 {
    32  		t.Errorf("expected: s.Age should be 56")
    33  	}
    34  
    35  	c = Collection{}
    36  	err = SetPathValue("ison", "true", &c)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	if !c.IsOn {
    41  		t.Errorf("expected: s.IsOn should be true")
    42  	}
    43  
    44  	c = Collection{}
    45  	err = SetPathValue("ison", true, &c)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	if !c.IsOn {
    50  		t.Errorf("expected: s.IsOn should be true")
    51  	}
    52  
    53  	c = Collection{}
    54  	err = SetPathValue("ison", "false", &c)
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  	if c.IsOn {
    59  		t.Errorf("expected: s.IsOn should be false")
    60  	}
    61  
    62  	c = Collection{}
    63  	err = SetPathValue("ison", 123, &c)
    64  	expect := `at "ison": need bool, got int: 123`
    65  	if err == nil {
    66  		t.Fatalf("expected: error \"%s\", got no error", expect)
    67  	}
    68  	if err.Error() != expect {
    69  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
    70  	}
    71  
    72  	c = Collection{}
    73  	err = SetPathValue("ison", "abc", &c)
    74  	expect = `at "ison": need bool, got string: "abc"`
    75  	if err == nil {
    76  		t.Fatalf("expected: error \"%s\", got no error", expect)
    77  	}
    78  	if err.Error() != expect {
    79  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
    80  	}
    81  
    82  	c = Collection{}
    83  	err = SetPathValue("big", 1234567890123, &c)
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  	if c.Big != 1234567890123 {
    88  		t.Errorf("expected: s.Big should be 1234567890123")
    89  	}
    90  
    91  	err = SetPathValue("big", "2345678901234", &c)
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  	if c.Big != 2345678901234 {
    96  		t.Errorf("expected: s.Big should be 2345678901234")
    97  	}
    98  
    99  	c = Collection{}
   100  	err = SetPathValue("ptr", 123, &c)
   101  	if err != nil {
   102  		panic(err)
   103  	}
   104  	if *(c.Ptr) != 123 {
   105  		t.Errorf("expected: s.Ptr should be 123")
   106  	}
   107  
   108  	c = Collection{}
   109  	err = SetPathValue("non_existent", "missing", &c)
   110  	expect = `at "non_existent": not found`
   111  	if err == nil {
   112  		t.Fatalf("expected: error \"%s\", got no error", expect)
   113  	}
   114  	if err.Error() != expect {
   115  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
   116  	}
   117  
   118  	// TODO: path like `list.0` where the index == len(list), should extend the list by 1
   119  
   120  	c = Collection{}
   121  	err = SetPathValue("list.2", "abc", &c)
   122  	expect = `at "list.2": index outside of range: 2, len is 0`
   123  	if err == nil {
   124  		t.Fatalf("expected: error \"%s\", got no error", expect)
   125  	}
   126  	if err.Error() != expect {
   127  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
   128  	}
   129  
   130  	c = Collection{}
   131  	c.List = make([]string, 4)
   132  	err = SetPathValue("list.2", 123, &c)
   133  	expect = `at "list.2": need string, got int: 123`
   134  	if err == nil {
   135  		t.Fatalf("expected: error \"%s\", got no error", expect)
   136  	}
   137  	if err.Error() != expect {
   138  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
   139  	}
   140  
   141  	c = Collection{}
   142  	err = SetPathValue("sub.num", 7, &c)
   143  	if err != nil {
   144  		panic(err)
   145  	}
   146  	if c.Sub.Num != 7 {
   147  		t.Errorf("expected: s.Sub.Num should be 7")
   148  	}
   149  
   150  	c = Collection{}
   151  	err = SetPathValue("dict.cat", "meow", &c)
   152  	if err != nil {
   153  		panic(err)
   154  	}
   155  	if c.Dict["cat"] != "meow" {
   156  		t.Errorf("expected: s.Dict[\"cat\"] should be \"meow\"")
   157  	}
   158  
   159  	// Don't allocate a new map.
   160  	err = SetPathValue("dict.dog", "bark", &c)
   161  	if err != nil {
   162  		panic(err)
   163  	}
   164  	if c.Dict["cat"] != "meow" {
   165  		t.Errorf("expected: s.Dict[\"cat\"] should be \"meow\"")
   166  	}
   167  	if c.Dict["dog"] != "bark" {
   168  		t.Errorf("expected: s.Dict[\"dog\"] should be \"bark\"")
   169  	}
   170  
   171  	s := &SubElement{}
   172  	err = SetPathValue("things.eel", "zap", &s)
   173  	if err != nil {
   174  		panic(err)
   175  	}
   176  	if (*s.Things)["eel"] != "zap" {
   177  		t.Errorf("expected: c.Things[\"eel\"] should be \"zap\"")
   178  	}
   179  
   180  	// Don't allocate a new map.
   181  	err = SetPathValue("things.frog", "ribbit", &s)
   182  	if err != nil {
   183  		panic(err)
   184  	}
   185  	if (*s.Things)["eel"] != "zap" {
   186  		t.Errorf("expected: c.Things[\"eel\"] should be \"zap\"")
   187  	}
   188  	if (*s.Things)["frog"] != "ribbit" {
   189  		t.Errorf("expected: c.Things[\"frog\"] should be \"ribbit\"")
   190  	}
   191  
   192  	// Error
   193  	c = Collection{}
   194  	err = SetPathValue("sub.num", "abc", &c)
   195  	expect = `at "sub.num": need int, got string: "abc"`
   196  	if err == nil {
   197  		t.Fatalf("expected: error \"%s\", got no error", expect)
   198  	}
   199  	if err.Error() != expect {
   200  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
   201  	}
   202  }
   203  
   204  func TestGetPathValue(t *testing.T) {
   205  	c := Collection{
   206  		Name: "Alice",
   207  		Dict: map[string]string{
   208  			"extra": "misc",
   209  		},
   210  		List: []string{"cat", "dog", "eel"},
   211  		Sub: SubElement{
   212  			Num:  7,
   213  			Text: "sandwich",
   214  		},
   215  	}
   216  
   217  	val, err := GetPathValue("name", &c)
   218  	if err != nil {
   219  		panic(err)
   220  	}
   221  	if val != "Alice" {
   222  		t.Errorf("expected: val should be \"Alice\"")
   223  	}
   224  
   225  	val, err = GetPathValue("dict.extra", &c)
   226  	if err != nil {
   227  		panic(err)
   228  	}
   229  	if val != "misc" {
   230  		t.Errorf("expected: val should be \"misc\"")
   231  	}
   232  
   233  	val, err = GetPathValue("sub.num", &c)
   234  	if err != nil {
   235  		panic(err)
   236  	}
   237  	if val != 7 {
   238  		t.Errorf("expected: val should be 7, got %v", val)
   239  	}
   240  
   241  	val, err = GetPathValue("sub.text", &c)
   242  	if err != nil {
   243  		panic(err)
   244  	}
   245  	if val != "sandwich" {
   246  		t.Errorf("expected: val should be \"sandwich\", got %v", val)
   247  	}
   248  
   249  	val, err = GetPathValue("non_existent", &c)
   250  	expect := `at "non_existent": not found`
   251  	if err == nil {
   252  		t.Fatalf("expected: error \"%s\", got no error", expect)
   253  	}
   254  	if err.Error() != expect {
   255  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
   256  	}
   257  
   258  	val, err = GetPathValue("dict.missing_key", &c)
   259  	expect = `at "dict.missing_key": not found`
   260  	if err == nil {
   261  		t.Fatalf("expected: error \"%s\", got no error", expect)
   262  	}
   263  	if err.Error() != expect {
   264  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
   265  	}
   266  
   267  	val, err = GetPathValue("list.1", &c)
   268  	if err != nil {
   269  		panic(err)
   270  	}
   271  	if val != "dog" {
   272  		t.Errorf("expected: val should be \"dog\"")
   273  	}
   274  
   275  	val, err = GetPathValue("list.invalid", &c)
   276  	expect = `at "list.invalid": need int, got string: "invalid"`
   277  	if err == nil {
   278  		t.Fatalf("expected: error \"%s\", got no error", expect)
   279  	}
   280  	if err.Error() != expect {
   281  		t.Errorf("expected: error should be \"%s\", got \"%s\"", expect, err.Error())
   282  	}
   283  
   284  }
   285  
   286  func TestDictKeysCaseInsenstive(t *testing.T) {
   287  	obj := map[string]interface{}{
   288  		"Parent": map[string]interface{}{
   289  			"Child": "ok",
   290  		},
   291  		"First": map[string]interface{}{
   292  			"Second": map[string]interface{}{
   293  				"Third": "alright",
   294  			},
   295  		},
   296  	}
   297  
   298  	val, err := GetPathValue("parent.child", obj)
   299  	if err != nil {
   300  		panic(err)
   301  	}
   302  	if val != "ok" {
   303  		t.Errorf("expected: val should be \"ok\"")
   304  	}
   305  
   306  	val, err = GetPathValue("parent.Child", obj)
   307  	if err != nil {
   308  		panic(err)
   309  	}
   310  	if val != "ok" {
   311  		t.Errorf("expected: val should be \"ok\"")
   312  	}
   313  
   314  	val, err = GetPathValue("parent.CHILD", obj)
   315  	if err != nil {
   316  		panic(err)
   317  	}
   318  	if val != "ok" {
   319  		t.Errorf("expected: val should be \"ok\"")
   320  	}
   321  
   322  	val, err = GetPathValue("Parent.Child", obj)
   323  	if err != nil {
   324  		panic(err)
   325  	}
   326  	if val != "ok" {
   327  		t.Errorf("expected: val should be \"ok\"")
   328  	}
   329  
   330  	val, err = GetPathValue("first.second.third", obj)
   331  	if err != nil {
   332  		panic(err)
   333  	}
   334  	if val != "alright" {
   335  		t.Errorf("expected: val should be \"alright\"")
   336  	}
   337  
   338  	val, err = GetPathValue("FIRST.SECOND.THIRD", obj)
   339  	if err != nil {
   340  		panic(err)
   341  	}
   342  	if val != "alright" {
   343  		t.Errorf("expected: val should be \"alright\"")
   344  	}
   345  }