github.com/relnod/pegomock@v2.0.1+incompatible/pegomock/testutil/gomega_file_matcher.go (about)

     1  // Copyright 2016 Peter Goetz
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutil
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"strings"
    21  )
    22  
    23  type fileContentSubStringMatcher struct {
    24  	substring   string
    25  	fileContent []byte
    26  }
    27  
    28  func BeAFileContainingSubString(substring string) *fileContentSubStringMatcher {
    29  	return &fileContentSubStringMatcher{substring: substring}
    30  }
    31  
    32  func (matcher *fileContentSubStringMatcher) Match(actual interface{}) (bool, error) {
    33  	actualFilePath, ok := actual.(string)
    34  	if !ok {
    35  		return false, fmt.Errorf("Matcher expects the actual file path as string")
    36  	}
    37  	var err error
    38  	matcher.fileContent, err = ioutil.ReadFile(actualFilePath)
    39  	if err != nil {
    40  		return false, fmt.Errorf("File content could not be read due to: %v", err)
    41  	}
    42  	return strings.Contains(string(matcher.fileContent), matcher.substring), nil
    43  }
    44  
    45  func (matcher *fileContentSubStringMatcher) FailureMessage(actual interface{}) string {
    46  	return fmt.Sprintf("Expected:\n\t%s to contain a substring '%s'\n\nBut got:\n%s",
    47  		actual, matcher.substring, matcher.fileContent)
    48  }
    49  
    50  func (matcher *fileContentSubStringMatcher) NegatedFailureMessage(actual interface{}) string {
    51  	return fmt.Sprintf("Expected:\n\t%s not to contain a substring '%s'\n\nBut got:\n%s",
    52  		actual, matcher.substring, matcher.fileContent)
    53  }