go-ml.dev/pkg/base@v0.0.0-20200610162856-60c38abac71b/tests/subst_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"go-ml.dev/pkg/base/fu"
     6  	"gotest.tools/assert"
     7  	"testing"
     8  )
     9  
    10  type STR struct {
    11  	text, result string
    12  	ok           bool
    13  }
    14  
    15  type STRr struct {
    16  	ref STR
    17  	r   string
    18  	ok  bool
    19  }
    20  
    21  func (str STR) Apply(f func(string) (string, bool)) STRr {
    22  	r, ok := f(str.text)
    23  	return STRr{str, r, ok}
    24  }
    25  
    26  func (r STRr) String() string {
    27  	return fmt.Sprintf("x=%v; f(%v) -> %v, %v", r.ref, r.ref.text, r.r, r.ok)
    28  }
    29  
    30  func (r STRr) Ok() bool {
    31  	return r.r == r.ref.result && r.ok == r.ref.ok
    32  }
    33  
    34  func (str STR) Assert(t *testing.T, f func(string) (string, bool)) {
    35  	r := str.Apply(f)
    36  	assert.Assert(t, r.Ok(), r)
    37  }
    38  
    39  func Test_Subst1(t *testing.T) {
    40  	f := fu.Starsub("test*", "F*")
    41  	for _, x := range []STR{
    42  		{"test1", "F1", true},
    43  		{"tes1", "tes1", false},
    44  		{"test2", "F2", true},
    45  		{"test", "F", true},
    46  	} {
    47  		x.Assert(t, f)
    48  	}
    49  }
    50  
    51  func Test_Subst3(t *testing.T) {
    52  	f := fu.Starsub("*test", "F*")
    53  	for _, x := range []STR{
    54  		{"123test", "F123", true},
    55  		{"tes1", "tes1", false},
    56  		{"2test", "F2", true},
    57  		{"test", "F", true},
    58  	} {
    59  		x.Assert(t, f)
    60  	}
    61  }
    62  
    63  func Test_Subst4(t *testing.T) {
    64  	f := fu.Starsub("test*", "F")
    65  	for _, x := range []STR{
    66  		{"testXX", "F1", true},
    67  		{"testYY", "F2", true},
    68  		{"test", "F", true},
    69  		{"testZZ", "F3", true},
    70  	} {
    71  		x.Assert(t, f)
    72  	}
    73  }
    74  
    75  func Test_Subst5(t *testing.T) {
    76  	f := fu.Starsub("test*", "F*i")
    77  	for _, x := range []STR{
    78  		{"testXX", "FXXi", true},
    79  		{"testYY", "FYYi", true},
    80  		{"test", "Fi", true},
    81  		{"tes", "tes", false},
    82  	} {
    83  		x.Assert(t, f)
    84  	}
    85  }