dbg_tags 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dbg_tags.rb +20 -1
  3. data/spec/01_tag_spec.rb +60 -3
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aaad620ad2b1c43ee4fad44431ff99f22d5fe07f66dbb68eb11247246d99083e
4
- data.tar.gz: e33c27bb2b870e18bd4a910ef0918db018174f89eb3aaf95c2619bca74e0dee6
3
+ metadata.gz: d23d3bfe9f58ffa0b26481b6aa296208c6febff871649c197506ee836a54f36e
4
+ data.tar.gz: 669f9963e7dcdb9bffceabff6c7f45ba48d7b75c0d7d7293d9c65d16d772eb1d
5
5
  SHA512:
6
- metadata.gz: 660f4307b55f0c7b0ff5e029916b436f000346c31cca80e1956eaada5a71599a2e1e21f654bc0d8cd6dfb086e0489445a4f4385394ec4f558e8f8284a9aaee59
7
- data.tar.gz: 5be152c20aa033ba1372d132b7f514a0ddbd5fe5da459148e9790f120ad5c342564e24402c344cfb61aee4e437aa16238380b13f9a86520fe624242f1c4ec00b
6
+ metadata.gz: 1b2f84b3808a68011ae0a489c294e20f81d10ebc8a99f28d620191dd862b9ae399046ddfd5e9952334730377a11c6b49c8912aaca3a91c9617b24838899b5785
7
+ data.tar.gz: db37f12455aef1520cd5a0c1e93dbe2f352b07f3cd345fc92b367e26da649b6eb817e66550b90d64a46ebc9fa7feeb971edd0344292854b756f7e2bff23fab1d
data/lib/dbg_tags.rb CHANGED
@@ -130,7 +130,7 @@ module Tag
130
130
  #
131
131
  # enable performs a merge with an existing enable.
132
132
  def enable *features, **opts
133
- org_enabled = @enabled.dup # to restore the state at the end (unused unless block_given)
133
+ org_enabled = @enabled.clone # to restore the state at the end (unused unless block_given)
134
134
  features.each do |feature|
135
135
  case feature
136
136
  when 0..5 # not a feature, apply to :generic
@@ -163,6 +163,21 @@ module Tag
163
163
  end # block_given?
164
164
  end # GlobalState.enable
165
165
 
166
+ # @param state [{Symbol=>0..5}] As returned by Tag.state (aka Tag.enabled)
167
+ # A block can be given to restore the original state afterwards.
168
+ # restore_state overwrites any existing enabled feature.
169
+ def restore_state state
170
+ org_enabled = @enabled.clone # to restore the state at the end (unused unless block_given)
171
+ @enabled = state.dup
172
+ if block_given?
173
+ begin
174
+ yield
175
+ ensure
176
+ @enabled = org_enabled
177
+ end # ensure
178
+ end # block_given?
179
+ end # GlobalState.restore_state
180
+
166
181
  # @param feature [Symbol]
167
182
  # @return [0..5] Current effective level for feature.
168
183
  def level feature
@@ -240,8 +255,12 @@ module Tag
240
255
  # @see GlobalState.enable
241
256
  def enable(...); global_state.enable(...); end
242
257
 
258
+ # @see GlobalState.restore_state
259
+ def restore_state(...); global_state.restore_state(...); end
260
+
243
261
  # @return [{Symbol=>0..5}] Keys are the features
244
262
  def enabled; global_state.enabled; end
263
+ alias state enabled
245
264
 
246
265
  # @return [IO] By default this is STDERR
247
266
  def stream; global_state.stream; end
data/spec/01_tag_spec.rb CHANGED
@@ -1,6 +1,8 @@
1
1
 
2
- require 'simplecov'
3
- SimpleCov.start
2
+ if t = ENV['USE_SIMPLECOV'] and !t.empty?
3
+ require 'simplecov'
4
+ SimpleCov.start { add_filter '/spec/' }
5
+ end
4
6
 
5
7
  require_relative '../lib/dbg_tags'
6
8
 
@@ -9,7 +11,7 @@ class Pathological
9
11
  Tag.trc { "HERE in #{self}!" }
10
12
  super
11
13
  end # to_s
12
- end
14
+ end # class Pathological
13
15
 
14
16
  describe 'tag' do
15
17
  before :each do
@@ -290,6 +292,61 @@ describe 'tag' do
290
292
  end
291
293
  end # it
292
294
 
295
+ it 'each fiber has a private tag system (tag_302)' do
296
+ Tag.enable threads: :trc do
297
+ expect(Tag.enabled).to eq({threads: 3})
298
+ t1 = Fiber.new do
299
+ expect(Tag.enabled).to eq({})
300
+ Tag.enable threads: :log do
301
+ expect(Tag.enabled).to eq({threads: 2})
302
+ Tag.trc(:threads) {
303
+ expect(Tag.inside?).to be true
304
+ sleep 1
305
+ nil
306
+ }
307
+ end
308
+ end
309
+ t1.resume
310
+ expect(Tag.enabled).to eq({threads: 3})
311
+ end
312
+ end # it
313
+
314
+ it 'does allow restore_state to transfer state through a Fiber barrier (tag_310)' do
315
+ did_something = false
316
+ Tag.enable example: :dtl, fiber: :trc do
317
+ state = Tag.state # same as Tag.enabled
318
+ expect(state).to eq({example: Tag::DTL, fiber: Tag::TRC})
319
+ t1 = Fiber.new do
320
+ expect(Tag.state).to eq({})
321
+ Tag.enable foo: :trc
322
+ Tag.restore_state state do
323
+ expect(Tag.state).to eq({example: Tag::DTL, fiber: Tag::TRC})
324
+ did_something = true
325
+ end
326
+ end
327
+ t1.resume
328
+ expect(Tag.state).to eq({example: Tag::DTL, fiber: Tag::TRC})
329
+ end # enable
330
+ expect(did_something).to be true
331
+ end # it
332
+
333
+ it 'does allow a nil-state to transfer state through a Fiber barrier (tag_311)' do
334
+ did_something = false
335
+ state = Tag.state
336
+ expect(state).to eq({})
337
+ t1 = Fiber.new do
338
+ expect(Tag.state).to eq({})
339
+ Tag.enable foo: :trc
340
+ Tag.restore_state state do
341
+ expect(Tag.state).to eq({})
342
+ did_something = true
343
+ end
344
+ end
345
+ t1.resume
346
+ expect(Tag.state).to eq({})
347
+ expect(did_something).to be true
348
+ end # it
349
+
293
350
  it 'does not allow levels out of range (tag_900)' do
294
351
  expect do
295
352
  Tag.enable nest: 24
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbg_tags
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Brazwick
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-06 00:00:00.000000000 Z
11
+ date: 2024-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec