summaryrefslogtreecommitdiff
path: root/spec/commands/show_source_spec.rb
blob: e6b8acffe7fb72c40f6653b91a6d87e2c1bac62a (plain)
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
# frozen_string_literal: true

describe "show-source" do # rubocop:disable Metrics/BlockLength
  def define_persistent_class(file, class_body)
    file.puts(class_body)
    file.close
    require(file.path)
  end

  before do
    @o = Object.new
    def @o.sample_method
      :sample
    end
    Object.remove_const :Test if Object.const_defined? :Test
    Object.const_set(:Test, Module.new)
  end

  after do
    Pad.clear
  end

  it "should output a method's source" do
    expect(pry_eval(binding, 'show-source @o.sample_method')).to match(/def @o.sample/)
  end

  it "should output help" do
    expect(pry_eval('show-source -h')).to match(/Usage:\s+show-source/)
  end

  it "should output a method's source with line numbers" do
    expect(pry_eval(binding, 'show-source -l @o.sample_method'))
      .to match(/\d+: def @o.sample/)
  end

  it "should output a method's source with line numbers starting at 1" do
    expect(pry_eval(binding, 'show-source -b @o.sample_method'))
      .to match(/1: def @o.sample/)
  end

  it "should output a method's source if inside method and no name given" do
    def @o.sample
      pry_eval(binding, 'show-source')
    end
    docs = @o.sample
    expect(docs).to match(/def @o.sample/)
  end

  it "should output a method's source inside method using the -l switch" do
    def @o.sample
      pry_eval(binding, 'show-source -l')
    end
    docs = @o.sample
    expect(docs).to match(/def @o.sample/)
  end

  it "should find methods even if there are spaces in the arguments" do
    def @o.foo(*_bars)
      @foo = "Mr flibble"
      self
    end

    out = pry_eval(binding, "show-source @o.foo('bar', 'baz bam').foo")
    expect(out).to match(/Mr flibble/)
  end

  it "should find methods even if the object overrides method method" do
    _c = Class.new do
      def method
        98
      end
    end

    expect(pry_eval(binding, "show-source _c.new.method")).to match(/98/)
  end

  it "should not show the source when a non-extant method is requested" do
    _c = Class.new do
      def method
        98
      end
    end
    expect(mock_pry(binding, "show-source _c#wrongmethod"))
      .to match(/Couldn't locate/)
  end

  it "doesn't show the source and deliver an error message without exclamation point" do
    _c = Class.new
    error_message = "Error: Couldn't locate a definition for _c#wrongmethod\n"
    expect(mock_pry(binding, "show-source _c#wrongmethod")).to eq(error_message)
  end

  it "should find instance_methods if the class overrides instance_method" do
    _c = Class.new do
      def method
        98
      end

      def self.instance_method
        789
      end
    end

    expect(pry_eval(binding, "show-source _c#method")).to match(/98/)
  end

  it "should find instance methods with self#moo" do
    _c = Class.new do
      def moo
        "ve over!"
      end
    end

    expect(pry_eval(binding, "cd _c", "show-source self#moo")).to match(/ve over/)
  end

  it "should not find instance methods with self.moo" do
    _c = Class.new do
      def moo
        "ve over!"
      end
    end

    expect { pry_eval(binding, 'cd _c', 'show-source self.moo') }
      .to raise_error(Pry::CommandError, /Couldn't locate/)
  end

  it "should find normal methods with self.moo" do
    _c = Class.new do
      def self.moo
        "ve over!"
      end
    end

    expect(pry_eval(binding, 'cd _c', 'show-source self.moo')).to match(/ve over/)
  end

  it "should not find normal methods with self#moo" do
    _c = Class.new do
      def self.moo
        "ve over!"
      end
    end

    expect { pry_eval(binding, 'cd _c', 'show-source self#moo') }
      .to raise_error(Pry::CommandError, /Couldn't locate/)
  end

  it "should find normal methods (i.e non-instance methods) by default" do
    _c = Class.new do
      def self.moo
        "ve over!"
      end
    end

    expect(pry_eval(binding, "cd _c", "show-source moo")).to match(/ve over/)
  end

  it "should find instance methods if no normal methods available" do
    _c = Class.new do
      def moo
        "ve over!"
      end
    end

    expect(pry_eval(binding, "cd _c", "show-source moo")).to match(/ve over/)
  end

  describe "with -e option" do
    before do
      class FooBar
        def bar
          :bar
        end
      end
    end

    after do
      Object.remove_const(:FooBar)
    end

    it "shows the source code for the returned value as Ruby" do
      ReplTester.start target: binding do
        input 'show-source -e FooBar.new'
        output(/class FooBar/)
      end
    end
  end

  it "should raise a CommandError when super method doesn't exist" do
    def @o.foo(*bars); end

    expect { pry_eval(binding, "show-source --super @o.foo") }
      .to raise_error(Pry::CommandError, /No superclass found/)
  end

  it "should output the source of a method defined inside Pry" do
    out = pry_eval("def dyn_method\n:test\nend", 'show-source dyn_method')
    expect(out).to match(/def dyn_method/)
    Object.remove_method :dyn_method
  end

  it 'should output source for an instance method defined inside pry' do
    pry_tester.tap do |t|
      t.eval "class Test::A\n  def yo\n  end\nend"
      expect(t.eval('show-source Test::A#yo')).to match(/def yo/)
    end
  end

  it 'should output source for a repl method defined using define_method' do
    pry_tester.tap do |t|
      t.eval "class Test::A\n  define_method(:yup) {}\nend"
      expect(t.eval('show-source Test::A#yup')).to match(/define_method\(:yup\)/)
    end
  end

  it "should output the source of a command defined inside Pry" do
    command_definition = %(
      Pry.config.commands.command "hubba-hubba" do
        puts "that's what she said!"
      end
    )
    out = pry_eval(command_definition, 'show-source hubba-hubba')
    expect(out).to match(/what she said/)
    Pry.config.commands.delete "hubba-hubba"
  end

  context "when there's no source code but the comment exists" do
    before do
      class Foo
        # Bingo.
        def bar; end
      end

      allow_any_instance_of(Pry::Method).to receive(:source).and_return(nil)
    end

    after do
      Object.remove_const(:Foo)
    end

    it "outputs zero line numbers" do
      out = pry_eval('show-source Foo#bar')
      expect(out).to match(/
        Owner:\sFoo
        .+
        Number\sof\slines:\s0
        .+
        \*\*\sWarning:\sCannot\sfind\scode\sfor\s'bar'\s\(source_location\sis\snil\)
      /mx)
    end
  end

  describe "finding super methods with help of `--super` switch" do
    before do
      class Foo
        def foo(*_bars)
          :super_wibble
        end
      end
    end

    after do
      Object.remove_const(:Foo)
    end

    it "finds super methods with explicit method argument" do
      o = Foo.new
      def o.foo(*_bars)
        :wibble
      end

      expect(pry_eval(binding, "show-source --super o.foo")).to match(/:super_wibble/)
    end

    it "finds super methods without explicit method argument" do
      o = Foo.new
      def o.foo(*bars)
        @foo = :wibble
        pry_eval(binding, 'show-source --super')
      end

      expect(o.foo).to match(/:super_wibble/)
    end

    it "finds super methods with multiple --super " do
      o = Foo.new

      o.extend(
        Module.new do
          def foo
            :nibble
          end
        end
      )

      def o.foo(*bars)
        @foo = :wibble
        pry_eval(binding, 'show-source --super --super')
      end

      expect(o.foo).to match(/:super_wibble/)
    end
  end

  describe "on sourcable objects" do
    it "should output source defined inside pry" do
      pry_tester.tap do |t|
        t.eval "hello = proc { puts 'hello world!' }"
        expect(t.eval("show-source hello")).to match(/proc \{ puts/)
      end
    end

    it "should output source for procs/lambdas stored in variables" do
      _hello = proc { puts 'hello world!' }
      expect(pry_eval(binding, 'show-source _hello')).to match(/proc \{ puts/)
    end

    it "should output source for procs/lambdas stored in constants" do
      HELLO = proc { puts 'hello world!' }
      expect(pry_eval(binding, "show-source HELLO")).to match(/proc \{ puts/)
      Object.remove_const(:HELLO)
    end

    it "should output source for method objects" do
      def @o.hi
        puts 'hi world'
      end
      _meth = @o.method(:hi)
      expect(pry_eval(binding, "show-source _meth")).to match(/puts 'hi world'/)
    end

    describe "on variables that shadow methods" do
      before do
        @t = pry_tester.eval(unindent(<<-SHADOWED_VAR))
          class ::TestHost
            def hello
              hello = proc { ' smile ' }
              _foo = hello
              pry_tester(binding)
            end
          end
          ::TestHost.new.hello
        SHADOWED_VAR
      end

      after { Object.remove_const(:TestHost) }

      it "source of variable takes precedence over method that is being shadowed" do
        source = @t.eval('show-source hello')
        expect(source).not_to match(/def hello/)
        expect(source).to match(/proc \{ ' smile ' \}/)
      end

      it "source of method being shadowed should take precedence over variable
          if given self.meth_name syntax" do
        expect(@t.eval('show-source self.hello')).to match(/def hello/)
      end
    end
  end

  describe "on variable or constant" do
    before do
      class TestHost
        def hello
          "hi there"
        end
      end
    end

    after { Object.remove_const(:TestHost) }

    it "outputs source of its class if variable doesn't respond to source_location" do
      _test_host = TestHost.new
      expect(pry_eval(binding, 'show-source _test_host'))
        .to match(/class TestHost\n.*def hello/)
    end

    it "outputs source of its class if constant doesn't respond to source_location" do
      TEST_HOST = TestHost.new
      expect(pry_eval(binding, 'show-source TEST_HOST'))
        .to match(/class TestHost\n.*def hello/)
      Object.remove_const(:TEST_HOST)
    end
  end

  describe "on modules" do
    before do
      class ShowSourceTestSuperClass
        def alpha; end
      end

      class ShowSourceTestClass < ShowSourceTestSuperClass
        def alpha; end
      end

      module ShowSourceTestSuperModule
        def alpha; end
      end

      module ShowSourceTestModule
        include ShowSourceTestSuperModule
        def alpha; end
      end

      ShowSourceTestClassWeirdSyntax = Class.new do
        def beta; end
      end

      ShowSourceTestModuleWeirdSyntax = Module.new do
        def beta; end
      end
    end

    after do
      Object.remove_const :ShowSourceTestSuperClass
      Object.remove_const :ShowSourceTestClass
      Object.remove_const :ShowSourceTestClassWeirdSyntax
      Object.remove_const :ShowSourceTestSuperModule
      Object.remove_const :ShowSourceTestModule
      Object.remove_const :ShowSourceTestModuleWeirdSyntax
    end

    describe "basic functionality, should find top-level module definitions" do
      it 'should show source for a class' do
        expect(pry_eval('show-source ShowSourceTestClass'))
          .to match(/class ShowSourceTestClass.*?def alpha/m)
      end

      it 'should show source for a super class' do
        expect(pry_eval('show-source -s ShowSourceTestClass'))
          .to match(/class ShowSourceTestSuperClass.*?def alpha/m)
      end

      it 'should show source for a module' do
        expect(pry_eval('show-source ShowSourceTestModule'))
          .to match(/module ShowSourceTestModule/)
      end

      it 'should show source for an ancestor module' do
        expect(pry_eval('show-source -s ShowSourceTestModule'))
          .to match(/module ShowSourceTestSuperModule/)
      end

      it 'should show source for a class when Const = Class.new syntax is used' do
        expect(pry_eval('show-source ShowSourceTestClassWeirdSyntax'))
          .to match(/ShowSourceTestClassWeirdSyntax = Class.new/)
      end

      it 'should show source for a super class when Const = Class.new syntax is used' do
        expect(pry_eval('show-source -s ShowSourceTestClassWeirdSyntax'))
          .to match(/class Object/)
      end

      it 'should show source for a module when Const = Module.new syntax is used' do
        expect(pry_eval('show-source ShowSourceTestModuleWeirdSyntax'))
          .to match(/ShowSourceTestModuleWeirdSyntax = Module.new/)
      end
    end

    before do
      pry_eval(unindent(<<-CLASSES))
        class Dog
          def woof
          end
        end

        class TobinaMyDog < Dog
          def woof
          end
        end
      CLASSES
    end

    after do
      Object.remove_const :Dog
      Object.remove_const :TobinaMyDog
    end

    describe "in REPL" do
      it 'should find class defined in repl' do
        expect(pry_eval('show-source TobinaMyDog')).to match(/class TobinaMyDog/)
      end

      it 'should find superclass defined in repl' do
        expect(pry_eval('show-source -s TobinaMyDog')).to match(/class Dog/)
      end
    end

    it 'should lookup module name with respect to current context' do
      temporary_constants(:AlphaClass, :BetaClass) do
        class BetaClass
          def alpha; end
        end

        class AlphaClass
          class BetaClass
            def beta; end
          end
        end

        expect(pry_eval(AlphaClass, 'show-source BetaClass')).to match(/def beta/)
      end
    end

    it 'should lookup nested modules' do
      temporary_constants(:AlphaClass) do
        class AlphaClass
          class BetaClass
            def beta; end
          end
        end

        expect(pry_eval('show-source AlphaClass::BetaClass')).to match(/class Beta/)
      end
    end

    # note that pry assumes a class is only monkey-patched at most
    # ONCE per file, so will not find multiple monkeypatches in the
    # SAME file.
    describe "show-source -a" do
      let(:tempfile) { Tempfile.new(%w[pry .rb]) }

      context "when there are instance method monkeypatches in different files" do
        before do
          define_persistent_class(tempfile, <<-CLASS)
            class TestClass
              def alpha; end
            end
          CLASS

          class TestClass
            def beta; end
          end
        end

        after do
          Object.remove_const(:TestClass)
          tempfile.unlink
        end

        it "shows the source for all monkeypatches" do
          result = pry_eval('show-source TestClass -a')
          expect(result).to match(/def alpha/)
          expect(result).to match(/def beta/)
        end
      end

      context "when there are class method monkeypatches in different files" do
        before do
          define_persistent_class(tempfile, <<-CLASS)
            class TestClass
              def self.alpha; end
            end
          CLASS

          class TestClass
            def self.beta; end
          end
        end

        after do
          Object.remove_const(:TestClass)
          tempfile.unlink
        end

        it "shows the source for all monkeypatches" do
          result = pry_eval('show-source TestClass -a')
          expect(result).to match(/def self.alpha/)
          expect(result).to match(/def self.beta/)
        end
      end

      context "when there are class-eval monkeypatches in different files" do
        let(:tempfile) { Tempfile.new(%w[pry .rb]) }

        before do
          define_persistent_class(tempfile, <<-CLASS)
            class TestClass
              def self.alpha; end
            end
          CLASS

          TestClass.class_eval do
            def class_eval_method
              :bing
            end
          end
        end

        after do
          Object.remove_const(:TestClass)
          tempfile.unlink
        end

        it "shows the source for all monkeypatches" do
          result = pry_eval('show-source TestClass -a')
          expect(result).to match(/def class_eval_method/)
        end

        it "ignores -a because object is not a module" do
          result = pry_eval('show-source TestClass#class_eval_method -a')
          expect(result).to match(/bing/)
        end
      end

      context "when there are instance-eval monkeypatches in different files" do
        let(:tempfile) { Tempfile.new(%w[pry .rb]) }

        before do
          define_persistent_class(tempfile, <<-CLASS)
            class TestClass
              def self.alpha; end
            end
          CLASS

          TestClass.instance_eval do
            def instance_eval_method
              :bing
            end
          end
        end

        after do
          Object.remove_const(:TestClass)
          tempfile.unlink
        end

        it "shows the source for all monkeypatches" do
          result = pry_eval('show-source TestClass -a')
          expect(result).to match(/def instance_eval_method/)
        end
      end

      context "when -a is not used and there are multiple monkeypatches" do
        let(:tempfile) { Tempfile.new(%w[pry .rb]) }

        before do
          define_persistent_class(tempfile, <<-CLASS)
            class TestClass
              def self.alpha; end
            end
          CLASS

          class TestClass
            def beta; end
          end
        end

        after do
          Object.remove_const(:TestClass)
          tempfile.unlink
        end

        it "mentions available monkeypatches" do
          result = pry_eval('show-source TestClass')
          expect(result).to match(/available monkeypatches/)
        end
      end

      context "when -a is not used and there's only one candidate for the class" do
        before do
          # alpha
          class TestClass
            def alpha; end
          end
        end

        after do
          Object.remove_const(:TestClass)
        end

        it "doesn't mention anything about monkeypatches" do
          result = pry_eval('show-source TestClass')
          expect(result).not_to match(/available monkeypatches/)
        end
      end
    end

    describe "when show-source is invoked without a method or class argument" do
      before do
        module TestHost
          class M
            def alpha; end

            def beta; end
          end

          module C
          end

          module D
            def self.invoked_in_method
              pry_eval(binding, 'show-source')
            end
          end
        end
      end

      after do
        Object.remove_const(:TestHost)
      end

      describe "inside a module" do
        it 'should display module source by default' do
          out = pry_eval(TestHost::M, 'show-source')
          expect(out).to match(/class M/)
          expect(out).to match(/def alpha/)
          expect(out).to match(/def beta/)
        end

        it 'should be unable to find module source if no methods defined' do
          expect { pry_eval(TestHost::C, 'show-source') }
            .to raise_error(Pry::CommandError, /Couldn't locate/)
        end

        it(
          'displays method code (rather than class) if Pry started inside ' \
          'method binding'
        ) do
          out = TestHost::D.invoked_in_method
          expect(out).to match(/invoked_in_method/)
          expect(out).not_to match(/module D/)
        end

        it 'should display class source when inside instance' do
          out = pry_eval(TestHost::M.new, 'show-source')
          expect(out).to match(/class M/)
          expect(out).to match(/def alpha/)
          expect(out).to match(/def beta/)
        end

        it 'should allow options to be passed' do
          out = pry_eval(TestHost::M, 'show-source -b')
          expect(out).to match(/\d:\s*class M/)
          expect(out).to match(/\d:\s*def alpha/)
          expect(out).to match(/\d:\s*def beta/)
        end

        describe "should skip over broken modules" do
          before do
            module BabyDuck
              module Muesli
                binding.eval("def a; end", "dummy.rb", 1)
                binding.eval("def b; end", "dummy.rb", 2)
                binding.eval("def c; end", "dummy.rb", 3)
              end

              module Muesli
                def d; end

                def e; end
              end
            end
          end

          after do
            Object.remove_const(:BabyDuck)
          end

          it 'should return source for first valid module' do
            out = pry_eval('show-source BabyDuck::Muesli')
            expect(out).to match(/def d; end/)
            expect(out).not_to match(/def a; end/)
          end
        end

        describe "monkey-patched C modules" do
          # Monkey-patch Array and add 15 methods, so its internal rank is
          # high enough to make this definition primary.
          class Array
            15.times do |i|
              define_method(:"doge#{i}") do
                :"doge#{i}"
              end
            end
          end

          describe "when current context is a C object" do
            it "should display a warning, and not monkey-patched definition" do
              out = pry_eval([1, 2, 3], 'show-source')
              expect(out).not_to match(/doge/)
              expect(out).to match(/Pry cannot display the information/)
            end

            it "recommends to use the --all switch when other candidates are found" do
              out = pry_eval([], 'show-source')
              expect(out).to match(/'--all' switch/i)
            end
          end

          describe "when current context is something other than a C object" do
            it "should display a candidate, not a warning" do
              out = pry_eval('show-source Array')
              expect(out).to match(/doge/)
              expect(out).not_to match(/warning/i)
            end
          end
        end
      end
    end
  end

  describe "on commands" do
    let(:default_commands) { Pry.config.commands }

    let(:command_set) do
      Pry::CommandSet.new { import Pry::Commands }
    end

    before { Pry.config.commands = command_set }
    after { Pry.config.commands = default_commands }

    describe "block commands" do
      it 'should show source for an ordinary command' do
        command_set.command('foo', :body_of_foo) {}
        expect(pry_eval(binding, 'show-source foo')).to match(/:body_of_foo/)
      end

      it "should output source of commands using special characters" do
        command_set.command('!%$', 'I gots the yellow fever') {}
        expect(pry_eval(binding, 'show-source !%$')).to match(/yellow fever/)
      end

      it 'should show source for a command with spaces in its name' do
        command_set.command('foo bar', :body_of_foo_bar) {}
        expect(pry_eval(binding, 'show-source foo bar')).to match(/:body_of_foo_bar/)
      end

      it 'should show source for a command by listing name' do
        command_set.command(/foo(.*)/, :body_of_foo_bar_regex, listing: "bar") {}
        expect(pry_eval(binding, 'show-source bar')).to match(/:body_of_foo_bar_regex/)
      end
    end

    describe "create_command commands" do
      it 'should show source for a command' do
        command_set.create_command "foo", "babble" do
          def process
            :body_of_foo
          end
        end
        expect(pry_eval(binding, 'show-source foo')).to match(/:body_of_foo/)
      end

      it 'should show source for a command defined inside pry' do
        pry_eval %{
          pry_instance.commands.create_command "foo", "babble" do
            def process() :body_of_foo end
          end
        }
        expect(pry_eval(binding, 'show-source foo')).to match(/:body_of_foo/)
      end
    end

    describe "real class-based commands" do
      before do
        # rubocop:disable Style/ClassAndModuleChildren
        class ::TemporaryCommand < Pry::ClassCommand
          match 'temp-command'
          def process
            :body_of_temp
          end
        end
        # rubocop:enable Style/ClassAndModuleChildren

        Pry.config.commands.add_command(::TemporaryCommand)
      end

      after do
        Object.remove_const(:TemporaryCommand)
      end

      it 'should show source for a command' do
        expect(pry_eval('show-source temp-command')).to match(/:body_of_temp/)
      end

      it 'should show source for a command defined inside pry' do
        pry_eval %{
          class ::TemporaryCommandInPry < Pry::ClassCommand
            match 'temp-command-in-pry'
            def process() :body_of_temp end
          end
        }
        Pry.config.commands.add_command(::TemporaryCommandInPry)
        expect(pry_eval('show-source temp-command-in-pry')).to match(/:body_of_temp/)
        Object.remove_const(:TemporaryCommandInPry)
      end
    end
  end

  describe "should set _file_ and _dir_" do
    let(:tempfile) { Tempfile.new(%w[pry .rb]) }

    before do
      define_persistent_class(tempfile, <<-CLASS)
        class TestClass
          def alpha; end
        end
      CLASS
    end

    after do
      Object.remove_const(:TestClass)
      tempfile.unlink
    end

    it 'should set _file_ and _dir_ to file containing method source' do
      t = pry_tester
      t.process_command "show-source TestClass#alpha"

      path = tempfile.path.split('/')[0..-2].join('/')
      expect(t.pry.last_dir).to match(path)

      expect(t.pry.last_file).to match(tempfile.path)
    end
  end

  describe "can't find class/module code" do
    describe "for classes" do
      before do
        module Jesus
          module Pig
            def lillybing
              :lillybing
            end
          end

          class Brian; end
          class Jingle
            def a
              :doink
            end
          end

          class Jangle < Jingle; include Pig; end
          class Bangle < Jangle; end
        end
      end

      after do
        Object.remove_const(:Jesus)
      end

      it 'shows superclass code' do
        t = pry_tester
        t.process_command "show-source Jesus::Jangle"
        expect(t.last_output).to match(/doink/)
      end

      it 'ignores included modules' do
        t = pry_tester
        t.process_command "show-source Jesus::Jangle"
        expect(t.last_output).not_to match(/lillybing/)
      end

      it 'errors when class has no superclass to show' do
        t = pry_tester
        expect { t.process_command "show-source Jesus::Brian" }
          .to raise_error(Pry::CommandError, /Couldn't locate/)
      end

      it 'shows warning when reverting to superclass code' do
        t = pry_tester
        t.process_command "show-source Jesus::Jangle"
        expect(t.last_output).to match(
          /Warning.*?Cannot find.*?Jesus::Jangle.*Showing.*Jesus::Jingle instead/
        )
      end

      it(
        'shows nth level superclass code (when no intermediary ' \
        'superclasses have code either)'
      ) do
        t = pry_tester
        t.process_command "show-source Jesus::Bangle"
        expect(t.last_output).to match(/doink/)
      end

      it 'shows correct warning when reverting to nth level superclass' do
        t = pry_tester
        t.process_command "show-source Jesus::Bangle"
        expect(t.last_output).to match(
          /Warning.*?Cannot find.*?Jesus::Bangle.*Showing.*Jesus::Jingle instead/
        )
      end
    end

    describe "for modules" do
      before do
        module Jesus
          module Alpha
            def alpha
              :alpha
            end
          end

          module Zeta; end

          module Beta
            include Alpha
          end

          module Gamma
            include Beta
          end
        end
      end

      after do
        Object.remove_const(:Jesus)
      end

      it 'shows included module code' do
        t = pry_tester
        t.process_command "show-source Jesus::Beta"
        expect(t.last_output).to match(/alpha/)
      end

      it 'shows warning when reverting to included module code' do
        t = pry_tester
        t.process_command "show-source Jesus::Beta"
        expect(t.last_output).to match(
          /Warning.*?Cannot find.*?Jesus::Beta.*Showing.*Jesus::Alpha instead/
        )
      end

      it 'errors when module has no included module to show' do
        t = pry_tester
        expect { t.process_command "show-source Jesus::Zeta" }
          .to raise_error(Pry::CommandError, /Couldn't locate/)
      end

      it(
        'shows nth level included module code (when no intermediary modules ' \
        'have code either)'
      ) do
        t = pry_tester
        t.process_command "show-source Jesus::Gamma"
        expect(t.last_output).to match(/alpha/)
      end

      it 'shows correct warning when reverting to nth level included module' do
        t = pry_tester
        t.process_command "show-source Jesus::Gamma"
        expect(t.last_output).to match(
          /Warning.*?Cannot find.*?Jesus::Gamma.*Showing.*Jesus::Alpha instead/
        )
      end
    end
  end

  describe "show-source --doc" do
    context "when given a class with a doc" do
      before do
        # Foo has docs.
        class Foo
          def bar; end
        end
      end

      after { Object.remove_const(:Foo) }

      it "shows documentation for the code object along with source code" do
        expect(pry_eval(binding, "show-source Foo -d")).to match(
          /Foo has docs\.\n\s+class Foo/
        )
      end
    end

    context "when given a module with a doc" do
      before do
        # TestMod has docs
        module TestMod
          def foo; end
        end
      end

      after { Object.remove_const(:TestMod) }

      it "shows documentation for the code object along with source code" do
        expect(pry_eval(binding, "show-source TestMod -d")).to match(
          /TestMod has docs\n\s+module TestMod/
        )
      end
    end

    context "when the Const = Class.new syntax is used" do
      before do
        # TestClass has docs
        TestClass = Class.new do
          def foo; end
        end
      end

      after { Object.remove_const(:TestClass) }

      it "shows documentation for the class" do
        expect(pry_eval(binding, "show-source TestClass -d")).to match(
          /TestClass has docs\n\s+TestClass = Class.new/
        )
      end
    end

    context "when the Const = Module.new syntax is used" do
      before do
        # TestMod has docs
        TestMod = Module.new do
          def foo; end
        end
      end

      after { Object.remove_const(:TestMod) }

      it "shows documentation for the module" do
        expect(pry_eval(binding, "show-source TestMod -d")).to match(
          /TestMod has docs\n\s+TestMod = Module.new/
        )
      end
    end

    context "when given a class defined in a REPL session" do
      after { Object.remove_const(:TobinaMyDog) }

      it "shows documentation for the class" do
        t = pry_tester
        t.eval <<-RUBY
          # hello tobina
          class TobinaMyDog
            def woof
            end
          end
        RUBY
        expect(t.eval('show-source -d TobinaMyDog')).to match(/hello tobina/)
      end
    end

    context "when the current context is a non-nested class" do
      before do
        # top-level beta
        class BetaClass
          def alpha; end
        end

        class AlphaClass
          # nested beta
          class BetaClass
            def beta; end
          end
        end
      end

      after do
        %i[BetaClass AlphaClass].each { |name| Object.remove_const(name) }
      end

      it "shows docs for the nested classes" do
        expect(pry_eval(AlphaClass, "show-source -d BetaClass"))
          .to match(/nested beta/)
      end
    end

    context "when given a nested class" do
      before do
        # top-level beta
        class BetaClass
          def alpha; end
        end

        class AlphaClass
          # nested beta
          class BetaClass
            def beta; end
          end
        end
      end

      after do
        %i[BetaClass AlphaClass].each { |name| Object.remove_const(name) }
      end

      it "shows docs for the nested classes" do
        expect(pry_eval(AlphaClass, "show-source -d AlphaClass::BetaClass"))
          .to match(/nested beta/)
      end
    end

    context "when given a method with a doc" do
      before do
        @obj = Object.new

        # test doc
        def @obj.test_method; end
      end

      it "finds the method's documentation" do
        expect(pry_eval(binding, "show-source -d @obj.test_method"))
          .to match(/test doc/)
      end
    end

    context "when #call is defined on Symbol" do
      before do
        class Symbol
          def call; end
        end

        @obj = Object.new

        # test doc
        def @obj.test_method; end
      end

      after { Symbol.class_eval { undef :call } }

      it "still finds documentation" do
        expect(pry_eval(binding, "show-source -d @obj.test_method"))
          .to match(/test doc/)
      end
    end

    context "when no docs can be found for the given class" do
      before do
        class TestClass
          def test_method; end
        end
      end

      after { Object.remove_const(:TestClass) }

      it "raises Pry::CommandError" do
        expect { pry_eval(binding, "show-source -d TestClass") }
          .to raise_error(Pry::CommandError)
      end
    end

    context "when no docs can be found for the given method" do
      before do
        @obj = Object.new
        def @obj.test_method; end
      end

      it "raises Pry::CommandError" do
        expect { pry_eval(binding, "show-source -d @obj.test_method") }
          .to raise_error(Pry::CommandError)
      end
    end

    context "when the --line-numbers switch is provided" do
      before do
        @obj = Object.new

        # test doc
        def @obj.test_method; end
      end

      it "outputs a method's docs with line numbers" do
        expect(pry_eval(binding, "show-source -d --line-numbers @obj.test_method"))
          .to match(/\d: test doc/)
      end
    end

    context "when the --base-one switch is provided" do
      before do
        @obj = Object.new

        # test doc
        def @obj.test_method; end
      end

      it "outputs a method's docs with line numbering starting at 1" do
        expect(pry_eval(binding, "show-source -d --base-one @obj.test_method"))
          .to match(/1: test doc/)
      end
    end

    context "when the current context is a method" do
      it "outputs the method without needing to use its name" do
        obj = Object.new

        # test method
        def obj.test_method
          pry_eval(binding, 'show-source -d')
        end

        expect(obj.test_method).to match(/test method/)
      end
    end

    context "when given a proc" do
      it "should show documentation for object" do
        # this is a documentation
        _the_proc = proc { puts 'hello world!' }

        expect(mock_pry(binding, "show-source -d _the_proc"))
          .to match(/this is a documentation/)
      end
    end

    context "when no class/module arg is given" do
      before do
        module TestHost
          # hello there froggy
          module M
            def d; end

            def e; end
          end
        end
      end

      after { Object.remove_const(:TestHost) }

      it "returns the doc for the current module" do
        expect(pry_eval(TestHost::M, 'show-source -d'))
          .to match(/hello there froggy/)
      end
    end

    context "when given a 'broken' module" do
      before do
        module TestHost
          # hello
          module M
            binding.eval("def a; end", "dummy.rb", 1)
            binding.eval("def b; end", "dummy.rb", 2)
            binding.eval("def c; end", "dummy.rb", 3)
          end

          # goodbye
          module M
            def d; end

            def e; end
          end
        end
      end

      after { Object.remove_const(:TestHost) }

      # FIXME: THis is nto a good spec anyway, because i dont think it
      # SHOULD skip!
      it "skips over the module" do
        output = pry_eval('show-source -d TestHost::M')
        expect(output).to match(/goodbye/)
        expect(output).not_to match(/hello/)
      end
    end

    describe "should set _file_ and _dir_" do
      let(:tempfile) { Tempfile.new(%w[pry .rb]) }

      before do
        define_persistent_class(tempfile, <<-CLASS)
          class TestClass
            # this is alpha
            def alpha; end
          end
        CLASS
      end

      after do
        Object.remove_const(:TestClass)
        tempfile.unlink
      end

      it "sets _file_ and _dir_ to file containing method source" do
        t = pry_tester
        t.process_command "show-source -d TestClass#alpha"

        path = tempfile.path.split('/')[0..-2].join('/')
        expect(t.pry.last_dir).to match(path)

        expect(t.pry.last_file).to match(tempfile.path)
      end
    end

    context "when provided a class without docs that has a superclass with docs" do
      before do
        # parent class
        class Parent
          def foo; end
        end

        class Child < Parent; end
      end

      after do
        %i[Child Parent].each { |name| Object.remove_const(name) }
      end

      it "shows the docs of the superclass" do
        expect(pry_eval(binding, 'show-source -d Child')).to match(/parent class/)
      end

      it "shows a warning about superclass reversion" do
        expect(pry_eval(binding, 'show-source -d Child')).to match(
          /Warning.*?Cannot find.*?Child.*Showing.*Parent instead/
        )
      end
    end

    context "when provided a class without docs that has nth superclass with docs" do
      before do
        # grandparent class
        class Grandparent
          def foo; end
        end

        class Parent < Grandparent; end
        class Child < Parent; end
      end

      after do
        %i[Grandparent Child Parent].each { |name| Object.remove_const(name) }
      end

      it "shows the docs of the superclass" do
        expect(pry_eval(binding, 'show-source -d Child'))
          .to match(/grandparent class/)
      end

      it "shows a warning about superclass reversion" do
        expect(pry_eval(binding, 'show-source -d Child')).to match(
          /Warning.*?Cannot find.*?Child.*Showing.*Grandparent instead/
        )
      end
    end

    context "when provided a class without docs that has a superclass without docs" do
      before do
        class Parent
          def foo; end
        end

        class Child < Parent; end
      end

      after do
        %i[Child Parent].each { |name| Object.remove_const(name) }
      end

      it "raises Pry::CommandError" do
        expect { pry_eval(binding, 'show-source -d Child') }
          .to raise_error(Pry::CommandError)
      end
    end

    context "when the module with docs was included in another module" do
      before do
        # mod module doc
        module Alpha
          def foo; end
        end

        module Beta
          include Alpha
        end
      end

      after do
        %i[Beta Alpha].each { |name| Object.remove_const(name) }
      end

      it "shows the included module's doc" do
        expect(pry_eval(binding, 'show-source -d Beta'))
          .to match(/mod module doc/)
      end

      it "shows a warning about the included module reversion" do
        expect(pry_eval(binding, 'show-source -d Beta')).to match(
          /Warning.*?Cannot find.*?Beta.*Showing.*Alpha instead/
        )
      end
    end

    context "when both the base mod and the included module have no docs" do
      before do
        module Alpha
          def foo; end
        end

        module Beta
          include Alpha
        end
      end

      after do
        %i[Beta Alpha].each { |name| Object.remove_const(name) }
      end

      it "raises Pry::CommandError" do
        expect { pry_eval(binding, 'show-source -d Beta') }
          .to raise_error(Pry::CommandError)
      end
    end

    context "when included module has docs and there are intermediary docless modules" do
      before do
        # alpha doc
        module Alpha
          def alpha; end
        end

        module Beta
          include Alpha
        end

        module Gamma
          include Beta
        end
      end

      after do
        %i[Gamma Beta Alpha].each { |name| Object.remove_const(name) }
      end

      it "shows nth level included module doc" do
        expect(pry_eval(binding, 'show-source -d Gamma')).to match(/alpha doc/)
      end

      it "shows a warning about module reversion" do
        expect(pry_eval(binding, 'show-source -d Gamma')).to match(
          /Warning.*?Cannot find.*?Gamma.*Showing.*Alpha instead/
        )
      end
    end

    context "when the --super switch is provided" do
      before do
        class Grandparent
          # grandparent init
          def initialize; end
        end

        class Parent < Grandparent
          # parent init
          def initialize; end
        end

        class Child < Parent
          # child init
          def initialize; end
        end

        @obj = Child.new

        # instance init
        def @obj.initialize; end
      end

      after do
        %i[Grandparent Parent Child].each { |name| Object.remove_const(name) }
      end

      context "and when it's passed once" do
        it "finds the super method docs" do
          expect(pry_eval(binding, 'show-source -d --super @obj.initialize'))
            .to match(/child init/)
        end
      end

      context "and when it's passed twice" do
        it "finds the parent method docs" do
          expect(pry_eval(binding, 'show-source -d -ss @obj.initialize'))
            .to match(/parent init/)
        end
      end

      context "and when it's passed thrice" do
        it "finds the grandparent method docs" do
          expect(pry_eval(binding, 'show-source -d -sss @obj.initialize'))
            .to match(/parent init/)
        end
      end

      context "and when the super method doesn't exist" do
        it "raises Pry::CommandError" do
          expect { pry_eval(binding, 'show-source -d -ssss @obj.initialize') }
            .to raise_error(Pry::CommandError)
        end
      end

      context "and when the explicit argument is not provided" do
        let(:son) { Child.new }
        it "finds super method docs without explicit method argument" do
          # son init
          def son.initialize
            pry_eval(binding, 'show-source -d --super')
          end

          expect(son.initialize).to match(/child init/)
        end

        it "finds super method docs with multiple `--super` switches" do
          son.extend(
            Module.new do
              def initialize; end
            end
          )

          # son init
          def son.initialize
            pry_eval(binding, 'show-source -d --super --super')
          end

          expect(son.initialize).to match(/child init/)
        end
      end
    end

    describe "code highlighting" do
      context "when there's code in the docs" do
        let(:klass) do
          Class.new do
            # This can initialize your class:
            #
            #   a = klass.new :foo
            #
            # @param foo
            def initialize(foo); end
          end
        end

        it "highlights the code" do
          expect(pry_eval(binding, 'show-source -d klass#initialize'))
            .to match(/klass.new :foo/)

          # We don't want the test to rely on which colour codes are there, so
          # we just assert that something is being colorized.
          expect(
            pry_eval(
              binding,
              'pry_instance.color = true',
              "show-source -d klass#initialize"
            )
          ).not_to match(/klass.new :foo/)
        end
      end

      context "when there's inline code in the docs" do
        let(:klass) do
          Class.new do
            # After initializing your class with `klass.new(:inline)`, go have
            # fun!
            #
            # @param foo
            def initialize(foo); end
          end
        end

        it "highlights the code" do
          expect(pry_eval(binding, 'show-source -d klass#initialize'))
            .to match(/klass.new\(:inline\)/)

          # We don't want the test to rely on which colour codes are there, so
          # we just assert that something is being colorized.
          expect(
            pry_eval(
              binding,
              'pry_instance.color = true',
              "show-source -d klass#initialize"
            )
          ).not_to match(/klass.new\(:inline\)/)
        end
      end

      context "when there's inline code with backticks the docs" do
        let(:klass) do
          Class.new do
            # Convert aligned output (from many shell commands) into nested arrays:
            #
            #   a = decolumnize `ls -l $HOME`
            #
            # @param output
            def decolumnize(output); end
          end
        end

        it "doesn't highlight the backticks" do
          output = pry_eval(
            binding,
            'pry_instance.color = true',
            "show-source -d klass#decolumnize"
          )

          expect(output).to match(/ls -l \$HOME/)
          expect(output).not_to match(/`ls -l \$HOME`/)
        end
      end
    end

    describe "the --all switch behavior" do
      let(:tempfile) { Tempfile.new(%w[pry .rb]) }

      context "when there are monkeypatches in different files" do
        before do
          define_persistent_class(tempfile, <<-CLASS)
            # file monkeypatch
            class TestClass
              def alpha; end
            end
          CLASS

          # local monkeypatch
          class TestClass
            def beta; end
          end
        end

        after do
          Object.remove_const(:TestClass)
          tempfile.unlink
        end

        it "shows them" do
          result = pry_eval(binding, 'show-source -d TestClass -a')
          expect(result).to match(/file monkeypatch/)
          expect(result).to match(/local monkeypatch/)
        end
      end

      context "when --all is not used but there are multiple monkeypatches" do
        before do
          define_persistent_class(tempfile, <<-CLASS)
            # alpha
            class TestClass
              def alpha; end
            end
          CLASS

          class TestClass
            def beta; end
          end
        end

        after do
          Object.remove_const(:TestClass)
          tempfile.unlink
        end

        it "correctly displays the number of monkeypatches" do
          result = pry_eval(binding, 'show-source -d TestClass')
          expect(result).to match(/Number of monkeypatches: 2/)
        end

        it "displays the original definition first" do
          result = pry_eval(binding, 'show-source -d TestClass')
          expect(result).to match(/alpha/)
        end

        it "mentions available monkeypatches" do
          result = pry_eval(binding, 'show-source -d TestClass')
          expect(result).to match(/available monkeypatches/)
        end
      end

      context "when --all is not used and there's only 1 candidate for the class" do
        before do
          # alpha
          class TestClass
            def alpha; end
          end
        end

        after { Object.remove_const(:TestClass) }

        it "doesn't mention anything about monkeypatches" do
          result = pry_eval(binding, 'show-source -d TestClass')
          expect(result).not_to match(/available monkeypatches/)
        end
      end
    end

    context "when used against a command" do
      let(:default_commands) { Pry.config.commands }

      let(:command_set) do
        Pry::CommandSet.new { import Pry::Commands }
      end

      before { Pry.config.commands = command_set }
      after { Pry.config.commands = default_commands }

      it "displays help for a specific command" do
        expect(pry_eval(binding, 'show-source -d ls')).to match(/Usage: ls/)
      end

      it "displays help for a regex command with a \"listing\"" do
        command_set.command(/bar(.*)/, 'Test listing', listing: 'foo') {}
        expect(pry_eval(binding, 'show-source -d foo')).to match(/Test listing/)
      end

      it "displays help for a command with a spaces in its name" do
        command_set.command('command with spaces', 'command with spaces desc') {}
        expect(pry_eval(binding, 'show-source -d command with spaces')).to match(
          /command with spaces desc/
        )
      end

      describe "class commands" do
        before do
          # pretty pink pincers
          class LobsterLady < Pry::ClassCommand
            match 'lobster-lady'
            description 'nada.'
            def process
              'lobster'
            end
          end

          command_set.add_command(LobsterLady)
        end

        after { Object.remove_const(:LobsterLady) }

        context "when looking up by command name" do
          it "displays help" do
            expect(pry_eval('show-source -d lobster-lady')).to match(/nada/)
          end
        end

        context "when class is used (rather than command name) is used for lookup" do
          it "displays actual preceding comment for a class command" do
            expect(pry_eval('show-source -d LobsterLady')).to match(/pretty pink pincers/)
          end
        end
      end
    end
  end
end