github.com/paketo-buildpacks/packit@v1.3.2-0.20211206231111-86b75c657449/matchers/match_toml.go (about)

     1  package matchers
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/BurntSushi/toml"
     8  	"github.com/onsi/gomega/types"
     9  )
    10  
    11  func MatchTOML(expected interface{}) types.GomegaMatcher {
    12  	return &matchTOML{
    13  		expected: expected,
    14  	}
    15  }
    16  
    17  type matchTOML struct {
    18  	expected interface{}
    19  }
    20  
    21  func (matcher *matchTOML) Match(actual interface{}) (success bool, err error) {
    22  	var e, a string
    23  
    24  	switch eType := matcher.expected.(type) {
    25  	case string:
    26  		e = eType
    27  	case []byte:
    28  		e = string(eType)
    29  	default:
    30  		return false, fmt.Errorf("expected value must be []byte or string, received %T", matcher.expected)
    31  	}
    32  
    33  	switch aType := actual.(type) {
    34  	case string:
    35  		a = aType
    36  	case []byte:
    37  		a = string(aType)
    38  	default:
    39  		return false, fmt.Errorf("actual value must be []byte or string, received %T", matcher.expected)
    40  	}
    41  
    42  	var eValue map[string]interface{}
    43  	_, err = toml.Decode(e, &eValue)
    44  	if err != nil {
    45  		return false, err
    46  	}
    47  
    48  	var aValue map[string]interface{}
    49  	_, err = toml.Decode(a, &aValue)
    50  	if err != nil {
    51  		return false, err
    52  	}
    53  
    54  	return reflect.DeepEqual(eValue, aValue), nil
    55  }
    56  
    57  func (matcher *matchTOML) FailureMessage(actual interface{}) (message string) {
    58  	return fmt.Sprintf("Expected\n%s\nto match the TOML representation of\n%s", actual, matcher.expected)
    59  }
    60  
    61  func (matcher *matchTOML) NegatedFailureMessage(actual interface{}) (message string) {
    62  	return fmt.Sprintf("Expected\n%s\nnot to match the TOML representation of\n%s", actual, matcher.expected)
    63  }