| 1 | package confopt |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | |
| 7 | "gopkg.in/yaml.v3" |
| 8 | ) |
| 9 | |
| 10 | func TestAutoBoolStringAndStates(t *testing.T) { |
| 11 | tests := []struct { |
| 12 | value AutoBool |
| 13 | expectString string |
| 14 | enabled bool |
| 15 | disabled bool |
| 16 | auto bool |
| 17 | }{ |
| 18 | {AutoBool(""), "auto", false, false, true}, |
| 19 | {AutoBoolAuto, "auto", false, false, true}, |
| 20 | {AutoBoolEnabled, "enabled", true, false, false}, |
| 21 | {AutoBoolDisabled, "disabled", false, true, false}, |
| 22 | {AutoBool("ENABLED"), "enabled", true, false, false}, |
| 23 | } |
| 24 | |
| 25 | for _, tc := range tests { |
| 26 | if got := tc.value.String(); got != tc.expectString { |
| 27 | t.Fatalf("String() => %q, want %q", got, tc.expectString) |
| 28 | } |
| 29 | if tc.value.IsEnabled() != tc.enabled { |
| 30 | t.Fatalf("IsEnabled() => %t, want %t for %q", tc.value.IsEnabled(), tc.enabled, tc.value) |
| 31 | } |
| 32 | if tc.value.IsDisabled() != tc.disabled { |
| 33 | t.Fatalf("IsDisabled() => %t, want %t for %q", tc.value.IsDisabled(), tc.disabled, tc.value) |
| 34 | } |
| 35 | if tc.value.IsAuto() != tc.auto { |
| 36 | t.Fatalf("IsAuto() => %t, want %t for %q", tc.value.IsAuto(), tc.auto, tc.value) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestAutoBoolToBool(t *testing.T) { |
| 42 | if ptr := AutoBoolAuto.ToBool(); ptr != nil { |
| 43 | t.Fatalf("AutoBoolAuto.ToBool() => %v, want nil", *ptr) |
| 44 | } |
| 45 | if ptr := AutoBoolEnabled.ToBool(); ptr == nil || *ptr != true { |
| 46 | t.Fatalf("AutoBoolEnabled.ToBool() => %v, want true", ptr) |
| 47 | } |
| 48 | if ptr := AutoBoolDisabled.ToBool(); ptr == nil || *ptr != false { |
| 49 | t.Fatalf("AutoBoolDisabled.ToBool() => %v, want false", ptr) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestAutoBoolBool(t *testing.T) { |
| 54 | if got := AutoBoolAuto.Bool(true); got != true { |
| 55 | t.Fatalf("AutoBoolAuto.Bool(true) => %t, want true", got) |
| 56 | } |
| 57 | if got := AutoBoolAuto.Bool(false); got != false { |
| 58 | t.Fatalf("AutoBoolAuto.Bool(false) => %t, want false", got) |
| 59 | } |
| 60 | if got := AutoBoolEnabled.Bool(false); got != true { |
| 61 | t.Fatalf("AutoBoolEnabled.Bool(false) => %t, want true", got) |
| 62 | } |
| 63 | if got := AutoBoolDisabled.Bool(true); got != false { |
| 64 | t.Fatalf("AutoBoolDisabled.Bool(true) => %t, want false", got) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestAutoBoolJSONRoundTrip(t *testing.T) { |
| 69 | for _, tc := range []AutoBool{AutoBoolAuto, AutoBoolEnabled, AutoBoolDisabled} { |
| 70 | data, err := json.Marshal(tc) |
| 71 | if err != nil { |
| 72 | t.Fatalf("json.Marshal(%q) unexpected error: %v", tc, err) |
| 73 | } |
| 74 | var back AutoBool |
| 75 | if err := json.Unmarshal(data, &back); err != nil { |
| 76 | t.Fatalf("json.Unmarshal(%q) unexpected error: %v", tc, err) |
| 77 | } |
| 78 | if back != tc { |
| 79 | t.Fatalf("json round-trip => %q, want %q", back, tc) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestAutoBoolYAMLRoundTrip(t *testing.T) { |
| 85 | for _, tc := range []AutoBool{AutoBoolAuto, AutoBoolEnabled, AutoBoolDisabled} { |
| 86 | data, err := yaml.Marshal(tc) |
| 87 | if err != nil { |
| 88 | t.Fatalf("yaml.Marshal(%q) unexpected error: %v", tc, err) |
| 89 | } |
| 90 | var back AutoBool |
| 91 | if err := yaml.Unmarshal(data, &back); err != nil { |
| 92 | t.Fatalf("yaml.Unmarshal(%q) unexpected error: %v", tc, err) |
| 93 | } |
| 94 | if back != tc { |
| 95 | t.Fatalf("yaml round-trip => %q, want %q", back, tc) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestAutoBoolInvalidInputs(t *testing.T) { |
| 101 | var a AutoBool |
| 102 | if err := json.Unmarshal([]byte(`"maybe"`), &a); err == nil { |
| 103 | t.Fatalf("expected json.Unmarshal error for invalid value") |
| 104 | } |
| 105 | if err := yaml.Unmarshal([]byte("maybe"), &a); err == nil { |
| 106 | t.Fatalf("expected yaml.Unmarshal error for invalid value") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestAutoBoolYAMLBooleanLiteral(t *testing.T) { |
| 111 | var a AutoBool |
| 112 | if err := yaml.Unmarshal([]byte("true\n"), &a); err != nil { |
| 113 | t.Fatalf("expected yaml.Unmarshal to accept boolean literal: %v", err) |
| 114 | } |
| 115 | if a != AutoBoolEnabled { |
| 116 | t.Fatalf("yaml bool literal true => %q, want %q", a, AutoBoolEnabled) |
| 117 | } |
| 118 | if err := yaml.Unmarshal([]byte("false\n"), &a); err != nil { |
| 119 | t.Fatalf("expected yaml.Unmarshal to accept boolean literal: %v", err) |
| 120 | } |
| 121 | if a != AutoBoolDisabled { |
| 122 | t.Fatalf("yaml bool literal false => %q, want %q", a, AutoBoolDisabled) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestAutoBoolWithDefault(t *testing.T) { |
| 127 | if got := AutoBoolAuto.WithDefault(true); got != AutoBoolEnabled { |
| 128 | t.Fatalf("AutoBoolAuto.WithDefault(true) => %q, want %q", got, AutoBoolEnabled) |
| 129 | } |
| 130 | if got := AutoBoolAuto.WithDefault(false); got != AutoBoolDisabled { |
| 131 | t.Fatalf("AutoBoolAuto.WithDefault(false) => %q, want %q", got, AutoBoolDisabled) |
| 132 | } |
| 133 | if got := AutoBoolEnabled.WithDefault(false); got != AutoBoolEnabled { |
| 134 | t.Fatalf("AutoBoolEnabled.WithDefault(false) => %q, want %q", got, AutoBoolEnabled) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestAutoBoolFromBool(t *testing.T) { |
| 139 | if got := AutoBoolFromBool(true); got != AutoBoolEnabled { |
| 140 | t.Fatalf("AutoBoolFromBool(true) => %q, want %q", got, AutoBoolEnabled) |
| 141 | } |
| 142 | if got := AutoBoolFromBool(false); got != AutoBoolDisabled { |
| 143 | t.Fatalf("AutoBoolFromBool(false) => %q, want %q", got, AutoBoolDisabled) |
| 144 | } |
| 145 | } |