From 9956dd113ed5b00dab6660cead345a394de734c1 Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Thu, 3 Oct 2024 19:24:17 +0200 Subject: [PATCH] Add tests --- lib/generator/api.rb | 4 +-- lib/generator/formatter.rb | 10 +++--- lib/generator/operation.rb | 6 ++-- test/generator/formatter_test.rb | 54 ++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 test/generator/formatter_test.rb diff --git a/lib/generator/api.rb b/lib/generator/api.rb index 7e8b2964..335087eb 100644 --- a/lib/generator/api.rb +++ b/lib/generator/api.rb @@ -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 diff --git a/lib/generator/formatter.rb b/lib/generator/formatter.rb index bd7cc1a4..f2b26256 100644 --- a/lib/generator/formatter.rb +++ b/lib/generator/formatter.rb @@ -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 = [] @@ -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 = [] diff --git a/lib/generator/operation.rb b/lib/generator/operation.rb index 2781b93f..97461486 100644 --- a/lib/generator/operation.rb +++ b/lib/generator/operation.rb @@ -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 @@ -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 @@ -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 diff --git a/test/generator/formatter_test.rb b/test/generator/formatter_test.rb new file mode 100644 index 00000000..a37ea628 --- /dev/null +++ b/test/generator/formatter_test.rb @@ -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 Example.' + expected = "Refer to {Example https://example.com}." + + assert_equal(expected, convert_html_links_to_yard(text)) + end + end +end