debug_me 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/debug_me.rb CHANGED
@@ -4,20 +4,25 @@ require_relative 'debug_me/version'
4
4
  module DebugMe
5
5
  def debug_me(options = {}, &block)
6
6
  default_options = {
7
- tag: 'DEBUG:', # A tag to prepend to each output line
7
+ tag: 'DEBUG:', # A tag to prepend to each output line
8
8
  time: true, # Include a time-stamp in front of the tag
9
- header: true, # Print a header string before printing the variables
9
+ header: true, # Print a header string before printing the variables
10
+ lvar: true, # Include local variables
10
11
  ivar: true, # Include instance variables in the output
11
12
  cvar: true, # Include class variables in the output
13
+ cconst: true, # Include class constants
12
14
  file: $stdout # The output file
13
15
  }
14
16
 
17
+
15
18
  if 'Hash' == options.class.to_s
16
19
  options = default_options.merge(options)
17
20
  else
18
21
  options = default_options.merge(tag: options)
19
22
  end
20
23
 
24
+ out_string = ''
25
+
21
26
  f = options[:file]
22
27
  s = ''
23
28
  s += "#{sprintf('%010.6f', Time.now.to_f)} " if options[:time]
@@ -25,29 +30,36 @@ module DebugMe
25
30
  wf = caller # where_from under 1.8.6 its a stack trace array under 1.8.7 is a string
26
31
  wf = wf[0] if 'Array' == wf.class.to_s
27
32
 
28
- f.puts "#{s} Source: #{wf}" if options[:header]
33
+ out_string = sprintf("%s Source: %s\n",s,wf) if options[:header]
29
34
 
30
35
  if block_given?
31
36
 
32
37
  block_value = [block.call].flatten.compact
33
38
 
34
39
  if block_value.empty?
35
- block_value = eval('local_variables', block.binding)
40
+ block_value = []
41
+ block_value += [eval('local_variables', block.binding)] if options[:lvar]
36
42
  block_value += [eval('instance_variables', block.binding)] if options[:ivar]
37
43
  block_value += [self.class.send('class_variables')] if options[:cvar]
44
+ block_value += [self.class.constants] if options[:cconst]
38
45
  block_value = block_value.flatten.compact
39
- else
40
- block_value.map!(&:to_s)
41
46
  end
42
47
 
48
+ block_value.map!(&:to_s)
49
+
43
50
  block_value.each do |v|
44
- ev = eval(v, block.binding)
45
- f.puts "#{s} #{v} -=> #{ev.pretty_inspect}"
51
+ ev = eval("defined?(#{v})",block.binding).nil? ? '<undefined>' : eval(v, block.binding)
52
+ out_string += sprintf( "%s %s -=> %s", s,v,ev.pretty_inspect)
46
53
  end
47
54
 
48
55
  end ## if block_given?
49
56
 
50
- f.flush
57
+ unless f.nil?
58
+ f.puts out_string
59
+ f.flush
60
+ end
61
+
62
+ return out_string
51
63
  end ## def debug_me( options={}, &block )
52
64
 
53
65
  # def log_me(msg, opts={})
