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

     1  package dasel_test
     2  
     3  import (
     4  	"github.com/tomwright/dasel"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func conditionTest(c dasel.Condition, input interface{}, exp bool, expErr error) func(t *testing.T) {
    10  	return func(t *testing.T) {
    11  		got, err := c.Check(reflect.ValueOf(input))
    12  		if expErr == nil && err != nil {
    13  			t.Errorf("expected err %v, got %v", expErr, err)
    14  			return
    15  		}
    16  		if expErr != nil && err == nil {
    17  			t.Errorf("expected err %v, got %v", expErr, err)
    18  			return
    19  		}
    20  		if expErr != nil && err != nil && err.Error() != expErr.Error() {
    21  			t.Errorf("expected err %v, got %v", expErr, err)
    22  			return
    23  		}
    24  		if exp != got {
    25  			t.Errorf("expected result %v, got %v", exp, got)
    26  		}
    27  	}
    28  }
    29  
    30  func TestEqualCondition_Check(t *testing.T) {
    31  	c := &dasel.EqualCondition{Key: "name", Value: "Tom"}
    32  
    33  	t.Run("MatchStringValue", conditionTest(
    34  		&dasel.EqualCondition{Key: "value", Value: "Tom"},
    35  		"Tom",
    36  		true, nil,
    37  	))
    38  	t.Run("MatchMapStringInterface", conditionTest(
    39  		c,
    40  		map[string]interface{}{"name": "Tom"},
    41  		true, nil,
    42  	))
    43  	t.Run("MatchMapInterfaceInterface", conditionTest(
    44  		c,
    45  		map[interface{}]interface{}{"name": "Tom"},
    46  		true, nil,
    47  	))
    48  
    49  	t.Run("NoMatchMissingKey", conditionTest(
    50  		c,
    51  		map[string]interface{}{},
    52  		false, nil,
    53  	))
    54  	t.Run("NoMatchMapStringInterface", conditionTest(
    55  		c,
    56  		map[string]interface{}{"name": "Wrong"},
    57  		false, nil,
    58  	))
    59  	t.Run("NoMatchMapInterfaceInterface", conditionTest(
    60  		c,
    61  		map[interface{}]interface{}{"name": "Wrong"},
    62  		false, nil,
    63  	))
    64  
    65  	t.Run("Nil", conditionTest(
    66  		c,
    67  		nil,
    68  		false, &dasel.UnhandledCheckType{Value: nil},
    69  	))
    70  }
    71  
    72  func TestSortedComparisonCondition_Check(t *testing.T) {
    73  	t.Run("IntNotLessThan", conditionTest(
    74  		&dasel.SortedComparisonCondition{Key: "x", Value: "3"},
    75  		map[string]interface{}{
    76  			"x": 3,
    77  		},
    78  		false, nil,
    79  	))
    80  	t.Run("IntNotLessThan", conditionTest(
    81  		&dasel.SortedComparisonCondition{Key: "x", Value: "2"},
    82  		map[string]interface{}{
    83  			"x": 3,
    84  		},
    85  		false, nil,
    86  	))
    87  	t.Run("IntLessThanEqual", conditionTest(
    88  		&dasel.SortedComparisonCondition{Key: "x", Value: "4", Equal: true},
    89  		map[string]interface{}{
    90  			"x": 3,
    91  		},
    92  		true, nil,
    93  	))
    94  	t.Run("IntLessThanEqual", conditionTest(
    95  		&dasel.SortedComparisonCondition{Key: "x", Value: "3", Equal: true},
    96  		map[string]interface{}{
    97  			"x": 3,
    98  		},
    99  		true, nil,
   100  	))
   101  	t.Run("IntNotMoreThan", conditionTest(
   102  		&dasel.SortedComparisonCondition{Key: "x", Value: "3", After: true},
   103  		map[string]interface{}{
   104  			"x": 3,
   105  		},
   106  		false, nil,
   107  	))
   108  	t.Run("IntNotMoreThan", conditionTest(
   109  		&dasel.SortedComparisonCondition{Key: "x", Value: "4", After: true},
   110  		map[string]interface{}{
   111  			"x": 3,
   112  		},
   113  		false, nil,
   114  	))
   115  	t.Run("IntMoreThanEqual", conditionTest(
   116  		&dasel.SortedComparisonCondition{Key: "x", Value: "2", Equal: true, After: true},
   117  		map[string]interface{}{
   118  			"x": 3,
   119  		},
   120  		true, nil,
   121  	))
   122  	t.Run("IntMoreThanEqual", conditionTest(
   123  		&dasel.SortedComparisonCondition{Key: "x", Value: "3", Equal: true, After: true},
   124  		map[string]interface{}{
   125  			"x": 3,
   126  		},
   127  		true, nil,
   128  	))
   129  	t.Run("MatchMapStringInterfaceLength", conditionTest(
   130  		&dasel.SortedComparisonCondition{Key: "name.[#]", Value: "4"},
   131  		map[string]interface{}{"name": "Tom"},
   132  		true, nil,
   133  	))
   134  	t.Run("MatchMapInterfaceInterfaceLength", conditionTest(
   135  		&dasel.SortedComparisonCondition{Key: "name.[#]", Value: "4"},
   136  		map[interface{}]interface{}{"name": "Tom"},
   137  		true, nil,
   138  	))
   139  
   140  	t.Run("NoMatchMissingKey", conditionTest(
   141  		&dasel.SortedComparisonCondition{Key: "x", Value: "4"},
   142  		map[string]interface{}{},
   143  		false, nil,
   144  	))
   145  	t.Run("NoMatchMapStringInterface", conditionTest(
   146  		&dasel.SortedComparisonCondition{Key: "x", Value: "4"},
   147  		map[string]interface{}{"name": "Wrong"},
   148  		false, nil,
   149  	))
   150  	t.Run("NoMatchMapInterfaceInterface", conditionTest(
   151  		&dasel.SortedComparisonCondition{Key: "x", Value: "4"},
   152  		map[interface{}]interface{}{"name": "Wrong"},
   153  		false, nil,
   154  	))
   155  
   156  	t.Run("Nil", conditionTest(
   157  		&dasel.SortedComparisonCondition{Key: "x", Value: "4"},
   158  		nil,
   159  		false, &dasel.UnhandledCheckType{Value: nil},
   160  	))
   161  }
   162  
   163  func TestKeyEqualCondition_Check(t *testing.T) {
   164  	c := &dasel.KeyEqualCondition{Value: "name"}
   165  
   166  	t.Run("MatchStringValue", conditionTest(
   167  		c,
   168  		"name",
   169  		true, nil,
   170  	))
   171  	t.Run("NoMatchMissingKey", conditionTest(
   172  		c,
   173  		"asd",
   174  		false, nil,
   175  	))
   176  	t.Run("Nil", conditionTest(
   177  		c,
   178  		nil,
   179  		false, &dasel.UnhandledCheckType{Value: nil},
   180  	))
   181  }