# file      : tests/function/builtin/testscript
# license   : MIT; see accompanying LICENSE file

.include ../../common.testscript

: defined
:
{{
  : true
  :
  $* <<EOI >'true'
  foo = [null]
  print $defined(foo)
  EOI

  : false
  :
  $* <<EOI >'false'
  print $defined(foo)
  EOI
}}

: type
:
{{
  $* <'print $type([string])'      >'string'  : empty-typed
  $* <'print $type("")'            >''        : empty-untyped

  $* <'print $type([string, null])' >'string' : null-typed
  $* <'print $type([null])'        >''        : null-untyped

  $* <'print $type([string] abc)'  >'string'  : value-typed
  $* <'print $type(abc)'           >''        : value-untyped
}}

: null
:
{{
  $* <'print $null("")'     >'false'       : empty
  $* <'print $null(abc)'    >'false'       : value
  $* <'print $null([null])' >'true'        : null
}}

: empty
:
{{
  $* <<EOI     >'true'                       : empty-untyped
  x =
  print $empty($x)
  EOI

  $* <'print $empty([string])'     >'true'   : empty-typed
  $* <'print $empty(abc)'          >'false'  : name
  $* <'print $empty(abc cxx{foo})' >'false'  : names
  $* <'print $empty([bool] false)' >'false'  : bool
  $* <'print $empty([json] null)'  >'true'   : json-null
  $* <'print $empty([json] "[]")'  >'true'   : json-array
  $* <'print $empty([json] "{}")'  >'true'   : json-object
}}

: first-second
:
{{
  $* <'print $first(a@1)'     >'a'                            : first
  $* <'print $second(a@1)'    >'1'                            : second

  $* <'print $first(@1)'     >'{}'                            : first-empty
  $* <'print $second(a@)'    >'{}'                            : second-empty

  $* <'print $first(1)'     >'[null]'                         : first-null
  $* <'print $second(a)'    >'[null]'                         : second-null

  $* <'print $first(1, true)'     >'1'                        : first-all
  $* <'print $second(a, true)'    >'a'                        : second-all

  $* <'print $first(0 a@1 b@2 c@ 4)'     >'a b c'             : firsts
  $* <'print $second(z a@1 b@2 @3 d)'    >'1 2 3'             : seconds

  $* <'print $first(0 a@1 b@2 c@ 4, true)'     >'0 a b c 4'   : firsts-all
  $* <'print $second(z a@1 b@2 @3 d, true)'    >'z 1 2 3 d'   : seconds-all

  $* <'print $first([name_pair] a@1)'     >'a'                : first-typed
  $* <'print $second([name_pair] a@1)'    >'1'                : second-typed
}}

: identity
:
{{
  $* <'print       $identity([string])'        >'';
  $* <'print $type($identity([string]))'       >'string'   : empty-typed

  $* <'print       $identity("")'              >'{}';
  $* <'print $type($identity(""))'             >''         : empty-untyped

  $* <'print       $identity([string, null])'  >'[null]';
  $* <'print $type($identity([string, null]))' >'string'   : null-typed

  $* <'print       $identity([null])'          >'[null]';
  $* <'print $type($identity([null]))'         >''         : null-untyped

  $* <'print       $identity([string] abc)'    >'abc';
  $* <'print $type($identity([string] abc))'   >'string'   : typed

  $* <'print       $identity(abc)'             >'abc';
  $* <'print $type($identity(abc))'            >''         : untyped
}}

: getenv
:
{{
  : set
  :
  {{
    : string
    :
    env foo=bar -- $* <'print $getenv([string] "foo")' >'bar'

    : untyped
    :
    env foo=bar -- $* <'print $getenv("foo")' >'bar'

    : path
    :
    $* <'print $getenv([path] a)' >'[null]'

    : none
    :
    $* <'print $getenv()' 2>>~/EOE/ != 0
    <stdin>:1:8: error: unmatched call to getenv()
    /((
      info: candidate: getenv(<untyped>), qualified name builtin.getenv
    /)|(
      info: candidate: getenv(<untyped>), qualified name builtin.getenv
    /))
    EOE

    : names
    :
    $* <'print $getenv(a b)' 2>>EOE != 0
    error: invalid argument: invalid string value: multiple names
      <stdin>:1:8: info: while calling getenv(<untyped>)
    EOE
  }}

  : unset
  :
  $* <'print $getenv("non-existent-var-name")' >'[null]'
}}

: generate-uuid
:
{{
  : basics
  :
  {
    $* <'print $generate_uuid()' >>~/EOO/
      /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
      EOO
  }
}}

: sha256sum
:
{{
  : not-empty
  :
  {
    sha256sum --sum-only <:'abc' | set checksum

    $* <'print $sha256sum("abc")' >$checksum
  }

  : empty
  :
  {
    sha256sum --sum-only <:'' | set checksum

    $* <'print $sha256sum("")' >$checksum
  }
}}

: xxh64sum
:
{{
  : not-empty
  :
  {
    xxh64sum --sum-only <:'abc' | set checksum

    $* <'print $xxh64sum("abc")' >$checksum
  }

  : empty
  :
  {
    xxh64sum --sum-only <:'' | set checksum

    $* <'print $xxh64sum("")' >$checksum
  }
}}
