File: extend_test.rb

package info (click to toggle)
ruby-sass 3.7.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,396 kB
  • sloc: ruby: 32,443; sh: 26; makefile: 25
file content (1679 lines) | stat: -rwxr-xr-x 46,136 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
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
require File.dirname(__FILE__) + '/../test_helper'

class ExtendTest < Minitest::Test
  def test_basic
    assert_equal <<CSS, render(<<SCSS)
.foo, .bar {
  a: b; }
CSS
.foo {a: b}
.bar {@extend .foo}
SCSS

    assert_equal <<CSS, render(<<SCSS)
.foo, .bar {
  a: b; }
CSS
.bar {@extend .foo}
.foo {a: b}
SCSS

    assert_equal <<CSS, render(<<SCSS)
.foo, .bar {
  a: b; }

.bar {
  c: d; }
CSS
.foo {a: b}
.bar {c: d; @extend .foo}
SCSS

    assert_equal <<CSS, render(<<SCSS)
.foo, .bar {
  a: b; }

.bar {
  c: d; }
CSS
.foo {a: b}
.bar {@extend .foo; c: d}
SCSS
  end

  def test_indented_syntax
    assert_equal <<CSS, render(<<SASS, :syntax => :sass)
.foo, .bar {
  a: b; }
CSS
.foo
  a: b
.bar
  @extend .foo
SASS

    assert_equal <<CSS, render(<<SASS, :syntax => :sass)
.foo, .bar {
  a: b; }
CSS
.foo
  a: b
.bar
  @extend \#{".foo"}
SASS
  end

  def test_multiple_targets
    assert_equal <<CSS, render(<<SCSS)
.foo, .bar {
  a: b; }

.blip .foo, .blip .bar {
  c: d; }
CSS
.foo {a: b}
.bar {@extend .foo}
.blip .foo {c: d}
SCSS
  end

  def test_multiple_extendees
    assert_equal <<CSS, render(<<SCSS)
.foo, .baz {
  a: b; }

.bar, .baz {
  c: d; }
CSS
.foo {a: b}
.bar {c: d}
.baz {@extend .foo; @extend .bar}
SCSS
  end

  def test_multiple_extends_with_single_extender_and_single_target
    assert_extends('.foo .bar', '.baz {@extend .foo; @extend .bar}',
      '.foo .bar, .baz .bar, .foo .baz, .baz .baz')
    assert_extends '.foo.bar', '.baz {@extend .foo; @extend .bar}', '.foo.bar, .baz'
  end

  def test_multiple_extends_with_multiple_extenders_and_single_target
    assert_equal <<CSS, render(<<SCSS)
.foo .bar, .baz .bar, .foo .bang, .baz .bang {
  a: b; }
CSS
.foo .bar {a: b}
.baz {@extend .foo}
.bang {@extend .bar}
SCSS

    assert_equal <<CSS, render(<<SCSS)
.foo.bar, .bar.baz, .baz.bang, .foo.bang {
  a: b; }
CSS
.foo.bar {a: b}
.baz {@extend .foo}
.bang {@extend .bar}
SCSS
  end

  def test_chained_extends
    assert_equal <<CSS, render(<<SCSS)
.foo, .bar, .baz, .bip {
  a: b; }
