...

Source file src/github.com/clbanning/mxj/v2/rename_test.go

Documentation: github.com/clbanning/mxj/v2

     1  package mxj
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestRenameKey(t *testing.T) {
     9  	fmt.Println("------------ rename_test.go")
    10  	m := map[string]interface{}{
    11  		"Div": map[string]interface{}{
    12  			"Colour": "blue",
    13  			"Width":  "100%",
    14  		},
    15  	}
    16  	mv := Map(m)
    17  	err := mv.RenameKey("Div.Colour", "Color")
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	values, err := mv.ValuesForPath("Div.Color")
    22  	if len(values) != 1 {
    23  		t.Fatal("didn't add the new key")
    24  	}
    25  	if values[0] != "blue" {
    26  		t.Fatal("value is changed")
    27  	}
    28  	values, err = mv.ValuesForPath("Div.Colour")
    29  	if len(values) > 0 {
    30  		t.Fatal("didn't removed the old key")
    31  	}
    32  
    33  	err = mv.RenameKey("not.existing.path", "newname")
    34  	if err == nil {
    35  		t.Fatal("should raise an error on a non existing path")
    36  	}
    37  
    38  	err = mv.RenameKey("Div.Color", "Width")
    39  	if err == nil {
    40  		t.Fatal("should raise an error if the newName already exists")
    41  	}
    42  }
    43  

View as plain text