summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/lua-stdlib/spec/debug_spec.yaml
blob: 51027230444fda7b176a5ae449c1171089060dcf (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# General Lua Libraries for Lua 5.1, 5.2 & 5.3
# Copyright (C) 2011-2018 stdlib authors

before: |
  base_module  = 'debug'
  this_module  = 'std.debug'
  global_table = '_G'

  extend_base  = {'getfenv', 'setfenv', 'say', 'trace'}

  M = require(this_module)


specify std.debug:
- context when required:
  - context by name:
    - it does not touch the global table:
        expect(show_apis {added_to=global_table, by=this_module}).
           to_equal {}
    - it does not touch the core debug table:
        expect(show_apis {added_to=base_module, by=this_module}).
           to_equal {}
    - it contains apis from the core debug table:
        expect(show_apis {from=base_module, not_in=this_module}).
           to_contain.a_permutation_of(extend_base)

  - context via the std module:
    - it does not touch the global table:
        expect(show_apis {added_to=global_table, by='std'}).
           to_equal {}
    - it does not touch the core debug table:
        expect(show_apis {added_to=base_module, by='std'}).
           to_equal {}


- describe debug:
  - it does nothing when std._debug is disabled:
      expect(luaproc [[
         require 'std._debug'(false)
         require 'std.debug'('nothing to see here')
      ]]).not_to_contain_error 'nothing to see here'
  - it writes to stderr when std._debug is not set:
      expect(luaproc [[
         require 'std.debug'('debugging')
      ]]).to_contain_error 'debugging'
  - it writes to stderr when std._debug is enabled:
      expect(luaproc [[
         require 'std._debug'(true)
         require 'std.debug'('debugging')
      ]]).to_contain_error 'debugging'
  - it writes to stderr when std._debug.level is not set:
      expect(luaproc [[
         require 'std._debug'()
         require 'std.debug'('debugging')
      ]]).to_contain_error 'debugging'
  - it writes to stderr when std._debug.level is specified:
      expect(luaproc [[
         require 'std._debug'.level = 0
         require 'std.debug'('debugging')
      ]]).to_contain_error 'debugging'
      expect(luaproc [[
         require 'std._debug'.level = 1
         require 'std.debug'('debugging')
      ]]).to_contain_error 'debugging'
      expect(luaproc [[
         require 'std._debug'.level = 2
         require 'std.debug'('debugging')
      ]]).to_contain_error 'debugging'


- describe say:
  - it uses normalize.str:
      expect(luaproc [[require 'std.debug'.say {'debugging'}]]).
         to_contain_error(require 'std.normalize'.str {'debugging'})
  - context when std._debug is disabled:
    - before:
        preamble = [[
           require 'std._debug'(false)
        ]]
    - it does nothing when message level is not set:
        expect(luaproc(preamble .. [[
           require 'std.debug'.say 'nothing to see here'
        ]])).not_to_contain_error 'nothing to see here'
    - it does nothing when message is set:
        for _, level in next, {-999, 0, 1, 2, 999} do
           expect(luaproc(preamble .. [[
              require 'std.debug'.say(]] .. level .. [[, 'nothing to see here')
           ]])).not_to_contain_error 'nothing to see here'
        end
  - context when std._debug is not set:
    - it writes to stderr when message level is not set:
        expect(luaproc [[
           require 'std.debug'.say 'debugging'
        ]]).to_contain_error 'debugging'
    - it writes to stderr when message level is 1 or lower:
        for _, level in next, {-999, 0, 1} do
           expect(luaproc([[
              require 'std.debug'.say(]] .. level .. [[, 'debugging')
           ]])).to_contain_error 'debugging'
        end
    - it does nothing when message level is 2 or higher:
        for _, level in next, {2, 999} do
           expect(luaproc([[
              require 'std.debug'.say(]] .. level .. [[, 'nothing to see here')
           ]])).not_to_contain_error 'nothing to see here'
        end
  - context when std._debug is enabled:
    - before:
        preamble = [[
           require 'std._debug'(true)
        ]]
    - it writes to stderr when message level is not set:
        expect(luaproc(preamble .. [[
           require 'std.debug'.say 'debugging'
        ]])).to_contain_error 'debugging'
    - it writes to stderr when message level is 1 or lower:
        for _, level in next, {-999, 0, 1} do
           expect(luaproc(preamble .. [[
              require 'std.debug'.say(]] .. level .. [[, 'debugging')
           ]])).to_contain_error 'debugging'
        end
    - it does nothing when message level is 2 or higher:
        for _, level in next, {2, 999} do
           expect(luaproc(preamble .. [[
              require 'std.debug'.say(]] .. level .. [[, 'nothing to see here')
           ]])).not_to_contain_error 'nothing to see here'
        end
  - context when std._debug.level is not set:
    - it writes to stderr when message level is not set:
        expect(luaproc [[
           require 'std.debug'.say 'debugging'
        ]]).to_contain_error 'debugging'
    - it writes to stderr when message level is 1 or lower:
        for _, level in next, {-999, 0, 1} do
           expect(luaproc([[
              require 'std.debug'.say(]] .. level .. [[, 'debugging')
           ]])).to_contain_error 'debugging'
        end
    - it does nothing when message level is 2 or higher:
        for _, level in next, {2, 999} do
           expect(luaproc([[
              require 'std.debug'.say(]] .. level .. [[, 'nothing to see here')
           ]])).not_to_contain_error 'nothing to see here'
        end
  - context when std._debug.level is specified:
    - it writes to stderr when message level is 1 or lower:
        for _, level in next, {0, 1, 2} do
           expect(luaproc([[
              require 'std._debug'.level = ]] .. level .. [[
              require 'std.debug'.say 'debugging'
           ]])).to_contain_error 'debugging'
        end
    - it does nothing when message level is higher than debug level:
        expect(luaproc [[
           require 'std._debug'.level = 2
           require 'std.debug'.say(3, 'nothing to see here')
        ]]).not_to_contain_error 'nothing to see here'
    - it writes to stderr when message level equals debug level:
        expect(luaproc [[
           require 'std._debug'.level = 2
           require 'std.debug'.say(2, 'debugging')
        ]]).to_contain_error 'debugging'
    - it writes to stderr when message level is lower than debug level:
        expect(luaproc [[
           require 'std._debug'.level = 2
           require 'std.debug'.say(1, 'debugging')
        ]]).to_contain_error 'debugging'


- describe trace:
  - before:
      f = init(M, this_module,  'trace')

  - it does nothing when debug hint is disabled:
      expect(luaproc [[
         require 'std._debug'(false)
         require 'std.debug'
         os.exit(0)
      ]]).to_succeed_with ''
  - it does nothing when debug hint is not set:
      expect(luaproc [[
         require 'std.debug'
         os.exit(0)
      ]]).to_succeed_with ''
  - it does nothing when debug hint is enabled:
      expect(luaproc [[
         require 'std._debug'(true)
         require 'std.debug'
         os.exit(0)
      ]]).to_succeed_with ''
  - it enables automatically when std._debug.call is set: |
      expect(luaproc [[
         require 'std._debug'.call = true
         require 'std.debug'
         os.exit(1)
      ]]).to_fail_while_containing ':3 call exit'
  - it is enabled manually with debug.sethook: |
      expect(luaproc [[
         local debug = require 'std.debug'
         debug.sethook(debug.trace, 'cr')
         os.exit(1)
      ]]).to_fail_while_containing ':3 call exit'
  - it writes call trace log to standard error: |
      expect(luaproc [[
         local debug = require 'std.debug'
         debug.sethook(debug.trace, 'cr')
         os.exit(0)
      ]]).to_contain_error ':3 call exit'
  - it traces lua calls: |
      expect(luaproc [[
         local debug = require 'std.debug'        -- line 1
         local function incr(i) return i + 1 end  -- line 2
         debug.sethook(debug.trace, 'cr')         -- line 3
         os.exit(incr(41))                        -- line 4
      ]]).to_fail_while_matching '.*:4 call incr <2:.*:4 return incr <2:.*'
  - it traces C api calls: |
      expect(luaproc [[
         local debug = require 'std.debug'
         local function incr(i) return i + 1 end
         debug.sethook(debug.trace, 'cr')
         os.exit(incr(41))
      ]]).to_fail_while_matching '.*:4 call exit %[C%]%s$'