github.com/cloudfoundry/libcfbuildpack@v1.91.23/test/have_plans.go (about) 1 /* 2 * Copyright 2018-2020 the original author or authors. 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 * https://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 test 18 19 import ( 20 "fmt" 21 "reflect" 22 23 "github.com/buildpack/libbuildpack/buildplan" 24 "github.com/onsi/gomega/types" 25 ) 26 27 // HavePlans tests that a set of plans is returned from detect. 28 func HavePlans(plans ...buildplan.Plan) types.GomegaMatcher { 29 return &havePlans{plans} 30 } 31 32 type havePlans struct { 33 plans []buildplan.Plan 34 } 35 36 func (p *havePlans) Match(actual interface{}) (bool, error) { 37 a, ok := actual.(buildplan.Plans) 38 if !ok { 39 return false, fmt.Errorf("actual must be a buildplan.Plans: %s", reflect.TypeOf(a)) 40 } 41 42 return reflect.DeepEqual(a, p.expected()), nil 43 } 44 45 func (p *havePlans) FailureMessage(actual interface{}) (message string) { 46 return fmt.Sprintf("Expected\n\t%#v\n to match\n\t%#v\n", actual, p.expected()) 47 } 48 49 func (p *havePlans) NegatedFailureMessage(actual interface{}) (message string) { 50 return fmt.Sprintf("Expected\n\t%#v\n not to match\n\t%#v\n", actual, p.expected()) 51 } 52 53 func (p *havePlans) expected() buildplan.Plans { 54 e := buildplan.Plans{} 55 56 if len(p.plans) > 0 { 57 e.Plan = p.plans[0] 58 } 59 60 if len(p.plans) > 1 { 61 e.Or = p.plans[1:] 62 } 63 64 return e 65 }