qualitysmith_extensions 0.0.20 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,244 @@
1
+ #--
2
+ # Author:: Tyler Rick
3
+ # Copyright:: Copyright (c) 2007 QualitySmith, Inc.
4
+ # License:: Ruby License
5
+ # Submit to Facets?::
6
+ # Developer notes::
7
+ #++
8
+
9
+
10
+ require 'rubygems'
11
+ gem 'colored'
12
+ require 'colored'
13
+ gem 'facets'
14
+ require 'facets/core/module/alias_method_chain'
15
+ gem 'qualitysmith_extensions', '>=0.0.22'
16
+ require 'qualitysmith_extensions/object/send_if'
17
+ require 'qualitysmith_extensions/string/each_char_with_index'
18
+ require 'qualitysmith_extensions/module/mattr_tester'
19
+ require 'qualitysmith_extensions/module/guard_method'
20
+ require 'qualitysmith_extensions/colored/toggleability'
21
+
22
+ class String
23
+ # For all of the next 3 methods, we will underline spaces so that they are actually visible on the screen.
24
+ # Newlines will be replaced with '\n'"\n".
25
+
26
+ # This is a (sub)string that is common to both expected and actual
27
+ def highlight_commonality
28
+ self.
29
+ make_control_characters_visible.
30
+ underline_spaces.
31
+ green.
32
+ send_unless(self == ' ', :bold) # spaces are not bold, '_'s (and everything else) are
33
+ end
34
+ # This is a (sub)string that is different between expected and actual
35
+ def highlight_difference
36
+ self.
37
+ make_control_characters_visible.
38
+ underline_spaces.
39
+ red.
40
+ send_unless(self == ' ', :bold) # spaces are not bold, '_'s (and everything else) are
41
+ end
42
+ # This is a (sub)string that exists only in *self*, not in the other string
43
+ def highlight_unique
44
+ self.
45
+ make_control_characters_visible.
46
+ underline_spaces.
47
+ yellow
48
+ end
49
+ # This is a (sub)string that doesn't exist in self, only in the *other* string
50
+ def highlight_absence
51
+ self.white.on_yellow.bold
52
+ end
53
+ def underline_spaces
54
+ #:todo: Make this optional? Might be useful if you are comparing things with lots of spaces and underscores and you want to be able to tell the difference between them...?
55
+ self.gsub(' ', ' '.underline)
56
+ end
57
+ def make_control_characters_visible
58
+ self.gsub(/\n/, '\n'+"\n"). # Show '\n' in addition to actually doing the line break
59
+ gsub(/\r/, '\r'). # Just escape it...
60
+ gsub(/\t/, '\t')
61
+ #:todo: Add other control characters?
62
+ end
63
+ end
64
+ module Test
65
+ module Unit
66
+ module Assertions
67
+ class AssertionMessage
68
+ @@inspect_strings = false
69
+ #mattr_tester :inspect_strings
70
+ mguard_method :inspect_strings!, :@@inspect_strings, :disable_inspect_strings!
71
+
72
+ # The problem with the original convert() is that it always called #inspect on strings... which is fine if you really
73
+ # want to see all those \n's and such. But not so great if you want to visually compare the strings. And if you have
74
+ # ANSI color codes in the strings, it will escape those so that you see the codes (\e[33m1) rather than the nice
75
+ # colored strings that you (sometimes) *want* to see...
76
+ #
77
+ def convert_with_option_to_not_use_inspect_for_strings(object)
78
+ if String === object
79
+ if self.class.inspect_strings?
80
+ # Use the original method, which used pp or inspect
81
+ convert_without_option_to_not_use_inspect_for_strings(object)
82
+ else
83
+ object
84
+ end
85
+ else
86
+ # We only care about strings. Everything else can just keep happening like it was before.
87
+ convert_without_option_to_not_use_inspect_for_strings(object)
88
+ end
89
+ end
90
+ alias_method_chain :convert, :option_to_not_use_inspect_for_strings
91
+ end
92
+
93
+ # Rather than showing the expected and the actual and asking the user to painstakingly compare the two and figure out the
94
+ # commonalities and differences himself, this method will highlight the differences for the user (in color), so that they
95
+ # can be spotted in less than an instant!
96
+ #
97
+ # Strings:
98
+ # Does a characterwise comparison between the two strings.
99
+ # Common characters are displayed in green, different characters in red, and characters that exist in one but not the
100
+ # other are displayed in yellow (a blank yellow background as placeholders for the string in which those characters
101
+ # are missing).
102
+ # Arrays:
103
+ # [:todo:]
104
+ # Hashes:
105
+ # [:todo:]
106
+ #
107
+ # Difference in method signature from assert_equal_without_difference_highlighting:
108
+ # * last argument is a hash (+options+) rather than message=nil, since I don't see the use in passing in a message if
109
+ # the default message can be made useful enough.
110
+ # * If you really want to pass in a message, use :message => 'my message'
111
+ #
112
+ # * If you want everything to be escaped (so you would see the color codes instead of the color itself), use :inspect_strings => true
113
+ #
114
+ # Spaces are displayed with an underline so that they are actually visible on the screen.
115
+ # * :
116
+ def assert_equal_with_difference_highlighting(expected, actual, options = {})
117
+ message = options.delete(:message) || nil
118
+ AssertionMessage.inspect_strings!(options.delete(:inspect_strings) || false)
119
+
120
+ if String===expected and String===actual and expected!=actual
121
+ expected_with_highlighting = ''
122
+ actual_with_highlighting = ''
123
+ full_message = nil
124
+ String.color_on! do
125
+ longest_string = [expected, actual].max {|a, b| a.length <=> b.length}
126
+ longest_string.each_char_with_index do |i, exp|
127
+ exp = expected[i] ? expected[i].chr : nil
128
+ act = actual[i] ? actual[i].chr : nil
129
+ if act.nil?
130
+ expected_with_highlighting << exp.highlight_unique
131
+ actual_with_highlighting << ' '.highlight_absence
132
+ elsif exp.nil?
133
+ expected_with_highlighting << ' '.highlight_absence
134
+ actual_with_highlighting << act.highlight_unique
135
+
136
+ elsif exp != act
137
+ expected_with_highlighting << exp.highlight_difference
138
+ actual_with_highlighting << act.highlight_difference
139
+ else
140
+ expected_with_highlighting << exp.highlight_commonality
141
+ actual_with_highlighting << exp.highlight_commonality
142
+ end
143
+
144
+ end
145
+ full_message = build_message(message, <<End, expected_with_highlighting, actual_with_highlighting)
146
+ #{(' '*50 + ' Expected: ' + ' '*50).blue.on_white }
147
+ ?
148
+ #{(' '*50 + ' But was: ' + ' '*50).yellow.on_red }
149
+ ?
150
+ End
151
+ end
152
+ assert_block(full_message) { expected == actual }
153
+ else
154
+ assert_equal_without_difference_highlighting(expected, actual, message)
155
+ end
156
+ end # def assert_equal_with_difference_highlighting
157
+ #alias_method :assert_equal_with_highlighting, :assert_equal_with_difference_highlighting
158
+ alias_method_chain :assert_equal, :difference_highlighting
159
+
160
+ end
161
+ end
162
+ end
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+ # _____ _
172
+ # |_ _|__ ___| |_
173
+ # | |/ _ \/ __| __|
174
+ # | | __/\__ \ |_
175
+ # |_|\___||___/\__|
176
+ #
177
+ =begin test
178
+ require 'test/unit'
179
+
180
+ class TheTest < Test::Unit::TestCase
181
+ def test1_single_character_difference
182
+ assert_equal <<End, <<End
183
+ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed feugiat mi. In sagittis, augue non eleifend sodales, arcu urna congue sapien, aliquet molestie pede urna sit amet dolor. Etiam diam. Vestibulum ornare, felis et porta faucibus, magna sapien vulputate arcu, vel facilisis lectus ipsum et ipsum.
184
+
185
+ Vivamus massa odio, lacinia eu, euismod vitae, lobortis eu, erat. Duis tincidunt, neque ac tincidunt convallis, nibh tellus sodales eros, ut tristique nunc purus in urna. Nullam semper. Fusce quis augue ut metus interdum congue. Duis id dolor eu mi pellentesque sagittis. Quisque imperdiet orci a odio.
186
+ End
187
+ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed feugiat mi. In sagittis, augue non eleifend sodales, arcu urna congue sapien, aliquet molestie pede urna sit amet dolor. Etiam diam. Vestibulum ornare, felis et porta faucibus, magna sapien vulputate arcu, vel facilisis lectus ipsum et ipsum.
188
+
189
+ Vivamus massa odio, lacinia eu, euismod vitae, lobortis eu, erat. Duis tincidunt, neque ac tincidunt convallis, nibh tellus sodales eros, ut tristique nunc purus in urna. Nullam semper. Fusce quis augue ut metus interdum congue. Duis id color eu mi pellentesque sagittis. Quisque imperdiet orci a odio.
190
+ End
191
+ end
192
+
193
+ def test2_difference_in_control_characters
194
+ assert_equal "Lorem ipsum dolor sit amet,\nconsectetuer adipiscing elit.\nSed feugiat mi.",
195
+ "Lorem ipsum_dolor sit amet, consectetuer\tadipiscing elit.\n\rSed feugiat mi."
196
+ end
197
+
198
+ def test3_expected_is_longer
199
+ assert_equal '1234567890', '123'
200
+ end
201
+
202
+ def test4_actual_is_longer
203
+ assert_equal '123', '1234567890'
204
+ end
205
+
206
+ # Use the ones above this line as screenshot material...
207
+
208
+ def test5_one_is_much_longer
209
+ assert_equal <<End, <<End
210
+ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed feugiat mi. In sagittis, augue non eleifend sodales, arcu urna congue sapien, aliquet molestie pede urna sit amet dolor. Etiam diam. Vestibulum ornare, felis et porta faucibus, magna sapien vulputate arcu, vel facilisis lectus ipsum et ipsum. Sed dictum, dolor suscipit malesuada pharetra, orci augue lobortis lectus, porta porta magna magna ut dui. Duis viverra enim sed felis. Mauris semper volutpat pede. Integer lectus lorem, lacinia in, iaculis ut, euismod non, nulla. Nunc non libero eget diam congue ornare. Nunc dictum tellus sed turpis. Sed venenatis, pede non ultricies pharetra, dolor felis malesuada nisl, id imperdiet lorem dui vel velit.
211
+ End
212
+ Lorem ipsum dolor sit amet
213
+ End
214
+ end
215
+
216
+ def test6_only_minor_single_character_differences_but_then_it_gets_out_of_sync
217
+ assert_equal <<End, <<End
218
+ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed feugiat mi. In sagittis, augue non eleifend sodales, arcu urna congue sapien.
219
+
220
+ Vivamus massa odio, lacinia eu, euismod vitae, lobortis eu, erat. Duis tincidunt, neque ac tincidunt convallis, nibh tellus sodales eros, ut tristique nunc purus in urna. Nullam semper. Fusce quis augue ut metus interdum congue. Duis id dolor eu mi pellentesque sagittis. Quisque imperdiet orci a odio.
221
+ End
222
+ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed feugiat mi. In sagittis, argue non eleifend sodales, arcu urna conque sapien.
223
+
224
+ Vivamus massa odio, lacinia eu, euismod vitae, lobortis eu, erat. Duis tincidunt, neque ac tincidunt convallis, nibh tellus sodales eros, ut tristique nunc purus in urna. Nullam semper. Fusce quis augue ut metus interdum congue. Duis id dolor eu mi pellentesque sagittis. Quisque imperdiet orci a odio.
225
+ End
226
+ end
227
+ def test7_underscores_versus_underlines
228
+ assert_equal <<End, <<End
229
+ If you look really closely, you'll see that underscores are a brighter color (bold), while spaces appear somewhaat faded, less visible:
230
+ ___[Underscores]___[Underscores] [Spaces] _ _ _ _ _ [Mix]
231
+ End
232
+ If you look really closely, you'll see that underscores are a brighter color (bold), while spaces appear somewhaat faded, less visible:
233
+ [Spaces] ___[Underscores] [Spaces] _ _ __ _ _[Mix]
234
+ End
235
+ end
236
+
237
+ def test12_expected_is_longer
238
+ assert_equal '1234567890', '123', :inspect_strings => true
239
+ end
240
+
241
+ end
242
+ =end
243
+
244
+
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: qualitysmith_extensions
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.20
7
- date: 2007-04-26 00:00:00 -07:00
6
+ version: 0.0.24
7
+ date: 2007-04-27 00:00:00 -07:00
8
8
  summary: A collection of reusable Ruby methods developed by QualitySmith.
