github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/mods/re/re_test.go (about)

     1  package re
     2  
     3  import (
     4  	"regexp/syntax"
     5  	"testing"
     6  
     7  	"github.com/markusbkk/elvish/pkg/eval"
     8  	"github.com/markusbkk/elvish/pkg/eval/errs"
     9  	. "github.com/markusbkk/elvish/pkg/eval/evaltest"
    10  	"github.com/markusbkk/elvish/pkg/eval/vals"
    11  )
    12  
    13  func TestRe(t *testing.T) {
    14  	setup := func(ev *eval.Evaler) {
    15  		ev.ExtendGlobal(eval.BuildNs().AddNs("re", Ns))
    16  	}
    17  	TestWithSetup(t, setup,
    18  		That("re:match . xyz").Puts(true),
    19  		That("re:match . ''").Puts(false),
    20  		That("re:match '[a-z]' A").Puts(false),
    21  
    22  		// Invalid pattern in re:match
    23  		That("re:match '(' x").Throws(ErrorWithType(&syntax.Error{})),
    24  
    25  		That("re:find . ab").Puts(
    26  			matchStruct{"a", 0, 1, vals.MakeList(submatchStruct{"a", 0, 1})},
    27  			matchStruct{"b", 1, 2, vals.MakeList(submatchStruct{"b", 1, 2})},
    28  		),
    29  		That("re:find '[A-Z]([0-9])' 'A1 B2'").Puts(
    30  			matchStruct{"A1", 0, 2, vals.MakeList(
    31  				submatchStruct{"A1", 0, 2}, submatchStruct{"1", 1, 2})},
    32  			matchStruct{"B2", 3, 5, vals.MakeList(
    33  				submatchStruct{"B2", 3, 5}, submatchStruct{"2", 4, 5})},
    34  		),
    35  
    36  		// Access to fields in the match StructMap
    37  		That("put (re:find . a)[text start end groups]").
    38  			Puts("a", 0, 1, vals.MakeList(submatchStruct{"a", 0, 1})),
    39  
    40  		// Invalid pattern in re:find
    41  		That("re:find '(' x").Throws(ErrorWithType(&syntax.Error{})),
    42  
    43  		// Without any flag, finds ax
    44  		That("put (re:find 'a(x|xy)' AaxyZ)[text]").Puts("ax"),
    45  		// With &longest, finds axy
    46  		That("put (re:find &longest 'a(x|xy)' AaxyZ)[text]").Puts("axy"),
    47  		// Basic verification of &posix behavior.
    48  		That("put (re:find &posix 'a(x|xy)+' AaxyxxxyZ)[text]").Puts("axyxxxy"),
    49  
    50  		// re:find bubbles output error
    51  		That("re:find . ab >&-").Throws(eval.ErrNoValueOutput),
    52  
    53  		That("re:replace '(ba|z)sh' '${1}SH' 'bash and zsh'").Puts("baSH and zSH"),
    54  		That("re:replace &literal '(ba|z)sh' '$sh' 'bash and zsh'").Puts("$sh and $sh"),
    55  		That("re:replace '(ba|z)sh' {|x| put [&bash=BaSh &zsh=ZsH][$x] } 'bash and zsh'").Puts("BaSh and ZsH"),
    56  
    57  		// Invalid pattern in re:replace
    58  		That("re:replace '(' x bash").Throws(ErrorWithType(&syntax.Error{})),
    59  		That("re:replace &posix '[[:argle:]]' x bash").Throws(ErrorWithType(&syntax.Error{})),
    60  		// Replacement function outputs more than one value
    61  		That("re:replace x {|x| put a b } xx").Throws(ErrorWithType(&errs.ArityMismatch{})),
    62  		// Replacement function outputs non-string value
    63  		That("re:replace x {|x| put [] } xx").Throws(ErrorWithType(&errs.BadValue{})),
    64  		// Replacement is not string or function
    65  		That("re:replace x [] xx").Throws(ErrorWithType(&errs.BadValue{})),
    66  		// Replacement is function when &literal is set
    67  		That("re:replace &literal x {|_| put y } xx").Throws(ErrorWithType(&errs.BadValue{})),
    68  
    69  		That("re:split : /usr/sbin:/usr/bin:/bin").Puts("/usr/sbin", "/usr/bin", "/bin"),
    70  		That("re:split &max=2 : /usr/sbin:/usr/bin:/bin").Puts("/usr/sbin", "/usr/bin:/bin"),
    71  		// Invalid pattern in re:split
    72  		That("re:split '(' x").Throws(ErrorWithType(&syntax.Error{})),
    73  
    74  		// re:split bubbles output error
    75  		That("re:split . ab >&-").Throws(eval.ErrNoValueOutput),
    76  
    77  		That("re:quote a.txt").Puts(`a\.txt`),
    78  		That("re:quote '(*)'").Puts(`\(\*\)`),
    79  	)
    80  }