Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Oct 3, 2024
1 parent 27d0425 commit 9956dd1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/generator/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def generate
end

def title
split_long_comment_line(model["info"]["title"], 4)
split_long_comment_line(model["info"]["title"], base_indent: 4)
end

def description
description = model["info"]["description"]
description = convert_doc_links_to_full_url(description)

split_long_comment_line(description, 4)
split_long_comment_line(description, base_indent: 4)
end

def library_name
Expand Down
10 changes: 5 additions & 5 deletions lib/generator/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Generator
module Formatter
MAX_LINE_LENGTH = 120

def split_long_comment_line(line, base_indent, wrap_indent = 0)
max_width = MAX_LINE_LENGTH - base_indent - 2 # Account for the space and `#`
def split_long_comment_line(line, base_indent: 0, wrap_indent: 0, max_line_length: MAX_LINE_LENGTH)
max_width = max_line_length - base_indent - 2 # Account for the space and `#`

current_line = []
lines = []
Expand All @@ -23,11 +23,11 @@ def split_long_comment_line(line, base_indent, wrap_indent = 0)
lines << " " * base_indent + "# " + " " * (lines.empty? ? 0 : wrap_indent) + current_line.join(" ")
end

lines.empty? ? " " * base_indent + "#\n" : lines.join("\n")
lines.empty? ? " " * base_indent + "#" : lines.join("\n")
end

def build_method_definition(method_name, params, base_indent)
max_width = MAX_LINE_LENGTH - base_indent
def format_method_definition(method_name, params, base_indent: 0, max_line_length: MAX_LINE_LENGTH)
max_width = max_line_length - base_indent

current_line = ["def #{method_name}("]
lines = []
Expand Down
6 changes: 3 additions & 3 deletions lib/generator/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def description
description = convert_html_links_to_yard(description)
description = convert_doc_links_to_full_url(description)

split_long_comment_line(description, 6)
split_long_comment_line(description, base_indent: 6)
end

def tags
Expand All @@ -63,7 +63,7 @@ def tags
output.map do |line|
line = convert_html_links_to_yard(line)
line = convert_doc_links_to_full_url(line)
split_long_comment_line(line, 6, 2)
split_long_comment_line(line, base_indent: 6, wrap_indent: 2)
end
end

Expand All @@ -81,7 +81,7 @@ def method_definition
end
params = required_params + optional_params

build_method_definition(method_name, params, 6)
format_method_definition(method_name, params, base_indent: 6)
end

def sandbox_rule
Expand Down
54 changes: 54 additions & 0 deletions test/generator/formatter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require "minitest/autorun"
require "generator/formatter"

module Generator
class FormatterTest < Minitest::Test
include Generator::Formatter

def test_split_long_comment_line
line = "# This is a very long comment line."
expected = "# This is a very\n# long comment line."

assert_equal(expected, split_long_comment_line(line, max_line_length: 20))

line = "#Short comment."
expected = "# Short comment."

assert_equal(expected, split_long_comment_line(line))

line = "#"
expected = "#"

assert_equal(expected, split_long_comment_line(line))
end

def test_format_method_definition
method_name = "example_method"
params = ["param1", "param2", "param3"]
expected = "def example_method(param1, param2, param3)"

assert_equal(expected, format_method_definition(method_name, params))

params = ["param1", "param2", "param3", "param4: nil", "param5: nil", "param6: nil"]
expected = "def example_method(param1, param2, param3, param4: nil,\n param5: nil, param6: nil)"

assert_equal(expected, format_method_definition(method_name, params, max_line_length: 60))
end

def test_convert_doc_links_to_full_url
text = "Refer to [API Documentation](doc:api-doc)."
expected = "Refer to {API Documentation https://developer-docs.amazon.com/sp-api/docs/api-doc}."

assert_equal(expected, convert_doc_links_to_full_url(text))
end

def test_convert_html_links_to_yard
text = 'Refer to <a href="https://example.com">Example</a>.'
expected = "Refer to {Example https://example.com}."

assert_equal(expected, convert_html_links_to_yard(text))
end
end
end

0 comments on commit 9956dd1

Please sign in to comment.