9
9
  require_paths:
10
10
  - lib
@@ -37,9 +37,11 @@ files:
37
37
  - lib/qualitysmith_extensions/test/assert_exception.rb
38
38
  - lib/qualitysmith_extensions/test/assert_user_error.rb
39
39
  - lib/qualitysmith_extensions/test/assert_changed.rb
40
+ - lib/qualitysmith_extensions/test/difference_highlighting.rb
40
41
  - lib/qualitysmith_extensions/test/all.rb
41
42
  - lib/qualitysmith_extensions/test/assert_includes.rb
42
43
  - lib/qualitysmith_extensions/exception/inspect_with_backtrace.rb
44
+ - lib/qualitysmith_extensions/colored/toggleability.rb
43
45
  - lib/qualitysmith_extensions/file_test/binary_file.rb
44
46
  - lib/qualitysmith_extensions/kernel/require_all.rb
45
47
  - lib/qualitysmith_extensions/kernel/simulate_input.rb
@@ -51,6 +53,13 @@ files:
51
53
  - lib/qualitysmith_extensions/kernel/backtrace.rb
52
54
  - lib/qualitysmith_extensions/kernel/capture_output.rb
53
55
  - lib/qualitysmith_extensions/kernel/trap_chain.rb
56
+ - lib/qualitysmith_extensions/module.bak/guard_method.rb
57
+ - lib/qualitysmith_extensions/module.bak/alias_method.rb
58
+ - lib/qualitysmith_extensions/module.bak/includable_once.rb
59
+ - lib/qualitysmith_extensions/module.bak/mattr_tester.rb
60
+ - lib/qualitysmith_extensions/module.bak/create_setter.rb
61
+ - lib/qualitysmith_extensions/module.bak/attribute_accessors.rb
62
+ - lib/qualitysmith_extensions/module.bak/bool_attr_accessor.rb
54
63
  - lib/qualitysmith_extensions/dir/each_child.rb
