Skip to content

Commit

Permalink
Merge pull request #1 from mkevinosullivan/code-cleaning
Browse files Browse the repository at this point in the history
Code cleaning with rubocop
  • Loading branch information
mkevinosullivan authored Nov 25, 2019
2 parents c5c266a + dacd633 commit 029796d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
/spec/reports/
/tmp/
/.byebug_history
/dev.yml
/Gemfile.lock
/.rubocop*
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "paragraphify"
Expand Down
7 changes: 4 additions & 3 deletions lib/paragraphify.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true
require "paragraphify/version"

module Paragraphify
class Error < StandardError; end

class Paragraph
attr_accessor :linebreak, :leading_indent, :hanging_indent

def initialize(linebreak: 80, leading_indent: 0, hanging_indent: 0)
@linebreak = linebreak
@leading_indent = leading_indent
Expand All @@ -20,8 +21,8 @@ def paragraphify(string:)
# DEBUG byebug if "test_paragraphify_len50_lb80_li4_hi0" == caller_locations.first.label
while input_str.size > current_breakpoint
# need to find a whitespace character before the breakpoint
current_breakpoint = input_str[0..(current_breakpoint-1)].rindex(/\s/)
return_str += input_str[0..(current_breakpoint-1)].strip + "\n" + " " * @hanging_indent
current_breakpoint = input_str[0..(current_breakpoint - 1)].rindex(/\s/)
return_str += input_str[0..(current_breakpoint - 1)].strip + "\n" + " " * @hanging_indent
input_str = input_str[current_breakpoint..-1].lstrip

# reset breakpoint for while() test
Expand Down
1 change: 1 addition & 0 deletions lib/paragraphify/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module Paragraphify
VERSION = "0.1.0"
end
57 changes: 30 additions & 27 deletions test/paragraphify_test.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# frozen_string_literal: true

require "test_helper"
require 'yaml'
require 'byebug'

class ParagraphifyTest < Minitest::Test
def store_test_data(test:)
result_hash = {}
hash = {}

# DEBUG puts "######### TEST = "
# DEBUG p test

result_hash[:input] = test["input"]
result_hash[:lb80_li0_hi0] = test["expected"]["lb80_li0_hi0"].reduce("") { |res, v| res == "" ? res = v[1] : res += "\n" + v[1] }
result_hash[:lb80_li4_hi0] = test["expected"]["lb80_li4_hi0"].reduce("") { |res, v| res == "" ? res = v[1] : res += "\n" + v[1] }
result_hash[:lb80_li0_hi8] = test["expected"]["lb80_li0_hi8"].reduce("") { |res, v| res == "" ? res = v[1] : res += "\n" + v[1] }
result_hash[:lb80_li4_hi8] = test["expected"]["lb80_li4_hi8"].reduce("") { |res, v| res == "" ? res = v[1] : res += "\n" + v[1] }
hash[:input] = test["input"]
hash[:lb80_li0_hi0] = test["expected"]["lb80_li0_hi0"].reduce("") { |res, v| res == "" ? v[1] : res << "\n" + v[1] }
hash[:lb80_li4_hi0] = test["expected"]["lb80_li4_hi0"].reduce("") { |res, v| res == "" ? v[1] : res << "\n" + v[1] }
hash[:lb80_li0_hi8] = test["expected"]["lb80_li0_hi8"].reduce("") { |res, v| res == "" ? v[1] : res << "\n" + v[1] }
hash[:lb80_li4_hi8] = test["expected"]["lb80_li4_hi8"].reduce("") { |res, v| res == "" ? v[1] : res << "\n" + v[1] }

result_hash
hash
end

def setup
Expand All @@ -35,7 +37,7 @@ def setup
end

def test_that_it_has_a_version_number
refute_nil ::Paragraphify::VERSION
refute_nil(::Paragraphify::VERSION)
end

#
Expand Down Expand Up @@ -128,13 +130,13 @@ def test_new_with_linebreak_and_leading_indent_and_hanging_indent_args
#
# test paragraphify()
#
def test_paragraphify_returns_a_String
def test_paragraphify_returns_a_string
p = Paragraphify::Paragraph.new

assert_kind_of String, p.paragraphify(string: "")
assert_kind_of(String, p.paragraphify(string: ""))
end

def test_paragraphify_returns_a_String_with_leading_indent
def test_paragraphify_returns_a_string_with_leading_indent
p = Paragraphify::Paragraph.new(leading_indent: 4)

