Skip to content

Commit

Permalink
Fix visiblity of NamedChildren methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mbj committed Mar 21, 2016
1 parent 237d032 commit f0ae1d1
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/flay.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
threshold: 18
total_score: 1160
total_score: 1162
2 changes: 1 addition & 1 deletion config/flog.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
threshold: 29.3
threshold: 27.7
2 changes: 2 additions & 0 deletions lib/mutant/ast/meta/const.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Const

children :base, :name

public :base, :name

# Test if AST node is possibly a top level constant
#
# @return [Boolean]
Expand Down
2 changes: 2 additions & 0 deletions lib/mutant/ast/meta/optarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Optarg

children :name, :default_value

public :name, :default_value

# Test if optarg definition intends to be used
#
# @return [Boolean]
Expand Down
2 changes: 2 additions & 0 deletions lib/mutant/ast/meta/resbody.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Resbody
include NamedChildren, Concord.new(:node)

children :captures, :assignment, :body

public :captures, :assignment, :body
end # Resbody

end # Meta
Expand Down
2 changes: 2 additions & 0 deletions lib/mutant/ast/meta/restarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Restarg
include NamedChildren, Concord.new(:node)

children :name

public :name
end # Restarg

end # Meta
Expand Down
3 changes: 3 additions & 0 deletions lib/mutant/ast/meta/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ class Send

children :receiver, :selector

public :receiver, :selector

INDEX_ASSIGNMENT_SELECTOR = :[]=
ATTRIBUTE_ASSIGNMENT_SELECTOR_SUFFIX = '='.freeze

# Arguments of mutated node
#
# @return [Enumerable<Parser::AST::Node>]
alias_method :arguments, :remaining_children
public :arguments

# Test if AST node is a valid assignment target
#
Expand Down
2 changes: 2 additions & 0 deletions lib/mutant/ast/meta/symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Symbol

children :name

public :name

end # Symbol
end # Meta
end # AST
Expand Down
16 changes: 12 additions & 4 deletions lib/mutant/ast/named_children.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module ClassMethods
#
# @return [undefined]
def define_named_child(name, index)
define_method(name) do
define_private_method(name) do
children.at(index)
end
end
Expand All @@ -53,15 +53,15 @@ def define_named_child(name, index)
#
# @return [undefined]
def define_remaining_children(names)
define_method(:remaining_children_with_index) do
define_private_method(:remaining_children_with_index) do
children.each_with_index.drop(names.length)
end

define_method(:remaining_children_indices) do
define_private_method(:remaining_children_indices) do
children.each_index.drop(names.length)
end

define_method(:remaining_children) do
define_private_method(:remaining_children) do
children.drop(names.length)
end
end
Expand All @@ -76,6 +76,14 @@ def children(*names)
define_remaining_children(names)
end

# Define private method
#
# @param [Symbol] name
def define_private_method(name, &block)
define_method(name, &block)
private(name)
end

end # ClassMethods
end # NamedChildren
end # AST
Expand Down
26 changes: 26 additions & 0 deletions spec/unit/mutant/ast/named_children_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
end
end

def publish
klass.class_eval do
public :foo, :bar

public :remaining_children, :remaining_children_indices, :remaining_children_with_index
end
end

let(:instance) { klass.new(node) }

let(:node_foo) { s(:foo) }
Expand All @@ -17,33 +25,50 @@
let(:node) { s(:node, node_foo, node_bar, node_baz) }

describe 'generated methods' do
specify 'are private by default' do
%i[
foo
bar
remaining_children
remaining_children_indices
remaining_children_with_index
].each do |name|
expect(klass.private_instance_methods.include?(name)).to be(true)
end
end

describe '#remaining_children' do
it 'returns remaining unnamed children' do
publish
expect(instance.remaining_children).to eql([node_baz])
end
end

describe '#remaining_children_indices' do
it 'returns remaining unnamed children indices' do
publish
expect(instance.remaining_children_indices).to eql([2])
end
end

describe '#remaining_children_with_index' do
it 'returns remaining unnamed children indices' do
publish
expect(instance.remaining_children_with_index).to eql([[node_baz, 2]])
end
end

describe '#foo' do
it 'returns named child foo' do
publish
expect(instance.foo).to be(node_foo)
end
end

describe '#bar' do
context 'when node is present' do
it 'returns named child bar' do
publish
expect(instance.bar).to be(node_bar)
end
end
Expand All @@ -52,6 +77,7 @@
let(:node) { s(:node, node_foo) }

it 'returns nil' do
publish
expect(instance.bar).to be(nil)
end
end
Expand Down

0 comments on commit f0ae1d1

Please sign in to comment.