@@ -1,3 +1,3 @@
1
1
  module DebugMe
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -0,0 +1,229 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/debug_me'
4
+ include DebugMe
5
+
6
+ require_relative './world_view'
7
+
8
+
9
+
10
+ require 'minitest/autorun'
11
+
12
+ describe DebugMe do
13
+
14
+ describe "source header included" do
15
+
16
+ before do
17
+ @line = __LINE__
18
+ @file = __FILE__
19
+ @out_string = debug_me(file:nil)
20
+ end
21
+
22
+ it "has the keywords DEBUG and Source" do
23
+ assert @out_string.include? "DEBUG"
24
+ assert @out_string.include? "Source"
25
+ end
26
+
27
+ it "includes the filename" do
28
+ assert @out_string.include? __FILE__
29
+ end
30
+
31
+ it "includes the line number" do
32
+ assert @out_string.include? ".rb:#{@line+2}:"
33
+ end
34
+
35
+ end # describe "source header included" do
36
+
37
+ describe "source header excluded" do
38
+
39
+ before do
40
+ @out_string = debug_me(file:nil,header:false)
41
+ end
42
+
43
+ it "is empty" do
44
+ assert @out_string.empty?
45
+ end
46
+
47
+ end # describe "source header excluded" do
48
+
49
+ describe "works with local variable" do
50
+
51
+ it "supports a single variable" do
52
+ a,b,c = 1,2,3
53
+ out_string = debug_me(file:nil,header:false){:a}
54
+ assert out_string.include? "a -=> #{a}"
55
+ out_string.split("\n").size.must_equal 1
56
+ end
57
+
58
+ it "supports multible variables" do
59
+ a,b,c = 1,2,3
60
+ out_string = debug_me(file:nil,header:false){[:a, :b, :c]}
61
+ assert out_string.include? "a -=> #{a}"
62
+ assert out_string.include? "b -=> #{b}"
63
+ assert out_string.include? "c -=> #{c}"
64
+ out_string.split("\n").size.must_equal 3
65
+ end
66
+
67
+ it "shows all local variables" do
68
+ a,b,c = 1,2,3
69
+ out_string = debug_me(
70
+ file:nil,
71
+ header:false,
72
+ lvar: true,
73
+ ivar: false,
74
+ cvar: false,
75
+ cconst: false
76
+ ){}
77
+ assert out_string.include? "a -=> #{a}"
78
+ assert out_string.include? "b -=> #{b}"
79
+ assert out_string.include? "c -=> #{c}"
80
+ assert out_string.include? "out_string -=> "
81
+
82
+ out_string.split("\n").size.must_equal 4
83
+ end
84
+
85
+ end # describe "works with local variable" do
86
+
87
+ describe "works with instance variable" do
88
+
89
+ it "supports a single variable" do
90
+ @a,@b,@c = 1,2,3
91
+ out_string = debug_me(file:nil,header:false){:@a}
92
+ assert out_string.include? "@a -=> #{@a}"
93
+ out_string.split("\n").size.must_equal 1
94
+ end
95
+
96
+ it "supports multible variables" do
97
+ @a,@b,@c = 1,2,3
98
+ out_string = debug_me(file:nil,header:false){[:@a, :@b, :@c]}
99
+ assert out_string.include? "@a -=> #{@a}"
100
+ assert out_string.include? "@b -=> #{@b}"
101
+ assert out_string.include? "@c -=> #{@c}"
102
+ out_string.split("\n").size.must_equal 3
103
+ end
104
+
105
+ it "shows all instance variables" do
106
+ @a,@b,@c = 1,2,3
107
+ out_string = debug_me(
108
+ file:nil,
109
+ header:false,
110
+ lvar: false,
111
+ ivar: true,
112
+ cvar: false,
113
+ cconst: false
114
+ ){}
115
+ assert out_string.include? "@a -=> #{@a}"
116
+ assert out_string.include? "@b -=> #{@b}"
117
+ assert out_string.include? "@c -=> #{@c}"
118
+
119
+ out_string.include?("out_string -=> ").must_equal false
120
+ out_string.split("\n").size.must_equal instance_variables.size
121
+ end
122
+
123
+ end # describe "works with instance variable" do
124
+
125
+
126
+ describe "works with class variable" do
127
+
128
+ it "supports a single variable" do
129
+ @@a,@@b,@@c = 1,2,3
130
+ out_string = debug_me(file:nil,header:false){:@@a}
131
+ assert out_string.include? "@@a -=> #{@@a}"
132
+ out_string.split("\n").size.must_equal 1
133
+ end
134
+
135
+ it "supports multible variables" do
136
+ @@a,@@b,@@c = 1,2,3
137
+ out_string = debug_me(file:nil,header:false){[:@@a, :@@b, :@@c]}
138
+ assert out_string.include? "@@a -=> #{@@a}"
139
+ assert out_string.include? "@@b -=> #{@@b}"
140
+ assert out_string.include? "@@c -=> #{@@c}"
141
+ out_string.split("\n").size.must_equal 3
142
+ end
143
+
144
+ it "shows all class variables" do
145
+ @@a,@@b,@@c = 1,2,3
146
+ @d = 4
147
+ out_string = debug_me(
148
+ file:nil,
149
+ header:false,
150
+ lvar: false,
151
+ ivar: false,
152
+ cvar: true,
153
+ cconst: false
154
+ ){}
155
+
156
+ assert out_string.include? "@@a -=> #{@@a}"
157
+ assert out_string.include? "@@b -=> #{@@b}"
158
+ assert out_string.include? "@@c -=> #{@@c}"
159
+
160
+ out_string.include?("out_string -=> ").must_equal false
161
+ out_string.include?("@d -=> ").must_equal false
162
+
163
+ out_string.split("\n").size.must_equal self.class.class_variables.size
164
+ end
165
+
166
+ end # describe "works with class variable" do
167
+
168
+
169
+ describe "works with CONSTANTS" do
170
+
171
+ it "supports a single CONSTANT" do
172
+ A,B,C = 1,2,3
173
+ out_string = debug_me(file:nil,header:false){:A}
174
+ assert out_string.include? "A -=> #{A}"
175
+ out_string.split("\n").size.must_equal 1
176
+ end
177
+
178
+ it "supports multible CONSTANTS" do
179
+ A,B,C = 1,2,3
180
+ out_string = debug_me(file:nil,header:false){[:A, :B, :C]}
181
+ assert out_string.include? "A -=> #{A}"
182
+ assert out_string.include? "B -=> #{B}"
183
+ assert out_string.include? "C -=> #{C}"
184
+ out_string.split("\n").size.must_equal 3
185
+ end
186
+
187
+ end # describe "works with CONSTANTS" do
188
+
189
+
190
+ describe "works with class CONSTANTS" do
191
+
192
+ before do
193
+ @my_world_view = WorldView.new
194
+ end
195
+
196
+ it "supports a single class CONSTANT" do
197
+ out_string = debug_me(file:nil,header:false){'WorldView::A'}
198
+ assert out_string.include? "WorldView::A -=> #{WorldView::A}"
199
+ out_string.split("\n").size.must_equal 1
200
+ end
201
+
202
+ it "supports multible class CONSTANTS" do
203
+ out_string = debug_me(file:nil,header:false){[
204
+ 'WorldView::A', 'WorldView::B', 'WorldView::C']}
205
+ assert out_string.include? "WorldView::A -=> #{WorldView::A}"
206
+ assert out_string.include? "WorldView::B -=> #{WorldView::B}"
207
+ assert out_string.include? "WorldView::C -=> #{WorldView::C}"
208
+ out_string.split("\n").size.must_equal 3
209
+ end
210
+
211
+ it "shows all class CONSTANTS" do
212
+ out_string = @my_world_view.my_constants
213
+
214
+ assert out_string.include? "A -=> #{WorldView::A}"
215
+ assert out_string.include? "B -=> #{WorldView::B}"
216
+ assert out_string.include? "C -=> #{WorldView::C}"
217
+ assert out_string.include? 'MY_CONSTANT -=> "Jesus"'
218
+
219
+ out_string.include?("out_string -=> ").must_equal false
220
+ out_string.include?("@d -=> ").must_equal false
221
+
222
+ out_string.split("\n").size.must_equal WorldView.constants.size
223
+ end
224
+
225
+ end # describe "works with class CONSTANTS" do
226
+
227
+
228
+
229
+ end # describe DebugMe do
@@ -0,0 +1,31 @@
1
+ class WorldView
2
+ MY_CONSTANT = 'Jesus'
3
+ A = 1
4
+ B = 2
5
+ C = 3
6
+ def initialize
7
+ @a,@b,@c = 1,2,3
8
+ @@d, @@e, @@f = 4,5,6
9
+ end
10
+ def everything
11
+ debug_me(
12
+ file:nil,
13
+ header:true,
14
+ lvar: true,
15
+ ivar: true,
16
+ cvar: true,
17
+ cconst: true
18
+ ){}
19
+ end
20
+ def my_constants
21
+ debug_me(
22
+ file:nil,
23
+ header:false,
24
+ lvar: false,
25
+ ivar: false,
26
+ cvar: false,
27
+ cconst: true
28
+ ){}
29
+
30
+ end
31
+ end # class WorldView
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debug_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-18 00:00:00.000000000 Z
12
+ date: 2015-01-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -66,6 +66,8 @@ files:
66
66
  - lib/debug_me.rb
67
67
  - lib/debug_me/version.rb
68
68
  - rubygems.yml.asc
69
+ - tests/debug_me_test.rb
70
+ - tests/world_view.rb
69
71
  homepage: http://github.com/MadBomber/debug_me
70
72
  licenses:
71
73
  - You want it, its yours