blab 0.0.1.pre.alpha → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0f2fa8c029f19b494bc57a4fddf76cb8c5b5265e208c7630e1d56cdaf52cb1d
4
- data.tar.gz: 16b98eb6a98c16491e650169e4b0bfeea0c6b43b31cfdff9741a2a97443c0c33
3
+ metadata.gz: 91cbcb0ecff4366f95c0039e94952f5e364d538ecc588060bf33c40262dd4ad4
4
+ data.tar.gz: e47c8a122f138859de18c66c095d62caf326a613ac1c4bcfd3c5fc5e2358ec8e
5
5
  SHA512:
6
- metadata.gz: 5b0ce5055f845ad8eeb3cc1daf1c93532cb927e471830413baba7b2b75565d0b91cd6bcdf0d340c352a3e8c2a6fe95bac3e531d74c6ac61a80c462953e1590c5
7
- data.tar.gz: da14317d5e95869fbadd1c57d809a8878341dcd6dd6c9c65216c2f8c0d3757cba593b8681ef284f333f85d46aa7bb72c57be440284271f8996f515bd0aa4a334
6
+ metadata.gz: 9af0cfe4d8f881ba10d4d7297a6fe280d871bdedae5302e27e6b824db9ccd460d264d77a9c3c8d23ca4f3aefb97e8d31ace9b85cfbc21bb2b2b5c7ac3f207925
7
+ data.tar.gz: 308c1b8edee335be6cd588c8aa59f2266a18a529db07c188a6b89ab1f707d288d50294762c011f6d714aa75077a1fd48f26d2b97cc679eaec4e8bca1d8bf9c73
data/README.md CHANGED
@@ -3,7 +3,8 @@
3
3
 
4
4
  A debugging tool.
5
5
 
