github.com/tomwright/dasel@v1.27.3/node_propagate_internal_test.go (about)

     1  package dasel
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestPropagateValueProperty(t *testing.T) {
    10  	t.Run("NilValue", func(t *testing.T) {
    11  		n := getNodeWithValue(nil)
    12  		n.Previous.Selector.Current = "."
    13  		err := propagateValueProperty(n)
    14  		assertErrResult(t, &UnexpectedPreviousNilValue{Selector: "."}, err)
    15  	})
    16  	t.Run("UnsupportedType", func(t *testing.T) {
    17  		val := make([]interface{}, 0)
    18  		n := getNodeWithValue(val)
    19  		err := propagateValueProperty(n)
    20  		assertErrResult(t, &UnsupportedTypeForSelector{Selector: n.Selector, Value: n.Previous.Value}, err)
    21  	})
    22  }
    23  
    24  func TestPropagateValueIndex(t *testing.T) {
    25  	t.Run("NilValue", func(t *testing.T) {
    26  		n := getNodeWithValue(nil)
    27  		n.Previous.Selector.Current = "."
    28  		err := propagateValueIndex(n)
    29  		assertErrResult(t, &UnexpectedPreviousNilValue{Selector: "."}, err)
    30  	})
    31  	t.Run("UnsupportedType", func(t *testing.T) {
    32  		val := map[string]interface{}{}
    33  		n := getNodeWithValue(val)
    34  		n.Selector.Current = "[0]"
    35  		n.Selector.Index = 0
    36  		err := propagateValueIndex(n)
    37  		assertErrResult(t, &UnsupportedTypeForSelector{Selector: n.Selector, Value: reflect.ValueOf(val)}, err)
    38  	})
    39  	t.Run("ExistingIndex", func(t *testing.T) {
    40  		val := []interface{}{
    41  			"hello",
    42  		}
    43  		n := getNodeWithValue(val)
    44  		n.Selector.Current = "[0]"
    45  		n.Selector.Index = 0
    46  		n.Value = reflect.ValueOf("world")
    47  		err := propagateValueIndex(n)
    48  		if !assertErrResult(t, nil, err) {
    49  			return
    50  		}
    51  		if !reflect.DeepEqual(val[0], "world") {
    52  			t.Errorf("expected val %v, got %v", "world", val[0])
    53  		}
    54  	})
    55  }
    56  
    57  func TestPropagateValueNextAvailableIndex(t *testing.T) {
    58  	t.Run("MissingPreviousNode", func(t *testing.T) {
    59  		n := getNodeWithValue(nil)
    60  		n.Previous.Selector.Current = "x"
    61  		err := propagateValueNextAvailableIndex(n)
    62  		assertErrResult(t, &UnexpectedPreviousNilValue{Selector: "x"}, err)
    63  	})
    64  	t.Run("UnsupportedType", func(t *testing.T) {
    65  		val := map[string]interface{}{}
    66  		n := getNodeWithValue(val)
    67  		err := propagateValueNextAvailableIndex(n)
    68  		assertErrResult(t, &UnsupportedTypeForSelector{Selector: n.Selector, Value: reflect.ValueOf(val)}, err)
    69  	})
    70  }
    71  
    72  func TestPropagateValue(t *testing.T) {
    73  	t.Run("MissingPreviousNode", func(t *testing.T) {
    74  		n := New(nil)
    75  		err := propagateValue(n)
    76  		assertErrResult(t, nil, err)
    77  	})
    78  	t.Run("UnsupportedSelector", func(t *testing.T) {
    79  		n := getNodeWithValue([]interface{}{})
    80  		n.Selector.Type = "BAD"
    81  		err := propagateValue(n)
    82  		assertErrResult(t, &UnsupportedSelector{Selector: "BAD"}, err)
    83  	})
    84  }
    85  
    86  func TestPropagate(t *testing.T) {
    87  	t.Run("UnsupportedSelector", func(t *testing.T) {
    88  		n := getNodeWithValue([]interface{}{})
    89  		n.Selector.Type = "BAD"
    90  		err := propagate(n)
    91  		assertErrResult(t, fmt.Errorf("could not propagate value: %w", &UnsupportedSelector{Selector: "BAD"}), err)
    92  	})
    93  }