aboutsummaryrefslogtreecommitdiff
path: root/tests/function/builtin/testscript
blob: 02c73eed9527a1ab1a57ce5fe8cee594faea783d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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
}

: 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
}

: string
:
{
  $* <'print $string([uint64] 0xffff)'        >'65535'      : uint
  $* <'print $string([uint64] 0xffff, 16)'    >'0xffff'     : uint-hex
  $* <'print $string([uint64] 0xffff, 16, 8)' >'0x0000ffff' : uint-hex-width
}

: sort
:
{
  $* <'print $sort([uint64s] 0 2 1 000)'        >'0 0 1 2' : basics
  $* <'print $sort([uint64s] 0 2 1 000, dedup)' >'0 1 2'   : dedup
}

: getenv
:
{
  : set
  :
  : Here we rely on the facts that on POSIX PATH environment variable most
  : likely is set at the time of login, and on Windows it is set by build2 on
  : startup.
  :
  : @@ Use a custom variable, when an ability to set environment variables in
  :    testscript is implemented.
  :
  {
    : string
    :
    $* <'print $getenv([string] "PATH")' | set v;
    ($v != '[null]') || exit "PATH environment variable is not set"

    : untyped
    :
    $* <'print $getenv("PATH")' | set v;
    ($v != '[null]') || exit "PATH environment variable is not set"

    : 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]'
}