github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/builtin_special_test.go (about)

     1  package eval
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  )
     7  
     8  var builtinSpecialTests = []Test{
     9  	// del
    10  	That("x = [&k=v &k2=v2]; del x[k2]; keys $x").Puts("k"),
    11  	That("x = [[&k=v &k2=v2]]; del x[0][k2]; keys $x[0]").Puts("k"),
    12  
    13  	// if
    14  	That("if true { put then }").Puts("then"),
    15  	That("if $false { put then } else { put else }").Puts("else"),
    16  	That("if $false { put 1 } elif $false { put 2 } else { put 3 }").Puts("3"),
    17  	That("if $false { put 2 } elif true { put 2 } else { put 3 }").Puts("2"),
    18  
    19  	// try
    20  	That("try { nop } except { put bad } else { put good }").Puts("good"),
    21  	That("try { e:false } except - { put bad } else { put good }").Puts("bad"),
    22  	That("try { fail tr }").ErrorsWith(errors.New("tr")),
    23  	That("try { fail tr } finally { put final }").Puts(
    24  		"final").ErrorsWith(errors.New("tr")),
    25  	That("try { fail tr } except { fail ex } finally { put final }").Puts(
    26  		"final").ErrorsWith(errors.New("ex")),
    27  	That("try { fail tr } except { put ex } finally { fail final }").Puts(
    28  		"ex").ErrorsWith(errors.New("final")),
    29  	That("try { fail tr } except { fail ex } finally { fail final }").ErrorsWith(
    30  		errors.New("final")),
    31  
    32  	// while
    33  	That("x=0; while (< $x 4) { put $x; x=(+ $x 1) }").Puts("0", "1", "2", "3"),
    34  	That("x = 0; while (< $x 4) { put $x; break }").Puts("0"),
    35  	That("x = 0; while (< $x 4) { fail haha }").Errors(),
    36  
    37  	// for
    38  	That("for x [tempora mores] { put 'O '$x }").Puts("O tempora", "O mores"),
    39  	// break
    40  	That("for x [a] { break } else { put $x }").DoesNothing(),
    41  	// else
    42  	That("for x [a] { put $x } else { put $x }").Puts("a"),
    43  	// continue
    44  	That("for x [a b] { put $x; continue; put $x; }").Puts("a", "b"),
    45  
    46  	// fn.
    47  	That("fn f [x]{ put x=$x'.' }; f lorem; f ipsum").Puts(
    48  		"x=lorem.", "x=ipsum."),
    49  	// return.
    50  	That("fn f []{ put a; return; put b }; f").Puts("a"),
    51  
    52  	// Modules (see testmain_test.go for setup)
    53  	// "use" imports a module.
    54  	That(`use lorem; put $lorem:name`).Puts("lorem"),
    55  	// imports are lexically scoped
    56  	// TODO: Support testing for compilation error
    57  	// That(`{ use lorem }; put $lorem:name`).ErrorsAny(),
    58  
    59  	// use of imported variable is captured in upvalue
    60  	That(`({ use lorem; put { { put $lorem:name } } })`).Puts("lorem"),
    61  	// use of imported function is also captured in upvalue
    62  	That(`{ use lorem; { lorem:put-name } }`).Puts("lorem"),
    63  
    64  	// use of a nested module
    65  	That(`use a:b/c/d; put $d:name`).Puts("a/b/c/d"),
    66  	// module is cached after first use
    67  	That(`use has/init; use has/init`).Puts("has/init"),
    68  	// overriding module
    69  	That(`use d; put $d:name; use a/b/c/d; put $d:name`).Puts("d", "a/b/c/d"),
    70  	// relative uses
    71  	That(`use a/b/c/x; put $x:d $x:lorem`).Puts("a/b/c/d", "lorem"),
    72  
    73  	// Variables defined in the default global scope is invisible from modules
    74  	That("x = foo; use put-x").Errors(),
    75  
    76  	// TODO: Test module namespace
    77  }
    78  
    79  func TestBuiltinSpecial(t *testing.T) {
    80  	runTests(t, builtinSpecialTests)
    81  }