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

     1  //each:go-fns-mod-in-global
     2  
     3  ///////////
     4  # nullary #
     5  ///////////
     6  
     7  ~> go-fns:nullary
     8  
     9  ////////////
    10  # argument #
    11  ////////////
    12  
    13  ~> go-fns:takes-two-strings lorem ipsum
    14  a = "lorem", b = "ipsum"
    15  ~> go-fns:takes-variadic-strings lorem ipsum
    16  args = ["lorem" "ipsum"]
    17  ~> go-fns:takes-string-and-variadic-strings lorem ipsum
    18  first = "lorem", more = ["ipsum"]
    19  ~> go-fns:takes-int-float64 314 1.25
    20  i = 314, f = 1.25
    21  
    22  ## wrong number of arguments ##
    23  ~> go-fns:nullary foo
    24  Exception: arity mismatch: arguments must be 0 values, but is 1 value
    25    [tty]:1:1-18: go-fns:nullary foo
    26  ~> go-fns:takes-two-strings foo
    27  Exception: arity mismatch: arguments must be 2 values, but is 1 value
    28    [tty]:1:1-28: go-fns:takes-two-strings foo
    29  ~> go-fns:takes-string-and-variadic-strings
    30  Exception: arity mismatch: arguments must be 1 or more values, but is 0 values
    31    [tty]:1:1-40: go-fns:takes-string-and-variadic-strings
    32  
    33  ## wrong argument type ##
    34  ~> go-fns:takes-two-strings foo []
    35  Exception: wrong type for arg #1: wrong type: need string, got list
    36    [tty]:1:1-31: go-fns:takes-two-strings foo []
    37  ~> go-fns:takes-int-float64 foo 1.2
    38  Exception: wrong type for arg #0: cannot parse as integer: foo
    39    [tty]:1:1-32: go-fns:takes-int-float64 foo 1.2
    40  
    41  //////////
    42  # inputs #
    43  //////////
    44  
    45  ~> go-fns:takes-input [foo bar]
    46  input: foo
    47  input: bar
    48  ~> put foo bar | go-fns:takes-input
    49  input: foo
    50  input: bar
    51  
    52  ///////////
    53  # options #
    54  ///////////
    55  
    56  ## parsed options ##
    57  ~> go-fns:takes-options &foo=lorem
    58  opts = eval_test.someOptions{Foo:"lorem", Bar:"default"}
    59  ~> go-fns:takes-options &foo=lorem &bar=ipsum
    60  opts = eval_test.someOptions{Foo:"lorem", Bar:"ipsum"}
    61  
    62  ## RawOptions ##
    63  ~> go-fns:takes-raw-options &foo=ipsum
    64  opts = eval.RawOptions{"foo":"ipsum"}
    65  ~> go-fns:takes-raw-options &foo=ipsum &bar=ipsum
    66  opts = eval.RawOptions{"bar":"ipsum", "foo":"ipsum"}
    67  
    68  ## errors ##
    69  ~> go-fns:nullary &foo=lorem
    70  Exception: function does not accept any options
    71    [tty]:1:1-25: go-fns:nullary &foo=lorem
    72  // Regression tests for b.elv.sh/958.
    73  ~> go-fns:takes-options &bad=value
    74  Exception: unknown option: bad
    75    [tty]:1:1-31: go-fns:takes-options &bad=value
    76  
    77  /////////////////
    78  # return values #
    79  /////////////////
    80  
    81  ~> go-fns:returns-string
    82  ▶ 'a string'
    83  ~> go-fns:returns-int
    84  ▶ (num 233)
    85  ~> go-fns:returns-small-big-int
    86  ▶ (num 233)
    87  ~> go-fns:returns-slice
    88  ▶ foo
    89  ▶ bar
    90  ~> go-fns:returns-array
    91  ▶ foo
    92  ▶ bar
    93  // Named type with underlying slice type is not treated as slices
    94  ~> go-fns:returns-named-slice-type
    95  ▶ <unknown [foo bar]>
    96  
    97  ////////////////
    98  # error return #
    99  ////////////////
   100  
   101  ~> go-fns:returns-non-nil-error
   102  Exception: bad
   103    [tty]:1:1-28: go-fns:returns-non-nil-error
   104  ~> go-fns:returns-nil-error