gitlab.com/evatix-go/core@v1.3.55/coretests/Compare.go (about)

     1  package coretests
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  )
     9  
    10  type Compare struct {
    11  	sortedString   *string
    12  	sortedStrings  []string
    13  	MatchingLength int // 0/-1 means all, number means, that number must match
    14  	StringContains string
    15  }
    16  
    17  func (it *Compare) SortedStrings() []string {
    18  	if it.sortedStrings != nil {
    19  		return it.sortedStrings
    20  	}
    21  
    22  	it.sortedStrings = GetMessageToSortedArray(
    23  		false,
    24  		true,
    25  		strings.TrimSpace(it.StringContains))
    26  
    27  	return it.sortedStrings
    28  }
    29  
    30  func (it *Compare) SortedString() string {
    31  	if it.sortedString != nil {
    32  		return *it.sortedString
    33  	}
    34  
    35  	sortedStrings := it.SortedStrings()
    36  	sortedString := strings.Join(
    37  		sortedStrings,
    38  		commonJoiner)
    39  
    40  	it.sortedString = &sortedString
    41  
    42  	return *it.sortedString
    43  }
    44  
    45  func (it *Compare) GetPrintMessage(index int) string {
    46  	return fmt.Sprintf(
    47  		"\n\tIndex:%d\n\tString Contains:%s\n\tString Processed:%s",
    48  		index,
    49  		it.StringContains,
    50  		it.SortedString())
    51  }
    52  
    53  func (it *Compare) IsMatch(
    54  	isPrint bool,
    55  	index int,
    56  	instruction *ComparingInstruction,
    57  ) bool {
    58  	actualHashset := instruction.ActualHashset()
    59  	sortedStrings := it.SortedStrings()
    60  
    61  	// all
    62  	if it.MatchingLength <= constants.Zero {
    63  		isMatch := actualHashset.HasAll(sortedStrings...)
    64  
    65  		if !isMatch && isPrint {
    66  			compiledMessage := it.GetPrintMessage(index)
    67  
    68  			fmt.Println(compiledMessage)
    69  		}
    70  
    71  		return isMatch
    72  	}
    73  
    74  	foundMatches := 0
    75  
    76  	for _, item := range sortedStrings {
    77  		if actualHashset.Has(item) {
    78  			foundMatches++
    79  		}
    80  	}
    81  
    82  	isMatch := foundMatches >= it.MatchingLength
    83  	if !isMatch && isPrint {
    84  		compiledMessage := it.GetPrintMessage(index)
    85  
    86  		fmt.Println(compiledMessage)
    87  	}
    88  
    89  	return isMatch
    90  }