CSS
.foo {a: b}
.bar {@extend .foo}
.baz {@extend .bar}
.bip {@extend .bar}
SCSS
  end

  def test_dynamic_extendee
    assert_extends '.foo', '.bar {@extend #{".foo"}}', '.foo, .bar'
    assert_extends('[baz^="blip12px"]', '.bar {@extend [baz^="blip#{12px}"]}',
      '[baz^="blip12px"], .bar')
  end

  def test_nested_target
    assert_extends '.foo .bar', '.baz {@extend .bar}', '.foo .bar, .foo .baz'
  end

  def test_target_with_child
    assert_extends '.foo .bar', '.baz {@extend .foo}', '.foo .bar, .baz .bar'
  end

  def test_class_unification
    assert_unification '.foo.bar', '.baz {@extend .foo}', '.foo.bar, .bar.baz'
    assert_unification '.foo.baz', '.baz {@extend .foo}', '.baz'
  end

  def test_id_unification
    assert_unification '.foo.bar', '#baz {@extend .foo}', '.foo.bar, .bar#baz'
    assert_unification '.foo#baz', '#baz {@extend .foo}', '#baz'
    assert_unification '.foo#baz', '#bar {@extend .foo}', '.foo#baz'
  end

  def test_universal_unification_with_simple_target
    assert_unification '.foo', '* {@extend .foo}', '.foo, *'
    assert_unification '.foo', '*|* {@extend .foo}', '.foo, *|*'
    assert_unification '.foo.bar', '* {@extend .foo}', '.bar'
    assert_unification '.foo.bar', '*|* {@extend .foo}', '.bar'
    assert_unification '.foo.bar', 'ns|* {@extend .foo}', '.foo.bar, ns|*.bar'
  end

  def test_universal_unification_with_namespaceless_universal_target
    assert_unification '*.foo', 'ns|* {@extend .foo}', '*.foo'
    assert_unification '*.foo', '* {@extend .foo}', '*'
    assert_unification '*.foo', '*|* {@extend .foo}', '*'
    assert_unification '*|*.foo', '* {@extend .foo}', '*|*.foo, *'
    assert_unification '*|*.foo', '*|* {@extend .foo}', '*|*'
    assert_unification '*|*.foo', 'ns|* {@extend .foo}', '*|*.foo, ns|*'
  end

  def test_universal_unification_with_namespaced_universal_target
    assert_unification 'ns|*.foo', '* {@extend .foo}', 'ns|*.foo'
    assert_unification 'ns1|*.foo', 'ns2|* {@extend .foo}', 'ns1|*.foo'
    assert_unification 'ns|*.foo', '*|* {@extend .foo}', 'ns|*'
    assert_unification 'ns|*.foo', 'ns|* {@extend .foo}', 'ns|*'
  end

  def test_universal_unification_with_namespaceless_element_target
    assert_unification 'a.foo', 'ns|* {@extend .foo}', 'a.foo'
    assert_unification 'a.foo', '* {@extend .foo}', 'a'
    assert_unification 'a.foo', '*|* {@extend .foo}', 'a'
    assert_unification '*|a.foo', '* {@extend .foo}', '*|a.foo, a'
    assert_unification '*|a.foo', '*|* {@extend .foo}', '*|a'
    assert_unification '*|a.foo', 'ns|* {@extend .foo}', '*|a.foo, ns|a'
  end

  def test_universal_unification_with_namespaced_element_target
    assert_unification 'ns|a.foo', '* {@extend .foo}', 'ns|a.foo'
    assert_unification 'ns1|a.foo', 'ns2|* {@extend .foo}', 'ns1|a.foo'
    assert_unification 'ns|a.foo', '*|* {@extend .foo}', 'ns|a'
    assert_unification 'ns|a.foo', 'ns|* {@extend .foo}', 'ns|a'
  end

  def test_element_unification_with_simple_target
    assert_unification '.foo', 'a {@extend .foo}', '.foo, a'
    assert_unification '.foo.bar', 'a {@extend .foo}', '.foo.bar, a.bar'
    assert_unification '.foo.bar', '*|a {@extend .foo}', '.foo.bar, *|a.bar'
    assert_unification '.foo.bar', 'ns|a {@extend .foo}', '.foo.bar, ns|a.bar'
  end

  def test_element_unification_with_namespaceless_universal_target
    assert_unification '*.foo', 'ns|a {@extend .foo}', '*.foo'
    assert_unification '*.foo', 'a {@extend .foo}', '*.foo, a'
    assert_unification '*.foo', '*|a {@extend .foo}', '*.foo, a'
    assert_unification '*|*.foo', 'a {@extend .foo}', '*|*.foo, a'
    assert_unification '*|*.foo', '*|a {@extend .foo}', '*|*.foo, *|a'
    assert_unification '*|*.foo', 'ns|a {@extend .foo}', '*|*.foo, ns|a'
  end

  def test_element_unification_with_namespaced_universal_target
    assert_unification 'ns|*.foo', 'a {@extend .foo}', 'ns|*.foo'
    assert_unification 'ns1|*.foo', 'ns2|a {@extend .foo}', 'ns1|*.foo'
    assert_unification 'ns|*.foo', '*|a {@extend .foo}', 'ns|*.foo, ns|a'
    assert_unification 'ns|*.foo', 'ns|a {@extend .foo}', 'ns|*.foo, ns|a'
  end

  def test_element_unification_with_namespaceless_element_target
    assert_unification 'a.foo', 'ns|a {@extend .foo}', 'a.foo'
    assert_unification 'a.foo', 'h1 {@extend .foo}', 'a.foo'
    assert_unification 'a.foo', 'a {@extend .foo}', 'a'
    assert_unification 'a.foo', '*|a {@extend .foo}', 'a'
    assert_unification '*|a.foo', 'a {@extend .foo}', '*|a.foo, a'
    assert_unification '*|a.foo', '*|a {@extend .foo}', '*|a'
    assert_unification '*|a.foo', 'ns|a {@extend .foo}', '*|a.foo, ns|a'
  end

  def test_element_unification_with_namespaced_element_target
    assert_unification 'ns|a.foo', 'a {@extend .foo}', 'ns|a.foo'
    assert_unification 'ns1|a.foo', 'ns2|a {@extend .foo}', 'ns1|a.foo'
    assert_unification 'ns|a.foo', '*|a {@extend .foo}', 'ns|a'
    assert_unification 'ns|a.foo', 'ns|a {@extend .foo}', 'ns|a'
  end

  def test_attribute_unification
    assert_unification '[foo=bar].baz', '[foo=baz] {@extend .baz}', '[foo=bar].baz, [foo=bar][foo=baz]'
    assert_unification '[foo=bar].baz', '[foo^=bar] {@extend .baz}', '[foo=bar].baz, [foo=bar][foo^=bar]'
    assert_unification '[foo=bar].baz', '[foot=bar] {@extend .baz}', '[foo=bar].baz, [foo=bar][foot=bar]'
    assert_unification '[foo=bar].baz', '[ns|foo=bar] {@extend .baz}', '[foo=bar].baz, [foo=bar][ns|foo=bar]'
    assert_unification '%-a [foo=bar].bar', '[foo=bar] {@extend .bar}', '-a [foo=bar]'
  end

  def test_pseudo_unification
    assert_unification ':foo.baz', ':foo(2n+1) {@extend .baz}', ':foo.baz, :foo:foo(2n+1)'
    assert_unification ':foo.baz', '::foo {@extend .baz}', ':foo.baz, :foo::foo'
    assert_unification '::foo.baz', '::bar {@extend .baz}', '::foo.baz'
    assert_unification '::foo.baz', '::foo(2n+1) {@extend .baz}', '::foo.baz'
    assert_unification '::foo.baz', '::foo {@extend .baz}', '::foo'
    assert_unification '::foo(2n+1).baz', '::foo(2n+1) {@extend .baz}', '::foo(2n+1)'
    assert_unification ':foo.baz', ':bar {@extend .baz}', ':foo.baz, :foo:bar'
    assert_unification '.baz:foo', ':after {@extend .baz}', '.baz:foo, :foo:after'
    assert_unification '.baz:after', ':foo {@extend .baz}', '.baz:after, :foo:after'
    assert_unification ':foo.baz', ':foo {@extend .baz}', ':foo'
  end

  def test_pseudoelement_remains_at_end_of_selector
    assert_extends '.foo::bar', '.baz {@extend .foo}', '.foo::bar, .baz::bar'
    assert_extends 'a.foo::bar', '.baz {@extend .foo}', 'a.foo::bar, a.baz::bar'
  end

  def test_pseudoclass_remains_at_end_of_selector
    assert_extends '.foo:bar', '.baz {@extend .foo}', '.foo:bar, .baz:bar'
    assert_extends 'a.foo:bar', '.baz {@extend .foo}', 'a.foo:bar, a.baz:bar'
  end

  def test_id_unification_again
    assert_unification('#id.foo .bar', '#id.baz .qux {@extend .bar}',
        '#id.foo .bar, #id.baz.foo .qux')
  end

  def test_root_unification
    assert_extends(
      ".foo:root .bar",
      ".baz:root .qux {@extend .bar}",
      ".foo:root .bar, .baz.foo:root .qux")
  end

  def test_not_remains_at_end_of_selector
    assert_extends '.foo:not(.bar)', '.baz {@extend .foo}', '.foo:not(.bar), .baz:not(.bar)'
  end

  def test_pseudoelement_goes_lefter_than_pseudoclass
    assert_extends '.foo::bar', '.baz:bang {@extend .foo}', '.foo::bar, .baz:bang::bar'
    assert_extends '.foo:bar', '.baz::bang {@extend .foo}', '.foo:bar, .baz:bar::bang'
  end

  def test_pseudoelement_goes_lefter_than_not
    assert_extends '.foo::bar', '.baz:not(.bang) {@extend .foo}', '.foo::bar, .baz:not(.bang)::bar'
    assert_extends '.foo:not(.bang)', '.baz::bar {@extend .foo}', '.foo:not(.bang), .baz:not(.bang)::bar'
  end

  def test_negation_unification
    assert_extends ':not(.foo).baz', ':not(.bar) {@extend .baz}', ':not(.foo).baz, :not(.foo):not(.bar)'
    # Unifying to :not(.foo) here would reduce the specificity of the original selector.
    assert_extends ':not(.foo).baz', ':not(.foo) {@extend .baz}', ':not(.foo).baz, :not(.foo)'
  end

  def test_prefixed_pseudoclass_unification
    assert_unification(
      ':nth-child(2n+1 of .foo).baz',
      ':nth-child(2n of .foo) {@extend .baz}',
      ':nth-child(2n+1 of .foo).baz, :nth-child(2n+1 of .foo):nth-child(2n of .foo)')

    assert_unification(
      ':nth-child(2n+1 of .foo).baz',
      ':nth-child(2n+1 of .bar) {@extend .baz}',
      ':nth-child(2n+1 of .foo).baz, :nth-child(2n+1 of .foo):nth-child(2n+1 of .bar)')

    assert_unification(
      ':nth-child(2n+1 of .foo).baz',
      ':nth-child(2n+1 of .foo) {@extend .baz}',
      ':nth-child(2n+1 of .foo)')
  end

  def test_extend_into_not
    assert_extends(':not(.foo)', '.x {@extend .foo}', ':not(.foo):not(.x)')
    assert_extends(':not(.foo.bar)', '.x {@extend .bar}', ':not(.foo.bar):not(.foo.x)')
    assert_extends(
      ':not(.foo.bar, .baz.bar)',
      '.x {@extend .bar}',
      ':not(.foo.bar, .foo.x, .baz.bar, .baz.x)')
  end

  def test_extend_into_mergeable_pseudoclasses
    assert_extends(':matches(.foo)', '.x {@extend .foo}', ':matches(.foo, .x)')
    assert_extends(':matches(.foo.bar)', '.x {@extend .bar}', ':matches(.foo.bar, .foo.x)')
    assert_extends(
      ':matches(.foo.bar, .baz.bar)',
      '.x {@extend .bar}',
      ':matches(.foo.bar, .foo.x, .baz.bar, .baz.x)')

    assert_extends(':-moz-any(.foo)', '.x {@extend .foo}', ':-moz-any(.foo, .x)')
    assert_extends(':current(.foo)', '.x {@extend .foo}', ':current(.foo, .x)')
    assert_extends(':has(.foo)', '.x {@extend .foo}', ':has(.foo, .x)')
    assert_extends(':host(.foo)', '.x {@extend .foo}', ':host(.foo, .x)')
    assert_extends(':host-context(.foo)', '.x {@extend .foo}', ':host-context(.foo, .x)')
    assert_extends(':nth-child(n of .foo)', '.x {@extend .foo}', ':nth-child(n of .foo, .x)')
    assert_extends(
      ':nth-last-child(n of .foo)',
      '.x {@extend .foo}',
      ':nth-last-child(n of .foo, .x)')
  end

  def test_complex_extend_into_pseudoclass
    # Unlike other selectors, we don't allow complex selectors to be
    # added to `:not` if they weren't there before. At time of
    # writing, most browsers don't support that and will throw away
    # the entire selector if it exists.
    #assert_extends(':not(.bar)', '.x .y {@extend .bar}', ':not(.bar)')

    # If the `:not()` already has a complex selector, we won't break
    # anything by adding a new one.
    assert_extends(':not(.baz .bar)', '.x .y {@extend .bar}',
      ':not(.baz .bar):not(.baz .x .y):not(.x .baz .y)')

    # If the `:not()` would only contain complex selectors, there's no
    # harm in letting it continue to exist.
    assert_extends(':not(%bar)', '.x .y {@extend %bar}', ':not(.x .y)')

    assert_extends(':matches(.bar)', '.x .y {@extend .bar}', ':matches(.bar, .x .y)')
    assert_extends(':current(.bar)', '.x .y {@extend .bar}', ':current(.bar, .x .y)')
    assert_extends(':has(.bar)', '.x .y {@extend .bar}', ':has(.bar, .x .y)')
    assert_extends(':host(.bar)', '.x .y {@extend .bar}', ':host(.bar, .x .y)')
    assert_extends(':host-context(.bar)', '.x .y {@extend .bar}', ':host-context(.bar, .x .y)')
    assert_extends(
      ':-moz-any(.bar)',
      '.x .y {@extend .bar}',
      ':-moz-any(.bar, .x .y)')
    assert_extends(
      ':nth-child(n of .bar)',
      '.x .y {@extend .bar}',
      ':nth-child(n of .bar, .x .y)')
    assert_extends(
      ':nth-last-child(n of .bar)',
      '.x .y {@extend .bar}',
      ':nth-last-child(n of .bar, .x .y)')
  end

  def test_extend_over_selector_pseudoclass
    assert_extends(':not(.foo)', '.x {@extend :not(.foo)}', ':not(.foo), .x')
    assert_extends(
      ':matches(.foo, .bar)',
      '.x {@extend :matches(.foo, .bar)}',
      ':matches(.foo, .bar), .x')
  end

  def test_matches_within_not
    assert_extends(
      ':not(.foo, .bar)',
      ':matches(.x, .y) {@extend .foo}',
      ':not(.foo, .x, .y, .bar)')
  end

  def test_pseudoclasses_merge
    assert_extends(':matches(.foo)', ':matches(.bar) {@extend .foo}', ':matches(.foo, .bar)')
    assert_extends(':-moz-any(.foo)', ':-moz-any(.bar) {@extend .foo}', ':-moz-any(.foo, .bar)')
    assert_extends(':current(.foo)', ':current(.bar) {@extend .foo}', ':current(.foo, .bar)')
    assert_extends(
      ':nth-child(n of .foo)',
      ':nth-child(n of .bar) {@extend .foo}',
      ':nth-child(n of .foo, .bar)')
    assert_extends(
      ':nth-last-child(n of .foo)',
      ':nth-last-child(n of .bar) {@extend .foo}',
      ':nth-last-child(n of .foo, .bar)')
  end

  def test_nesting_pseudoclasses_merge
    assert_extends(':has(.foo)', ':has(.bar) {@extend .foo}', ':has(.foo, :has(.bar))')
    assert_extends(':host(.foo)', ':host(.bar) {@extend .foo}', ':host(.foo, :host(.bar))')
    assert_extends(
      ':host-context(.foo)',
      ':host-context(.bar) {@extend .foo}',
      ':host-context(.foo, :host-context(.bar))')
  end

  def test_not_unifies_with_unique_values
    assert_unification('foo', ':not(bar) {@extend foo}', ':not(bar)')
    assert_unification('#foo', ':not(#bar) {@extend #foo}', ':not(#bar)')
  end

  def test_not_adds_no_specificity
    assert_specificity_equals(':not(.foo)', '.foo')
  end

  def test_matches_has_a_specificity_range
    # `:matches(.foo, #bar)` has minimum specificity equal to that of `.foo`,
    # which means `:matches(.foo, #bar) .a` can have less specificity than
    # `#b.a`. Thus the selector generated by `#b.a` should be preserved.
    assert_equal <<CSS, render(<<SCSS)
