bool_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package opt
  4. import (
  5. "encoding/json"
  6. "reflect"
  7. "testing"
  8. )
  9. func TestBool(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. in any
  13. want string // JSON
  14. wantBack any
  15. }{
  16. {
  17. name: "null_for_unset",
  18. in: struct {
  19. True Bool
  20. False Bool
  21. Unset Bool
  22. }{
  23. True: "true",
  24. False: "false",
  25. },
  26. want: `{"True":true,"False":false,"Unset":null}`,
  27. wantBack: struct {
  28. True Bool
  29. False Bool
  30. Unset Bool
  31. }{
  32. True: "true",
  33. False: "false",
  34. Unset: "unset",
  35. },
  36. },
  37. {
  38. name: "omitempty_unset",
  39. in: struct {
  40. True Bool
  41. False Bool
  42. Unset Bool `json:",omitempty"`
  43. }{
  44. True: "true",
  45. False: "false",
  46. },
  47. want: `{"True":true,"False":false}`,
  48. },
  49. {
  50. name: "unset_marshals_as_null",
  51. in: struct {
  52. True Bool
  53. False Bool
  54. Foo Bool
  55. }{
  56. True: "true",
  57. False: "false",
  58. Foo: "unset",
  59. },
  60. want: `{"True":true,"False":false,"Foo":null}`,
  61. wantBack: struct {
  62. True Bool
  63. False Bool
  64. Foo Bool
  65. }{"true", "false", "unset"},
  66. },
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. j, err := json.Marshal(tt.in)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if string(j) != tt.want {
  75. t.Errorf("wrong JSON:\n got: %s\nwant: %s\n", j, tt.want)
  76. }
  77. wantBack := tt.in
  78. if tt.wantBack != nil {
  79. wantBack = tt.wantBack
  80. }
  81. // And back again:
  82. newVal := reflect.New(reflect.TypeOf(tt.in))
  83. out := newVal.Interface()
  84. if err := json.Unmarshal(j, out); err != nil {
  85. t.Fatalf("Unmarshal %#q: %v", j, err)
  86. }
  87. got := newVal.Elem().Interface()
  88. if !reflect.DeepEqual(got, wantBack) {
  89. t.Errorf("value mismatch\n got: %+v\nwant: %+v\n", got, wantBack)
  90. }
  91. })
  92. }
  93. }
  94. func TestBoolEqualBool(t *testing.T) {
  95. tests := []struct {
  96. b Bool
  97. v bool
  98. want bool
  99. }{
  100. {"", true, false},
  101. {"", false, false},
  102. {"sdflk;", true, false},
  103. {"sldkf;", false, false},
  104. {"true", true, true},
  105. {"true", false, false},
  106. {"false", true, false},
  107. {"false", false, true},
  108. {"1", true, false}, // "1" is not true; only "true" is
  109. {"True", true, false}, // "True" is not true; only "true" is
  110. }
  111. for _, tt := range tests {
  112. if got := tt.b.EqualBool(tt.v); got != tt.want {
  113. t.Errorf("(%q).EqualBool(%v) = %v; want %v", string(tt.b), tt.v, got, tt.want)
  114. }
  115. }
  116. }