debug_log 0.0.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Peter Marklund
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Run all tests'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
data/lib/debug_log.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'active_support'
2
+
3
+ module Kernel
4
+ alias_method :debug, :binding
5
+ end
6
+
7
+ class Binding
8
+ def log(comment, *expressions)
9
+ DebugLog.log(self, comment, *expressions)
10
+ end
11
+ end
12
+
13
+ module DebugLog
14
+ mattr_accessor :logger
15
+ self.logger = nil
16
+
17
+ mattr_accessor :stdout
18
+ self.stdout = true
19
+
20
+ mattr_accessor :enabled
21
+ self.enabled = true
22
+
23
+ def self.prefix
24
+ name
25
+ end
26
+
27
+ def self.separator
28
+ " | "
29
+ end
30
+
31
+ def self.log(binding, comment, *expressions)
32
+ return unless enabled
33
+ message = [prefix, comment, expression_string(binding, *expressions), calling_method].join(separator)
34
+ puts(message) if stdout
35
+ logger.info(message) if logger
36
+ end
37
+
38
+ def self.expression_string(binding, *expressions)
39
+ expressions.map { |expression| format_expression(binding, expression) }.join(", ")
40
+ end
41
+
42
+ def self.calling_method
43
+ Kernel.caller(3).first
44
+ end
45
+
46
+ def self.format_value(value)
47
+ value.inspect
48
+ end
49
+
50
+ def self.format_expression(binding, expression)
51
+ value = eval(expression, binding)
52
+ %Q{#{expression}="#{format_value(value)}" (#{value.class.name})}
53
+ end
54
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CoreExtensionsTest < Test::Unit::TestCase
4
+ context "Kernel.debug" do
5
+ it "should return a Binding object" do
6
+ assert debug.is_a?(Binding)
7
+ end
8
+
9
+ it "should allow evaluation of local variables in the binding" do
10
+ assert_equal 5, eval("my_variable", debug_with_my_variable)
11
+ end
12
+ end
13
+
14
+ context "Binding" do
15
+ it "should have a log method" do
16
+ args = ["a debug message", 'var1', 'var2']
17
+ the_binding = binding
18
+ DebugLog.expects(:log).with(the_binding, *args)
19
+ the_binding.log(*args)
20
+ end
21
+ end
22
+
23
+ def debug_with_my_variable
24
+ my_variable = 5
25
+ debug
26
+ end
27
+ end
@@ -0,0 +1,146 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class DebugLogTest < Test::Unit::TestCase
4
+ context "log" do
5
+ before do
6
+ @comment = "a debug printout"
7
+ @expression_string = "expression string"
8
+ @the_binding = binding
9
+ @calling_method = "calling method"
10
+ @expected_message = [DebugLog.prefix, @comment, @expression_string, @calling_method].join(DebugLog.separator)
11
+ DebugLog.stdout = true
12
+ DebugLog.enabled = true
13
+ end
14
+
15
+ it "should output a message and a number of variables to stdout" do
16
+ DebugLog.logger = nil
17
+
18
+ log_expectations
19
+ DebugLog.expects(:puts).with(@expected_message)
20
+
21
+ invoke_log
22
+ end
23
+
24
+ it "does not output to stdout if DebugLog.stdout is false" do
25
+ DebugLog.stdout = false
26
+
27
+ log_expectations
28
+ DebugLog.expects(:puts).never
29
+
30
+ invoke_log
31
+ end
32
+
33
+ it "invokes info on the logger object if there is one" do
34
+ logger = Logger.new($stdout)
35
+ DebugLog.logger = logger
36
+
37
+ log_expectations
38
+ DebugLog.expects(:puts).with(@expected_message)
39
+ logger.expects(:info).with(@expected_message)
40
+
41
+ invoke_log
42
+ end
43
+
44
+ it "does nothing if DebugLog.enabled = false" do
45
+ DebugLog.enabled = false
46
+
47
+ DebugLog.expects(:puts).never
48
+
49
+ invoke_log
50
+ end
51
+
52
+ def log_expectations
53
+ DebugLog.expects(:calling_method).returns(@calling_method)
54
+ DebugLog.expects(:expression_string).with(@the_binding, 'my_var', 'my_var[0]').returns(@expression_string)
55
+ end
56
+
57
+ def invoke_log
58
+ DebugLog.log(@the_binding, @comment, 'my_var', 'my_var[0]')
59
+ end
60
+ end
61
+
62
+ context "logger" do
63
+ it "defaults to nil but can be initialized to a logger object" do
64
+ assert_equal nil, DebugLog.logger
65
+ logger = Logger.new($stdout)
66
+ DebugLog.logger = logger
67
+ assert_equal logger, DebugLog.logger
68
+ end
69
+ end
70
+
71
+ context "stdout" do
72
+ it "is true by default but can be set to false" do
73
+ assert_equal true, DebugLog.stdout
74
+ DebugLog.stdout = false
75
+ assert_equal false, DebugLog.stdout
76
+ end
77
+ end
78
+
79
+ context "enabled" do
80
+ it "is true by default but can be set to false" do
81
+ assert_equal true, DebugLog.enabled
82
+ DebugLog.enabled = false
83
+ assert_equal false, DebugLog.enabled
84
+ end
85
+ end
86
+
87
+ context "calling_method" do
88
+ it "should get the calling method from the Kernel.caller stack, three steps up" do
89
+ assert_match /method_that_invokes_debug_log/, method_that_invokes_debug_log
90
+ end
91
+
92
+ def method_that_invokes_debug_log
93
+ invoke_binding_log
94
+ end
95
+
96
+ def invoke_binding_log
97
+ invoke_debug_log
98
+ end
99
+
100
+ def invoke_debug_log
101
+ DebugLog.calling_method
102
+ end
103
+ end
104
+
105
+ context "format_value" do
106
+ it "invokes inspect by default" do
107
+ the_value = [:foo, :bar]
108
+ assert_equal the_value.inspect, DebugLog.format_value(the_value)
109
+ end
110
+ end
111
+
112
+ context "format_expression" do
113
+ it "evaluates an expression and also prints the class" do
114
+ the_value = [:foo, :bar]
115
+ formated_value = "formated value"
116
+ DebugLog.expects(:format_value).with(the_value).returns(formated_value)
117
+ expected_format = %Q{the_value="#{formated_value}" (#{the_value.class.name})}
118
+ assert_equal expected_format, DebugLog.format_expression(binding, 'the_value')
119
+ end
120
+ end
121
+
122
+ context "separator" do
123
+ it "returns pipe by default" do
124
+ assert_equal " | ", DebugLog.separator
125
+ end
126
+ end
127
+
128
+ context "expression_string" do
129
+ it "creates a comma separated key-value string from a number of expressions and a binding object" do
130
+ my_var = "Peter"
131
+ the_binding = binding
132
+ expressions = ['my_var', 'my_var[0,1]']
133
+ formats = ['my_var formated', 'myvar[0,1] formated']
134
+ DebugLog.expects(:format_expression).with(the_binding, 'my_var').returns(formats[0])
135
+ DebugLog.expects(:format_expression).with(the_binding, 'my_var[0,1]').returns(formats[1])
136
+ expected_string = formats.join(", ")
137
+ assert_equal expected_string, DebugLog.expression_string(the_binding, 'my_var', 'my_var[0,1]')
138
+ end
139
+ end
140
+
141
+ context "prefix" do
142
+ it "returns class name by default" do
143
+ assert_equal "DebugLog", DebugLog.prefix
144
+ end
145
+ end
146
+ end
data/test/manual.rb ADDED
@@ -0,0 +1,10 @@
1
+ # Manual sanity check test
2
+
3
+ require 'rubygems'
4
+ require File.join(File.dirname(__FILE__), "..", "lib", "debug_log")
5
+
6
+ def hello
7
+ foo = "fOOOOOOO"
8
+ debug.log("pm debug hello there", 'foo')
9
+ end
10
+ hello
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ gem 'jeremymcanally-context', '0.5.5'
3
+ gem 'mocha', '0.9.8'
4
+ require 'context'
5
+ require 'mocha'
6
+ require File.dirname(__FILE__) + '/../lib/debug_log'
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: debug_log
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Peter Marklund
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-17 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 2
32
+ - 3
33
+ - 8
34
+ version: 2.3.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: " Allows you to easily and readably printout variable values (or any other Ruby expressions) to stdout\n and/or a log file of your choice for debugging purposes. The approach to accessing\n the binding object was inspired by the dp gem by Niclas Nilsson.\n"
38
+ email: peter@marklunds.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - MIT-LICENSE
47
+ - Rakefile
48
+ - lib/debug_log.rb
49
+ - test/core_extensions_test.rb
50
+ - test/debug_log_test.rb
51
+ - test/manual.rb
52
+ - test/test_helper.rb
53
+ has_rdoc: true
54
+ homepage: http://marklunds.com
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 57
68
+ segments:
69
+ - 1
70
+ - 8
71
+ - 7
72
+ version: 1.8.7
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 23
79
+ segments:
80
+ - 1
81
+ - 3
82
+ - 6
83
+ version: 1.3.6
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A more powerful way to do debug printouts than puts
91
+ test_files:
92
+ - test/core_extensions_test.rb
93
+ - test/debug_log_test.rb
94
+ - test/manual.rb
95
+ - test/test_helper.rb