55
64
  - lib/qualitysmith_extensions/object/singleton_send.rb
56
65
  - lib/qualitysmith_extensions/object/mcall.rb
@@ -73,10 +82,9 @@ files:
73
82
  - lib/qualitysmith_extensions/module/guard_method.rb
74
83
  - lib/qualitysmith_extensions/module/alias_method.rb
75
84
  - lib/qualitysmith_extensions/module/includable_once.rb
76
- - lib/qualitysmith_extensions/module/mattr_tester.rb
77
85
  - lib/qualitysmith_extensions/module/create_setter.rb
78
86
  - lib/qualitysmith_extensions/module/attribute_accessors.rb
79
- - lib/qualitysmith_extensions/module/attr_tester.rb
87
+ - lib/qualitysmith_extensions/module/bool_attr_accessor.rb
80
88
  - lib/qualitysmith_extensions/symbol/match.rb
81
89
  - lib/qualitysmith_extensions/enumerable/enum.rb
82
90
  - lib/qualitysmith_extensions/hash/to_query_string.rb
@@ -84,6 +92,7 @@ files:
84
92
  - lib/qualitysmith_extensions/hash/to_date.rb
85
93
  - lib/qualitysmith_extensions/array/expand_ranges.rb
86
94
  - lib/qualitysmith_extensions/array/shell_escape.rb
