-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #145 Add cop for bad command title capitalization
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module SketchupSuggestions | ||
# SketchUp command titles should be in title case, e.g. "Make Component" | ||
# and "Follow Me". | ||
# | ||
# In English, capitalize the first letter of each word. Other languages | ||
# may have different rules. | ||
class CommandTitle < SketchUp::Cop | ||
|
||
def_node_matcher :init_entity?, <<-PATTERN | ||
(send (const (const nil? :UI) :Command ) :new ... ) | ||
PATTERN | ||
|
||
MESSAGE = 'Use title case in command titles. '\ | ||
'In English, capitalize the first letter of each word.' | ||
|
||
def on_send(node) | ||
return unless init_entity?(node) | ||
return if title_case?(node.arguments.first.str_content) | ||
|
||
add_offense(node, message: MESSAGE) | ||
end | ||
|
||
private | ||
|
||
# REVIEW: Extract to where they can be re-used? | ||
def title_case?(text) | ||
text == title_case(text) | ||
end | ||
|
||
def title_case(text) | ||
text.gsub(/^(.)/) { ::Regexp.last_match(1).upcase } | ||
.gsub(/\ (.)/) { " #{::Regexp.last_match(1).upcase}" } | ||
.gsub(/-(.)/) { "-#{::Regexp.last_match(1).upcase}" } | ||
.gsub(/(\.)$/, '') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
spec/rubocop/cop/sketchup_suggestions/command_title_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::SketchupSuggestions::CommandTitle do | ||
|
||
subject(:cop) { described_class.new } | ||
|
||
bad_capitalization = [ | ||
'text', | ||
'text text', | ||
'Text text', | ||
'text Text', | ||
'Text.', | ||
'Text Text.', | ||
'Text text.', | ||
'text 2-Point', | ||
'text 2-point' | ||
] | ||
|
||
bad_capitalization.each do |keyword| | ||
it "registers an offense when using UI::Command.new(\"#{keyword}\")" do | ||
expect_offense(<<~RUBY, keyword: keyword) | ||
UI::Command.new("%{keyword}") | ||
^^^^^^^^^^^^^^^^^^{keyword}^^ Use title case in command titles. [...] | ||
RUBY | ||
end | ||
|
||
# TODO: Add for setter methods. | ||
# Can we test by how a local variable was defined? Or only by its name? | ||
# | ||
# command = UI::Command.new("Test") | ||
# command.menu_text = "%{keyword}" | ||
# | ||
# menu.add_item("%{keyword}") | ||
end | ||
|
||
good_capitalization = [ | ||
'Text', | ||
'Text Text', | ||
'Text 2-Point', | ||
'文本' | ||
] | ||
|
||
good_capitalization.each do |keyword| | ||
it "does not register an offense when using UI::Command.new(\"#{keyword}\")" do | ||
expect_no_offenses(<<~RUBY, keyword: keyword) | ||
UI::Command.new("%{keyword}") | ||
RUBY | ||
end | ||
end | ||
|
||
end |