|
| 1 | +# test_tree_checks.rb - This file is part of the RubyTree package. |
| 2 | +# |
| 3 | +# Copyright (c) 2026 Anupam Sengupta. All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without modification, |
| 6 | +# are permitted provided that the following conditions are met: |
| 7 | +# |
| 8 | +# - Redistributions of source code must retain the above copyright notice, this |
| 9 | +# list of conditions and the following disclaimer. |
| 10 | +# |
| 11 | +# - Redistributions in binary form must reproduce the above copyright notice, this |
| 12 | +# list of conditions and the following disclaimer in the documentation and/or |
| 13 | +# other materials provided with the distribution. |
| 14 | +# |
| 15 | +# - Neither the name of the organization nor the names of its contributors may |
| 16 | +# be used to endorse or promote products derived from this software without |
| 17 | +# specific prior written permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +# |
| 30 | +# frozen_string_literal: true |
| 31 | + |
| 32 | +require 'test/unit' |
| 33 | +require_relative '../lib/tree/tree_deps' |
| 34 | + |
| 35 | +module TestTree |
| 36 | + class TestTreeChecks < Test::Unit::TestCase |
| 37 | + def test_checks_disabled_skips_validation |
| 38 | + root = Tree::TreeNode.new('root', nil, { checks: false }) |
| 39 | + child1 = Tree::TreeNode.new('dup', nil, { checks: false }) |
| 40 | + child2 = Tree::TreeNode.new('dup', nil, { checks: false }) |
| 41 | + |
| 42 | + root.add(child1) |
| 43 | + assert_nothing_raised { root.add(child2) } |
| 44 | + assert_equal(2, root.children.size) |
| 45 | + assert_nil(root[nil]) |
| 46 | + end |
| 47 | + |
| 48 | + def test_checks_enabled_validation_guards |
| 49 | + root = Tree::TreeNode.new('root') |
| 50 | + child1 = Tree::TreeNode.new('dup') |
| 51 | + child2 = Tree::TreeNode.new('dup') |
| 52 | + |
| 53 | + root.add(child1) |
| 54 | + assert_raise(RuntimeError) { root.add(child2) } |
| 55 | + assert_raise(ArgumentError) { root[nil] } |
| 56 | + end |
| 57 | + |
| 58 | + def test_checks_disabled_allows_out_of_range_insert |
| 59 | + root = Tree::TreeNode.new('root', nil, { checks: false }) |
| 60 | + child1 = Tree::TreeNode.new('child1', nil, { checks: false }) |
| 61 | + child2 = Tree::TreeNode.new('child2', nil, { checks: false }) |
| 62 | + |
| 63 | + root.add(child1, 999) |
| 64 | + root.add(child2, -999) |
| 65 | + assert_equal(2, root.children.size) |
| 66 | + end |
| 67 | + |
| 68 | + def test_checks_setting_propagates_to_children |
| 69 | + root = Tree::TreeNode.new('root', nil, { checks: false }) |
| 70 | + child = Tree::TreeNode.new('child') |
| 71 | + |
| 72 | + root.add(child) |
| 73 | + assert_equal(false, child.checks_enabled?) |
| 74 | + |
| 75 | + dup = Tree::TreeNode.new('child') |
| 76 | + assert_nothing_raised { root.add(dup) } |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments