src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/compile_value_test.elvts (about)

     1  ////////////
     2  # compound #
     3  ////////////
     4  
     5  ~> put {fi,elvi}sh{1.0,1.1}
     6  ▶ fish1.0
     7  ▶ fish1.1
     8  ▶ elvish1.0
     9  ▶ elvish1.1
    10  
    11  ## empty compound evaluates to '' ##
    12  ~> put {}
    13  ▶ ''
    14  ~> put [&k=][k]
    15  ▶ ''
    16  
    17  ## exception from any component is propagated ##
    18  ~> put a{[][1]}
    19  Exception: out of range: index must be from 0 to -1, but is 1
    20    [tty]:1:7-11: put a{[][1]}
    21  
    22  ## error in concatenating the values throws an exception ##
    23  ~> put []a
    24  Exception: cannot concatenate list and string
    25    [tty]:1:5-7: put []a
    26  
    27  ## error when applying tilde throws an exception ##
    28  ~> put ~[]
    29  Exception: tilde doesn't work on value of type list
    30    [tty]:1:5-7: put ~[]
    31  
    32  ////////////
    33  # indexing #
    34  ////////////
    35  
    36  ~> put [a b c][2]
    37  ▶ c
    38  ~> put [][0]
    39  Exception: out of range: index must be from 0 to -1, but is 0
    40    [tty]:1:5-9: put [][0]
    41  ~> put [&key=value][key]
    42  ▶ value
    43  ~> put [&key=value][bad]
    44  Exception: no such key: bad
    45    [tty]:1:5-21: put [&key=value][bad]
    46  ~> put (fail x)[a]
    47  Exception: x
    48    [tty]:1:6-11: put (fail x)[a]
    49  ~> put [foo][(fail x)]
    50  Exception: x
    51    [tty]:1:12-17: put [foo][(fail x)]
    52  
    53  ////////////////
    54  # list literal #
    55  ////////////////
    56  
    57  ~> put [a b c]
    58  ▶ [a b c]
    59  ~> put []
    60  ▶ []
    61  
    62  ## exception from element expression is propagated ##
    63  ~> put [ [][0] ]
    64  Exception: out of range: index must be from 0 to -1, but is 0
    65    [tty]:1:7-11: put [ [][0] ]
    66  
    67  ///////////////
    68  # map literal #
    69  ///////////////
    70  
    71  ~> put [&key=value]
    72  ▶ [&key=value]
    73  ~> put [&]
    74  ▶ [&]
    75  // Keys and values may evaluate to multiple values as long as their numbers
    76  // match.
    77  ~> put [&{a b}={foo bar}]
    78  ▶ [&a=foo &b=bar]
    79  
    80  ## exception from key or value is propagated ##
    81  ~> put [ &[][0]=a ]
    82  Exception: out of range: index must be from 0 to -1, but is 0
    83    [tty]:1:8-12: put [ &[][0]=a ]
    84  ~> put [ &a=[][0] ]
    85  Exception: out of range: index must be from 0 to -1, but is 0
    86    [tty]:1:10-14: put [ &a=[][0] ]
    87  
    88  ## error if number of keys and values in a single pair does not match ##
    89  ~> put [&{a b}={foo bar lorem}]
    90  Exception: 2 keys but 3 values
    91    [tty]:1:6-27: put [&{a b}={foo bar lorem}]
    92  
    93  //////////////////
    94  # string literal #
    95  //////////////////
    96  
    97  ~> put 'such \"''literal'
    98  ▶ 'such \"''literal'
    99  ~> put "much \n\033[31;1m$cool\033[m"
   100  ▶ "much \n\e[31;1m$cool\e[m"
   101  
   102  /////////
   103  # tilde #
   104  /////////
   105  
   106  //each:with-temp-home
   107  
   108  ~> eq ~ $E:HOME
   109  ▶ $true
   110  ~> eq ~/src $E:HOME/src
   111  ▶ $true
   112  // Trailing slash is retained
   113  ~> eq ~/src/ $E:HOME/src/
   114  ▶ $true
   115  
   116  ## tilde and wildcard ##
   117  ~> echo > ~/file1
   118     echo > ~/file2
   119  ~> eq [~/*] [~/file1 ~/file2]
   120  ▶ $true
   121  
   122  ## ~other doesn't add superfluous trailing slash ##
   123  // Regression test for b.elv.sh/1246
   124  //mock-one-other-home
   125  ~> eq ~other $other-home
   126  ▶ $true
   127  
   128  ## ~other and glob ##
   129  // Regression test for b.elv.sh/793
   130  //mock-one-other-home
   131  ~> echo > $other-home/file1
   132     echo > $other-home/file2
   133  ~> eq [~other/*] [$other-home/file1 $other-home/file2]
   134  ▶ $true
   135  
   136  ## unknown user ##
   137  //mock-no-other-home
   138  ~> put ~bad/*
   139  Exception: don't know home of bad
   140    [tty]:1:5-10: put ~bad/*
   141  
   142  ## ~* is an error ##
   143  // TODO: This should be a compilation error
   144  ~> put ~*
   145  Exception: cannot determine user name from glob pattern
   146    [tty]:1:5-6: put ~*
   147  
   148  ## error in GetHome ##
   149  //mock-get-home-error fake error
   150  ~> put ~
   151  Exception: fake error
   152    [tty]:1:5-5: put ~
   153  ~> put ~/foo
   154  Exception: fake error
   155    [tty]:1:5-9: put ~/foo
   156  ~> put ~/*
   157  Exception: fake error
   158    [tty]:1:5-7: put ~/*
   159  
   160  ////////////
   161  # wildcard #
   162  ////////////
   163  
   164  ~> put ***
   165  Compilation error: bad wildcard: "***"
   166    [tty]:1:5-7: put ***
   167  
   168  // More tests in glob_test.elvts
   169  
   170  //////////////////
   171  # output capture #
   172  //////////////////
   173  
   174  ~> put (put lorem ipsum)
   175  ▶ lorem
   176  ▶ ipsum
   177  ~> put (print "lorem\nipsum")
   178  ▶ lorem
   179  ▶ ipsum
   180  // \r\n is also supported as a line separator
   181  ~> print "lorem\r\nipsum\r\n" | all
   182  ▶ lorem
   183  ▶ ipsum
   184  
   185  /////////////////////
   186  # exception capture #
   187  /////////////////////
   188  
   189  ## Exception capture ##
   190  ~> bool ?(nop)
   191  ▶ $true
   192  ~> bool ?(e:false)
   193  ▶ $false
   194  
   195  ////////////////
   196  # variable use #
   197  ////////////////
   198  
   199  ## basic usage ##
   200  ~> var x = foo
   201     put $x
   202  ▶ foo
   203  
   204  ## must exist before use ##
   205  ~> put $x
   206  Compilation error: variable $x not found
   207    [tty]:1:5-6: put $x
   208  ~> put $x[0]
   209  Compilation error: variable $x not found
   210    [tty]:1:5-6: put $x[0]
   211  
   212  ## variable use in compound expr ##
   213  ~> var x = world
   214     put 'Hello, '$x'!'
   215  ▶ 'Hello, world!'
   216  
   217  ## exploding with $@ ##
   218  ~> var x = [elvish rules]
   219     put $@x
   220  ▶ elvish
   221  ▶ rules
   222  
   223  ## unqualified name resolves to local name before upvalue ##
   224  ~> var x = outer; { var x = inner; put $x }
   225  ▶ inner
   226  
   227  ## unqualified name resolves to upvalue if no local name exists ##
   228  ~> var x = outer; { put $x }
   229  ▶ outer
   230  
   231  ## unqualified name resolves to builtin if no local name or upvalue exists ##
   232  ~> put $true
   233  ▶ $true
   234  
   235  ## names like $:foo are reserved for now. ##
   236  ~> var x = val; put $:x
   237  Compilation error: variable $:x not found
   238    [tty]:1:18-20: var x = val; put $:x
   239  
   240  ## pseudo-namespace E: for environment variables ##
   241  //unset-env x
   242  ~> set-env x value
   243     put $E:x
   244  ▶ value
   245  ~> set E:x = new-value
   246     get-env x
   247  ▶ new-value
   248  
   249  ## colons after E: are part of the variable name ##
   250  //unset-env a:b
   251  ~> set-env a:b value
   252     put $E:a:b
   253  ▶ value
   254  ~> set E:a:b = new-value
   255     get-env a:b
   256  ▶ new-value
   257  
   258  ## pseudo-namespace e: for external commands ##
   259  // Resolution always succeeds regardless of whether the command exists
   260  ~> put $e:true~
   261  ▶ <external true>
   262  // Colons are always considered part of the name
   263  ~> put $e:a:b~
   264  ▶ <external a:b>
   265  
   266  ## namespace access ##
   267  ~> var ns: = (ns [&a= val])
   268     put $ns:a
   269  ▶ val
   270  
   271  ## multi-level namespace access ##
   272  ~> var ns: = (ns [&a:= (ns [&b= val])])
   273     put $ns:a:b
   274  ▶ val
   275  
   276  ## module name access is checked at runtime ##
   277  ~> use os
   278  ~> put $os:non-existent-variable
   279  Exception: variable $os:non-existent-variable not found
   280    [tty]:1:5-29: put $os:non-existent-variable
   281  
   282  ///////////
   283  # closure #
   284  ///////////
   285  
   286  ~> {|| }
   287  ~> {|x| put $x} foo
   288  ▶ foo
   289  
   290  ## assigning to captured variable ##
   291  ~> var x = lorem; {|| put $x; set x = ipsum }; put $x
   292  ▶ lorem
   293  ▶ ipsum
   294  
   295  ## Assigning to element of captured variable ##
   296  ~> var x = [a]; { set x[0] = b }; put $x[0]
   297  ▶ b
   298  
   299  ## shadowing ##
   300  ~> var x = ipsum; { var x = lorem; put $x }; put $x
   301  ▶ lorem
   302  ▶ ipsum
   303  
   304  ## shadowing by argument ##
   305  ~> var x = ipsum; {|x| put $x; set x = BAD } lorem; put $x
   306  ▶ lorem
   307  ▶ ipsum
   308  
   309  ## closure semantics ##
   310  ~> fn f {
   311       var x = (num 0)
   312       put { set x = (+ $x 1) } { put $x }
   313     }
   314  ~> var inc1 put1 = (f); $put1; $inc1; $put1
   315  ▶ (num 0)
   316  ▶ (num 1)
   317  ~> var inc2 put2 = (f); $put2; $inc2; $put2
   318  ▶ (num 0)
   319  ▶ (num 1)
   320  
   321  ## rest argument. ##
   322  ~> {|x @xs| put $x $xs } a b c
   323  ▶ a
   324  ▶ [b c]
   325  ~> {|a @b c| put $a $b $c } a b c d
   326  ▶ a
   327  ▶ [b c]
   328  ▶ d
   329  
   330  ## options ##
   331  ~> {|a &k=v| put $a $k } foo &k=bar
   332  ▶ foo
   333  ▶ bar
   334  
   335  ## option default value ##
   336  ~> {|a &k=v| put $a $k } foo
   337  ▶ foo
   338  ▶ v
   339  
   340  ## option must have default value ##
   341  ~> {|&k| }
   342  Compilation error: option must have default value
   343    [tty]:1:4-4: {|&k| }
   344  
   345  ## exception when evaluating option default value ##
   346  ~> {|&a=[][0]| }
   347  Exception: out of range: index must be from 0 to -1, but is 0
   348    [tty]:1:6-10: {|&a=[][0]| }
   349  
   350  ## option default value must be one value ##
   351  ~> {|&a=(put foo bar)| }
   352  Exception: arity mismatch: option default value must be 1 value, but is 2 values
   353    [tty]:1:6-18: {|&a=(put foo bar)| }
   354  
   355  ## argument name must be unqualified ##
   356  ~> {|a:b| }
   357  Compilation error: argument name must be unqualified
   358    [tty]:1:3-5: {|a:b| }
   359  
   360  ## argument name must not be empty ##
   361  ~> {|''| }
   362  Compilation error: argument name must not be empty
   363    [tty]:1:3-4: {|''| }
   364  ~> {|@| }
   365  Compilation error: argument name must not be empty
   366    [tty]:1:3-3: {|@| }
   367  
   368  ## argument name must not be duplicated ##
   369  ~> {|a a| }
   370  Compilation error: duplicate argument name 'a'
   371    [tty]:1:5-5: {|a a| }
   372  // but multiple _s are OK
   373  ~> nop {|_ a _| }
   374  
   375  ## option name must be unqualified ##
   376  ~> {|&a:b=1| }
   377  Compilation error: option name must be unqualified
   378    [tty]:1:4-6: {|&a:b=1| }
   379  
   380  ## option name must not be empty ##
   381  ~> {|&''=b| }
   382  Compilation error: option name must not be empty
   383    [tty]:1:4-5: {|&''=b| }
   384  
   385  ## should not have multiple rest arguments ##
   386  ~> {|@a @b| }
   387  Compilation error: only one argument may have @ prefix
   388    [tty]:1:6-7: {|@a @b| }