input_string = "This is a test string."
Expand All @@ -146,7 +148,7 @@ def test_paragraphify_returns_a_String_with_leading_indent
# test paragraphify() with defaults - linebreak = 80, leading_indent = 0, hanging_indent = 0
#
def test_paragraphify_len50_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len50[:input], "\n"
# DEBUG puts "\n", @len50[:lb80_li0_hi0], "\n"
Expand All @@ -155,7 +157,7 @@ def test_paragraphify_len50_lb80_li0_hi0
end

def test_paragraphify_len79_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len79[:input], "\n"
# DEBUG puts "\n", @len79[:lb80_li0_hi0], "\n"
Expand All @@ -164,7 +166,7 @@ def test_paragraphify_len79_lb80_li0_hi0
end

def test_paragraphify_len80_ends_with_space_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len80_ends_with_space[:input], "\n"
# DEBUG puts "\n", @len80_ends_with_space[:lb80_li0_hi0], "\n"
Expand All @@ -173,7 +175,7 @@ def test_paragraphify_len80_ends_with_space_lb80_li0_hi0
end

def test_paragraphify_len100_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len100[:input], "\n"
# DEBUG puts "\n", @len100[:lb80_li0_hi0], "\n"
Expand All @@ -182,7 +184,7 @@ def test_paragraphify_len100_lb80_li0_hi0
end

def test_paragraphify_len159_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len159[:input], "\n"
# DEBUG puts "\n", @len159[:lb80_li0_hi0], "\n"
Expand All @@ -191,25 +193,25 @@ def test_paragraphify_len159_lb80_li0_hi0
end

def test_paragraphify_len159_ends_with_space_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len159_ends_with_space[:input], "\n"
# DEBUG puts "\n", @len159_ends_with_space[:lb80_li0_hi0], "\n"

assert_equal(@len159_ends_with_space[:lb80_li0_hi0], p.paragraphify(string: @len159_ends_with_space[:input]))
end

def test_paragraphify_len159_ends_with_space_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
def test_paragraphify_len159_strategic_spacing_lb80_li0_hi0
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len159_ends_with_space[:input], "\n"
# DEBUG puts "\n", @len159_ends_with_space[:lb80_li0_hi0], "\n"
# DEBUG puts "\n", @len159_strategic_spacing[:input], "\n"
# DEBUG puts "\n", @len159_strategic_spacing[:lb80_li0_hi0], "\n"

assert_equal(@len159_ends_with_space[:lb80_li0_hi0], p.paragraphify(string: @len159_ends_with_space[:input]))
assert_equal(@len159_strategic_spacing[:lb80_li0_hi0], p.paragraphify(string: @len159_strategic_spacing[:input]))
end

def test_paragraphify_len235_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len235[:input], "\n"
# DEBUG puts "\n", @len235[:lb80_li0_hi0], "\n"
Expand All @@ -218,7 +220,7 @@ def test_paragraphify_len235_lb80_li0_hi0
end

def test_paragraphify_len320_lb80_li0_hi0
p = Paragraphify::Paragraph.new()
p = Paragraphify::Paragraph.new

# DEBUG puts "\n", @len320[:input], "\n"
# DEBUG puts "\n", @len320[:lb80_li0_hi0], "\n"
Expand Down Expand Up @@ -352,8 +354,10 @@ def test_paragraphify_len159_lb80_li0_hi8
def test_paragraphify_len159_ends_with_space_lb80_li0_hi8
p = Paragraphify::Paragraph.new(hanging_indent: 8)

# DEBUG puts "\n", @len159_ends_with_space[:input], "\n"
# DEBUG puts "\n", @len159_ends_with_space[:lb80_li0_hi8], "\n"
# DEBUG
puts "#*#*#*#*#*#*#*#*#\n[", @len159_ends_with_space[:input], "]\n"
# DEBUG
puts "\n[", @len159_ends_with_space[:lb80_li0_hi8], "]\n#*#*#*#*#*#*#*#*#\n"

assert_equal(@len159_ends_with_space[:lb80_li0_hi8], p.paragraphify(string: @len159_ends_with_space[:input]))
end
Expand Down Expand Up @@ -468,5 +472,4 @@ def test_paragraphify_len320_lb80_li4_hi8

assert_equal(@len320[:lb80_li4_hi8], p.paragraphify(string: @len320[:input]))
end

end
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
# frozen_string_literal: true
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
require "paragraphify"

require "minitest/autorun"

0 comments on commit 029796d

Please sign in to comment.