github.com/Jeffail/benthos/v3@v3.65.0/lib/util/config/inference_test.go (about)

     1  package config
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestInference(t *testing.T) {
     9  	type testCase struct {
    10  		input         interface{}
    11  		ignore        []string
    12  		expCandidates []string
    13  	}
    14  
    15  	tests := []testCase{
    16  		{
    17  			input:         nil,
    18  			expCandidates: nil,
    19  		},
    20  		{
    21  			input: map[string]interface{}{
    22  				"type": "foo",
    23  				"bar": map[string]interface{}{
    24  					"baz": "test",
    25  				},
    26  			},
    27  			expCandidates: nil,
    28  		},
    29  		{
    30  			input: map[string]interface{}{
    31  				"bar": map[string]interface{}{
    32  					"baz": "test",
    33  				},
    34  				"ignoreme": "hello",
    35  			},
    36  			ignore:        []string{"ignoreme"},
    37  			expCandidates: []string{"bar"},
    38  		},
    39  		{
    40  			input: map[string]interface{}{
    41  				"bar": map[string]interface{}{
    42  					"baz": "test",
    43  				},
    44  				"qux": "quz",
    45  			},
    46  			expCandidates: []string{"bar", "qux"},
    47  		},
    48  		{
    49  			input: map[string]interface{}{
    50  				"type": 5,
    51  				"bar": map[string]interface{}{
    52  					"baz": "test",
    53  				},
    54  				"qux": "quz",
    55  			},
    56  			expCandidates: nil,
    57  		},
    58  		{
    59  			input: map[interface{}]interface{}{
    60  				"type": "foo",
    61  				"bar": map[interface{}]interface{}{
    62  					"baz": "test",
    63  				},
    64  			},
    65  			expCandidates: nil,
    66  		},
    67  		{
    68  			input: map[interface{}]interface{}{
    69  				"bar": map[interface{}]interface{}{
    70  					"baz": "test",
    71  				},
    72  				"qux": "quz",
    73  				5:     "ignored",
    74  			},
    75  			expCandidates: []string{"bar", "qux"},
    76  		},
    77  		{
    78  			input: map[interface{}]interface{}{
    79  				"type": 5,
    80  				"bar": map[interface{}]interface{}{
    81  					"baz": "test",
    82  				},
    83  				"qux": "quz",
    84  				5:     "ignored",
    85  			},
    86  			expCandidates: nil,
    87  		},
    88  	}
    89  
    90  	for i, test := range tests {
    91  		actCandidates := GetInferenceCandidates(test.input, test.ignore...)
    92  		if !reflect.DeepEqual(actCandidates, test.expCandidates) {
    93  			t.Errorf("Wrong candidates inferred '%v': %v != %v", i, actCandidates, test.expCandidates)
    94  		}
    95  	}
    96  }