:matches(.foo, #bar) .a, :matches(.foo, #bar) #b.a {
  a: b; }
CSS
:matches(.foo, #bar) %x {a: b}
.a {@extend %x}
#b.a {@extend %x}
SCSS

    # `:matches(.foo, #bar)` has maximum specificity equal to that of `#bar`,
    # which means `:matches(.foo, #bar).b` can have greater specificity than `.a
    # .b`. Thus the selector generated by `:matches(.foo, #bar).b` should be
    # preserved.
    assert_equal <<CSS, render(<<SCSS)
.a .b, .a .b:matches(.foo, #bar) {
  a: b; }
CSS
.a %x {a: b}
.b {@extend %x}
.b:matches(.foo, #bar) {@extend %x}
SCSS
  end

  def test_extend_into_not_and_normal_extend
    assert_equal <<CSS, render(<<SCSS)
.x:not(.y):not(.bar), .foo:not(.y):not(.bar) {
  a: b; }
CSS
.x:not(.y) {a: b}
.foo {@extend .x}
.bar {@extend .y}
SCSS
  end

  def test_extend_into_matches_and_normal_extend
    assert_equal <<CSS, render(<<SCSS)
.x:matches(.y, .bar), .foo:matches(.y, .bar) {
  a: b; }
CSS
.x:matches(.y) {a: b}
.foo {@extend .x}
.bar {@extend .y}
SCSS
  end

  def test_multilayer_pseudoclass_extend
    assert_equal <<CSS, render(<<SCSS)
:matches(.x, .foo, .bar) {
  a: b; }
CSS
:matches(.x) {a: b}
.foo {@extend .x}
.bar {@extend .foo}
SCSS
  end

  def test_root_only_allowed_at_root
    assert_extends(':root .foo', '.bar .baz {@extend .foo}',
      ':root .foo, :root .bar .baz')
    assert_extends('.foo:root .bar', '.baz:root .bang {@extend .bar}',
      '.foo:root .bar, .baz.foo:root .bang')
    assert_extends('html:root .bar', 'xml:root .bang {@extend .bar}', 'html:root .bar')
    assert_extends('.foo:root > .bar .x', '.baz:root .bang .y {@extend .x}',
      '.foo:root > .bar .x, .baz.foo:root > .bar .bang .y')
  end

  def test_comma_extendee
    assert_equal <<CSS, render(<<SCSS)
.foo, .baz {
  a: b; }

.bar, .baz {
  c: d; }
CSS
.foo {a: b}
.bar {c: d}
.baz {@extend .foo, .bar}
SCSS
  end

  def test_redundant_selector_elimination
    assert_equal <<CSS, render(<<SCSS)
.foo.bar, .x, .y {
  a: b; }
CSS
.foo.bar {a: b}
.x {@extend .foo, .bar}
.y {@extend .foo, .bar}
SCSS
  end

  def test_nested_pseudo_selectors
    assert_equal <<CSS, render(<<SCSS)
.foo .bar:not(.baz), .bang .bar:not(.baz) {
  a: b; }
CSS
.foo {
  .bar:not(.baz) {a: b}
}
.bang {@extend .foo}
SCSS
  end

  ## Long Extendees

  def test_long_extendee
    assert_warning(<<WARNING) {assert_extends '.foo.bar', '.baz {@extend .foo.bar}', '.foo.bar, .baz'}
DEPRECATION WARNING on line 2 of test_long_extendee_inline.scss:
Extending a compound selector, .foo.bar, is deprecated and will not be supported in a future release.
Consider "@extend .foo, .bar" instead.
See http://bit.ly/ExtendCompound for details.
WARNING
  end

  def test_long_extendee_requires_all_selectors
    silence_warnings do
      assert_extend_doesnt_match('.baz', '.foo.bar', :not_found, 2) do
        render_extends '.foo', '.baz {@extend .foo.bar}'
      end
    end
  end

  def test_long_extendee_matches_supersets
    silence_warnings {assert_extends '.foo.bar.bap', '.baz {@extend .foo.bar}', '.foo.bar.bap, .bap.baz'}
  end

  def test_long_extendee_runs_unification
    silence_warnings {assert_extends 'ns|*.foo.bar', '*|a.baz {@extend .foo.bar}', 'ns|*.foo.bar, ns|a.baz'}
  end

  ## Long Extenders

  def test_long_extender
    assert_extends '.foo.bar', '.baz.bang {@extend .foo}', '.foo.bar, .bar.baz.bang'
  end

  def test_long_extender_runs_unification
    assert_extends 'ns|*.foo.bar', '*|a.baz {@extend .foo}', 'ns|*.foo.bar, ns|a.bar.baz'
  end

  def test_long_extender_doesnt_unify
    assert_extends 'a.foo#bar', 'h1.baz {@extend .foo}', 'a.foo#bar'
    assert_extends 'a.foo#bar', '.bang#baz {@extend .foo}', 'a.foo#bar'
  end

  ## Nested Extenders

  def test_nested_extender
    assert_extends '.foo', 'foo bar {@extend .foo}', '.foo, foo bar'
  end

  def test_nested_extender_runs_unification
    assert_extends '.foo.bar', 'foo bar {@extend .foo}', '.foo.bar, foo bar.bar'
  end

  def test_nested_extender_doesnt_unify
    assert_extends 'baz.foo', 'foo bar {@extend .foo}', 'baz.foo'
  end

  def test_nested_extender_alternates_parents
    assert_extends('.baz .bip .foo', 'foo .grank bar {@extend .foo}',
      '.baz .bip .foo, .baz .bip foo .grank bar, foo .grank .baz .bip bar')
  end

  def test_nested_extender_unifies_identical_parents
    assert_extends('.baz .bip .foo', '.baz .bip bar {@extend .foo}',
      '.baz .bip .foo, .baz .bip bar')
  end

  def test_nested_extender_unifies_common_substring
    assert_extends('.baz .bip .bap .bink .foo', '.brat .bip .bap bar {@extend .foo}',
      '.baz .bip .bap .bink .foo, .baz .brat .bip .bap .bink bar, .brat .baz .bip .bap .bink bar')
  end

  def test_nested_extender_unifies_common_subseq
    assert_extends('.a .x .b .y .foo', '.a .n .b .m bar {@extend .foo}',
      '.a .x .b .y .foo, .a .x .n .b .y .m bar, .a .n .x .b .y .m bar, .a .x .n .b .m .y bar, .a .n .x .b .m .y bar')
  end

  def test_nested_extender_chooses_first_subseq
    assert_extends('.a .b .c .d .foo', '.c .d .a .b .bar {@extend .foo}',
      '.a .b .c .d .foo, .a .b .c .d .a .b .bar')
  end

  def test_nested_extender_counts_extended_subselectors
    assert_extends('.a .bip.bop .foo', '.b .bip .bar {@extend .foo}',
      '.a .bip.bop .foo, .a .b .bip.bop .bar, .b .a .bip.bop .bar')
  end

  def test_nested_extender_counts_extended_superselectors
    assert_extends('.a .bip .foo', '.b .bip.bop .bar {@extend .foo}',
      '.a .bip .foo, .a .b .bip.bop .bar, .b .a .bip.bop .bar')
  end

  def test_nested_extender_with_child_selector
    assert_extends '.baz .foo', 'foo > bar {@extend .foo}', '.baz .foo, .baz foo > bar'
  end

  def test_nested_extender_finds_common_selectors_around_child_selector
    assert_extends 'a > b c .c1', 'a c .c2 {@extend .c1}', 'a > b c .c1, a > b c .c2'
    assert_extends 'a > b c .c1', 'b c .c2 {@extend .c1}', 'a > b c .c1, a > b c .c2'
  end

  def test_nested_extender_doesnt_find_common_selectors_around_adjacent_sibling_selector
    assert_extends 'a + b c .c1', 'a c .c2 {@extend .c1}', 'a + b c .c1, a + b a c .c2, a a + b c .c2'
    assert_extends 'a + b c .c1', 'a b .c2 {@extend .c1}', 'a + b c .c1, a a + b c .c2'
    assert_extends 'a + b c .c1', 'b c .c2 {@extend .c1}', 'a + b c .c1, a + b c .c2'
  end

  def test_nested_extender_doesnt_find_common_selectors_around_sibling_selector
    assert_extends 'a ~ b c .c1', 'a c .c2 {@extend .c1}', 'a ~ b c .c1, a ~ b a c .c2, a a ~ b c .c2'
    assert_extends 'a ~ b c .c1', 'a b .c2 {@extend .c1}', 'a ~ b c .c1, a a ~ b c .c2'
    assert_extends 'a ~ b c .c1', 'b c .c2 {@extend .c1}', 'a ~ b c .c1, a ~ b c .c2'
  end

  def test_nested_extender_doesnt_find_common_selectors_around_reference_selector
    silence_warnings {assert_extends 'a /for/ b c .c1', 'a c .c2 {@extend .c1}', 'a /for/ b c .c1, a /for/ b a c .c2, a a /for/ b c .c2'}
    silence_warnings {assert_extends 'a /for/ b c .c1', 'a b .c2 {@extend .c1}', 'a /for/ b c .c1, a a /for/ b c .c2'}
    silence_warnings {assert_extends 'a /for/ b c .c1', 'b c .c2 {@extend .c1}', 'a /for/ b c .c1, a /for/ b c .c2'}
  end

  def test_nested_extender_with_early_child_selectors_doesnt_subseq_them
    assert_extends('.bip > .bap .foo', '.grip > .bap .bar {@extend .foo}',
      '.bip > .bap .foo, .bip > .bap .grip > .bap .bar, .grip > .bap .bip > .bap .bar')
    assert_extends('.bap > .bip .foo', '.bap > .grip .bar {@extend .foo}',
      '.bap > .bip .foo, .bap > .bip .bap > .grip .bar, .bap > .grip .bap > .bip .bar')
  end

  def test_nested_extender_with_child_selector_unifies
    assert_extends '.baz.foo', 'foo > bar {@extend .foo}', '.baz.foo, foo > bar.baz'

    assert_equal <<CSS, render(<<SCSS)
.baz > .foo, .baz > .bar {
  a: b; }
CSS
.baz > {
  .foo {a: b}
  .bar {@extend .foo}
}
SCSS

    assert_equal <<CSS, render(<<SCSS)
.foo .bar, .foo > .baz {
  a: b; }
CSS
.foo {
  .bar {a: b}
  > .baz {@extend .bar}
}
SCSS
  end

  def test_nested_extender_with_early_child_selector
    assert_equal <<CSS, render(<<SCSS)
.foo .bar, .foo .bip > .baz {
  a: b; }
CSS
.foo {
  .bar {a: b}
  .bip > .baz {@extend .bar}
}
SCSS

    assert_equal <<CSS, render(<<SCSS)
.foo .bip .bar, .foo .bip .foo > .baz {
  a: b; }
CSS
.foo {
  .bip .bar {a: b}
  > .baz {@extend .bar}
}
SCSS

    assert_extends '.foo > .bar', '.bip + .baz {@extend .bar}', '.foo > .bar, .foo > .bip + .baz'
    assert_extends '.foo + .bar', '.bip > .baz {@extend .bar}', '.foo + .bar, .bip > .foo + .baz'
    assert_extends '.foo > .bar', '.bip > .baz {@extend .bar}', '.foo > .bar, .bip.foo > .baz'
  end

  def test_nested_extender_with_trailing_child_selector
    assert_raises(Sass::SyntaxError, "bar > can't extend: invalid selector") do
      render("bar > {@extend .baz}")
    end
  end

  def test_nested_extender_with_sibling_selector
    assert_extends '.baz .foo', 'foo + bar {@extend .foo}', '.baz .foo, .baz foo + bar'
  end

  def test_nested_extender_with_hacky_selector
    assert_extends('.baz .foo', 'foo + > > + bar {@extend .foo}',
      '.baz .foo, .baz foo + > > + bar, foo .baz + > > + bar')
    assert_extends '.baz .foo', '> > bar {@extend .foo}', '.baz .foo, > > .baz bar'
  end

  def test_nested_extender_merges_with_same_selector
    assert_equal <<CSS, render(<<SCSS)
.foo .bar, .foo .baz {
  a: b; }
CSS
.foo {
  .bar {a: b}
  .baz {@extend .bar} }
SCSS
  end

  def test_nested_extender_with_child_selector_merges_with_same_selector
    assert_extends('.foo > .bar .baz', '.foo > .bar .bang {@extend .baz}',
      '.foo > .bar .baz, .foo > .bar .bang')
  end

  # Combinator Unification

  def test_combinator_unification_for_hacky_combinators
    assert_extends '.a > + x', '.b y {@extend x}', '.a > + x, .a .b > + y, .b .a > + y'
    assert_extends '.a x', '.b > + y {@extend x}', '.a x, .a .b > + y, .b .a > + y'
    assert_extends '.a > + x', '.b > + y {@extend x}', '.a > + x, .a .b > + y, .b .a > + y'
    assert_extends '.a ~ > + x', '.b > + y {@extend x}', '.a ~ > + x, .a .b ~ > + y, .b .a ~ > + y'
    assert_extends '.a + > x', '.b > + y {@extend x}', '.a + > x'
    assert_extends '.a + > x', '.b > + y {@extend x}', '.a + > x'
    assert_extends '.a ~ > + .b > x', '.c > + .d > y {@extend x}', '.a ~ > + .b > x, .a .c ~ > + .d.b > y, .c .a ~ > + .d.b > y'
  end

  def test_combinator_unification_double_tilde
    assert_extends '.a.b ~ x', '.a ~ y {@extend x}', '.a.b ~ x, .a.b ~ y'
    assert_extends '.a ~ x', '.a.b ~ y {@extend x}', '.a ~ x, .a.b ~ y'
    assert_extends '.a ~ x', '.b ~ y {@extend x}', '.a ~ x, .a ~ .b ~ y, .b ~ .a ~ y, .b.a ~ y'
    assert_extends 'a.a ~ x', 'b.b ~ y {@extend x}', 'a.a ~ x, a.a ~ b.b ~ y, b.b ~ a.a ~ y'
  end

  def test_combinator_unification_tilde_plus
    assert_extends '.a.b + x', '.a ~ y {@extend x}', '.a.b + x, .a.b + y'
    assert_extends '.a + x', '.a.b ~ y {@extend x}', '.a + x, .a.b ~ .a + y, .a.b + y'
    assert_extends '.a + x', '.b ~ y {@extend x}', '.a + x, .b ~ .a + y, .b.a + y'
    assert_extends 'a.a + x', 'b.b ~ y {@extend x}', 'a.a + x, b.b ~ a.a + y'
    assert_extends '.a.b ~ x', '.a + y {@extend x}', '.a.b ~ x, .a.b ~ .a + y, .a.b + y'
    assert_extends '.a ~ x', '.a.b + y {@extend x}', '.a ~ x, .a.b + y'
    assert_extends '.a ~ x', '.b + y {@extend x}', '.a ~ x, .a ~ .b + y, .a.b + y'
    assert_extends 'a.a ~ x', 'b.b + y {@extend x}', 'a.a ~ x, a.a ~ b.b + y'
  end

  def test_combinator_unification_angle_sibling
    assert_extends '.a > x', '.b ~ y {@extend x}', '.a > x, .a > .b ~ y'
    assert_extends '.a > x', '.b + y {@extend x}', '.a > x, .a > .b + y'
    assert_extends '.a ~ x', '.b > y {@extend x}', '.a ~ x, .b > .a ~ y'
    assert_extends '.a + x', '.b > y {@extend x}', '.a + x, .b > .a + y'
  end

  def test_combinator_unification_double_angle
    assert_extends '.a.b > x', '.b > y {@extend x}', '.a.b > x, .b.a > y'
    assert_extends '.a > x', '.a.b > y {@extend x}', '.a > x, .a.b > y'
    assert_extends '.a > x', '.b > y {@extend x}', '.a > x, .b.a > y'
    assert_extends 'a.a > x', 'b.b > y {@extend x}', 'a.a > x'
  end

  def test_combinator_unification_double_plus
    assert_extends '.a.b + x', '.b + y {@extend x}', '.a.b + x, .b.a + y'
    assert_extends '.a + x', '.a.b + y {@extend x}', '.a + x, .a.b + y'
    assert_extends '.a + x', '.b + y {@extend x}', '.a + x, .b.a + y'
    assert_extends 'a.a + x', 'b.b + y {@extend x}', 'a.a + x'
  end

  def test_combinator_unification_angle_space
    assert_extends '.a.b > x', '.a y {@extend x}', '.a.b > x, .a.b > y'
    assert_extends '.a > x', '.a.b y {@extend x}', '.a > x, .a.b .a > y'
    assert_extends '.a > x', '.b y {@extend x}', '.a > x, .b .a > y'
    assert_extends '.a.b x', '.a > y {@extend x}', '.a.b x, .a.b .a > y'
    assert_extends '.a x', '.a.b > y {@extend x}', '.a x, .a.b > y'
    assert_extends '.a x', '.b > y {@extend x}', '.a x, .a .b > y'
  end

  def test_combinator_unification_plus_space
    assert_extends '.a.b + x', '.a y {@extend x}', '.a.b + x, .a .a.b + y'
    assert_extends '.a + x', '.a.b y {@extend x}', '.a + x, .a.b .a + y'
    assert_extends '.a + x', '.b y {@extend x}', '.a + x, .b .a + y'
    assert_extends '.a.b x', '.a + y {@extend x}', '.a.b x, .a.b .a + y'
    assert_extends '.a x', '.a.b + y {@extend x}', '.a x, .a .a.b + y'
    assert_extends '.a x', '.b + y {@extend x}', '.a x, .a .b + y'
  end

  def test_combinator_unification_nested
    assert_extends '.a > .b + x', '.c > .d + y {@extend x}', '.a > .b + x, .c.a > .d.b + y'
    assert_extends '.a > .b + x', '.c > y {@extend x}', '.a > .b + x, .c.a > .b + y'
  end

  def test_combinator_unification_with_newlines
    assert_equal <<CSS, render(<<SCSS)
.a >
.b
+ x, .c.a > .d.b + y {
  a: b; }
CSS
.a >
.b
+ x {a: b}
.c
> .d +
y {@extend x}
SCSS
  end

  # Loops

  def test_extend_self_loop
    assert_equal <<CSS, render(<<SCSS)
.foo {
  a: b; }
CSS
.foo {a: b; @extend .foo}
SCSS
  end

  def test_basic_extend_loop
    assert_equal <<CSS, render(<<SCSS)
.foo, .bar {
  a: b; }

.bar, .foo {
  c: d; }
CSS
.foo {a: b; @extend .bar}
.bar {c: d; @extend .foo}
SCSS
  end

  def test_three_level_extend_loop
    assert_equal <<CSS, render(<<SCSS)
.foo, .baz, .bar {
  a: b; }

.bar, .foo, .baz {
  c: d; }

.baz, .bar, .foo {
  e: f; }
CSS
.foo {a: b; @extend .bar}
.bar {c: d; @extend .baz}
.baz {e: f; @extend .foo}
SCSS
  end

  def test_nested_extend_loop
    assert_equal <<CSS, render(<<SCSS)
.bar, .bar .foo {
  a: b; }
  .bar .foo {
    c: d; }
CSS
.bar {
  a: b;
  .foo {c: d; @extend .bar}
}
SCSS
  end

  def test_cross_loop
    # The first law of extend means the selector should stick around.
    assert_equal <<CSS, render(<<SCSS)
.foo.bar, .foo, .bar {
  a: b; }
CSS
.foo.bar {a: b}
.foo {@extend .bar}
.bar {@extend .foo}
SCSS
  end

  def test_multiple_extender_merges_with_superset_selector
    assert_equal <<CSS, render(<<SCSS)
a.bar.baz, a.foo {
  a: b; }
CSS
.foo {@extend .bar; @extend .baz}
a.bar.baz {a: b}
SCSS
  end

  def test_control_flow_if
    assert_equal <<CSS, render(<<SCSS)
.true, .also-true {
  color: green; }

.false, .also-false {
  color: red; }
CSS
.true  { color: green; }
.false { color: red;   }
.also-true {
  @if true { @extend .true;  }
  @else    { @extend .false; }
}
.also-false {
  @if false { @extend .true;  }
  @else     { @extend .false; }
}
SCSS
  end

  def test_control_flow_for
    assert_equal <<CSS, render(<<SCSS)
.base-0, .added {
  color: green; }

.base-1, .added {
  display: block; }

.base-2, .added {
  border: 1px solid blue; }
CSS
.base-0  { color: green; }
.base-1  { display: block; }
.base-2  { border: 1px solid blue; }
.added {
  @for $i from 0 to 3 {
    @extend .base-\#{$i};
  }
}
SCSS
  end

  def test_control_flow_while
    assert_equal <<CSS, render(<<SCSS)
.base-0, .added {
  color: green; }

.base-1, .added {
  display: block; }

.base-2, .added {
  border: 1px solid blue; }
CSS
.base-0  { color: green; }
.base-1  { display: block; }
.base-2  { border: 1px solid blue; }
.added {
  $i : 0;
  @while $i < 3 {
    @extend .base-\#{$i};
    $i : $i + 1;
  }
}
SCSS
  end

  def test_basic_placeholder_selector
    assert_extends '%foo', '.bar {@extend %foo}', '.bar'
  end

  def test_unused_placeholder_selector
    assert_equal <<CSS, render(<<SCSS)
.baz {
  color: blue; }
CSS
%foo {color: blue}
%bar {color: red}
.baz {@extend %foo}
SCSS
  end

  def test_placeholder_descendant_selector
    assert_extends '#context %foo a', '.bar {@extend %foo}', '#context .bar a'
  end

  def test_semi_placeholder_selector
    assert_equal <<CSS, render(<<SCSS)
.bar .baz {
  color: blue; }
CSS
#context %foo, .bar .baz {color: blue}
SCSS
  end

  def test_placeholder_selector_with_multiple_extenders
    assert_equal <<CSS, render(<<SCSS)
.bar, .baz {
  color: blue; }
CSS
%foo {color: blue}
.bar {@extend %foo}
.baz {@extend %foo}
SCSS
  end

  def test_placeholder_selector_as_modifier
    assert_equal <<CSS, render(<<SCSS)
a.baz.bar {
  color: blue; }
CSS
a%foo.baz {color: blue}
.bar {@extend %foo}
div {@extend %foo}
SCSS
  end

  def test_placeholder_interpolation
    assert_equal <<CSS, render(<<SCSS)
.bar {
  color: blue; }
CSS
$foo: foo;

%\#{$foo} {color: blue}
.bar {@extend %foo}
SCSS
  end

  def test_placeholder_in_selector_pseudoclass
    assert_equal <<CSS, render(<<SCSS)
:matches(.bar, .baz) {
  color: blue; }
CSS
:matches(%foo) {color: blue}
.bar {@extend %foo}
.baz {@extend %foo}
SCSS
  end

  def test_media_in_placeholder_selector
    assert_equal <<CSS, render(<<SCSS)
.baz {
  c: d; }
CSS
%foo {bar {@media screen {a: b}}}
.baz {c: d}
SCSS
  end

  def test_extend_out_of_media
    assert_raise_message(Sass::SyntaxError, <<ERR) {render(<<SCSS)}
You may not @extend an outer selector from within @media.
You may only @extend selectors within the same directive.
From "@extend .foo" on line 3 of test_extend_out_of_media_inline.scss.
ERR
.foo {a: b}
@media screen {
  .bar {@extend .foo}
}
SCSS
  end

  def test_extend_out_of_unknown_directive
    assert_raise_message(Sass::SyntaxError, <<ERR) {render(<<SCSS)}
You may not @extend an outer selector from within @flooblehoof.
You may only @extend selectors within the same directive.
From "@extend .foo" on line 3 of test_extend_out_of_unknown_directive_inline.scss.
ERR
.foo {a: b}
@flooblehoof {
  .bar {@extend .foo}
}
SCSS
  end

  def test_extend_out_of_nested_directives
    assert_raise_message(Sass::SyntaxError, <<ERR) {render(<<SCSS)}
You may not @extend an outer selector from within @flooblehoof.
You may only @extend selectors within the same directive.
From "@extend .foo" on line 4 of test_extend_out_of_nested_directives_inline.scss.
ERR
@media screen {
  .foo {a: b}
  @flooblehoof {
    .bar {@extend .foo}
  }
}
SCSS
  end

  def test_extend_within_media
    assert_equal(<<CSS, render(<<SCSS))
@media screen {
  .foo, .bar {
    a: b; } }
CSS
@media screen {
  .foo {a: b}
  .bar {@extend .foo}
}
SCSS
  end

  def test_extend_within_unknown_directive
    assert_equal(<<CSS, render(<<SCSS))
@flooblehoof {
  .foo, .bar {
    a: b; } }
CSS
@flooblehoof {
  .foo {a: b}
  .bar {@extend .foo}
}
SCSS
  end

  def test_extend_within_nested_directives
    assert_equal(<<CSS, render(<<SCSS))
@media screen {
  @flooblehoof {
    .foo, .bar {
      a: b; } } }
CSS
@media screen {
  @flooblehoof {
    .foo {a: b}
    .bar {@extend .foo}
  }
}
SCSS
  end

  def test_extend_within_disparate_media
    assert_equal(<<CSS, render(<<SCSS))
@media screen {
  .foo, .bar {
    a: b; } }
CSS
@media screen {.foo {a: b}}
@media screen {.bar {@extend .foo}}
SCSS
  end

  def test_extend_within_disparate_unknown_directive
    assert_equal(<<CSS, render(<<SCSS))
@flooblehoof {
  .foo, .bar {
    a: b; } }
@flooblehoof {}
CSS
@flooblehoof {.foo {a: b}}
@flooblehoof {.bar {@extend .foo}}
SCSS
  end

  def test_extend_within_disparate_nested_directives
    assert_equal(<<CSS, render(<<SCSS))
@media screen {
  @flooblehoof {
    .foo, .bar {
      a: b; } } }
@media screen {
  @flooblehoof {} }
CSS
@media screen {@flooblehoof {.foo {a: b}}}
@media screen {@flooblehoof {.bar {@extend .foo}}}
SCSS
  end

  def test_extend_within_and_without_media
    assert_raise_message(Sass::SyntaxError, <<ERR) {render(<<SCSS)}
You may not @extend an outer selector from within @media.
You may only @extend selectors within the same directive.
From "@extend .foo" on line 4 of test_extend_within_and_without_media_inline.scss.
ERR
.foo {a: b}
@media screen {
  .foo {c: d}
  .bar {@extend .foo}
}
SCSS
  end

  def test_extend_within_and_without_unknown_directive
    assert_raise_message(Sass::SyntaxError, <<ERR) {render(<<SCSS)}
You may not @extend an outer selector from within @flooblehoof.
You may only @extend selectors within the same directive.
From "@extend .foo" on line 4 of test_extend_within_and_without_unknown_directive_inline.scss.
ERR
.foo {a: b}
@flooblehoof {
  .foo {c: d}
  .bar {@extend .foo}
}
SCSS
  end

  def test_extend_within_and_without_nested_directives
    assert_raise_message(Sass::SyntaxError, <<ERR) {render(<<SCSS)}
You may not @extend an outer selector from within @flooblehoof.
You may only @extend selectors within the same directive.
From "@extend .foo" on line 5 of test_extend_within_and_without_nested_directives_inline.scss.
ERR
@media screen {
  .foo {a: b}
  @flooblehoof {
    .foo {c: d}
    .bar {@extend .foo}
  }
}
SCSS
  end

  def test_extend_with_subject_transfers_subject_to_extender
    silence_warnings {assert_equal(<<CSS, render(<<SCSS))}
foo bar! baz, foo .bip .bap! baz, .bip foo .bap! baz {
  a: b; }
CSS
foo bar! baz {a: b}
.bip .bap {@extend bar}
SCSS

    silence_warnings {assert_equal(<<CSS, render(<<SCSS))}
foo.x bar.y! baz.z, foo.x .bip bar.bap! baz.z, .bip foo.x bar.bap! baz.z {
  a: b; }
CSS
foo.x bar.y! baz.z {a: b}
.bip .bap {@extend .y}
SCSS
  end

  def test_extend_with_subject_retains_subject_on_target
    silence_warnings {assert_equal(<<CSS, render(<<SCSS))}
.foo! .bar, .foo! .bip .bap, .bip .foo! .bap {
  a: b; }
CSS
.foo! .bar {a: b}
.bip .bap {@extend .bar}
SCSS
  end

  def test_extend_with_subject_transfers_subject_to_target
    silence_warnings {assert_equal(<<CSS, render(<<SCSS))}
a.foo .bar, .bip a.bap! .bar {
  a: b; }
CSS
a.foo .bar {a: b}
.bip .bap! {@extend .foo}
SCSS
  end

  def test_extend_with_subject_retains_subject_on_extender
    silence_warnings {assert_equal(<<CSS, render(<<SCSS))}
.foo .bar, .foo .bip! .bap, .bip! .foo .bap {
  a: b; }
CSS
.foo .bar {a: b}
.bip! .bap {@extend .bar}
SCSS
  end

  def test_extend_with_subject_fails_with_conflicting_subject
    silence_warnings {assert_equal(<<CSS, render(<<SCSS))}
x! .bar {
  a: b; }
CSS
x! .bar {a: b}
y! .bap {@extend .bar}
SCSS
  end

  def test_extend_warns_when_extendee_doesnt_exist
    assert_raise_message(Sass::SyntaxError, <<ERR) {render(<<SCSS)}
".foo" failed to @extend ".bar".
The selector ".bar" was not found.
Use "@extend .bar !optional" if the extend should be able to fail.
ERR
.foo {@extend .bar}
SCSS
  end

  def test_extend_succeeds_when_one_extension_fails_but_others_dont
    assert_equal(<<CSS, render(<<SCSS))
a.bar {
  a: b; }

.bar, b.foo {
  c: d; }
CSS
a.bar {a: b}
.bar {c: d}
b.foo {@extend .bar}
SCSS
  end

  def test_optional_extend_succeeds_when_extendee_doesnt_exist
    assert_equal("", render(<<SCSS))
.foo {@extend .bar !optional}
SCSS
  end

  def test_optional_extend_succeeds_when_extension_fails
    assert_equal(<<CSS, render(<<SCSS))
a.bar {
  a: b; }
CSS
a.bar {a: b}
b.foo {@extend .bar !optional}
SCSS
  end

  # Regression Tests

  def test_extend_with_middle_pseudo
    assert_equal(<<CSS, render(<<SCSS))
.btn:active.focus, :active.focus:before {
  a: b; }
CSS
.btn:active.focus {a: b}
:before {@extend .btn}
SCSS
  end

  def test_extend_parent_selector_suffix
    assert_equal <<CSS, render(<<SCSS)
.a-b, .c {
  x: y; }
CSS
.a {&-b {x: y}}
.c {@extend .a-b}
SCSS
  end

  def test_pseudo_element_superselector
    # Pseudo-elements shouldn't be removed in superselector calculations.
    assert_equal <<CSS, render(<<SCSS)
a#bar, a#bar::fblthp {
  a: b; }
CSS
%x#bar {a: b} // Add an id to make the results have high specificity
%y, %y::fblthp {@extend %x}
a {@extend %y}
SCSS

    # Pseudo-classes can be removed when the second law allows.
    assert_equal <<CSS, render(<<SCSS)
a#bar {
  a: b; }
