github.com/kristofferahl/go-centry@v1.5.0/internal/pkg/test/strings.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/franela/goblin"
     8  )
     9  
    10  const tmplExpectedKey string = `expected key "%s" to be found in input
    11  
    12  INPUT
    13  ----------------------------------------------------
    14  %s
    15  ----------------------------------------------------`
    16  
    17  const tmplExpectedContains string = `
    18  
    19  EXPECTED THIS
    20  ----------------------------------------------------
    21  %s
    22  ----------------------------------------------------
    23  
    24  TO BE FOUND IN
    25  ----------------------------------------------------
    26  %s
    27  ----------------------------------------------------`
    28  
    29  const tmplExpectedValue string = `expected value "%s" for key "%s" but found "%s"`
    30  
    31  // AssertStringHasKeyValue asserts the expected string is found in within the input
    32  func AssertStringHasKeyValue(g *goblin.G, s, key, value string) {
    33  	found := false
    34  	lines := strings.Split(s, "\n")
    35  	for _, l := range lines {
    36  		parts := strings.Split(l, "=")
    37  		k := parts[0]
    38  
    39  		var v string
    40  		if len(parts) > 1 {
    41  			v = parts[1]
    42  		}
    43  
    44  		if k == key {
    45  			found = true
    46  			if v != value {
    47  				g.Fail(fmt.Sprintf(tmplExpectedValue, value, key, v))
    48  			}
    49  		}
    50  	}
    51  
    52  	if !found {
    53  		g.Fail(fmt.Sprintf(tmplExpectedKey, key, s))
    54  	}
    55  }
    56  
    57  // AssertStringContains asserts the expected string is found in within the input
    58  func AssertStringContains(g *goblin.G, s, substring string) {
    59  	s = strings.TrimSpace(s)
    60  	substring = strings.TrimSpace(substring)
    61  	msg := fmt.Sprintf(tmplExpectedContains, substring, s)
    62  	g.Assert(strings.Contains(s, substring)).IsTrue(msg)
    63  }