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

     1  ///////////
     2  # kind-of #
     3  ///////////
     4  
     5  
     6  ~> kind-of a []
     7  ▶ string
     8  ▶ list
     9  // bubbling output error
    10  ~> kind-of a >&-
    11  Exception: port does not support value output
    12    [tty]:1:1-13: kind-of a >&-
    13  
    14  //////////////
    15  # constantly #
    16  //////////////
    17  ~> var f = (constantly foo)
    18     $f
    19  ▶ foo
    20  ~> $f
    21  ▶ foo
    22  // bubbling output error
    23  ~> (constantly foo) >&-
    24  Exception: port does not support value output
    25    [tty]:1:1-20: (constantly foo) >&-
    26  
    27  ////////
    28  # call #
    29  ////////
    30  
    31  ~> call {|arg &opt=v| put $arg $opt } [foo] [&opt=bar]
    32  ▶ foo
    33  ▶ bar
    34  ~> call { } [foo] [&[]=bar]
    35  Exception: bad value: option key must be string, but is list
    36    [tty]:1:1-24: call { } [foo] [&[]=bar]
    37  
    38  ////////
    39  # eval #
    40  ////////
    41  
    42  ~> eval 'put x'
    43  ▶ x
    44  
    45  ## using variable from the local scope ##
    46  ~> var x = foo
    47     eval 'put $x'
    48  ▶ foo
    49  
    50  ## setting a variable in the local scope ##
    51  ~> var x = foo
    52     eval 'set x = bar'
    53     put $x
    54  ▶ bar
    55  
    56  ## using variable from the upvalue scope ##
    57  ~> var x = foo
    58     { nop $x; eval 'put $x' }
    59  ▶ foo
    60  
    61  ## using &ns to specify a namespace ##
    62  ~> var n = (ns [&x=foo])
    63     eval 'put $x' &ns=$n
    64  ▶ foo
    65  
    66  ## altering variables in the specified namespace ##
    67  ~> var n = (ns [&x=foo])
    68     eval 'set x = bar' &ns=$n
    69     put $n[x]
    70  ▶ bar
    71  
    72  ## newly created variables do not appear in the local namespace ##
    73  ~> eval 'x = foo'
    74     put $x
    75  Compilation error: variable $x not found
    76    [tty]:2:5-6: put $x
    77  
    78  ## newly created variables do not alter the specified namespace either ##
    79  ~> var n = (ns [&])
    80     eval &ns=$n 'var x = foo'
    81     put $n[x]
    82  Exception: no such key: x
    83    [tty]:3:5-9: put $n[x]
    84  
    85  ## newly created variable can be accessed in the final namespace using &on-end ##
    86  ~> eval &on-end={|n| put $n[x] } 'var x = foo'
    87  ▶ foo
    88  
    89  ## parse error ##
    90  //force-eval-source-count 100
    91  ~> eval '['
    92  Exception: Parse error: should be ']'
    93    [eval 100]:1:2: [
    94    [tty]:1:1-8: eval '['
    95  
    96  ## compilation error ##
    97  //force-eval-source-count 100
    98  ~> eval 'put $x'
    99  Exception: Compilation error: variable $x not found
   100    [eval 100]:1:5-6: put $x
   101    [tty]:1:1-13: eval 'put $x'
   102  
   103  ## exception ##
   104  //force-eval-source-count 100
   105  ~> eval 'fail x'
   106  Exception: x
   107    [eval 100]:1:1-6: fail x
   108    [tty]:1:1-13: eval 'fail x'
   109  
   110  /////////////
   111  # deprecate #
   112  /////////////
   113  
   114  ~> deprecate msg
   115  Deprecation: msg
   116    [tty]:1:1-13: deprecate msg
   117  
   118  ## different call sites trigger multiple deprecation messages ##
   119  ~> fn f { deprecate msg }
   120  ~> f
   121  Deprecation: msg
   122    [tty]:1:1-1: f
   123  // Normally, just calling f from the next prompt will result in a different call
   124  // site because the source will have a different name like "[tty 3]" vs "[tty
   125  // 2]". But since in tests we always use "[tty]" for the source name, we need to
   126  // make the call appear on a different position to force it to be recognized as
   127  // a different call site.
   128  ~> nop; f
   129  Deprecation: msg
   130    [tty]:1:6-6: nop; f
   131  
   132  ## the same call site only triggers the message once ##
   133  ~> fn f { deprecate msg}
   134     fn g { f }
   135  ~> g
   136  Deprecation: msg
   137    [tty]:2:8-9: fn g { f }
   138  // See comment above about call site. In this case, the (immediate) call site of
   139  // f is from g, so even if the call site of g differs, the call site of f is the
   140  // same.
   141  ~> nop; g
   142  
   143  ///////////
   144  # use-mod #
   145  ///////////
   146  
   147  //tmp-lib-dir
   148  ~> echo 'var x = value' > $lib/mod.elv
   149  ~> put (use-mod mod)[x]
   150  ▶ value
   151  
   152  ///////////
   153  # resolve #
   154  ///////////
   155  
   156  ~> resolve for
   157  ▶ special
   158  ~> resolve put
   159  ▶ '$put~'
   160  ~> fn f { }
   161     resolve f
   162  ▶ '$f~'
   163  // Unknown commands resolve to an external even if it doesn't exist.
   164  ~> resolve cat
   165  ▶ '(external cat)'
   166  
   167  ## module function ##
   168  //tmp-lib-dir
   169  ~> echo 'fn func { }' > $lib/mod.elv
   170  ~> use mod
   171  ~> resolve mod:func
   172  ▶ '$mod:func~'