CSS
%x#bar {a: b}
%y, %y:fblthp {@extend %x}
a {@extend %y}
SCSS

    # A few pseudo-elements can be written as pseudo-elements for historical
    # reasons. See http://www.w3.org/TR/selectors4/#pseudo-elements.
    %w[first-line first-letter before after].each do |pseudo|
      assert_equal <<CSS, render(<<SCSS)
a#bar, a#bar:#{pseudo} {
  a: b; }
CSS
%x#bar {a: b}
%y, %y:#{pseudo} {@extend %x}
a {@extend %y}
SCSS
    end
  end

  def test_multiple_source_redundancy_elimination
    assert_equal <<CSS, render(<<SCSS)
.test-case, .test-case:active {
  color: red; }

.test-case:hover {
  color: green; }
CSS
%default-color {color: red}
%alt-color {color: green}

%default-style {
  @extend %default-color;
  &:hover {@extend %alt-color}
  &:active {@extend %default-color}
}

.test-case {@extend %default-style}
SCSS
  end

  def test_nested_sibling_extend
    assert_equal <<CSS, render(<<SCSS)
.parent .bar, .parent .foo {
  width: 2000px; }
CSS
.foo {@extend .bar}

.parent {
  .bar {
    width: 2000px;
  }
  .foo {
    @extend .bar
  }
}
SCSS
  end

  def test_parent_and_sibling_extend
    assert_equal <<CSS, render(<<SCSS)
