structured_warnings 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +200 -0
  3. data/lib/{structured_warnings/dynamic.rb → dynamic.rb} +0 -0
  4. data/lib/structured_warnings.rb +19 -9
  5. data/lib/structured_warnings/base.rb +132 -0
  6. data/lib/structured_warnings/kernel.rb +5 -65
  7. data/lib/structured_warnings/minitest.rb +5 -0
  8. data/lib/structured_warnings/test.rb +3 -5
  9. data/lib/structured_warnings/test/assertions.rb +93 -98
  10. data/lib/structured_warnings/test/warner.rb +32 -35
  11. data/lib/structured_warnings/test_unit.rb +5 -0
  12. data/lib/structured_warnings/version.rb +3 -0
  13. data/lib/structured_warnings/warner.rb +13 -11
  14. data/lib/structured_warnings/warning.rb +61 -119
  15. data/lib/warning.rb +9 -0
  16. metadata +53 -92
  17. data/Gemfile +0 -3
  18. data/Gemfile.lock +0 -21
  19. data/History.txt +0 -36
  20. data/License.txt +0 -20
  21. data/README.rdoc +0 -137
  22. data/Rakefile +0 -55
  23. data/doc/DeprecatedMethodWarning.html +0 -105
  24. data/doc/DeprecatedSignatureWarning.html +0 -106
  25. data/doc/DeprecationWarning.html +0 -106
  26. data/doc/Dynamic.html +0 -125
  27. data/doc/Object.html +0 -117
  28. data/doc/README_rdoc.html +0 -283
  29. data/doc/StandardWarning.html +0 -104
  30. data/doc/StructuredWarnings.html +0 -125
  31. data/doc/StructuredWarnings/ClassMethods.html +0 -192
  32. data/doc/StructuredWarnings/Kernel.html +0 -222
  33. data/doc/StructuredWarnings/Test.html +0 -97
  34. data/doc/StructuredWarnings/Test/Assertions.html +0 -272
  35. data/doc/StructuredWarnings/Test/Warner.html +0 -208
  36. data/doc/StructuredWarnings/Warner.html +0 -162
  37. data/doc/Test.html +0 -95
  38. data/doc/Test/Unit.html +0 -95
  39. data/doc/Warning.html +0 -398
  40. data/doc/Warning/ClassMethods.html +0 -278
  41. data/doc/created.rid +0 -10
  42. data/doc/css/fonts.css +0 -167
  43. data/doc/css/rdoc.css +0 -590
  44. data/doc/fonts/Lato-Light.ttf +0 -0
  45. data/doc/fonts/Lato-LightItalic.ttf +0 -0
  46. data/doc/fonts/Lato-Regular.ttf +0 -0
  47. data/doc/fonts/Lato-RegularItalic.ttf +0 -0
  48. data/doc/fonts/SourceCodePro-Bold.ttf +0 -0
  49. data/doc/fonts/SourceCodePro-Regular.ttf +0 -0
  50. data/doc/images/add.png +0 -0
  51. data/doc/images/arrow_up.png +0 -0
  52. data/doc/images/brick.png +0 -0
  53. data/doc/images/brick_link.png +0 -0
  54. data/doc/images/bug.png +0 -0
  55. data/doc/images/bullet_black.png +0 -0
  56. data/doc/images/bullet_toggle_minus.png +0 -0
  57. data/doc/images/bullet_toggle_plus.png +0 -0
  58. data/doc/images/date.png +0 -0
  59. data/doc/images/delete.png +0 -0
  60. data/doc/images/find.png +0 -0
  61. data/doc/images/loadingAnimation.gif +0 -0
  62. data/doc/images/macFFBgHack.png +0 -0
  63. data/doc/images/package.png +0 -0
  64. data/doc/images/page_green.png +0 -0
  65. data/doc/images/page_white_text.png +0 -0
  66. data/doc/images/page_white_width.png +0 -0
  67. data/doc/images/plugin.png +0 -0
  68. data/doc/images/ruby.png +0 -0
  69. data/doc/images/tag_blue.png +0 -0
  70. data/doc/images/tag_green.png +0 -0
  71. data/doc/images/transparent.png +0 -0
  72. data/doc/images/wrench.png +0 -0
  73. data/doc/images/wrench_orange.png +0 -0
  74. data/doc/images/zoom.png +0 -0
  75. data/doc/index.html +0 -121
  76. data/doc/js/darkfish.js +0 -161
  77. data/doc/js/jquery.js +0 -4
  78. data/doc/js/navigation.js +0 -142
  79. data/doc/js/navigation.js.gz +0 -0
  80. data/doc/js/search.js +0 -109
  81. data/doc/js/search_index.js +0 -1
  82. data/doc/js/search_index.js.gz +0 -0
  83. data/doc/js/searcher.js +0 -228
  84. data/doc/js/searcher.js.gz +0 -0
  85. data/doc/table_of_contents.html +0 -200
  86. data/structured_warnings.gemspec +0 -24
  87. data/test/structured_warnings_test.rb +0 -187
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b48e72d14f6d01966512679d69e6fbc7f012b15
4
- data.tar.gz: 71d1ba1bf37e469cee648f774dc8a8e805e557d8
3
+ metadata.gz: 82e448e3265ea35dd71196fb6f3adbc6b629cbb5
4
+ data.tar.gz: 5ed838c91e79e3d459f10d2cb9c7b596f03b5d98
5
5
  SHA512:
6
- metadata.gz: b66bd37bd801f9de3541d9a2793ffa8da342a8f2cf253a6295a6da55ae3230dab6f2cfba46994c27351c8050d8e3ed7998cb4dd2936b830a823023c2e1a77ed1
7
- data.tar.gz: e6db07f6d4e5393cc009d62300c62a8ff32d500d00ebf44b4dec97e8a5c6cc92226db98048f6db8a241673b398b186daf36f5b893e9b0fd8740ba4b4cee24534
6
+ metadata.gz: c784e0acb8c6aa8840ba44a36f5fdd090d7a46d178b4701fc97f1c6203d0f0de41f8d90a8579a53a7064a4df9146a314a936b6c6d290c892d3022840bce58282
7
+ data.tar.gz: f816ed5e9f249359c9c8108ba65c978140f9c2cef49f7d70202b26537ba4ced18ad1699c76ee9954ea8771bde64da59e98a08f34deee41766b06f76328a836df
@@ -0,0 +1,200 @@
1
+ # StructuredWarnings
2
+
3
+ > **Disclaimer:** This is an experimental reimplementation of structured
4
+ > warnings, based on Ruby 2.4's new warning module. This is not yet ready for
5
+ > prime time.
6
+
7
+ This is an implementation of Daniel Berger's [proposal of structured warnings
8
+ for Ruby](https://web.archive.org/web/20140328021259/http://www.oreillynet.com/ruby/blog/2008/02/structured_warnings_now.html).
9
+ They provide dynamic suppression and activation, as well as, an inheritance
10
+ hierarchy to model their relations. This library preserves the old `warn`
11
+ signature, but additionally allows a `raise`-like use.
12
+
13
+ For more information on the usage and benefits of this library have a look at
14
+ the inspiring article at O'Reilly.
15
+
16
+ [www.oreillynet.com/ruby/blog/2008/02/structured\_warnings\_now.html](https://web.archive.org/web/20140328021259/http://www.oreillynet.com/ruby/blog/2008/02/structured_warnings_now.html)
17
+ (link to web archive - O'Reilly took it down)
18
+
19
+
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'structured_warnings'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install structured_warnings
36
+
37
+
38
+ ## Compatibility
39
+
40
+ `structured_warnings` aims to work with all Ruby interpreters. Please file a bug
41
+ for any incompatibilities.
42
+
43
+
44
+ Versions of `structured_warnings` before `v0.3.0` are incompatible with Ruby
45
+ 2.4+. Please upgrade accordingly, if you need Ruby 2.4 compatibility. Please
46
+ note on the otherhand, that many class names changed in an incompatible way
47
+ with `structured_warnings` `v0.3.0`. This was done to avoid future name clashes.
48
+
49
+ Here's a table which should ease upgrading.
50
+
51
+ | v0.2.0 and before | v0.3.0 and after |
52
+ |------------------------------|--------------------------------------------------|
53
+ | `Warning` | `StructuredWarnings::Base` |
54
+ | `StandardWarning` | `StructuredWarnings::StandardWarning` |
55
+ | `DeprecationWarning` | `StructuredWarnings::DeprecationWarning` |
56
+ | `DeprecatedMethodWarning` | `StructuredWarnings::DeprecatedMethodWarning` |
57
+ | `DeprecatedSignatureWarning` | `StructuredWarnings::DeprecatedSignatureWarning` |
58
+
59
+
60
+ ### Test framework support
61
+
62
+ `structured_warnings` supports both
63
+ [test-unit](https://github.com/test-unit/test-unit/) and
64
+ [minitest/test](https://github.com/seattlerb/minitest/) by adding the
65
+ `assert_warn` and `assert_no_warn` assertions.
66
+
67
+ Pull requests which add support for `RSpec` or `minitest/spec` are very welcome.
68
+
69
+
70
+ ### Known Issues
71
+
72
+ In Ruby versions before 2.4, the library may not extend Ruby's built-in
73
+ warnings handled by the C-level function `rb_warn`. Therefore warnings like
74
+ "method redefined", "void context", and "parenthesis" may not be manipulated by
75
+ `structured_warnings`.
76
+
77
+
78
+ ## Usage
79
+
80
+ To get you started - here is a short example
81
+
82
+ In order to use `structured_warnings` in library code, use the following code.
83
+
84
+ ```ruby
85
+ # in lib/...
86
+ require 'structured_warnings'
87
+
88
+ class Foo
89
+ def old_method
90
+ warn StructuredWarnings::DeprecatedMethodWarning, 'This method is deprecated. Use new_method instead'
91
+ # Do stuff
92
+ end
93
+ end
94
+
95
+ # in test/...
96
+ require 'test/unit'
97
+ require 'structured_warnings'
98
+
99
+ class FooTests < Test::Unit::TestCase
100
+ def setup
101
+ @foo = Foo.new
102
+ end
103
+
104
+ def test_old_method_emits_deprecation_warning
105
+ assert_warn(StructuredWarnings::DeprecatedMethodWarning){ @foo.old_method }
106
+ end
107
+ end
108
+ ```
109
+
110
+ `StructuredWarnings::DeprecatedMethodWarning` is only one of multiple predefined
111
+ warning types. You may add your own types by subclassing
112
+ `StructuredWarnings::Base` if you like.
113
+
114
+ Client code of your library will look as follows:
115
+
116
+ ```ruby
117
+ require "foo"
118
+
119
+ foo = Foo.new
120
+ foo.old_method # => will print
121
+ # ... `old_method' : This method is deprecated. Use new_method instead (StructuredWarnings::DeprecatedMethodWarning)
122
+ ```
123
+
124
+ But the main difference to the standard warning concept shipped with ruby, is
125
+ that the client is able to selectively disable certain warnings s/he is aware of
126
+ and not willing to fix.
127
+
128
+ ```ruby
129
+ StructuredWarnings::DeprecatedMethodWarning.disable # Globally disable warnings about deprecated methods!
130
+
131
+ foo.old_method # => will print nothing
132
+
133
+ StructuredWarnings::DeprecatedMethodWarning.enable # Reenable warnings again.
134
+ ```
135
+
136
+ And there is an even more powerful option for your clients, the can selectively
137
+ disable warnings in a dynamic block scope.
138
+
139
+ ```ruby
140
+ # Don't bug me about deprecated method warnings within this block, I know
141
+ # what I'm doing.
142
+ #
143
+ StructuredWarnings::DeprecatedMethodWarning.disable do
144
+ foo.old_method
145
+ end
146
+ ```
147
+
148
+ These settings are scoped to the local thread (and all threads spawned in the
149
+ block scope) and automatically reset after the block.
150
+
151
+
152
+ ## Detailed Documentation
153
+
154
+ Have closer look at the RDoc of `StructuredWarnings::Warning`,
155
+ `StructuredWarnings::Base` and `StructuredWarnings::Base::ClassMethods`.
156
+
157
+ Part of this library is a set of different warnings:
158
+
159
+ * `StructuredWarnings::Base`
160
+ * `StructuredWarnings::BuiltInWarning`
161
+ * `StructuredWarnings::StandardWarning`
162
+ * `StructuredWarnings::DeprecationWarning`
163
+ * `StructuredWarnings::DeprecatedMethodWarning`
164
+ * `StructuredWarnings::DeprecatedSignatureWarning`
165
+
166
+ You are encouraged to use your own subclasses of `StructuredWarnings::Base` to
167
+ give as much feedback to your users as possible.
168
+
169
+
170
+ ## Resources
171
+
172
+ * [Inspiring article](https://web.archive.org/web/20140328021259/http://www.oreillynet.com/ruby/blog/2008/02/structured_warnings_now.html)
173
+ * [Implementation Highlights](http://www.nach-vorne.de/2008/2/22/structured_warnings-highlights)
174
+ * [Project's website](https://github.com/schmidt/structured_warnings/)
175
+ * [API doc](http://rdoc.info/projects/schmidt/structured_warnings)
176
+ * [Build status](https://travis-ci.org/schmidt/structured_warnings)
177
+
178
+
179
+ ## Development
180
+
181
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
182
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
183
+ prompt that will allow you to experiment.
184
+
185
+ To install this gem onto your local machine, run `bundle exec rake install`. To
186
+ release a new version, update the version number in `version.rb`, and then run
187
+ `bundle exec rake release`, which will create a git tag for the version, push
188
+ git commits and tags, and push the `.gem` file to
189
+ [rubygems.org](https://rubygems.org).
190
+
191
+ ## Contributing
192
+
193
+ Bug reports and pull requests are welcome on GitHub at
194
+ [github.com/schmidt/structured\_warnings](https://github.com/schmidt/structured_warnings).
195
+
196
+
197
+ ## License
198
+
199
+ The gem is available as open source under the terms of the [MIT
200
+ License](http://opensource.org/licenses/MIT).
@@ -1,11 +1,13 @@
1
- require "structured_warnings/dynamic"
2
- require "structured_warnings/kernel"
3
- require "structured_warnings/warner"
4
- require "structured_warnings/warning"
1
+ require 'structured_warnings/version'
2
+
3
+ # Compatibility layer
4
+ require 'warning' unless defined? ::Warning
5
+
6
+ require 'dynamic'
5
7
 
6
8
  module StructuredWarnings
7
- # If you <code>require "test/unit"</code> after +structured_warnings+ you
8
- # have to <code>require "structured_warnings/test"</code> manually,
9
+ # If you <code>require 'test/unit'</code> after +structured_warnings+ you
10
+ # have to <code>require 'structured_warnings/test'</code> manually,
9
11
  # otherwise the test extensions will be added automatically.
10
12
  module ClassMethods
11
13
  # Executes a block using the given warner. This may be used to suppress
@@ -58,12 +60,20 @@ module StructuredWarnings
58
60
  end
59
61
  #:startdoc:
60
62
  end
63
+
61
64
  extend ClassMethods
62
65
  end
63
66
 
64
- unless Object < StructuredWarnings::Kernel
65
- Object.class_eval { include StructuredWarnings::Kernel }
67
+ require 'structured_warnings/kernel'
68
+ require 'structured_warnings/warning'
69
+ require 'structured_warnings/warner'
70
+ require 'structured_warnings/base'
71
+
72
+
73
+ unless Dynamic.variables.key? :disabled_warnings
66
74
  StructuredWarnings::disabled_warnings = []
67
75
  StructuredWarnings::warner = StructuredWarnings::Warner.new
68
76
  end
69
- require "structured_warnings/test" if defined? Test::Unit::TestCase
77
+
78
+ require 'structured_warnings/minitest' if defined? Minitest::Test
79
+ require 'structured_warnings/test_unit' if defined? Test::Unit::TestCase
@@ -0,0 +1,132 @@
1
+ # Descendents of class StructuredWarnings::Base are used to raise structured
2
+ # warnings. They enable programmers to explicitly supress certain kinds of
3
+ # warnings and provide additional information in contrast to the plain warn
4
+ # method. They implement an Exception-like interface and carry information about
5
+ # the warning -- its type (the warning's class name), an optional descriptive
6
+ # string, and optional traceback information. Programs may subclass
7
+ # StructuredWarnings::Base to add additional information.
8
+ #
9
+ # Large portions of this class's documentation are taken from the Exception
10
+ # RDoc.
11
+ class StructuredWarnings::Base
12
+ # Construct a new StructuredWarning::Base object, optionally passing in a
13
+ # message.
14
+ def initialize(message = nil)
15
+ @message = message
16
+ @backtrace = caller(1)
17
+ end
18
+
19
+
20
+ # call-seq:
21
+ # warning.backtrace => array
22
+ #
23
+ # Returns any backtrace associated with the warning. The backtrace
24
+ # is an array of strings, each containing either ``filename:lineNo: in
25
+ # `method''' or ``filename:lineNo.''
26
+ def backtrace
27
+ @backtrace
28
+ end
29
+
30
+ # Sets the backtrace information associated with warning. The argument must
31
+ # be an array of String objects in the format described in
32
+ # Exception#backtrace.
33
+ def set_backtrace(backtrace)
34
+ @backtrace = backtrace
35
+ end
36
+
37
+ # Returns warning's message (or the name of the warning if no message is set).
38
+ def to_s
39
+ @message || self.class.name
40
+ end
41
+ alias_method :to_str, :to_s
42
+ alias_method :message, :to_s
43
+
44
+ # Return this warning's class name and message
45
+ def inspect
46
+ "#<#{self.class}: #{self}>"
47
+ end
48
+
49
+ # This module extends StructuredWarnings::Base and each subclass. It may be
50
+ # used to activate or deactivate a set of warnings.
51
+ module ClassMethods
52
+ # returns a Boolean, stating whether a warning of this type would be
53
+ # emmitted or not.
54
+ def active?
55
+ StructuredWarnings::disabled_warnings.all? {|w| !(w >= self)}
56
+ end
57
+
58
+ # call-seq:
59
+ # disable()
60
+ # disable() {...}
61
+ #
62
+ # If called without a block, warnings of this type will be disabled in the
63
+ # current thread and all new child threads.
64
+ #
65
+ # warn("this will be printed") # creates a StructuredWarnings::StandardWarning
66
+ # # which is enabled by default
67
+ #
68
+ # StructuredWarnings::Base.disable
69
+ #
70
+ # warn("this will not be printed") # creates a StructuredWarnings::StandardWarning
71
+ # # which is currently disabled
72
+ #
73
+ # If called with a block, warnings of this type will be disabled in the
74
+ # dynamic scope of the given block.
75
+ #
76
+ # StructuredWarnings::Base.disable do
77
+ # warn("this will not be printed") # creates a StructuredWarnings::StandardWarning
78
+ # # which is currently disabled
79
+ # end
80
+ #
81
+ # warn("this will be printed") # creates a StructuredWarnings::StandardWarning
82
+ # # which is currently enabled
83
+ def disable
84
+ if block_given?
85
+ StructuredWarnings::with_disabled_warnings(StructuredWarnings.disabled_warnings | [self]) do
86
+ yield
87
+ end
88
+ else
89
+ StructuredWarnings::disabled_warnings |= [self]
90
+ end
91
+ end
92
+
93
+ # call-seq:
94
+ # enable()
95
+ # enable() {...}
96
+ #
97
+ # This method has the same semantics as disable, only with the opposite
98
+ # outcome. In general the last assignment wins, so that disabled warnings
99
+ # may be enabled again and so on.
100
+ def enable
101
+ if block_given?
102
+ StructuredWarnings::with_disabled_warnings(StructuredWarnings.disabled_warnings - [self]) do
103
+ yield
104
+ end
105
+ else
106
+ StructuredWarnings::disabled_warnings -= [self]
107
+ end
108
+ end
109
+ end
110
+
111
+ extend ClassMethods
112
+ end
113
+
114
+ # This warning is used when calling #Kernel.warn without arguments.
115
+ class StructuredWarnings::StandardWarning < StructuredWarnings::Base; end
116
+
117
+ # This is a general warning used to mark certain actions as deprecated. We
118
+ # recommend to add a useful warning message, which alternative to use instead.
119
+ class StructuredWarnings::DeprecationWarning < StructuredWarnings::Base; end
120
+
121
+ # This warning marks single methods as deprecated. We
122
+ # recommend to add a useful warning message, which alternative to use instead.
123
+ class StructuredWarnings::DeprecatedMethodWarning < StructuredWarnings::DeprecationWarning; end
124
+
125
+ # This warning marks the given parameters for a certain methods as
126
+ # deprecated. We recommend to add a useful warning message, which
127
+ # alternative to use instead.
128
+ class StructuredWarnings::DeprecatedSignatureWarning < StructuredWarnings::DeprecationWarning; end
129
+
130
+ # This warning is used for Ruby's built in warnings about accessing unused
131
+ # instance vars, redefining constants, etc.
132
+ class StructuredWarnings::BuiltInWarning < StructuredWarnings::Base; end
@@ -1,67 +1,7 @@
1
- module StructuredWarnings
2
- # This module encapsulates the extensions to Object, that are introduced
3
- # by this library.
4
- module Kernel
5
-
6
- # :call-seq:
7
- # warn(message = nil)
8
- # warn(warning_class, message)
9
- # warn(warning_instance)
10
- #
11
- # This method provides a +raise+-like interface. It extends the default
12
- # warn in ::Kernel to allow the use of structured warnings.
13
- #
14
- # Internally it uses the StructuredWarnings::warner to format a message
15
- # based on the given warning class, the message and a stack frame.
16
- # The return value is passed to super, which is likely the implementation
17
- # in ::Kernel. That way, it is less likely, that structured_warnings
18
- # interferes with other extensions.
19
- #
20
- # It the warner return nil or an empty string the underlying warn will not
21
- # be called. That way, warnings may be transferred to other devices without
22
- # the need to redefine ::Kernel#warn.
23
- #
24
- # Just like the original version, this method does not take command line
25
- # switches or verbosity levels into account. In order to deactivate all
26
- # warnings use <code>Warning.disable</code>.
27
- #
28
- # warn "This is an old-style warning" # This will emit a StandardWarning
29
- #
30
- # class Foo
31
- # def bar
32
- # warn DeprecationWarning, "Never use bar again, use beer"
33
- # end
34
- # def beer
35
- # "Ahhh"
36
- # end
37
- # end
38
- #
39
- # warn Warning.new("The least specific warning you can get")
40
- #
41
- def warn(*args)
42
- first = args.shift
43
- if first.is_a? Class and first <= Warning
44
- warning = first
45
- message = args.shift
46
-
47
- elsif first.is_a? Warning
48
- warning = first.class
49
- message = first.message
50
-
51
- else
52
- warning = StandardWarning
53
- message = first.to_s
54
- end
55
-
56
- # If args is not empty, user passed an incompatible set of arguments.
57
- # Maybe somebody else is overriding warn as well and knows, what to do.
58
- # Better do nothing in this case. See #5
59
- return super unless args.empty?
60
-
61
- if warning.active?
62
- output = StructuredWarnings.warner.format(warning, message, caller(1))
63
- super(output) unless output.nil? or output.to_s.empty?
64
- end
65
- end
1
+ module StructuredWarnings::Kernel
2
+ def warn(*args)
3
+ Warning.warn(*args)
66
4
  end
67
5
  end
6
+
7
+ Object.class_eval { include StructuredWarnings::Kernel }