github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/mods/re/re_test.go (about)

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