Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

65 Zeilen
1.6 KiB

  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. package optional
  15. import "testing"
  16. func TestConvertSuccess(t *testing.T) {
  17. if got, want := ToBool(false), false; got != want {
  18. t.Errorf("got %v, want %v", got, want)
  19. }
  20. if got, want := ToString(""), ""; got != want {
  21. t.Errorf("got %v, want %v", got, want)
  22. }
  23. if got, want := ToInt(0), 0; got != want {
  24. t.Errorf("got %v, want %v", got, want)
  25. }
  26. if got, want := ToUint(uint(0)), uint(0); got != want {
  27. t.Errorf("got %v, want %v", got, want)
  28. }
  29. if got, want := ToFloat64(0.0), 0.0; got != want {
  30. t.Errorf("got %v, want %v", got, want)
  31. }
  32. }
  33. func TestConvertFailure(t *testing.T) {
  34. for _, f := range []func(){
  35. func() { ToBool(nil) },
  36. func() { ToBool(3) },
  37. func() { ToString(nil) },
  38. func() { ToString(3) },
  39. func() { ToInt(nil) },
  40. func() { ToInt("") },
  41. func() { ToUint(nil) },
  42. func() { ToUint("") },
  43. func() { ToFloat64(nil) },
  44. func() { ToFloat64("") },
  45. } {
  46. if !panics(f) {
  47. t.Error("got no panic, want panic")
  48. }
  49. }
  50. }
  51. func panics(f func()) (b bool) {
  52. defer func() {
  53. if recover() != nil {
  54. b = true
  55. }
  56. }()
  57. f()
  58. return false
  59. }