github.com/crossplane/upjet@v1.3.0/pkg/types/name/reference_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package name
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  )
    12  
    13  func TestReferenceFieldName(t *testing.T) {
    14  	type args struct {
    15  		n             Name
    16  		isList        bool
    17  		camelOverride string
    18  	}
    19  	type want struct {
    20  		n Name
    21  	}
    22  	cases := map[string]struct {
    23  		reason string
    24  		args
    25  		want
    26  	}{
    27  		"NonListDefault": {
    28  			reason: "It should work with normal case without override.",
    29  			args: args{
    30  				n:             NewFromSnake("some_field"),
    31  				isList:        false,
    32  				camelOverride: "",
    33  			},
    34  			want: want{
    35  				n: NewFromSnake("some_field_ref"),
    36  			},
    37  		},
    38  		"ListDefault": {
    39  			reason: "It should work with list case without override.",
    40  			args: args{
    41  				n:             NewFromSnake("some_field"),
    42  				isList:        true,
    43  				camelOverride: "",
    44  			},
    45  			want: want{
    46  				n: NewFromSnake("some_field_refs"),
    47  			},
    48  		},
    49  		"ListOverridden": {
    50  			reason: "It should work with list case even though it's overridden.",
    51  			args: args{
    52  				n:             NewFromSnake("some_field"),
    53  				isList:        true,
    54  				camelOverride: "AnotherField",
    55  			},
    56  			want: want{
    57  				n: NewFromSnake("another_field"),
    58  			},
    59  		},
    60  		"NonListOverridden": {
    61  			reason: "It should work with normal case even though it's overridden.",
    62  			args: args{
    63  				n:             NewFromSnake("some_field"),
    64  				isList:        false,
    65  				camelOverride: "AnotherField",
    66  			},
    67  			want: want{
    68  				n: NewFromSnake("another_field"),
    69  			},
    70  		},
    71  	}
    72  
    73  	for name, tc := range cases {
    74  		t.Run(name, func(t *testing.T) {
    75  			got := ReferenceFieldName(tc.args.n, tc.args.isList, tc.args.camelOverride)
    76  			if diff := cmp.Diff(tc.want.n, got); diff != "" {
    77  				t.Errorf("\nReferenceFieldName(...): -want, +got:\n%s", diff)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestSelectorFieldName(t *testing.T) {
    84  	type args struct {
    85  		n             Name
    86  		camelOverride string
    87  	}
    88  	type want struct {
    89  		n Name
    90  	}
    91  	cases := map[string]struct {
    92  		reason string
    93  		args
    94  		want
    95  	}{
    96  		"Default": {
    97  			reason: "It should work with normal case without override.",
    98  			args: args{
    99  				n:             NewFromSnake("some_field"),
   100  				camelOverride: "",
   101  			},
   102  			want: want{
   103  				n: NewFromSnake("some_field_selector"),
   104  			},
   105  		},
   106  		"Overridden": {
   107  			reason: "It should return the override if given.",
   108  			args: args{
   109  				n:             NewFromSnake("some_field"),
   110  				camelOverride: "AnotherFieldSelector",
   111  			},
   112  			want: want{
   113  				n: NewFromSnake("another_field_selector"),
   114  			},
   115  		},
   116  	}
   117  
   118  	for name, tc := range cases {
   119  		t.Run(name, func(t *testing.T) {
   120  			got := SelectorFieldName(tc.args.n, tc.args.camelOverride)
   121  			if diff := cmp.Diff(tc.want.n, got); diff != "" {
   122  				t.Errorf("\nSelectorFieldName(...): -want, +got:\n%s", diff)
   123  			}
   124  		})
   125  	}
   126  }