File: memo_wise_spec.rb

package info (click to toggle)
ruby-memo-wise 1.5.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 384 kB
  • sloc: ruby: 2,663; makefile: 4; sh: 4
file content (787 lines) | stat: -rw-r--r-- 29,334 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# frozen_string_literal: true

require "values"

RSpec.describe MemoWise do
  describe "#memo_wise" do
    shared_examples "#memo_wise shared examples" do
      #
      # These examples depend on `let(:target)` -- can be an instance or a class
      #

      it "memoizes methods with no arguments" do
        expect(Array.new(4) { target.no_args }).to all eq("no_args")
        expect(target.no_args_counter).to eq(1)
      end

      it "memoizes methods with one positional argument" do
        expect(Array.new(4) { target.with_one_positional_arg(1) }).to all eq("with_one_positional_arg: a=1")
        expect(Array.new(4) { target.with_one_positional_arg(2) }).to all eq("with_one_positional_arg: a=2")

        # This should be executed once for each set of arguments passed
        expect(target.with_one_positional_arg_counter).to eq(2)
      end

      it "memoizes methods with positional arguments" do
        expect(Array.new(4) { target.with_positional_args(1, 2) }).to all eq("with_positional_args: a=1, b=2")
        expect(Array.new(4) { target.with_positional_args(1, 3) }).to all eq("with_positional_args: a=1, b=3")

        # This should be executed once for each set of arguments passed
        expect(target.with_positional_args_counter).to eq(2)
      end

      it "memoizes methods with positional and splat arguments" do
        expect(Array.new(4) { target.with_positional_and_splat_args(1, 2, 3) }).
          to all eq("with_positional_and_splat_args: a=1, args=[2, 3]")

        expect(Array.new(4) { target.with_positional_and_splat_args(1, 3, 4) }).
          to all eq("with_positional_and_splat_args: a=1, args=[3, 4]")

        # This should be executed once for each set of arguments passed
        expect(target.with_positional_and_splat_args_counter).to eq(2)
      end

      it "memoizes methods with one keyword argument" do
        expect(Array.new(4) { target.with_one_keyword_arg(a: 1) }).to all eq("with_one_keyword_arg: a=1")
        expect(Array.new(4) { target.with_one_keyword_arg(a: 2) }).to all eq("with_one_keyword_arg: a=2")

        # This should be executed once for each set of arguments passed
        expect(target.with_one_keyword_arg_counter).to eq(2)
      end

      it "memoizes methods with keyword arguments" do
        expect(Array.new(4) { target.with_keyword_args(a: 1, b: 2) }).to all eq("with_keyword_args: a=1, b=2")
        expect(Array.new(4) { target.with_keyword_args(a: 2, b: 3) }).to all eq("with_keyword_args: a=2, b=3")

        # This should be executed once for each set of arguments passed
        expect(target.with_keyword_args_counter).to eq(2)
      end

      it "memoizes methods with keyword and double-splat arguments" do
        expect(Array.new(4) { target.with_keyword_and_double_splat_args(a: 1, b: 2, c: 3) }).
          to all eq("with_keyword_and_double_splat_args: a=1, kwargs={:b=>2, :c=>3}")

        expect(Array.new(4) { target.with_keyword_and_double_splat_args(a: 1, b: 2, c: 4) }).
          to all eq("with_keyword_and_double_splat_args: a=1, kwargs={:b=>2, :c=>4}")

        # This should be executed once for each set of arguments passed
        expect(target.with_keyword_and_double_splat_args_counter).to eq(2)
      end

      it "memoizes methods with positional and keyword arguments" do
        expect(Array.new(4) { target.with_positional_and_keyword_args(1, b: 2) }).
          to all eq("with_positional_and_keyword_args: a=1, b=2")

        expect(Array.new(4) { target.with_positional_and_keyword_args(2, b: 3) }).
          to all eq("with_positional_and_keyword_args: a=2, b=3")

        # This should be executed once for each set of arguments passed
        expect(target.with_positional_and_keyword_args_counter).to eq(2)
      end

      it "memoizes methods with positional, splat, keyword, and double-splat arguments" do
        expect(Array.new(4) { target.with_positional_splat_keyword_and_double_splat_args(1, 2, 3, b: 4, c: 5, d: 6) }).
          to all eq("with_positional_splat_keyword_and_double_splat_args: a=1, args=[2, 3] b=4 kwargs={:c=>5, :d=>6}")

        expect(Array.new(4) { target.with_positional_splat_keyword_and_double_splat_args(1, 2, b: 4, c: 5) }).
          to all eq("with_positional_splat_keyword_and_double_splat_args: a=1, args=[2] b=4 kwargs={:c=>5}")

        # This should be executed once for each set of arguments passed
        expect(target.with_positional_splat_keyword_and_double_splat_args_counter).to eq(2)
      end

      it "memoizes methods with special characters in the name" do
        expect(Array.new(4) { target.special_chars? }).to all eq("special_chars?")
        expect(target.special_chars_counter).to eq(1)
      end

      it "memoizes methods set to false values" do
        expect(Array.new(4) { target.false_method }).to all eq(false)
        expect(target.false_method_counter).to eq(1)
      end

      it "memoizes methods set to nil values" do
        expect(Array.new(4) { target.nil_method }).to all eq(nil)
        expect(target.nil_method_counter).to eq(1)
      end

      context "with private methods" do
        it "keeps private methods private" do
          expect(target.private_methods).to include(:private_memowise_method)
        end

        it "memoizes private methods" do
          expect(Array.new(4) { target.send(:private_memowise_method) }).to all eq("private_memowise_method")
          expect(target.private_memowise_method_counter).to eq(1)
        end
      end

      context "with public methods" do
        it "keeps public methods public" do
          expect(target.public_methods).to include(:public_memowise_method)
        end

        it "memoizes public methods" do
          expect(Array.new(4) { target.public_memowise_method }).to all eq("public_memowise_method")
          expect(target.public_memowise_method_counter).to eq(1)
        end
      end

      it "memoizes methods with proc arguments" do
        proc_param = proc { true }
        expect(Array.new(4) { target.proc_method(proc_param) }).to all eq(true)

        expect(target.proc_method_counter).to eq(1)
      end

      it "will not memoize methods with implicit block arguments" do
        expect { target.implicit_block_method }.to raise_error(LocalJumpError)
      end

      it "will not memoize methods with explicit block arguments" do
        expect { target.explicit_block_method { nil } }.to raise_error(LocalJumpError)
      end
    end

    shared_examples "handles memoized/non-memoized methods with the same name at different "\
                    "scopes" do
      context "with non-memoized method with same name as memoized method" do
        context "when methods have no arguments" do
          it "does not memoize the non-memoized method" do
            # Confirm the memoized method works correctly first.
            expect(Array.new(4) { memoized.no_args }).to all eq("no_args")
            expect(memoized.no_args_counter).to eq(1)

            # Now confirm our non-memoized method is not memoized.
            expect(Array.new(4) { non_memoized.no_args }).to all eq("#{non_memoized_name}_no_args")
            expect(non_memoized.send("#{non_memoized_name}_no_args_counter")).to eq(4)
          end
        end

        context "when methods have one positional argument" do
          it "does not memoize the non-memoized method" do
            # Confirm the memoized method works correctly first.
            expect(Array.new(4) { memoized.with_one_positional_arg(1) }).to all eq("with_one_positional_arg: a=1")
            expect(memoized.with_one_positional_arg_counter).to eq(1)

            # Now confirm our non-memoized method is not memoized.
            expect(Array.new(4) { non_memoized.with_one_positional_arg(1) }).
              to all eq("#{non_memoized_name}_with_one_positional_arg: a=1")

            expect(non_memoized.send("#{non_memoized_name}_one_positional_arg_counter")).to eq(4)
          end
        end

        context "when methods have multiple positional arguments" do
          it "does not memoize the non-memoized method" do
            # Confirm the memoized method works correctly first.
            expect(Array.new(4) { memoized.with_positional_args(1, 2) }).to all eq("with_positional_args: a=1, b=2")
            expect(memoized.with_positional_args_counter).to eq(1)

            # Now confirm our non-memoized method is not memoized.
            expect(Array.new(4) { non_memoized.with_positional_args(1, 2) }).
              to all eq("#{non_memoized_name}_with_positional_args: a=1, b=2")

            expect(non_memoized.send("#{non_memoized_name}_positional_args_counter")).to eq(4)
          end
        end
      end
    end

    context "with instance methods" do
      include_context "with context for instance methods"

      # Use the instance as the target of "#memo_wise shared examples"
      let(:target) { instance }

      it_behaves_like "#memo_wise shared examples"

      it "does not memoize methods across instances" do
        instance2 = class_with_memo.new

        instance.no_args

        expect(instance.no_args_counter).to eq(1)
        expect(instance2.no_args_counter).to eq(0)
      end

      context "with protected methods" do
        it "keeps protected methods protected" do
          expect(target.protected_methods).to include(:protected_memowise_method)
        end

        it "memoizes protected methods" do
          expect(Array.new(4) { target.send(:protected_memowise_method) }).to all eq("protected_memowise_method")
          expect(target.protected_memowise_method_counter).to eq(1)
        end
      end

      context "when memo_wise has *not* been called on a *class* method" do
        it "does *not* create class-level instance variable" do
          expect(class_with_memo.instance_variables).not_to include(:@_memo_wise)
        end
      end

      # This test nondeterministically fails in JRuby with the following error:
      # NoMethodError:
      #   super: no superclass method `allocate' for #<Class:0xacc6a69>
      unless RUBY_PLATFORM == "java"
        context "when instances are created with Class#allocate" do
          let(:instance) { class_with_memo.allocate }

          it "memoizes correctly" do
            expect(Array.new(4) { instance.no_args }).to all eq("no_args")
            expect(instance.no_args_counter).to eq(1)
          end
        end
      end

      context "when the name of the method to memoize is not a symbol" do
        let(:class_with_memo) do
          super().tap { |klass| klass.memo_wise "no_args" }
        end

        it { expect { instance }.to raise_error(ArgumentError) }
      end

      it_behaves_like "handles memoized/non-memoized methods with the same name at different scopes" do
        let(:memoized) { instance }
        let(:non_memoized) { class_with_memo }
        let(:non_memoized_name) { :class }
      end

      context "when the class is a Value class using the 'values' gem" do
        let(:external_counter) { [0] }

        let(:increment_proc) { -> { external_counter[0] += 1 } }

        let(:value_class) do
          Value.new(:increment_proc) do
            prepend MemoWise # rubocop:disable RSpec/DescribedClass

            def no_args
              increment_proc.call
              "no_args"
            end
            memo_wise :no_args
          end
        end

        let(:value_instance) { value_class.new(increment_proc) }

        it "memoizes methods" do
          expect(Array.new(4) { value_instance.no_args }).to all eq("no_args")
          expect(external_counter[0]).to eq(1)
        end
      end

      context "when the class has a child class" do
        let(:child_class) do
          Class.new(class_with_memo) do
            def child_method_counter
              @child_method_counter || 0
            end

            def child_method
              @child_method_counter = child_method_counter + 1
              "child_method"
            end
          end
        end

        let(:instance) { child_class.new }

        it "memoizes the parent methods" do
          expect(Array.new(4) { instance.no_args }).to all eq("no_args")
          expect(instance.no_args_counter).to eq(1)
          expect(Array.new(4) { instance.child_method }).to all eq("child_method")
          expect(instance.child_method_counter).to eq(4)
        end

        context "when the child class also memoizes methods" do
          before :each do
            child_class.prepend described_class
            child_class.memo_wise :child_method
          end

          it "memoizes the parent and child methods separately" do
            expect(Array.new(4) { instance.no_args }).to all eq("no_args")
            expect(instance.no_args_counter).to eq(1)
            expect(Array.new(4) { instance.child_method }).to all eq("child_method")
            expect(instance.child_method_counter).to eq(1)
          end
        end
      end

      context "when the class inherits memoization from multiple modules" do
        let(:module1) do
          Module.new do
            prepend MemoWise

            def module1_method_counter
              @module1_method_counter || 0 # rubocop:disable RSpec/InstanceVariable
            end

            def module1_method
              @module1_method_counter = module1_method_counter + 1
              "module1_method"
            end
            memo_wise :module1_method
          end
        end

        let(:module2) do
          Module.new do
            prepend MemoWise

            def module2_method_counter
              @module2_method_counter || 0 # rubocop:disable RSpec/InstanceVariable
            end

            def module2_method
              @module2_method_counter = module2_method_counter + 1
              "module2_method"
            end
            memo_wise :module2_method
          end
        end

        let(:klass) do
          Class.new do
            include Module1, Module2
          end
        end

        let(:instance) { klass.new }

        before(:each) do
          stub_const("Module1", module1)
          stub_const("Module2", module2)
        end

        it "memoizes inherited methods separately" do
          expect(Array.new(4) { instance.module1_method }).to all eq("module1_method")
          expect(instance.module1_method_counter).to eq(1)
          expect(Array.new(4) { instance.module2_method }).to all eq("module2_method")
          expect(instance.module2_method_counter).to eq(1)
        end
      end

      context "when the class, its superclass, and its module all memoize methods" do
        let(:parent_class) do
          Class.new do
            prepend MemoWise

            def parent_class_method_counter
              @parent_class_method_counter || 0
            end

            def parent_class_method
              @parent_class_method_counter = parent_class_method_counter + 1
              "parent_class_method"
            end
            memo_wise :parent_class_method
          end
        end

        let(:module1) do
          Module.new do
            prepend MemoWise

            def module1_method_counter
              @module1_method_counter || 0 # rubocop:disable RSpec/InstanceVariable
            end

            def module1_method
              @module1_method_counter = module1_method_counter + 1
              "module1_method"
            end
            memo_wise :module1_method
          end
        end

        let(:child_class) do
          Class.new(parent_class) do
            include Module1

            def child_class_method_counter
              @child_class_method_counter || 0
            end

            def child_class_method
              @child_class_method_counter = child_class_method_counter + 1
              "child_class_method"
            end
            memo_wise :child_class_method
          end
        end

        let(:instance) { child_class.new }

        before(:each) do
          stub_const("Module1", module1)
        end

        it "memoizes inherited methods separately" do
          expect(Array.new(4) { instance.parent_class_method }).to all eq("parent_class_method")
          expect(instance.parent_class_method_counter).to eq(1)
          expect(Array.new(4) { instance.module1_method }).to all eq("module1_method")
          expect(instance.module1_method_counter).to eq(1)
          expect(Array.new(4) { instance.child_class_method }).to all eq("child_class_method")
          expect(instance.child_class_method_counter).to eq(1)
        end
      end
    end

    # These test cases would fail due to a JRuby bug
    # Skipping to make build pass until the bug is fixed
    # https://github.com/jruby/jruby/issues/6896
    unless RUBY_PLATFORM == "java"
      context "with class methods" do
        context "when defined with 'def self.'" do
          include_context "with context for class methods via 'def self.'"

          # Use the class as the target of "#memo_wise shared examples"
          let(:target) { class_with_memo }

          it_behaves_like "#memo_wise shared examples"

          it "creates a class-level instance variable" do
            # NOTE: test implementation detail to ensure the inverse test is valid
            expect(class_with_memo.instance_variables).to include(:@_memo_wise)
          end

          it_behaves_like "handles memoized/non-memoized methods with the same name at different scopes" do
            let(:memoized) { class_with_memo }
            let(:non_memoized) { class_with_memo.new }
            let(:non_memoized_name) { :instance }
          end

          context "when an invalid hash key is passed to .memo_wise" do
            let(:class_with_memo) do
              Class.new do
                prepend MemoWise

                def self.class_method; end
              end
            end

            it "raises an error when passing a key which is not `self:`" do
              expect { class_with_memo.send(:memo_wise, bad_key: :class_method) }.
                to raise_error(ArgumentError, "`:self` is the only key allowed in memo_wise")
            end
          end

          context "when the class has a child class" do
            let(:child_class) do
              Class.new(class_with_memo) do
                def self.child_method_counter
                  @child_method_counter || 0
                end

                def self.child_method
                  @child_method_counter = child_method_counter + 1
                  "child_method"
                end
              end
            end

            it "memoizes the parent methods" do
              expect(Array.new(4) { child_class.no_args }).to all eq("no_args")
              expect(child_class.no_args_counter).to eq(1)
              expect(Array.new(4) { child_class.child_method }).to all eq("child_method")
              expect(child_class.child_method_counter).to eq(4)
            end

            context "when the child class also memoizes methods" do
              before :each do
                child_class.prepend described_class
                child_class.memo_wise self: :child_method
              end

              it "memoizes the parent and child methods separately" do
                expect(Array.new(4) { child_class.no_args }).to all eq("no_args")
                expect(child_class.no_args_counter).to eq(1)
                expect(Array.new(4) { child_class.child_method }).to all eq("child_method")
                expect(child_class.child_method_counter).to eq(1)
              end
            end
          end
        end

        context "when defined with scope 'class << self'" do
          include_context "with context for class methods via scope 'class << self'"

          # Use the class as the target of "#memo_wise shared examples"
          let(:target) { class_with_memo }

          it_behaves_like "#memo_wise shared examples"

          it "creates a class-level instance variable" do
            # NOTE: this test ensure the inverse test above continues to be valid
            expect(class_with_memo.instance_variables).to include(:@_memo_wise)
          end

          it_behaves_like "handles memoized/non-memoized methods with the same name at different scopes" do
            let(:memoized) { class_with_memo }
            let(:non_memoized) { class_with_memo.new }
            let(:non_memoized_name) { :instance }
          end

          context "when the class has a child class" do
            let(:child_class) do
              Class.new(class_with_memo) do
                class << self
                  def child_method_counter
                    @child_method_counter || 0
                  end

                  def child_method
                    @child_method_counter = child_method_counter + 1
                    "child_method"
                  end
                end
              end
            end

            it "memoizes the parent methods" do
              expect(Array.new(4) { child_class.no_args }).to all eq("no_args")
              expect(child_class.no_args_counter).to eq(1)
              expect(Array.new(4) { child_class.child_method }).to all eq("child_method")
              expect(child_class.child_method_counter).to eq(4)
            end

            context "when the child class also memoizes methods" do
              let(:child_class) do
                Class.new(class_with_memo) do
                  class << self
                    prepend MemoWise

                    def child_method_counter
                      @child_method_counter || 0
                    end

                    def child_method
                      @child_method_counter = child_method_counter + 1
                      "child_method"
                    end
                    memo_wise :child_method
                  end
                end
              end

              it "memoizes the parent and child methods separately" do
                expect(Array.new(4) { child_class.no_args }).to all eq("no_args")
                expect(child_class.no_args_counter).to eq(1)
                expect(Array.new(4) { child_class.child_method }).to all eq("child_method")
                expect(child_class.child_method_counter).to eq(1)
              end
            end
          end
        end
      end
    end

    # These test cases would fail due to a JRuby bug
    # Skipping to make build pass until the bug is fixed
    # https://github.com/jruby/jruby/issues/6896
    unless RUBY_PLATFORM == "java"
      context "with module methods" do
        context "when defined with 'def self.'" do
          include_context "with context for module methods via 'def self.'"

          # Use the module as the target of "#memo_wise shared examples"
          let(:target) { module_with_memo }

          it_behaves_like "#memo_wise shared examples"

          it "creates a module-level instance variable" do
            # NOTE: test implementation detail to ensure the inverse test is valid
            expect(module_with_memo.instance_variables).to include(:@_memo_wise)
          end

          context "when an invalid hash key is passed to .memo_wise" do
            let(:module_with_memo) do
              Module.new do
                prepend MemoWise

                def self.module_method; end
              end
            end

            it "raises an error when passing a key which is not `self:`" do
              expect { module_with_memo.send(:memo_wise, bad_key: :module_method) }.
                to raise_error(ArgumentError, "`:self` is the only key allowed in memo_wise")
            end
          end
        end

        context "when defined with scope 'module << self'" do
          include_context "with context for module methods via scope 'class << self'"

          # Use the module as the target of "#memo_wise shared examples"
          let(:target) { module_with_memo }

          it_behaves_like "#memo_wise shared examples"

          it "creates a module-level instance variable" do
            # NOTE: this test ensure the inverse test above continues to be valid
            expect(module_with_memo.instance_variables).to include(:@_memo_wise)
          end
        end
      end
    end

    context "with module mixed into other classes" do
      context "when extended" do
        context "when defined with 'def'" do
          include_context "with context for module methods via normal scope"

          before(:each) { stub_const("ModuleWithMemo", module_with_memo) }

          let(:class_extending_module_with_memo) do
            Class.new do
              extend ModuleWithMemo
            end
          end

          let(:target) { class_extending_module_with_memo }

          it_behaves_like "#memo_wise shared examples"
        end

        context "when 1 module extended by 2 classes" do
          let(:module_with_memo) do
            Module.new do
              prepend MemoWise

              def test_method
                Random.rand
              end
              memo_wise :test_method
            end
          end
          let(:class_a_extending_module_with_memo) do
            Class.new do
              extend ModuleWithMemo
            end
          end
          let(:class_b_extending_module_with_memo) do
            Class.new do
              extend ModuleWithMemo
            end
          end

          before(:each) do
            stub_const("ModuleWithMemo", module_with_memo)
          end

          it "memoizes each extended class separately" do
            aggregate_failures do
              expect(class_a_extending_module_with_memo.test_method). # rubocop:disable RSpec/IdenticalEqualityAssertion
                to eq(class_a_extending_module_with_memo.test_method)
              expect(class_b_extending_module_with_memo.test_method). # rubocop:disable RSpec/IdenticalEqualityAssertion
                to eq(class_b_extending_module_with_memo.test_method)
              expect(class_a_extending_module_with_memo.test_method).
                to_not eq(class_b_extending_module_with_memo.test_method)
            end
          end
        end
      end

      context "when included" do
        context "when defined with 'def'" do
          include_context "with context for module methods via normal scope"

          before(:each) do
            stub_const("ModuleWithMemo", module_with_memo)
          end

          let(:class_including_module_with_memo) do
            Class.new do
              include ModuleWithMemo
            end
          end
          let(:instance) { class_including_module_with_memo.new }

          let(:target) { instance }

          it_behaves_like "#memo_wise shared examples"

          it_behaves_like "handles memoized/non-memoized methods with the same name at different scopes" do
            let(:memoized) { instance }
            let(:non_memoized) { module_with_memo }
            let(:non_memoized_name) { :module }
          end
        end

        # These test cases would fail due to a JRuby bug
        # Skipping to make build pass until the bug is fixed
        unless RUBY_PLATFORM == "java"
          context "when defined with 'def self.' and 'def'" do
            let(:module_with_memo) do
              Module.new do
                prepend MemoWise

                def self.test_method
                  Random.rand
                end
                memo_wise self: :test_method

                def test_method
                  Random.rand
                end
                memo_wise :test_method
              end
            end
            let(:class_including_module_with_memo) do
              Class.new do
                include ModuleWithMemo
              end
            end
            let(:instance) { class_including_module_with_memo.new }

            before(:each) do
              stub_const("ModuleWithMemo", module_with_memo)
            end

            it "memoizes instance and singleton methods separately" do
              aggregate_failures do
                expect(instance.test_method).to eq(instance.test_method) # rubocop:disable RSpec/IdenticalEqualityAssertion
                expect(module_with_memo.test_method).to eq(module_with_memo.test_method) # rubocop:disable RSpec/IdenticalEqualityAssertion
                expect(instance.test_method).to_not eq(module_with_memo.test_method)
              end
            end
          end
        end
      end

      context "when prepended" do
        context "when defined with 'def'" do
          include_context "with context for module methods via normal scope"

          before(:each) do
            stub_const("ModuleWithMemo", module_with_memo)
          end

          let(:class_prepending_module_with_memo) do
            Class.new do
              prepend ModuleWithMemo
            end
          end
          let(:instance) { class_prepending_module_with_memo.new }

          let(:target) { instance }

          it_behaves_like "#memo_wise shared examples"

          it_behaves_like "handles memoized/non-memoized methods with the same name at different scopes" do
            let(:memoized) { instance }
            let(:non_memoized) { module_with_memo }
            let(:non_memoized_name) { :module }
          end
        end
      end
    end
  end
end