Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update NodeList and NodeList test
  • Loading branch information
mcmire committed Aug 16, 2008
1 parent 540182a commit 00ae444
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
36 changes: 18 additions & 18 deletions lib/papyrus/node_list.rb
@@ -1,47 +1,47 @@
module Papyrus
# A CommandBlock (not to be confused with a BlockCommand) provides a single
# interface to multiple Command objects. As it is a Node, it has an output method.
class CommandBlock < Node
attr_reader :commands
# A NodeList provides a single interface to multiple Node objects. As it is a
# Node itself, it has an output method.
class NodeList < Node
attr_reader :nodes

def initialize
@commands = []
@nodes = []
end

for meth in [ :length, :size, :first, :last, :empty? ]
class_eval <<-EOT, __FILE__, __LINE__
def #{meth}
@commands.send(:#{meth})
@nodes.send(:#{meth})
end
EOT
end
def [](i)
@commands[i]
@nodes[i]
end

# Adds +command+ to the end of the Block's chain of Commands.
# A TypeError is raised if the object being added is not a ((<Command>)).
# Adds +node+ to the NodeList.
# A TypeError is raised if the object being added is not a Node.
#
# This is also aliased as <<
def add(cmd)
raise TypeError, 'Command::Block#add: Attempt to add non-Command object' unless cmd.kind_of?(Base)
@commands << cmd
def add(node)
raise TypeError, 'NodeList#add: Attempt to add non-Node object' unless node.kind_of?(Node)
@nodes << node
self
end
def <<(cmd)
add(cmd)
def <<(node)
add(node)
end

# Calls Command#output(context) on each Command contained in this
# Calls Node#output(context) on each Node contained in this
# object. The output is returned as a single string. If no output
# is generated, returns an empty string.
def output(context = nil)
@commands.inject("") {|str, cmd| str << cmd.output(context) }
@nodes.inject("") {|str, node| str << node.output(context) }
end

# Returns Commands held, as a string
# Returns Nodes held, as a string
def to_s
'[ Blocks: ' + @commands.map {|cmd| "[#{cmd.to_s}]" }.join(' ') + ' ]'
'[ NodeList: ' + @nodes.map {|node| "[#{node.to_s}]" }.join(' ') + ' ]'
end
end
end
62 changes: 32 additions & 30 deletions test/unit/node_list_test.rb
@@ -1,54 +1,56 @@
require File.dirname(__FILE__)+'/../test_helper'
require File.dirname(__FILE__)+'/test_helper'

require 'command/base'
require 'command/text'
require 'command/comment'
require 'command/block'
require 'node'
require 'node_list'
require 'text'
require 'command'

include Papyrus

Expectations do

expect [] do
Papyrus::Command::CommandBlock.new.send(:instance_variable_get, "@commands")
NodeList.new.send(:instance_variable_get, "@nodes")
end

locally do
block = Papyrus::Command::CommandBlock.new
expect block.send(:commands).to.receive(:length) do
block = NodeList.new
expect block.nodes.to.receive(:length) do
block.length
end
end

locally do
block = Papyrus::Command::CommandBlock.new
expect block.send(:commands).to.receive(:size) do
block = NodeList.new
expect block.nodes.to.receive(:size) do
block.size
end
end

locally do
block = Papyrus::Command::CommandBlock.new
expect block.send(:commands).to.receive(:first) do
block = NodeList.new
expect block.nodes.to.receive(:first) do
block.first
end
end

locally do
block = Papyrus::Command::CommandBlock.new
expect block.send(:commands).to.receive(:last) do
block = NodeList.new
expect block.nodes.to.receive(:last) do
block.last
end
end

locally do
block = Papyrus::Command::CommandBlock.new
expect block.send(:commands).to.receive(:empty?) do
block = NodeList.new
expect block.nodes.to.receive(:empty?) do
block.empty?
end
end

locally do
block = Papyrus::Command::CommandBlock.new
expect block.send(:commands).to.receive(:[]).with(0) do
block = NodeList.new
expect block.nodes.to.receive(:[]).with(0) do
block[0]
end
end
Expand All @@ -57,34 +59,34 @@
begin
# when argument is not a Command
expect TypeError do
Papyrus::Command::CommandBlock.new.add("something else")
NodeList.new.add("something else")
end
# when argument is a Command
expect true do
block = Papyrus::Command::CommandBlock.new
cmd = Papyrus::Command::Base.new
block = NodeList.new
cmd = Command.new("", [])
block.add(cmd)
block.send(:commands).include?(cmd)
block.nodes.include?(cmd)
end
end

locally do
expect Papyrus::Command::CommandBlock.new.to.receive(:add) do |block|
expect NodeList.new.to.receive(:add) do |block|
block << "blah"
end
end

expect "foobar" do
block = Papyrus::Command::CommandBlock.new
block << Papyrus::Text.new("foo")
block << Papyrus::Text.new("bar")
block = NodeList.new
block << Text.new("foo")
block << Text.new("bar")
block.output
end

expect "[ Blocks: [foo] [bar] ]" do
block = Papyrus::Command::CommandBlock.new
block << Papyrus::Text.new("foo")
block << Papyrus::Text.new("bar")
expect "[ NodeList: [foo] [bar] ]" do
block = NodeList.new
block << Text.new("foo")
block << Text.new("bar")
block.to_s
end

Expand Down

0 comments on commit 00ae444

Please sign in to comment.