1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
describe Rubyvis::Dom do
it "should map correctly" do
map={
:a=>{:aa=>1,
:ab=>{:aba=>2}
},
:b=>4,
:c=>{:ca=>5}
}
dom_map=Rubyvis::Dom.new(map)
root=dom_map.root.sort {|a,b| a.node_name.to_s<=>b.node_name.to_s}
root.nodes.should be_instance_of Array
root.nodes.size.should eq 8
ar=root.nodes.map do |n|
[n.node_name, n.node_value]
end
ar.should eq [[nil, nil], [:a, nil], [:aa, 1], [:ab, nil], [:aba, 2], [:b, 4], [:c, nil], [:ca, 5]]
end
it "should treemap example works right" do
flare={:a=>{:aa=>1,:ab=>1,:ac=>1},:b=>{:ba=>1,:bb=>1},:c=>3}
dom_map=Rubyvis::Dom.new(flare)
nodes = dom_map.root("flare").nodes
root = nodes[0]
root.visit_after {|nn,i|
if nn.first_child
nn.size=Rubyvis.sum(nn.child_nodes, lambda {|v| v.size})
else
nn.size=nn.node_value.to_f
end
}
root.sort(lambda {|a,b|
if a.size!=b.size
a.size<=>b.size
else
a.node_name.to_s<=>b.node_name.to_s
end
})
ar=[]
root.visit_before {|n,i|
ar.push [n.node_name, n.size]
}
ar.should eq [["flare", 8.0], [:b, 2.0], [:ba, 1.0], [:bb, 1.0], [:a, 3.0], [:aa, 1.0], [:ab, 1.0], [:ac, 1.0], [:c, 3.0]]
end
describe Rubyvis::Dom::Node do
before do
@n =Rubyvis::Dom::Node.new(:a)
@n2=Rubyvis::Dom::Node.new(:b)
@n3=Rubyvis::Dom::Node.new(:c)
@n4=Rubyvis::Dom::Node.new(:d)
@n5=Rubyvis::Dom::Node.new(:e)
end
subject {@n}
it "should have node_value" do
@n.node_value.should eq :a
end
it {should respond_to :parent_node}
it {should respond_to :first_child}
it {should respond_to :last_child}
it {should respond_to :previous_sibling}
it {should respond_to :next_sibling}
it {should respond_to :node_name}
it {should respond_to :child_nodes}
it "should child_nodes be empty" do
@n.child_nodes.size==0
end
it "method append_child" do
@n.append_child(@n2).should eq @n2
@n.child_nodes.should eq [@n2]
@n2.parent_node.should eq @n
@n.first_child.should eq @n2
@n.last_child.should eq @n2
@n.append_child(@n3)
@n.child_nodes.should eq [@n2,@n3]
@n3.parent_node.should eq @n
@n.first_child.should eq @n2
@n.last_child.should eq @n3
@n2.previous_sibling.should be_nil
@n2.next_sibling.should eq @n3
@n3.previous_sibling.should eq @n2
@n3.next_sibling.should be_nil
@n.append_child(@n4)
@n.last_child.should eq @n4
@n2.next_sibling.should eq @n3
@n3.next_sibling.should eq @n4
@n4.next_sibling.should be_nil
@n4.previous_sibling.should eq @n3
@n3.previous_sibling.should eq @n2
@n2.previous_sibling.should be_nil
end
it "method remove_child" do
@n.append_child(@n2)
@n.append_child(@n3)
@n.append_child(@n4)
@n.remove_child(@n3).should eq @n3
@n3.next_sibling.should be_nil
@n3.previous_sibling.should be_nil
@n3.parent_node.should be_nil
@n2.next_sibling.should eq @n4
@n4.previous_sibling.should eq @n2
@n.remove_child(@n4)
@n2.next_sibling.should be_nil
@n.first_child.should eq @n2
@n.last_child.should eq @n2
end
it "method insert_before" do
@n.append_child(@n2)
@n.append_child(@n4)
@n.insert_before(@n3,@n4)
@n.child_nodes.size.should eq 3
@n.first_child.should eq @n2
@n.last_child.should eq @n4
@n2.next_sibling.should eq @n3
@n3.next_sibling.should eq @n4
@n4.next_sibling.should be_nil
@n2.previous_sibling.should be_nil
@n3.previous_sibling.should eq @n2
@n4.previous_sibling.should eq @n3
@n.child_nodes[0].should eq @n2
@n.child_nodes[1].should eq @n3
@n.child_nodes[2].should eq @n4
end
it "method replace_child" do
@n.append_child(@n2)
@n.append_child(@n3)
@n.replace_child(@n4,@n3)
@n.child_nodes.size.should eq 2
@n.child_nodes[0].should eq @n2
@n.child_nodes[1].should eq @n4
@n2.next_sibling.should eq @n4
@n4.next_sibling.should eq nil
@n2.previous_sibling.should be_nil
@n4.previous_sibling.should eq @n2
@n.first_child.should eq @n2
@n.last_child.should eq @n4
end
describe "visit methods" do
before do
@n.append_child(@n2)
@n2.append_child(@n5)
@n.append_child(@n3)
@n3.append_child(@n4)
@a=[]
end
it "method visit_before" do
@n.visit_before {|n,d|
@a.push([n.node_value,d])
}
@a.should eq [[:a, 0], [:b, 1], [:e, 2], [:c, 1], [:d, 2]]
end
it "method visit_after" do
@n.visit_after {|n,d|
@a.push([n.node_value,d])
}
@a.should eq [[:e, 2], [:b, 1], [:d, 2], [:c, 1], [:a, 0]]
end
it "method each_child" do
@n.append_child(@n5)
@n.each_child {|d|
@a.push(d.node_value)
}
@a.should eq [:b, :c,:e]
end
end
it "should sort correctly" do
@n.append_child(@n5)
@n.append_child(@n4)
@n.append_child(@n3)
@n.append_child(@n2)
@n.sort {|a,b| a.node_value.to_s<=>b.node_value.to_s}
@n.child_nodes.should eq [@n2,@n3,@n4,@n5]
@n.first_child.should eq @n2
@n.last_child.should eq @n5
@n2.next_sibling.should eq @n3
@n3.next_sibling.should eq @n4
@n4.next_sibling.should eq @n5
@n5.next_sibling.should be_nil
@n2.previous_sibling.should be_nil
@n3.previous_sibling.should eq @n2
@n4.previous_sibling.should eq @n3
@n5.previous_sibling.should eq @n4
end
it "should reverse correctly" do
@n.append_child(@n3)
@n.append_child(@n5)
@n.append_child(@n2)
@n.append_child(@n4)
@n.reverse
@n.child_nodes.should eq [@n4,@n2,@n5,@n3]
end
end
end
|