.parent1 .parent2 .child1.child2, .parent2 .parent1 .child1.child2 {
  c: d; }
CSS
%foo %bar%baz {c: d}

.parent1 {
  @extend %foo;
  .child1 {@extend %bar}
}

.parent2 {
  @extend %foo;
  .child2 {@extend %baz}
}
SCSS
  end

  def test_nested_extend_specificity
    assert_equal <<CSS, render(<<SCSS)
a :b, a :b:c {
  a: b; }
CSS
%foo {a: b}

a {
  :b {@extend %foo}
  :b:c {@extend %foo}
}
SCSS
  end

  def test_nested_double_extend_optimization
    assert_equal <<CSS, render(<<SCSS)
.parent1 .child {
  a: b; }
CSS
%foo %bar {
  a: b;
}

.parent1 {
  @extend %foo;

  .child {
    @extend %bar;
  }
}

.parent2 {
  @extend %foo;
}
SCSS
  end

  def test_extend_in_double_nested_media_query
    assert_equal <<CSS, render(<<SCSS)
@media all and (orientation: landscape) {
  .bar {
    color: blue; } }
CSS
@media all {
  @media (orientation: landscape) {
    %foo {color: blue}
    .bar {@extend %foo}
  }
}
SCSS
  end

  def test_partially_failed_extend
    assert_no_warning {assert_equal(<<CSS, render(<<SCSS))}
.rc, test {
  color: white; }

.prices span.pill span.rc {
  color: red; }
CSS
test { @extend .rc; }
.rc {color: white;}
.prices span.pill span.rc {color: red;}
SCSS
  end

  def test_newline_near_combinator
    assert_equal <<CSS, render(<<SCSS)
.a +
.b x, .a +
.b .c y, .c .a +
.b y {
  a: b; }
CSS
.a +
.b x {a: b}
.c y {@extend x}
SCSS
  end

  def test_duplicated_selector_with_newlines
    assert_equal(<<CSS, render(<<SCSS))
.example-1-1,
.example-1-2,
.my-page-1 .my-module-1-1,
.example-1-3 {
  a: b; }
CSS
.example-1-1,
.example-1-2,
.example-1-3 {
  a: b;
}

.my-page-1 .my-module-1-1 {@extend .example-1-2}
SCSS
  end

  def test_nested_selector_with_child_selector_hack_extendee
    assert_extends '> .foo', 'foo bar {@extend .foo}', '> .foo, > foo bar'
  end

  def test_nested_selector_with_child_selector_hack_extender
    assert_extends '.foo .bar', '> foo bar {@extend .bar}', '.foo .bar, > .foo foo bar, > foo .foo bar'
  end

  def test_nested_selector_with_child_selector_hack_extender_and_extendee
    assert_extends '> .foo', '> foo bar {@extend .foo}', '> .foo, > foo bar'
  end

  def test_nested_selector_with_child_selector_hack_extender_and_sibling_selector_extendee
    assert_extends '~ .foo', '> foo bar {@extend .foo}', '~ .foo'
  end

  def test_nested_selector_with_child_selector_hack_extender_and_extendee_and_newline
    assert_equal <<CSS, render(<<SCSS)
