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

     1  ///////
     2  # put #
     3  ///////
     4  
     5  ~> put foo bar
     6  ▶ foo
     7  ▶ bar
     8  ~> put $nil
     9  ▶ $nil
    10  // bubbling output error
    11  ~> put foo >&-
    12  Exception: port does not support value output
    13    [tty]:1:1-11: put foo >&-
    14  
    15  //////////
    16  # repeat #
    17  //////////
    18  
    19  ~> repeat 4 foo
    20  ▶ foo
    21  ▶ foo
    22  ▶ foo
    23  ▶ foo
    24  // bubbling output error
    25  ~> repeat 1 foo >&-
    26  Exception: port does not support value output
    27    [tty]:1:1-16: repeat 1 foo >&-
    28  
    29  //////////////
    30  # read-bytes #
    31  //////////////
    32  
    33  ~> print abcd | read-bytes 1
    34  ▶ a
    35  // read-bytes does not consume more than needed ##
    36  ~> print abcd | { read-bytes 1; slurp }
    37  ▶ a
    38  ▶ bcd
    39  // reads up to EOF ##
    40  ~> print abcd | read-bytes 10
    41  ▶ abcd
    42  // bubbling output error
    43  ~> print abcd | read-bytes 1 >&-
    44  Exception: port does not support value output
    45    [tty]:1:14-29: print abcd | read-bytes 1 >&-
    46  
    47  /////////////
    48  # read-upto #
    49  /////////////
    50  
    51  ~> print abcd | read-upto c
    52  ▶ abc
    53  // read-upto does not consume more than needed ##
    54  ~> print abcd | { read-upto c; slurp }
    55  ▶ abc
    56  ▶ d
    57  // read-upto reads up to EOF ##
    58  ~> print abcd | read-upto z
    59  ▶ abcd
    60  // bad terminator
    61  ~> print abcd | read-upto cd
    62  Exception: bad value: terminator must be a single ASCII character, but is cd
    63    [tty]:1:14-25: print abcd | read-upto cd
    64  // bubbling output error
    65  ~> print abcd | read-upto c >&-
    66  Exception: port does not support value output
    67    [tty]:1:14-28: print abcd | read-upto c >&-
    68  
    69  /////////////
    70  # read-line #
    71  /////////////
    72  
    73  ~> print eof-ending | read-line
    74  ▶ eof-ending
    75  ~> print "lf-ending\n" | read-line
    76  ▶ lf-ending
    77  ~> print "crlf-ending\r\n" | read-line
    78  ▶ crlf-ending
    79  ~> print "extra-cr\r\r\n" | read-line
    80  ▶ "extra-cr\r"
    81  // bubbling output error
    82  ~> print eof-ending | read-line >&-
    83  Exception: port does not support value output
    84    [tty]:1:20-32: print eof-ending | read-line >&-
    85  
    86  /////////
    87  # print #
    88  /////////
    89  
    90  ~> print [foo bar] ; print "\n"
    91  [foo bar]
    92  ~> print foo bar &sep=, ; print "\n"
    93  foo,bar
    94  // bubbling output error
    95  ~> print foo >&-
    96  Exception: invalid argument
    97    [tty]:1:1-13: print foo >&-
    98  
    99  ////////
   100  # echo #
   101  ////////
   102  
   103  ~> echo [foo bar]
   104  [foo bar]
   105  // bubbling output error
   106  ~> echo foo >&-
   107  Exception: invalid argument
   108    [tty]:1:1-12: echo foo >&-
   109  
   110  //////////
   111  # pprint #
   112  //////////
   113  
   114  ~> pprint [foo bar]
   115  [
   116   foo
   117   bar
   118  ]
   119  // bubbling output error
   120  ~> pprint foo >&-
   121  Exception: invalid argument
   122    [tty]:1:1-14: pprint foo >&-
   123  
   124  ////////
   125  # repr #
   126  ////////
   127  
   128  ~> repr foo bar ['foo bar']
   129  foo bar ['foo bar']
   130  // bubbling output error
   131  ~> repr foo >&-
   132  Exception: invalid argument
   133    [tty]:1:1-12: repr foo >&-
   134  
   135  ////////
   136  # show #
   137  ////////
   138  
   139  ~> var exc = ?(fail foo)
   140     echo 'Showing exception:'
   141     show $exc
   142  Showing exception:
   143  Exception: foo
   144    [tty]:1:13-20: var exc = ?(fail foo)
   145  // bubbling output error
   146  ~> repr ?(fail foo) >&-
   147  Exception: invalid argument
   148    [tty]:1:1-20: repr ?(fail foo) >&-
   149  
   150  //////////////
   151  # only-bytes #
   152  //////////////
   153  
   154  ~> { echo bytes; put values } | only-bytes
   155  bytes
   156  // bubbling output error
   157  ~> { print bytes; put values } | only-bytes >&-
   158  Exception: invalid argument
   159    [tty]:1:31-44: { print bytes; put values } | only-bytes >&-
   160  
   161  ///////////////
   162  # only-values #
   163  ///////////////
   164  
   165  ~> { echo bytes; put values } | only-values
   166  ▶ values
   167  // bubbling output error
   168  ~> { print bytes; put values } | only-values >&-
   169  Exception: port does not support value output
   170    [tty]:1:31-45: { print bytes; put values } | only-values >&-
   171  
   172  /////////
   173  # slurp #
   174  /////////
   175  ~> print "a\nb" | slurp
   176  ▶ "a\nb"
   177  // bubbling output error
   178  ~> print "a\nb" | slurp >&-
   179  Exception: port does not support value output
   180    [tty]:1:16-24: print "a\nb" | slurp >&-
   181  
   182  //////////////
   183  # from-lines #
   184  //////////////
   185  
   186  ~> print "a\nb" | from-lines
   187  ▶ a
   188  ▶ b
   189  ~> print "a\nb\n" | from-lines
   190  ▶ a
   191  ▶ b
   192  // bubbling output error
   193  ~> print "a\nb\n" | from-lines >&-
   194  Exception: port does not support value output
   195    [tty]:1:18-31: print "a\nb\n" | from-lines >&-
   196  
   197  ////////////
   198  # to-lines #
   199  ////////////
   200  
   201  ~> put "l\norem" ipsum | to-lines
   202  l
   203  orem
   204  ipsum
   205  // bubbling output error
   206  ~> to-lines [foo] >&-
   207  Exception: invalid argument
   208    [tty]:1:1-18: to-lines [foo] >&-
   209  
   210  ///////////////////
   211  # from-terminated #
   212  ///////////////////
   213  
   214  ~> print "a\nb\x00\x00c\x00d" | from-terminated "\x00"
   215  ▶ "a\nb"
   216  ▶ ''
   217  ▶ c
   218  ▶ d
   219  ~> print "a\x00b\x00" | from-terminated "\x00"
   220  ▶ a
   221  ▶ b
   222  ~> print aXbXcXXd | from-terminated "X"
   223  ▶ a
   224  ▶ b
   225  ▶ c
   226  ▶ ''
   227  ▶ d
   228  // bad argument
   229  ~> from-terminated "xyz"
   230  Exception: bad value: terminator must be a single ASCII character, but is xyz
   231    [tty]:1:1-21: from-terminated "xyz"
   232  // bubbling output error
   233  ~> print aXbX | from-terminated X >&-
   234  Exception: port does not support value output
   235    [tty]:1:14-34: print aXbX | from-terminated X >&-
   236  
   237  /////////////////
   238  # to-terminated #
   239  /////////////////
   240  
   241  ~> put "l\norem" ipsum | to-terminated "\x00" | slurp
   242  ▶ "l\norem\x00ipsum\x00"
   243  ~> to-terminated "X" [a b c] ; print "\n"
   244  aXbXcX
   245  ~> to-terminated "XYZ" [a b c]
   246  Exception: bad value: terminator must be a single ASCII character, but is XYZ
   247    [tty]:1:1-27: to-terminated "XYZ" [a b c]
   248  // bubbling output error
   249  ~> to-terminated "X" [a b c] >&-
   250  Exception: invalid argument
   251    [tty]:1:1-29: to-terminated "X" [a b c] >&-
   252  
   253  /////////////
   254  # from-json #
   255  /////////////
   256  
   257  ~> echo '{"k": "v", "a": [1, 2]}' '"foo"' | from-json
   258  ▶ [&a=[(num 1) (num 2)] &k=v]
   259  ▶ foo
   260  ~> echo '[null, "foo"]' | from-json
   261  ▶ [$nil foo]
   262  // Numbers greater than 2^63 are supported
   263  ~> echo 100000000000000000000 | from-json
   264  ▶ (num 100000000000000000000)
   265  // Numbers with fractional parts become float64
   266  ~> echo 1.0 | from-json
   267  ▶ (num 1.0)
   268  ~> echo 'invalid' | from-json
   269  Exception: invalid character 'i' looking for beginning of value
   270    [tty]:1:18-26: echo 'invalid' | from-json
   271  // bubbling output error
   272  ~> echo '[]' | from-json >&-
   273  Exception: port does not support value output
   274    [tty]:1:13-25: echo '[]' | from-json >&-
   275  
   276  ///////////
   277  # to-json #
   278  ///////////
   279  
   280  ~> put [&k=v &a=[1 2]] foo | to-json
   281  {"a":["1","2"],"k":"v"}
   282  "foo"
   283  ~> put [$nil foo] | to-json
   284  [null,"foo"]
   285  // bubbling output error
   286  ~> to-json [foo] >&-
   287  Exception: invalid argument
   288    [tty]:1:1-17: to-json [foo] >&-
   289  
   290  //////////
   291  # printf #
   292  //////////
   293  
   294  ~> printf "abcd\n"
   295  abcd
   296  ~> printf "%s\n%s\n" abc xyz
   297  abc
   298  xyz
   299  // %q uses repr
   300  ~> printf "%q\n" "abc xyz"
   301  'abc xyz'
   302  ~> printf "%q\n" ['a b']
   303  ['a b']
   304  // %v uses to-string
   305  ~> printf "%v\n" abc
   306  abc
   307  // %#v is the same as %q
   308  ~> printf "%#v\n" "abc xyz"
   309  'abc xyz'
   310  // width and precision
   311  ~> printf "%5.3s\n" 3.1415
   312    3.1
   313  ~> printf "%5.3s\n" (num 3.1415)
   314    3.1
   315  // %t converts to bool
   316  ~> printf "%t\n" $true
   317  true
   318  ~> printf "%t\n" $nil
   319  false
   320  // %d and %b convert to integer
   321  ~> printf "%3d\n" (num 5)
   322    5
   323  ~> printf "%3d\n" 5
   324    5
   325  ~> printf "%08b\n" (num 5)
   326  00000101
   327  ~> printf "%08b\n" 5
   328  00000101
   329  // %f converts to float64
   330  ~> printf "%.1f\n" 3.1415
   331  3.1
   332  ~> printf "%.1f\n" (num 3.1415)
   333  3.1
   334  // does not interpret escape sequences
   335  ~> printf '%s\n%s\n' abc xyz ; print "\n"
   336  abc\nxyz\n
   337  // float verb with argument that can't be converted to float
   338  ~> printf "%f\n" 1.3x
   339  %!f(cannot parse as number: 1.3x)
   340  // integer verb with argument that can't be converted to integer
   341  ~> printf "%d\n" 3.5
   342  %!d(cannot parse as integer: 3.5)
   343  // unsupported verb
   344  ~> printf "%A\n" foo
   345  %!A(unsupported formatting verb)
   346  // bubbling output error
   347  ~> printf foo >&-
   348  Exception: invalid argument
   349    [tty]:1:1-14: printf foo >&-