validators_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Copyright © The ESO Authors
  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. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package esutils
  14. import (
  15. "errors"
  16. "testing"
  17. )
  18. func TestValidateValueOrRef(t *testing.T) {
  19. errRef := errors.New("ref")
  20. ref := "ref"
  21. tests := []struct {
  22. name string
  23. value string
  24. ref *string
  25. policy ValueOrRefPolicy[string]
  26. wantErr error
  27. }{
  28. {
  29. name: "requires exactly one: value",
  30. value: "value",
  31. policy: ValueOrRefPolicy[string]{
  32. Presence: RequireValueOrRef,
  33. },
  34. },
  35. {
  36. name: "requires exactly one: ref",
  37. ref: &ref,
  38. policy: ValueOrRefPolicy[string]{
  39. Presence: RequireValueOrRef,
  40. },
  41. },
  42. {
  43. name: "requires exactly one: rejects both",
  44. value: "value",
  45. ref: &ref,
  46. policy: ValueOrRefPolicy[string]{
  47. Presence: RequireValueOrRef,
  48. },
  49. wantErr: ErrValueAndRefConflict,
  50. },
  51. {
  52. name: "requires exactly one: rejects missing both",
  53. policy: ValueOrRefPolicy[string]{
  54. Presence: RequireValueOrRef,
  55. },
  56. wantErr: ErrValueOrRefMissing,
  57. },
  58. {
  59. name: "validates ref when present",
  60. ref: &ref,
  61. policy: ValueOrRefPolicy[string]{
  62. Presence: RequireValueOrRef,
  63. ValidateRef: func(string) error {
  64. return errRef
  65. },
  66. },
  67. wantErr: errRef,
  68. },
  69. {
  70. name: "allows optional empty pair",
  71. value: "",
  72. ref: nil,
  73. policy: ValueOrRefPolicy[string]{
  74. Presence: AllowValueOrRef,
  75. },
  76. },
  77. {
  78. name: "allows optional value",
  79. value: "value",
  80. policy: ValueOrRefPolicy[string]{
  81. Presence: AllowValueOrRef,
  82. },
  83. },
  84. {
  85. name: "allows optional ref",
  86. ref: &ref,
  87. policy: ValueOrRefPolicy[string]{
  88. Presence: AllowValueOrRef,
  89. },
  90. },
  91. {
  92. name: "allows optional pair: rejects both",
  93. value: "value",
  94. ref: &ref,
  95. policy: ValueOrRefPolicy[string]{
  96. Presence: AllowValueOrRef,
  97. },
  98. wantErr: ErrValueAndRefConflict,
  99. },
  100. {
  101. name: "requires ref only",
  102. ref: &ref,
  103. policy: ValueOrRefPolicy[string]{
  104. Presence: RequireRefOnly,
  105. },
  106. },
  107. {
  108. name: "requires ref only: rejects missing ref",
  109. policy: ValueOrRefPolicy[string]{
  110. Presence: RequireRefOnly,
  111. },
  112. wantErr: ErrRefRequired,
  113. },
  114. {
  115. name: "requires ref only: rejects value",
  116. value: "value",
  117. ref: &ref,
  118. policy: ValueOrRefPolicy[string]{
  119. Presence: RequireRefOnly,
  120. },
  121. wantErr: ErrValueNotAllowed,
  122. },
  123. {
  124. name: "requires value only",
  125. value: "value",
  126. policy: ValueOrRefPolicy[string]{
  127. Presence: RequireValueOnly,
  128. },
  129. },
  130. {
  131. name: "requires value only: rejects missing value",
  132. policy: ValueOrRefPolicy[string]{
  133. Presence: RequireValueOnly,
  134. },
  135. wantErr: ErrValueRequired,
  136. },
  137. {
  138. name: "requires value only: rejects ref",
  139. value: "value",
  140. ref: &ref,
  141. policy: ValueOrRefPolicy[string]{
  142. Presence: RequireValueOnly,
  143. },
  144. wantErr: ErrRefNotAllowed,
  145. },
  146. {
  147. name: "rejects unknown policy",
  148. policy: ValueOrRefPolicy[string]{
  149. Presence: RefPresencePolicy(99),
  150. },
  151. wantErr: errors.New("unknown value/reference presence policy: 99"),
  152. },
  153. }
  154. for _, tt := range tests {
  155. t.Run(tt.name, func(t *testing.T) {
  156. got := ValidateValueOrRef(tt.value, tt.ref, tt.policy)
  157. if tt.wantErr == nil {
  158. if got != nil {
  159. t.Errorf("ValidateValueOrRef() got = %v, want no error", got)
  160. }
  161. return
  162. }
  163. if !errors.Is(got, tt.wantErr) && (got == nil || got.Error() != tt.wantErr.Error()) {
  164. t.Errorf("ValidateValueOrRef() got = %v, want = %v", got, tt.wantErr)
  165. }
  166. })
  167. }
  168. }