95
+ - lib/qualitysmith_extensions/array/classify.rb
87
96
  - lib/qualitysmith_extensions/array/to_query_string.rb
88
97
  - lib/qualitysmith_extensions/array/sequence.rb
89
98
  - lib/qualitysmith_extensions/array/to_a_recursive.rb
@@ -1,2 +0,0 @@
1
- require 'rubygems'
2
- require 'facets/core/module/attr_tester'
@@ -1,120 +0,0 @@
1
- #--
2
- # Author:: Tyler Rick
3
- # Copyright:: Copyright (c) 2007 QualitySmith, Inc.
4
- # License:: Ruby License
5
- # Submit to Facets?:: Yes
6
- # Developer notes::
7
- # * Based on /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/core/module/attr_tester.rb
8
- # * Hey Thomas, don't you think Module#attr_tester should only create the read-only a? method and have another method that creates the writer (like there how we have attr_reader, _writer, and _accessor?) ? "tester" does not imply "setter" in my mind...
9
- #++
10
-
11
- class Module
12
- # This creates two methods for each given variable name. One is used to test
13
- # the attribute and the other is used to set or toggle it.
14
- #
15
- # attr_tester :a
16
- #
17
- # is equivalent to
18
- #
19
- # def self.a?
20
- # @@a ? true : @@a
21
- # end
22
- #
23
- # def self.a!(switch=Exception)
24
- # if switch == Exception
25
- # @@a = !@@a
26
- # else
27
- # @@a = switch ? true : @@a
28
- # self
29
- # end
30
- # end
31
- #
32
- # Works for both classes and modules.
33
- #
34
- def mattr_tester(*args)
35
-
36
- make = {}
37
- args.each { |a|
38
- # Initialize it first so that we won't have any NameErrors.
39
- module_eval %{ @@#{a} = nil if !defined?(@@#{a}) }, __FILE__, __LINE__
40
-
41
- make["#{a}?".to_sym] = %{
42
- def self.#{a}?(true_value=true)
43
- @@#{a} ? true_value : @@#{a}
44
- end
45
- }
46
- make["#{a}!".to_sym] = %{
47
- def self.#{a}!(switch=Exception)
48
- if switch == Exception
49
- @@#{a} = !@@#{a}
50
- else
51
- @@#{a} = switch ? true : @@#{a}
52
- self
53
- end
54
- end
55
- }
56
- }
57
- module_eval make.values.join("\n"), __FILE__, __LINE__
58
-
59
- return make.keys
60
- end
61
- end
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
- # _____ _
70
- # |_ _|__ ___| |_
71
- # | |/ _ \/ __| __|
72
- # | | __/\__ \ |_
73
- # |_|\___||___/\__|
74
- #
75
- =begin test
76
- require 'test/unit'
77
- require 'rubygems'
78
- require 'qualitysmith_extensions/object/ignore_access'
79
-
80
-
81
- class TheTest < Test::Unit::TestCase
82
- class C
83
- mattr_tester :a
84
- end
85
- module M
86
- mattr_tester :a
87
- end
88
-
89
- def test_cattr_tester_exclamation
90
- C.access.class_variable_set(:@@a, false)
91
- assert_equal(false, C.a?) # otherwise would have been nil
92
- C.a!
93
- assert_equal(true, C.a?)
94
- C.a!
95
- assert_equal(false, C.a?)
96
- end
97
- def test_cattr_tester_return
98
- C.a!
99
- assert_equal(true, C.a?)
100
- C.a!("whatever")
101
- assert_equal(true, C.a?) # Still returns a boolean even though we set it to a string.
102
- end
103
-
104
- def test_mattr_tester_exclamation
105
- assert_equal(nil, M.a?)
106
- M.a!
107
- assert_equal(true, M.a?)
108
- M.a!
109
- assert_equal(false, M.a?)
110
- end
111
- def test_mattr_tester_return
112
- M.a!
113
- assert_equal(true, M.a?)
114
- M.a!("whatever")
115
- assert_equal(true, M.a?) # Still returns a boolean even though we set it to a string.
116
- end
117
-
118
- end
119
- =end
120
-