@@ -859,6 +859,65 @@ def assert_parent_and_children
859859 assert_equal ( graph , dot )
860860 end
861861 end
862+
863+ describe '.depth' do
864+ it 'should render for an empty scope' do
865+ @tag_class . find_or_create_by_path ( %w[ a b1 c1 ] )
866+ @tag_class . find_or_create_by_path ( %w[ a b2 c2 ] )
867+ @tag_class . find_or_create_by_path ( %w[ a b2 c3 ] )
868+ a , b1 , b2 , c1 , c2 , c3 = %w[ a b1 b2 c1 c2 c3 ] . map { |ea | @tag_class . where ( name : ea ) . first . id }
869+ dot = @tag_class . roots . first . to_dot_digraph
870+
871+ graph = <<~DOT
872+ digraph G {
873+ "#{ a } " [label="a"]
874+ "#{ a } " -> "#{ b1 } "
875+ "#{ b1 } " [label="b1"]
876+ "#{ a } " -> "#{ b2 } "
877+ "#{ b2 } " [label="b2"]
878+ "#{ b1 } " -> "#{ c1 } "
879+ "#{ c1 } " [label="c1"]
880+ "#{ b2 } " -> "#{ c2 } "
881+ "#{ c2 } " [label="c2"]
882+ "#{ b2 } " -> "#{ c3 } "
883+ "#{ c3 } " [label="c3"]
884+ }
885+ DOT
886+
887+ assert_equal ( graph , dot )
888+ end
889+ end
890+
891+ describe '.depth' do
892+ before do
893+ @d1 = @tag_class . find_or_create_by_path %w[ a b c1 d1 ]
894+ @c1 = @d1 . parent
895+ @b = @c1 . parent
896+ @a = @b . parent
897+ @a2 = @tag_class . create ( name : 'a2' )
898+ @b2 = @tag_class . find_or_create_by_path %w[ a b2 ]
899+ @c3 = @tag_class . find_or_create_by_path %w[ a3 b3 c3 ]
900+ @b3 = @c3 . parent
901+ @a3 = @b3 . parent
902+
903+
904+ end
905+
906+ it 'should return 0 for root' do
907+ assert_equal 0 , @a . depth
908+ assert_equal 0 , @a2 . depth
909+ assert_equal 0 , @a3 . depth
910+ end
911+
912+ it 'should return correct depth for nodes' do
913+ assert_equal 1 , @b . depth
914+ assert_equal 2 , @c1 . depth
915+ assert_equal 3 , @d1 . depth
916+ assert_equal 1 , @b2 . depth
917+ assert_equal 1 , @b3 . depth
918+ assert_equal 2 , @c3 . depth
919+ end
920+ end
862921 end
863922 end
864923end
0 commit comments