github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/cmds/elvish/eval/re/re_test.go (about) 1 package re 2 3 import ( 4 "testing" 5 6 "github.com/u-root/u-root/cmds/elvish/eval" 7 "github.com/xiaq/persistent/vector" 8 ) 9 10 var tests = []eval.Test{ 11 eval.That("re:match . xyz").Puts(true), 12 eval.That("re:match . ''").Puts(false), 13 eval.That("re:match '[a-z]' A").Puts(false), 14 15 eval.That("re:find . ab").Puts( 16 newMatch("a", 0, 1, vector.Empty.Cons(newSubmatch("a", 0, 1))), 17 newMatch("b", 1, 2, vector.Empty.Cons(newSubmatch("b", 1, 2))), 18 ), 19 eval.That("re:find '[A-Z]([0-9])' 'A1 B2'").Puts( 20 newMatch("A1", 0, 2, 21 vector.Empty.Cons(newSubmatch("A1", 0, 2)).Cons(newSubmatch("1", 1, 2))), 22 newMatch("B2", 3, 5, 23 vector.Empty.Cons(newSubmatch("B2", 3, 5)).Cons(newSubmatch("2", 4, 5))), 24 ), 25 26 eval.That("re:replace '(ba|z)sh' '${1}SH' 'bash and zsh'").Puts("baSH and zSH"), 27 eval.That("re:replace &literal '(ba|z)sh' '$sh' 'bash and zsh'").Puts("$sh and $sh"), 28 eval.That("re:replace '(ba|z)sh' [x]{ put [&bash=BaSh &zsh=ZsH][$x] } 'bash and zsh'").Puts("BaSh and ZsH"), 29 30 eval.That("re:split : /usr/sbin:/usr/bin:/bin").Puts("/usr/sbin", "/usr/bin", "/bin"), 31 eval.That("re:split &max=2 : /usr/sbin:/usr/bin:/bin").Puts("/usr/sbin", "/usr/bin:/bin"), 32 33 eval.That("re:quote a.txt").Puts(`a\.txt`), 34 eval.That("re:quote '(*)'").Puts(`\(\*\)`), 35 } 36 37 func TestRe(t *testing.T) { 38 if err := eval.RunTests(tests, func() *eval.Evaler { 39 ev := eval.NewEvaler() 40 ev.Builtin.AddNs("re", Ns) 41 return ev 42 }); err != nil { 43 t.Error(err) 44 } 45 }