File: require_tree_test.rb

package info (click to toggle)
ruby-derailed-benchmarks 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 760 kB
  • sloc: ruby: 1,317; makefile: 4
file content (95 lines) | stat: -rw-r--r-- 2,143 bytes parent folder | download
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
# frozen_string_literal: true

require 'test_helper'

class RequireTree < ActiveSupport::TestCase

  def tree(name)
    DerailedBenchmarks::RequireTree.new(name)
  end

  def teardown
    DerailedBenchmarks::RequireTree.const_set("REQUIRED_BY",  {})
  end

  test "default_cost" do
    parent =  tree("parent")
    assert_equal 0,       parent.cost
    value       = rand(0..100)
    parent.cost = value

    assert_equal value, parent.cost
  end

  test "stores child" do
    parent =  tree("parent")
    child  =  tree("child")
    parent << child

    # [](name)
    assert_equal child,   parent["child"]
    # children
    assert_equal [child], parent.children
    assert_equal [child], parent.sorted_children
  end

  test "sorts children" do
    parent = tree("parent")
    parent.cost = rand(5..10)
    small  = tree("small")
    small.cost = rand(10..100)

    large  = tree("large")
    large.cost = small.cost + 1

    parent << small
    parent << large

    expected = [large, small]
    assert_equal expected, parent.sorted_children

    expected = <<-OUT
parent: #{ parent.cost.round(4) } MiB
  large: #{ large.cost.round(4) } MiB
  small: #{ small.cost.round(4) } MiB
OUT
    capture  = StringIO.new

    parent.print_sorted_children(0, capture)

    assert_equal expected, capture.string
  end

  test "attributes duplicate children" do
    parent = tree("parent")
    parent.cost = rand(5..10)
    small  = tree("small")
    small.cost = rand(10..100)

    large  = tree("large")
    large.cost = small.cost + 1

    dup    = tree("large")
    dup.cost = 0.4
    small << dup

    parent << small
    parent << large

    expected = [large, small]
    assert_equal expected, parent.sorted_children

    expected = [dup]
    assert_equal expected, small.sorted_children

    expected = <<-OUT
parent: #{ parent.cost.round(4) } MiB
  large: #{ large.cost.round(4) } MiB (Also required by: small)
  small: #{ small.cost.round(4) } MiB
    large: #{ dup.cost.round(4) } MiB (Also required by: parent)
OUT
    capture  = StringIO.new
    parent.print_sorted_children(0, capture)
    assert_equal expected, capture.string
  end
end