1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/test/visitors/test_depth_first.rb

239 lines
6.1 KiB
Ruby
Raw Normal View History

require 'helper'
module Arel
module Visitors
2014-01-04 05:08:46 -05:00
class TestDepthFirst < Minitest::Test
Collector = Struct.new(:calls) do
def call object
calls << object
end
end
def setup
@collector = Collector.new []
@visitor = Visitors::DepthFirst.new @collector
end
def test_raises_with_object
assert_raises(TypeError) do
@visitor.accept(Object.new)
end
end
2010-11-29 18:17:59 -05:00
# unary ops
[
Arel::Nodes::Not,
Arel::Nodes::Group,
Arel::Nodes::On,
Arel::Nodes::Grouping,
Arel::Nodes::Offset,
Arel::Nodes::Ordering,
2010-11-29 18:17:59 -05:00
Arel::Nodes::Having,
2010-12-14 14:04:08 -05:00
Arel::Nodes::StringJoin,
2010-11-29 18:17:59 -05:00
Arel::Nodes::UnqualifiedColumn,
2011-01-03 14:12:46 -05:00
Arel::Nodes::Top,
Arel::Nodes::Limit,
2010-11-29 18:17:59 -05:00
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
op = klass.new(:a)
@visitor.accept op
assert_equal [:a, op], @collector.calls
end
end
# functions
[
Arel::Nodes::Exists,
Arel::Nodes::Avg,
Arel::Nodes::Min,
Arel::Nodes::Max,
Arel::Nodes::Sum,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
func = klass.new(:a, "b")
2010-11-29 18:17:59 -05:00
@visitor.accept func
assert_equal [:a, "b", false, func], @collector.calls
2010-11-29 18:17:59 -05:00
end
end
def test_named_function
func = Arel::Nodes::NamedFunction.new(:a, :b, "c")
@visitor.accept func
assert_equal [:a, :b, false, "c", func], @collector.calls
end
2010-11-29 18:17:59 -05:00
def test_lock
2011-02-21 18:14:29 -05:00
lock = Nodes::Lock.new true
2010-11-29 18:17:59 -05:00
@visitor.accept lock
assert_equal [lock], @collector.calls
end
def test_count
count = Nodes::Count.new :a, :b, "c"
2010-11-29 18:17:59 -05:00
@visitor.accept count
assert_equal [:a, "c", :b, count], @collector.calls
2010-11-29 18:17:59 -05:00
end
def test_inner_join
2010-12-14 12:43:19 -05:00
join = Nodes::InnerJoin.new :a, :b
2010-11-29 18:17:59 -05:00
@visitor.accept join
2010-12-14 12:43:19 -05:00
assert_equal [:a, :b, join], @collector.calls
2010-11-29 18:17:59 -05:00
end
def test_outer_join
2010-12-14 12:43:19 -05:00
join = Nodes::OuterJoin.new :a, :b
2010-11-29 18:17:59 -05:00
@visitor.accept join
2010-12-14 12:43:19 -05:00
assert_equal [:a, :b, join], @collector.calls
2010-11-29 18:17:59 -05:00
end
[
Arel::Nodes::Assignment,
Arel::Nodes::Between,
Arel::Nodes::DoesNotMatch,
Arel::Nodes::Equality,
Arel::Nodes::GreaterThan,
Arel::Nodes::GreaterThanOrEqual,
Arel::Nodes::In,
Arel::Nodes::LessThan,
Arel::Nodes::LessThanOrEqual,
Arel::Nodes::Matches,
Arel::Nodes::NotEqual,
Arel::Nodes::NotIn,
Arel::Nodes::Or,
2010-11-29 18:17:59 -05:00
Arel::Nodes::TableAlias,
Arel::Nodes::Values,
Arel::Nodes::As,
Arel::Nodes::DeleteStatement,
Arel::Nodes::JoinSource,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
binary = klass.new(:a, :b)
@visitor.accept binary
assert_equal [:a, :b, binary], @collector.calls
end
end
def test_Arel_Nodes_InfixOperation
binary = Arel::Nodes::InfixOperation.new(:o, :a, :b)
@visitor.accept binary
assert_equal [:a, :b, binary], @collector.calls
end
2010-12-09 17:52:11 -05:00
# N-ary
[
Arel::Nodes::And,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
binary = klass.new([:a, :b, :c])
2010-12-09 17:52:11 -05:00
@visitor.accept binary
assert_equal [:a, :b, :c, binary], @collector.calls
2010-12-09 17:52:11 -05:00
end
end
[
Arel::Attributes::Integer,
Arel::Attributes::Float,
Arel::Attributes::String,
Arel::Attributes::Time,
Arel::Attributes::Boolean,
Arel::Attributes::Attribute
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
binary = klass.new(:a, :b)
@visitor.accept binary
assert_equal [:a, :b, binary], @collector.calls
end
end
def test_table
relation = Arel::Table.new(:users)
@visitor.accept relation
assert_equal ['users', relation], @collector.calls
end
def test_array
node = Nodes::Or.new(:a, :b)
list = [node]
@visitor.accept list
assert_equal [:a, :b, node, list], @collector.calls
end
def test_hash
node = Nodes::Or.new(:a, :b)
hash = { node => node }
@visitor.accept hash
assert_equal [:a, :b, node, :a, :b, node, hash], @collector.calls
end
2010-11-29 17:15:58 -05:00
def test_update_statement
stmt = Nodes::UpdateStatement.new
stmt.relation = :a
stmt.values << :b
stmt.wheres << :c
stmt.orders << :d
stmt.limit = :e
@visitor.accept stmt
assert_equal [:a, :b, stmt.values, :c, stmt.wheres, :d, stmt.orders,
:e, stmt], @collector.calls
end
2010-11-29 17:23:58 -05:00
def test_select_core
core = Nodes::SelectCore.new
core.projections << :a
core.froms = :b
core.wheres << :c
core.groups << :d
2012-02-22 09:25:10 -05:00
core.windows << :e
core.having = :f
2010-11-29 17:23:58 -05:00
@visitor.accept core
assert_equal [
:a, core.projections,
2010-12-14 14:04:08 -05:00
:b, [],
core.source,
2010-11-29 17:23:58 -05:00
:c, core.wheres,
:d, core.groups,
2012-02-22 09:25:10 -05:00
:e, core.windows,
:f,
2010-11-29 17:23:58 -05:00
core], @collector.calls
end
2010-11-29 17:26:53 -05:00
def test_select_statement
ss = Nodes::SelectStatement.new
ss.cores.replace [:a]
ss.orders << :b
ss.limit = :c
ss.lock = :d
ss.offset = :e
@visitor.accept ss
assert_equal [
:a, ss.cores,
:b, ss.orders,
:c,
:d,
:e,
ss], @collector.calls
end
2010-11-29 17:30:06 -05:00
def test_insert_statement
stmt = Nodes::InsertStatement.new
stmt.relation = :a
stmt.columns << :b
stmt.values = :c
@visitor.accept stmt
assert_equal [:a, :b, stmt.columns, :c, stmt], @collector.calls
end
2010-11-29 18:31:28 -05:00
def test_node
node = Nodes::Node.new
@visitor.accept node
assert_equal [node], @collector.calls
end
end
end
end