# Copyright (c) 2010-2011 David Love # # Permission to use, copy, modify, and/or distribute this software for # any purpose with or without fee is hereby granted, provided that the # above copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # # @author David Love # # This test suite checks for the correct behaviour of the {FlatTree} data # structure # module Test::DataStructures require "data_structures" include WhiteCloth::DataStructures # Check that we can create a {StandardTree} context "Creating empty Standard Tree" do setup { StandardTree.new } asserts_topic.kind_of(WhiteCloth::DataStructures::StandardTree) asserts("the Standard Tree is empty") { topic.empty? } end # Create this structure for the tests # # +----------+ # | ROOT | # +-+--------+ # | # | +---------------+ # +----+ Child 1 | # | +---------------+ # | # | +---------------+ # +----+ Child 2 | # | +---------------+ # | # | +---------------+ +------------------+ +---------------------+ # +----+ Child 3 +---+ Grand Child 1 +---+ Great Grand Child 1 | # | +---------------+ +------------------+ +---------------------+ # | # | +---------------+ +------------------+ # +----+ Child 4 +---+ Grand Child 2 | # +---------------+ +------------------+ # # This test is for the root alone - without any children being linked context "Check the Standard Tree root node behaves as it should" do setup { @root = StandardTree.new("b081eabd-8799-4885-8c4d-eb3cf76b30b4") } denies("root node"){ topic }.nil asserts("parent of root node") { topic[nil].parent }.nil denies("root name") { topic[nil].id }.nil asserts("root name") { topic[nil].id }.equals("b081eabd-8799-4885-8c4d-eb3cf76b30b4") asserts("root contents") { topic[nil].content }.nil asserts("root node declares as the root node") { topic[nil].is_root? } denies("root cannot have any children") { topic[nil].has_children? } denies("root has contents") { topic[nil].has_content? } asserts("number of nodes should be one") { topic[nil].size }.equals(1) asserts("root does not have any children") { topic[nil].siblings.nil? } asserts("root should have an in-degree that") { topic[nil].in_degree }.equals(0) asserts("root's height before adding any children") { topic[nil].node_height }.equals(0) end context "Check that the Standard Tree adds new nodes correctly" do setup { @root = StandardTree.new("b081eabd-8799-4885-8c4d-eb3cf76b30b4") @root << {nil => "Child 1"} @root << {nil => "Child 2", nil => "Child 3"} @root << {nil => "Child 3", "Child Node 3" => "Grand Child 1"} @root << {nil => "Child 4"} @root << {"Child 4" => "Grand Child 2", "Grand Child 1" => "Great Grand Child 1"} @root } asserts("root's root is self") { topic[nil].root == topic[nil] } asserts("child 1's root should be ROOT") { topic["Child 1"].root == topic[nil] } asserts("child 4's root should be ROOT") { topic["Child 4"].root == topic[nil] } asserts("great grand child 1's parent should be \"Grand Child 1\"") { topic["Great Grand Child 1"].parent == topic["Grand Child 1"] } denies("great grand child 1's parent should be \"Child 3\"") { topic["Great Grand Child 1"].parent == topic["Child 3"] } asserts("root's height after adding the children") { topic[nil].node_height }.equals(2) end context "Test the new nodes have no content" do setup { a_node = Tree::TreeNode.new("A Node") } asserts("the node should not have content") { topic.content }.nil denies("the node should not have content") { topic.has_content? } end context "Test the presence of content in the nodes" do setup { a_node = Tree::TreeNode.new("A Node") a_node.content = "Something" a_node } denies("the node should now have content") { topic.content }.nil asserts("the node should now have content") { topic.has_content? } end end