Skip to content

Commit

Permalink
Merge pull request #732 from nevir/unit-spec-namefixes
Browse files Browse the repository at this point in the history
Consistent file naming for specs (and *TernaryOperator cops)
  • Loading branch information
bbatsov committed Jan 12, 2014
2 parents 1398674 + b026e3c commit 0490398
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
11 changes: 6 additions & 5 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
require 'rubocop/cop/style/block_comments'
require 'rubocop/cop/style/block_nesting'
require 'rubocop/cop/style/blocks'
require 'rubocop/cop/style/braces_around_hash_parameters'
require 'rubocop/cop/style/case_equality'
require 'rubocop/cop/style/case_indentation'
require 'rubocop/cop/style/character_literal'
Expand All @@ -101,8 +102,8 @@
require 'rubocop/cop/style/dot_position'
require 'rubocop/cop/style/empty_line_between_defs'
require 'rubocop/cop/style/empty_lines'
require 'rubocop/cop/style/empty_lines_around_body'
require 'rubocop/cop/style/empty_lines_around_access_modifier'
require 'rubocop/cop/style/empty_lines_around_body'
require 'rubocop/cop/style/empty_literal'
require 'rubocop/cop/style/encoding'
require 'rubocop/cop/style/end_block'
Expand All @@ -125,14 +126,16 @@
require 'rubocop/cop/style/lambda_call'
require 'rubocop/cop/style/leading_comment_space'
require 'rubocop/cop/style/line_length'
require 'rubocop/cop/style/method_def_parentheses'
require 'rubocop/cop/style/method_call_parentheses'
require 'rubocop/cop/style/method_called_on_do_end_block'
require 'rubocop/cop/style/method_def_parentheses'
require 'rubocop/cop/style/method_length'
require 'rubocop/cop/style/method_name'
require 'rubocop/cop/style/module_function'
require 'rubocop/cop/style/multiline_block_chain'
require 'rubocop/cop/style/multiline_if_then'
require 'rubocop/cop/style/multiline_ternary_operator'
require 'rubocop/cop/style/nested_ternary_operator'
require 'rubocop/cop/style/nil_comparison'
require 'rubocop/cop/style/not'
require 'rubocop/cop/style/numeric_literals'
Expand Down Expand Up @@ -164,14 +167,13 @@
require 'rubocop/cop/style/space_around_equals_in_parameter_default'
require 'rubocop/cop/style/space_around_operators'
require 'rubocop/cop/style/space_before_modifier_keyword'
require 'rubocop/cop/style/space_inside_hash_literal_braces'
require 'rubocop/cop/style/space_inside_brackets'
require 'rubocop/cop/style/space_inside_hash_literal_braces'
require 'rubocop/cop/style/space_inside_parens'
require 'rubocop/cop/style/special_global_vars'
require 'rubocop/cop/style/string_literals'
require 'rubocop/cop/style/symbol_array'
require 'rubocop/cop/style/tab'
require 'rubocop/cop/style/ternary_operator'
require 'rubocop/cop/style/trailing_blank_lines'
require 'rubocop/cop/style/trailing_whitespace'
require 'rubocop/cop/style/trivial_accessors'
Expand All @@ -182,7 +184,6 @@
require 'rubocop/cop/style/while_until_do'
require 'rubocop/cop/style/while_until_modifier'
require 'rubocop/cop/style/word_array'
require 'rubocop/cop/style/braces_around_hash_parameters'

require 'rubocop/cop/rails/default_scope'
require 'rubocop/cop/rails/has_and_belongs_to_many'
Expand Down
22 changes: 22 additions & 0 deletions lib/rubocop/cop/style/multiline_ternary_operator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# encoding: utf-8

module Rubocop
module Cop
module Style
# This cop checks for multi-line ternary op expressions.
class MultilineTernaryOperator < Cop
MSG =
'Avoid multi-line ?: (the ternary operator); use if/unless instead.'

def on_if(node)
loc = node.loc

# discard non-ternary ops
return unless loc.respond_to?(:question)

add_offence(node, :expression) if loc.line != loc.colon.line
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,6 @@
module Rubocop
module Cop
module Style
# This cop checks for multi-line ternary op expressions.
class MultilineTernaryOperator < Cop
MSG =
'Avoid multi-line ?: (the ternary operator); use if/unless instead.'

def on_if(node)
loc = node.loc

# discard non-ternary ops
return unless loc.respond_to?(:question)

add_offence(node, :expression) if loc.line != loc.colon.line
end
end

# This cop checks for nested ternary op expressions.
class NestedTernaryOperator < Cop
MSG = 'Ternary operators must not be nested. Prefer if/else ' +
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/multiline_ternary_operator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# encoding: utf-8

require 'spec_helper'

describe Rubocop::Cop::Style::MultilineTernaryOperator do
subject(:cop) { described_class.new }

it 'registers offence for a multiline ternary operator expression' do
inspect_source(cop, ['a = cond ?',
' b : c'])
expect(cop.offences.size).to eq(1)
end

it 'accepts a single line ternary operator expression' do
inspect_source(cop, ['a = cond ? b : c'])
expect(cop.offences).to be_empty
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@

require 'spec_helper'

describe Rubocop::Cop::Style::MultilineTernaryOperator do
subject(:cop) { described_class.new }

it 'registers offence for a multiline ternary operator expression' do
inspect_source(cop, ['a = cond ?',
' b : c'])
expect(cop.offences.size).to eq(1)
end

it 'accepts a single line ternary operator expression' do
inspect_source(cop, ['a = cond ? b : c'])
expect(cop.offences).to be_empty
end
end

describe Rubocop::Cop::Style::NestedTernaryOperator do
subject(:cop) { described_class.new }

Expand Down

0 comments on commit 0490398

Please sign in to comment.