...

Source file src/cloud.google.com/go/internal/optional/optional_test.go

Documentation: cloud.google.com/go/internal/optional

     1  // Copyright 2016 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package optional
    16  
    17  import "testing"
    18  
    19  func TestConvertSuccess(t *testing.T) {
    20  	if got, want := ToBool(false), false; got != want {
    21  		t.Errorf("got %v, want %v", got, want)
    22  	}
    23  	if got, want := ToString(""), ""; got != want {
    24  		t.Errorf("got %v, want %v", got, want)
    25  	}
    26  	if got, want := ToInt(0), 0; got != want {
    27  		t.Errorf("got %v, want %v", got, want)
    28  	}
    29  	if got, want := ToUint(uint(0)), uint(0); got != want {
    30  		t.Errorf("got %v, want %v", got, want)
    31  	}
    32  	if got, want := ToFloat64(0.0), 0.0; got != want {
    33  		t.Errorf("got %v, want %v", got, want)
    34  	}
    35  }
    36  
    37  func TestConvertFailure(t *testing.T) {
    38  	for _, f := range []func(){
    39  		func() { ToBool(nil) },
    40  		func() { ToBool(3) },
    41  		func() { ToString(nil) },
    42  		func() { ToString(3) },
    43  		func() { ToInt(nil) },
    44  		func() { ToInt("") },
    45  		func() { ToUint(nil) },
    46  		func() { ToUint("") },
    47  		func() { ToFloat64(nil) },
    48  		func() { ToFloat64("") },
    49  	} {
    50  		if !panics(f) {
    51  			t.Error("got no panic, want panic")
    52  		}
    53  	}
    54  }
    55  
    56  func panics(f func()) (b bool) {
    57  	defer func() {
    58  		if recover() != nil {
    59  			b = true
    60  		}
    61  	}()
    62  	f()
    63  	return false
    64  }
    65  

View as plain text