File: change_observer_test.rb

package info (click to toggle)
ruby-bootsnap 1.18.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 524 kB
  • sloc: ruby: 3,718; ansic: 756; sh: 14; makefile: 9
file content (107 lines) | stat: -rw-r--r-- 2,826 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require "test_helper"

module Bootsnap
  module LoadPathCache
    class ChangeObserverTest < Minitest::Test
      include LoadPathCacheHelper

      def setup
        super
        @observer = Object.new
        @observer.instance_variable_set(:@mutex, Mutex.new)
        @arr = []
        ChangeObserver.register(@arr, @observer)
      end

      def test_observes_changes
        @observer.expects(:push_paths).with(@arr, "a")
        @arr << "a"

        @observer.expects(:push_paths).with(@arr, "b", "c")
        @arr.push("b", "c")

        @observer.expects(:push_paths).with(@arr, "d", "e")
        @arr.append("d", "e")

        @observer.expects(:unshift_paths).with(@arr, "f", "g")
        @arr.unshift("f", "g")

        @observer.expects(:push_paths).with(@arr, "h", "i")
        @arr.push("h", "i")

        @observer.expects(:unshift_paths).with(@arr, "j", "k")
        @arr.prepend("j", "k")
      end

      def test_unregister
        @observer.expects(:push_paths).never
        @observer.expects(:unshift_paths).never
        @observer.expects(:reinitialize).never

        ChangeObserver.unregister(@arr)

        @arr << "a"
        @arr.push("b", "c")
        @arr.append("d", "e")
        @arr.unshift("f", "g")
        @arr.push("h", "i")
        @arr.prepend("j", "k")
        @arr.delete(3)
        @arr.compact!
        @arr.map!(&:upcase)
        assert_equal %w(J K F G A B C D E H I), @arr
      end

      def test_reinitializes_on_aggressive_modifications
        @observer.expects(:push_paths).with(@arr, "a", "b", "c")
        @arr.push("a", "b", "c")

        @observer.expects(:reinitialize).times(4)
        @arr.delete(3)
        @arr.compact!
        @arr.map!(&:upcase)
        assert_equal("C", @arr.pop)
        assert_equal(%w(A B), @arr)
      end

      def test_register_frozen
        # just assert no crash
        ChangeObserver.register(@arr.dup.freeze, @observer)
      end

      def test_register_twice_observes_once
        ChangeObserver.register(@arr, @observer)

        @observer.expects(:push_paths).with(@arr, "a").once
        @arr << "a"
        assert_equal(%w(a), @arr)
      end

      def test_dup_returns_ractor_shareable_instance
        return unless defined?(Ractor)

        ChangeObserver.register(@arr, @observer)
        Ractor.make_shareable(@arr.dup.freeze)
      end

      def test_clone_returns_ractor_shareable_instance
        return unless defined?(Ractor)

        ChangeObserver.register(@arr, @observer)
        Ractor.make_shareable(@arr.clone.freeze)
      end

      def test_uniq_without_block
        @observer.expects(:reinitialize).never
        @arr.uniq!
      end

      def test_uniq_with_block
        @observer.expects(:reinitialize).once
        @arr.uniq! { |i| i }
      end
    end
  end
end