github.com/paketo-buildpacks/libpak@v1.70.0/internal/match_toml.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 internal
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  
    23  	"github.com/BurntSushi/toml"
    24  	"github.com/onsi/gomega/types"
    25  )
    26  
    27  func MatchTOML(expected interface{}) types.GomegaMatcher {
    28  	return &matchTOML{
    29  		expected: expected,
    30  	}
    31  }
    32  
    33  type matchTOML struct {
    34  	expected interface{}
    35  }
    36  
    37  func (matcher *matchTOML) Match(actual interface{}) (success bool, err error) {
    38  	var e, a []byte
    39  
    40  	switch eType := matcher.expected.(type) {
    41  	case string:
    42  		e = []byte(eType)
    43  	case []byte:
    44  		e = eType
    45  	default:
    46  		return false, fmt.Errorf("expected value must be []byte or string, received %T", matcher.expected)
    47  	}
    48  
    49  	switch aType := actual.(type) {
    50  	case string:
    51  		a = []byte(aType)
    52  	case []byte:
    53  		a = aType
    54  	default:
    55  		return false, fmt.Errorf("actual value must be []byte or string, received %T", matcher.expected)
    56  	}
    57  
    58  	var eValue map[string]interface{}
    59  	if err := toml.Unmarshal(e, &eValue); err != nil {
    60  		return false, err
    61  	}
    62  
    63  	var aValue map[string]interface{}
    64  	if err := toml.Unmarshal(a, &aValue); err != nil {
    65  		return false, err
    66  	}
    67  
    68  	return reflect.DeepEqual(eValue, aValue), nil
    69  }
    70  
    71  func (matcher *matchTOML) FailureMessage(actual interface{}) (message string) {
    72  	return fmt.Sprintf("Expected\n%s\nto match the TOML representation of\n%s", actual, matcher.expected)
    73  }
    74  
    75  func (matcher *matchTOML) NegatedFailureMessage(actual interface{}) (message string) {
    76  	return fmt.Sprintf("Expected\n%s\nnot to match the TOML representation of\n%s", actual, matcher.expected)
    77  }