github.com/fluxcd/go-git-providers@v0.19.3/gitprovider/types_defaulting_test.go (about) 1 /* 2 Copyright 2020 The Flux CD contributors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package gitprovider 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func TestDefaulting(t *testing.T) { 25 tests := []struct { 26 name string 27 structName string 28 object DefaultedInfoRequest 29 expected DefaultedInfoRequest 30 }{ 31 { 32 name: "DeployKey: empty", 33 structName: "DeployKey", 34 object: &DeployKeyInfo{}, 35 expected: &DeployKeyInfo{ 36 ReadOnly: BoolVar(true), 37 }, 38 }, 39 { 40 name: "DeployKey: don't set if non-nil (default)", 41 structName: "DeployKey", 42 object: &DeployKeyInfo{ 43 ReadOnly: BoolVar(true), 44 }, 45 expected: &DeployKeyInfo{ 46 ReadOnly: BoolVar(true), 47 }, 48 }, 49 { 50 name: "DeployKey: don't set if non-nil (non-default)", 51 structName: "DeployKey", 52 object: &DeployKeyInfo{ 53 ReadOnly: BoolVar(false), 54 }, 55 expected: &DeployKeyInfo{ 56 ReadOnly: BoolVar(false), 57 }, 58 }, 59 { 60 name: "Repository: empty", 61 structName: "Repository", 62 object: &RepositoryInfo{}, 63 expected: &RepositoryInfo{ 64 Visibility: RepositoryVisibilityVar(RepositoryVisibilityPrivate), 65 DefaultBranch: StringVar("main"), 66 }, 67 }, 68 { 69 name: "Repository: don't set if non-nil (default)", 70 structName: "Repository", 71 object: &RepositoryInfo{ 72 Visibility: RepositoryVisibilityVar(RepositoryVisibilityPrivate), 73 DefaultBranch: StringVar("main"), 74 }, 75 expected: &RepositoryInfo{ 76 Visibility: RepositoryVisibilityVar(RepositoryVisibilityPrivate), 77 DefaultBranch: StringVar("main"), 78 }, 79 }, 80 { 81 name: "Repository: don't set if non-nil (non-default)", 82 structName: "Repository", 83 object: &RepositoryInfo{ 84 Visibility: RepositoryVisibilityVar(RepositoryVisibilityInternal), 85 DefaultBranch: StringVar("main"), 86 }, 87 expected: &RepositoryInfo{ 88 Visibility: RepositoryVisibilityVar(RepositoryVisibilityInternal), 89 DefaultBranch: StringVar("main"), 90 }, 91 }, 92 { 93 name: "TeamAccess: empty", 94 structName: "TeamAccess", 95 object: &TeamAccessInfo{}, 96 expected: &TeamAccessInfo{ 97 Permission: RepositoryPermissionVar(RepositoryPermissionPull), 98 }, 99 }, 100 { 101 name: "TeamAccess: don't set if non-nil (default)", 102 structName: "Repository", 103 object: &TeamAccessInfo{ 104 Permission: RepositoryPermissionVar(RepositoryPermissionPull), 105 }, 106 expected: &TeamAccessInfo{ 107 Permission: RepositoryPermissionVar(RepositoryPermissionPull), 108 }, 109 }, 110 { 111 name: "TeamAccess: don't set if non-nil (non-default)", 112 structName: "TeamAccess", 113 object: &TeamAccessInfo{ 114 Permission: RepositoryPermissionVar(RepositoryPermissionPush), 115 }, 116 expected: &TeamAccessInfo{ 117 Permission: RepositoryPermissionVar(RepositoryPermissionPush), 118 }, 119 }, 120 } 121 for _, tt := range tests { 122 t.Run(tt.name, func(t *testing.T) { 123 tt.object.Default() 124 125 if !reflect.DeepEqual(tt.object, tt.expected) { 126 t.Errorf("%s.Default(): got %v, expected %v", tt.structName, tt.object, tt.expected) 127 } 128 }) 129 } 130 }