> .foo, > flip,
> foo bar {
  a: b; }
CSS
> .foo {a: b}
flip,
> foo bar {@extend .foo}
SCSS
  end

  def test_extended_parent_and_child_redundancy_elimination
    assert_equal <<CSS, render(<<SCSS)
a b, d b, a c, d c {
  a: b; }
CSS
a {
  b {a: b}
  c {@extend b}
}
d {@extend a}
SCSS
  end

  def test_extend_redundancy_elimination_when_it_would_reduce_specificity
    assert_extends 'a', 'a.foo {@extend a}', 'a, a.foo'
  end

  def test_extend_redundancy_elimination_when_it_would_preserve_specificity
    assert_extends '.bar a', 'a.foo {@extend a}', '.bar a'
  end

  def test_extend_redundancy_elimination_never_eliminates_base_selector
    assert_extends 'a.foo', '.foo {@extend a}', 'a.foo, .foo'
  end

  def test_extend_cross_branch_redundancy_elimination
    assert_equal <<CSS, render(<<SCSS)
.a .c .d, .b .c .a .d {
  a: b; }
CSS
%x .c %y {a: b}
.a, .b {@extend %x}
.a .d {@extend %y}
SCSS

    assert_equal <<CSS, render(<<SCSS)
.e .a .c .d, .a .c .e .d, .e .b .c .a .d, .b .c .a .e .d {
  a: b; }
