knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1beta1/addressable_types_test.go (about)

     1  /*
     2  Copyright 2019 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1beta1
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"knative.dev/pkg/apis"
    25  	v1 "knative.dev/pkg/apis/duck/v1"
    26  )
    27  
    28  func TestConversion(t *testing.T) {
    29  	tests := []struct {
    30  		name        string
    31  		addr        *Addressable
    32  		conv        apis.Convertible
    33  		want        string
    34  		wantErrUp   bool
    35  		wantErrDown bool
    36  	}{{
    37  		name: "v1",
    38  		addr: &Addressable{
    39  			URL: &apis.URL{
    40  				Scheme: "https",
    41  				Host:   "bar.com",
    42  			},
    43  		},
    44  		conv:        &v1.Addressable{},
    45  		wantErrUp:   false,
    46  		wantErrDown: false,
    47  	}, {
    48  		name:        "v1 - empty",
    49  		addr:        &Addressable{},
    50  		conv:        &v1.Addressable{},
    51  		wantErrUp:   false,
    52  		wantErrDown: false,
    53  	}, {
    54  		name: "v1beta1",
    55  		addr: &Addressable{
    56  			URL: &apis.URL{
    57  				Scheme: "https",
    58  				Host:   "bar.com",
    59  			},
    60  		},
    61  		conv:        &Addressable{},
    62  		wantErrUp:   true,
    63  		wantErrDown: true,
    64  	}, {
    65  		name:        "v1beta1 - empty",
    66  		addr:        &Addressable{},
    67  		conv:        &Addressable{},
    68  		wantErrUp:   true,
    69  		wantErrDown: true,
    70  	}}
    71  
    72  	for _, test := range tests {
    73  		t.Run(test.name, func(t *testing.T) {
    74  			conv := test.conv
    75  			if err := test.addr.ConvertTo(context.Background(), conv); err != nil {
    76  				if !test.wantErrUp {
    77  					t.Error("ConvertTo() =", err)
    78  				}
    79  			} else if test.wantErrUp {
    80  				t.Errorf("ConvertTo() = %#v, wanted error", conv)
    81  			}
    82  			got := &Addressable{}
    83  			if err := got.ConvertFrom(context.Background(), conv); err != nil {
    84  				if !test.wantErrDown {
    85  					t.Error("ConvertFrom() =", err)
    86  				}
    87  				return
    88  			} else if test.wantErrDown {
    89  				t.Errorf("ConvertFrom() = %#v, wanted error", conv)
    90  				return
    91  			}
    92  
    93  			if diff := cmp.Diff(test.addr, got); diff != "" {
    94  				t.Error("roundtrip (-want, +got) =", diff)
    95  			}
    96  		})
    97  	}
    98  }