knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/ptr/value_test.go (about)

     1  /*
     2  Copyright 2021 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 ptr
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  func TestInt32Value(t *testing.T) {
    25  	want := int32(55)
    26  	gotValue := Int32Value(&want)
    27  	if want != gotValue {
    28  		t.Errorf("Int32() = &%v, wanted %v", gotValue, want)
    29  	}
    30  }
    31  
    32  func TestInt32Value_Nil(t *testing.T) {
    33  	want := int32(0)
    34  	gotValue := Int32Value(nil)
    35  	if want != gotValue {
    36  		t.Errorf("Int32() = &%v, wanted %v", gotValue, want)
    37  	}
    38  }
    39  
    40  func TestInt64Value(t *testing.T) {
    41  	want := int64(55)
    42  	gotValue := Int64Value(&want)
    43  	if want != gotValue {
    44  		t.Errorf("Int64() = &%v, wanted %v", gotValue, want)
    45  	}
    46  }
    47  
    48  func TestInt64Value_Nil(t *testing.T) {
    49  	want := int64(0)
    50  	gotValue := Int64Value(nil)
    51  	if want != gotValue {
    52  		t.Errorf("Int64() = &%v, wanted %v", gotValue, want)
    53  	}
    54  }
    55  
    56  func TestFloat32Value(t *testing.T) {
    57  	want := float32(1.25)
    58  	gotValue := Float32Value(&want)
    59  	if want != gotValue {
    60  		t.Errorf("Float32() = &%v, wanted %v", gotValue, want)
    61  	}
    62  }
    63  
    64  func TestFloat32Value_Nil(t *testing.T) {
    65  	want := float32(0)
    66  	gotValue := Float32Value(nil)
    67  	if want != gotValue {
    68  		t.Errorf("Float32() = &%v, wanted %v", gotValue, want)
    69  	}
    70  }
    71  
    72  func TestFloat64Value(t *testing.T) {
    73  	want := 1.25
    74  	gotValue := Float64Value(&want)
    75  	if want != gotValue {
    76  		t.Errorf("Float64() = &%v, wanted %v", gotValue, want)
    77  	}
    78  }
    79  
    80  func TestFloat64Value_Nil(t *testing.T) {
    81  	want := float64(0)
    82  	gotValue := Float64Value(nil)
    83  	if want != gotValue {
    84  		t.Errorf("Float64() = &%v, wanted %v", gotValue, want)
    85  	}
    86  }
    87  
    88  func TestBoolValue(t *testing.T) {
    89  	want := true
    90  	gotValue := BoolValue(&want)
    91  	if want != gotValue {
    92  		t.Errorf("Bool() = &%v, wanted %v", gotValue, want)
    93  	}
    94  }
    95  
    96  func TestBoolValue_Nil(t *testing.T) {
    97  	want := false
    98  	gotValue := BoolValue(nil)
    99  	if want != gotValue {
   100  		t.Errorf("Bool() = &%v, wanted %v", gotValue, want)
   101  	}
   102  }
   103  
   104  func TestStringValue(t *testing.T) {
   105  	want := "should be a pointer"
   106  	gotValue := StringValue(&want)
   107  	if want != gotValue {
   108  		t.Errorf("String() = &%v, wanted %v", gotValue, want)
   109  	}
   110  }
   111  
   112  func TestStringValue_Nil(t *testing.T) {
   113  	want := ""
   114  	gotValue := StringValue(nil)
   115  	if want != gotValue {
   116  		t.Errorf("String() = &%v, wanted %v", gotValue, want)
   117  	}
   118  }
   119  
   120  func TestTimeValue(t *testing.T) {
   121  	want := time.Now().Add(time.Minute)
   122  	if got, want := TimeValue(&want), want; got != want {
   123  		t.Errorf("got = %v, want: %v", got, want)
   124  	}
   125  }
   126  
   127  func TestTimeValue_Nil(t *testing.T) {
   128  	want := time.Time{}
   129  	if got, want := TimeValue(nil), want; got != want {
   130  		t.Errorf("got = %v, want: %v", got, want)
   131  	}
   132  }
   133  
   134  func TestDurationValue(t *testing.T) {
   135  	want := 42 * time.Second
   136  	gotValue := DurationValue(&want)
   137  	if want != gotValue {
   138  		t.Errorf("Duration() = &%v, wanted %v", gotValue, want)
   139  	}
   140  }
   141  
   142  func TestDurationValue_Nil(t *testing.T) {
   143  	want := time.Duration(0)
   144  	gotValue := DurationValue(nil)
   145  	if want != gotValue {
   146  		t.Errorf("Duration() = &%v, wanted %v", gotValue, want)
   147  	}
   148  }