6
- The gem allows to trace local variables and memory usage for a specific Ruby code. It's rather experemental and is intended for use in a development environment only. \
6
+ The gem allows to trace local variables and memory usage for Ruby code (MRI 2.6+). \
7
+ It's extremely experemental and is intended for use in a development environment only. \
7
8
  Blab is inspired by [PySnooper](https://github.com/cool-RR/PySnooper).
8
9
 
9
10
 
@@ -23,14 +24,12 @@ bundle install
23
24
 
24
25
  ## Usage
25
26
 
26
- Include `Blab` module and the destionation class and use the `blab` decorator in front of a method defenition.
27
+ Use the `blab` decorator in front of a method defenition.
27
28
 
28
29
  ```ruby
29
30
  require "blab"
30
31
 
31
32
  class Test
32
- include Blab
33
-
34
33
  blab def longest_rep(str)
35
34
  max = str.chars.chunk(&:itself).map(&:last).max_by(&:size)
36
35
  max ? [max[0], max.size] : ["", 0]
@@ -59,8 +58,6 @@ The gem allows to wrap only a piece of code in a block:
59
58
 
60
59
  ```ruby
61
60
  class Test
62
- include Blab
63
-
64
61
  def shuffle(arr)
65
62
  for n in 0...arr.size
66
63
  targ = n + rand(arr.size - n)
@@ -151,6 +148,11 @@ Blab::Config.output_order = output_order
151
148
  ```
152
149
  By default it doesn't show current class name and method name. You can adjust the width, change the order, skip/add the desired output info.
153
150
 
151
+ ## Contribution
152
+
153
+ Fork & Pull Request. \
154
+ Run the tests via `rake`.
155
+
154
156
  ## License
155
157
 
156
158
  MIT
@@ -1,18 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Blab
4
- module Formatter
5
- extend self
6
-
4
+ class Formatter
7
5
  ELLIPSIS = "..."
8
- MAX_LENGTH = 100
6
+ DEFAULT_MAX_LENGTH = 100
7
+
8
+ attr_reader :max_length
9
+
10
+ def initialize(max_length = DEFAULT_MAX_LENGTH)
11
+ @max_length = max_length
12
+ end
9
13
 
10
14
  def format(object)
11
15
  formatted = prepare_for_inspection(object).inspect
12
- return formatted if formatted.length < MAX_LENGTH
16
+ return formatted if formatted.length < max_length
13
17
 
14
- beginning = truncate(formatted, 0, MAX_LENGTH / 2)
15
- ending = truncate(formatted, -MAX_LENGTH / 2, -1)
18
+ beginning = truncate(formatted, 0, max_length / 2)
19
+ ending = truncate(formatted, -max_length / 2, -1)
16
20
  "#{beginning}#{ELLIPSIS}#{ending}"
17
21
  end
18
22
 
data/lib/blab/printer.rb CHANGED
@@ -1,83 +1,85 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Printer
4
- DEFAULT_CLASS_NAME_WIDTH = 5
5
- DEFAULT_CODE_LINES_WIDTH = 120
6
- DEFAULT_EVENT_WIDTH = 6
7
- DEFAULT_FILE_LINES_WIDTH = 60
8
- DEFAULT_RU_MAXSS_WIDTH = 50
9
- DEFAULT_METHOD_NAME_WIDTH = 10
10
- DEFAULT_TIME_WIDTH = 12
3
+ module Blab
4
+ class Printer
5
+ DEFAULT_CLASS_NAME_WIDTH = 5
6
+ DEFAULT_CODE_LINES_WIDTH = 120
7
+ DEFAULT_EVENT_WIDTH = 6
8
+ DEFAULT_FILE_LINES_WIDTH = 60
9
+ DEFAULT_RU_MAXSS_WIDTH = 50
10
+ DEFAULT_METHOD_NAME_WIDTH = 10
11
+ DEFAULT_TIME_WIDTH = 12
11
12
 
12
- PRINT_FIELDS = [
13
- :class_name,
14
- :event,
15
- :method_name,
16
- :time,
17
- :ru_maxss
18
- ].freeze
13
+ PRINT_FIELDS = [
14
+ :class_name,
15
+ :event,
16
+ :method_name,
17
+ :time,
18
+ :ru_maxss
19
+ ].freeze
19
20
 
20
- attr_reader :config, :logger
21
+ attr_reader :config, :logger
21
22
 
22
- def initialize(config, logger)
23
- @config = config
24
- @logger = logger
25
- end
26
-
27
- def print(options = {})
28
- strings = config.map do |(type, width)|
29
- send(type, options.merge(width: width))
23
+ def initialize(config, logger)
24
+ @config = config
25
+ @logger = logger
30
26
  end
31
27
 
32
- config_length = config.length
33
- final = strings.map { |e| e.first.length }.max.times.map do |i|
34
- config_length.times.map do |j|
35
- str = strings[j][0][i] || ""
36
- # TODO: do not ljust the last element
37
- config_length == (j + 1) ? str : str.ljust(strings[j][1])
38
- end.join(" ")
28
+ def print(options = {})
29
+ strings = config.map do |(type, width)|
30
+ send(type, options.merge(width: width))
31
+ end
32
+
33
+ config_length = config.length
34
+ final = strings.map { |e| e.first.length }.max.times.map do |i|
35
+ config_length.times.map do |j|
36
+ str = strings[j][0][i] || ""
37
+ # TODO: do not ljust the last element
38
+ config_length == (j + 1) ? str : str.ljust(strings[j][1])
39
+ end.join(" ")
40
+ end
41
+ logger.info(final.join("\n"))
39
42
  end
40
- logger.info(final.join("\n"))
41
- end
42
43
 
43
- def file_lines(options = {})
44
- file = options[:file]
45
- line = options[:line]
46
- width = options[:width] || DEFAULT_FILE_LINES_WIDTH
47
- ["#{file}:#{line}".scan(/.{#{width}}|.+/), width]
48
- end
44
+ def file_lines(options = {})
45
+ file = options[:file]
46
+ line = options[:line]
47
+ width = options[:width] || DEFAULT_FILE_LINES_WIDTH
48
+ ["#{file}:#{line}".scan(/.{#{width}}|.+/), width]
49
+ end
49
50
 
50
- def code_lines(options= {})
51
- file = options[:file]
52
- line = options[:line]
53
- width = options[:width] || DEFAULT_CODE_LINES_WIDTH
54
- [source_line(file, line).scan(/.{#{width}}|.+/), width]
55
- end
51
+ def code_lines(options= {})
52
+ file = options[:file]
53
+ line = options[:line]
54
+ width = options[:width] || DEFAULT_CODE_LINES_WIDTH
55
+ [source_line(file, line).scan(/.{#{width}}|.+/), width]
56
+ end
56
57
 
57
- PRINT_FIELDS.each do |name|
58
- define_method(name) do |options = {}|
59
- val = options[name]
60
- width = options[:width] || const_get("DEFAULT_#{name.upcase}_WIDTH")
61
- [val.scan(/.{#{width}}|.+/), width]
58
+ PRINT_FIELDS.each do |name|
59
+ define_method(name) do |options = {}|
60
+ val = options[name]
61
+ width = options[:width] || self.class.const_get("DEFAULT_#{name.upcase}_WIDTH")
62
+ [val.scan(/.{#{width}}|.+/), width]
63
+ end
62
64
  end
63
- end
64
65
 
65
- def reset_files
66
- @files_map && @files_map.keys.each { |key| @files_map.delete(key) }
67
- end
66
+ def reset_files
67
+ @files_map && @files_map.keys.each { |key| @files_map.delete(key) }
68
+ end
68
69
 
69
- def files_map
70
- @files_map ||= Hash.new do |h, f|
71
- h[f] = File.readlines(f)
70
+ def files_map
71
+ @files_map ||= Hash.new do |h, f|
72
+ h[f] = File.readlines(f)
73
+ end
72
74
  end
73
- end
74
75
 
75
- # TODO: show all relevant file-lines
76
- def source_line(file, line)
77
- begin
78
- files_map[file][line - 1]
79
- rescue
80
- "source is unavailable"
76
+ # TODO: show all relevant file-lines
77
+ def source_line(file, line)
78
+ begin
79
+ files_map[file][line - 1]
80
+ rescue
81
+ "source is unavailable"
82
+ end
81
83
  end
82
84
  end
83
85
  end
data/lib/blab/tracer.rb CHANGED
@@ -58,7 +58,11 @@ module Blab
58
58
  end
59
59
 
60
60
  def formatted_output(key, val)
61
- logger.info("Var......... #{key}=#{Blab::Formatter.format(val)}")
61
+ logger.info("Var......... #{key}=#{formatter.format(val)}")
62
+ end
63
+
64
+ def formatter
65
+ @formatter ||= Blab::Formatter.new
62
66
  end
63
67
 
64
68
  def defined_vars
data/lib/blab/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Blab
4
- VERSION = "0.0.1-alpha"
4
+ VERSION = "0.0.1"
5
5
  end
data/lib/blab.rb CHANGED
@@ -5,11 +5,12 @@ require_relative "blab/config"
5
5
  require_relative "blab/formatter"
6
6
  require_relative "blab/printer"
7
7
  require_relative "blab/tracer"
8
+ require_relative "blab/version"
8
9
 
9
10
  module Blab
10
11
  def self.included(base)
11
12
  base.define_singleton_method(:blab) do |name|
12
- old_m = base.instance_method(name)
13
+ old_m = self.instance_method(name)
13
14
 
14
15
  base.send(:define_method, name) do |*args|
15
16
  begin
@@ -33,3 +34,5 @@ module Blab
33
34
  end
34
35
  end
35
36
  end
37
+
38
+ BasicObject.include(Blab)
data/test/support/test.rb CHANGED
@@ -3,8 +3,6 @@ require_relative "../../lib/blab"
3
3
  require "set"
4
4
 
5
5
  class Y
6
- include Blab
7
-
8
6
  blab def x(name, &block)
9
7
  a = 15
10
8
  b = 30
@@ -40,8 +38,6 @@ end
40
38
  # Blab::Config.original_scope_only = true
41
39
 
42
40
  class Test
43
- include Blab
44
-
45
41
  def shuffle(arr)
46
42
  for n in 0...arr.size
47
43
  targ = n + rand(arr.size - n)
@@ -57,7 +53,6 @@ class Test
57
53
  b.each { |x| shuffle(a); a.each { |y| print y, " ", x, ".\n" } }
58
54
  end
59
55
  end
60
- Blab::Config.log_output = "blab.log"
61
56
 
62
57
  Test.new.pairs(["Bored", "Curious"], ["cat", "frog"])
63
58
 
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest/autorun"
4
+ require "blab"
5
+
6
+ class TestFormatter < Minitest::Test
7
+ def setup
8
+ @formatter = Blab::Formatter.new(15)
9
+ end
10
+
11
+ def test_array_formet
12
+ arr1 = Array.new(3, 5)
13
+ arr2 = Array.new(100, 10)
14
+
15
+ assert_equal @formatter.format(arr1), "[5, 5, 5]"
16
+ assert_equal @formatter.format(arr2), "[10, 10,... 10, 10]"
17
+ end
18
+
19
+ def test_string_format
20
+ str1 = "test"
21
+ str2 = str1 * 50
22
+
23
+ assert_equal @formatter.format(str1), "\"test\""
24
+ assert_equal @formatter.format(str2), "\"testtes...esttest\""
25
+ end
26
+
27
+ def test_hash_format
28
+ hsh1 = { a: :b }
29
+ hsh2 = { aaa: :bbbbb, dddd: :ccccccccccc, e: :f }
30
+
31
+ assert_equal @formatter.format(hsh1), "{:a=>:b}"
32
+ assert_equal @formatter.format(hsh2), "{:aaa=>:... :e=>:f}"
33
+ end
34
+
35
+ def test_struct_format
36
+ person = Struct.new(:name, :age).new("Hanna", 30)
37
+
38
+ assert_equal @formatter.format(person), "#<struct... age=30>"
39
+ end
40
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest/autorun"
4
+ require "blab"
5
+
6
+ class TestPrinter < Minitest::Test
7
+ def setup
8
+ @logger = Blab::Config.logger
9
+ @config = [
10
+ [:time, 12],
11
+ [:event, 6],
12
+ [:file_lines, 50],
13
+ [:class_name, 12],
14
+ [:method_name, 12],
15
+ [:ru_maxss, 12],
16
+ [:code_lines, 120]
17
+ ]
18
+ @printer = Blab::Printer.new(@config, @logger)
19
+ end
20
+
21
+ def test_file_lines
22
+ options = {
23
+ file: "/test/file.rb",
24
+ line: 10
25
+ }
26
+ expected = [["/test/file.rb:10"], Blab::Printer::DEFAULT_FILE_LINES_WIDTH]
27
+ assert_equal @printer.file_lines(options), expected
28
+ end
29
+
30
+ def test_extra_file_lines
31
+ options = {
32
+ file: "/some/pretty/long/path/to/test/file.rb",
33
+ line: 10,
34
+ width: 5
35
+ }
36
+ expected = [
37
+ ["/some", "/pret", "ty/lo", "ng/pa", "th/to", "/test", "/file", ".rb:1", "0"],
38
+ options[:width]
39
+ ]
40
+ assert_equal @printer.file_lines(options), expected
41
+ end
42
+
43
+ def test_method_name
44
+ options = {
45
+ method_name: "secret",
46
+ width: 15
47
+ }
48
+ expected = [[options[:method_name]], options[:width]]
49
+ assert_equal @printer.method_name(options), expected
50
+ end
51
+
52
+ def test_event
53
+ options = { event: "line" }
54
+ expected = [[options[:event]], Blab::Printer::DEFAULT_EVENT_WIDTH]
55
+ assert_equal @printer.event(options), expected
56
+ end
57
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre.alpha
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yulia Oletskaya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-12 00:00:00.000000000 Z
11
+ date: 2019-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: debase-ruby_core_source
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.11'
27
41
  description: A debugging tool
28
42
  email: yulia.oletskaya@gmail.com
29
43
  executables: []
@@ -47,6 +61,8 @@ files:
47
61
  - lib/blab/tracer.rb
48
62
  - lib/blab/version.rb
49
63
  - test/support/test.rb
64
+ - test/test_formatter.rb
65
+ - test/test_printer.rb
50
66
  homepage: http://rubygems.org/gems/blab
51
67
  licenses:
52
68
  - MIT
@@ -62,13 +78,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
78
  version: '0'
63
79
  required_rubygems_version: !ruby/object:Gem::Requirement
64
80
  requirements:
65
- - - ">"
81
+ - - ">="
66
82
  - !ruby/object:Gem::Version
67
- version: 1.3.1
83
+ version: '0'
68
84
  requirements: []
69
85
  rubygems_version: 3.0.1
70
86
  signing_key:
71
87
  specification_version: 4
72
88
  summary: Blab
73
89
  test_files:
90
+ - test/test_formatter.rb
91
+ - test/test_printer.rb
74
92
  - test/support/test.rb