CSS
.e %z {a: b}
%x .c %y {@extend %z}
.a, .b {@extend %x}
.a .d {@extend %y}
SCSS
  end

  private

  def assert_extend_doesnt_match(extender, target, reason, line, syntax = :scss)
    message = "\"#{extender}\" failed to @extend \"#{target}\"."
    reason = 
      if reason == :not_found
        "The selector \"#{target}\" was not found."
      else
        "No selectors matching \"#{target}\" could be unified with \"#{extender}\"."
      end

    assert_raise_message(Sass::SyntaxError, <<ERR) {yield}
#{message}
#{reason}
Use "@extend #{target} !optional" if the extend should be able to fail.
ERR
  end

  def assert_unification(selector, extension, unified, nested = true)
    # Do some trickery so the first law of extend doesn't get in our way.
    assert_extends(
      "%-a #{selector}",
      extension + " -a {@extend %-a}",
      unified.split(', ').map {|s| "-a #{s}"}.join(', '))
  end

  def assert_specificity_equals(sel1, sel2)
    assert_specificity_gte(sel1, sel2)
    assert_specificity_gte(sel2, sel1)
  end

  def assert_specificity_gte(sel1, sel2)
    assert_equal <<CSS, render(<<SCSS)
#{sel1} .a {
  a: b; }
CSS
#{sel1} %-a {a: b}
.a {@extend %-a}
#{sel2}.a {@extend %-a}
SCSS
  end

  def render_unification(selector, extension)
    render_extends(
      "%-a #{selector}",
      extension + " -a {@extend %-a}")
  end

  def assert_extends(selector, extension, result)
    assert_equal <<CSS, render_extends(selector, extension)
#{result} {
  a: b; }
CSS
  end

  def assert_extends_to_nothing(selector, extension)
    assert_equal '', render_extends(selector, extension)
  end

  def render_extends(selector, extension)
    render(<<SCSS)
#{selector} {a: b}
#{extension}
SCSS
  end

  def render(sass, options = {})
    options = {:syntax => :scss}.merge(options)
    munge_filename options
    Sass::Engine.new(sass, options).render
  end
end