@@ -558,3 +558,111 @@ def test_mixed_numeric_bool_folded(source, expected):
558558 pytest .skip ('NameConstant not in python < 3.4' )
559559
560560 run_test (source , expected )
561+
562+
563+ @pytest .mark .parametrize (
564+ ('source' , 'expected' ), [
565+ ('1 or 2' , '1' ),
566+ ('0 or 2' , '2' ),
567+ ('1 and 2' , '2' ),
568+ ('0 and 2' , '0' ),
569+ ]
570+ )
571+ def test_boolop_numeric_folded (source , expected ):
572+ """
573+ Test that and/or expressions with numeric operands are folded.
574+
575+ BoolOp returns an operand value, not necessarily a bool.
576+ """
577+ run_test (source , expected )
578+
579+
580+ @pytest .mark .parametrize (
581+ ('source' , 'expected' ), [
582+ ('True and False' , 'False' ),
583+ ('False or True' , 'True' ),
584+ ('None or False' , 'False' ),
585+ ('True and True and True' , 'True' ),
586+ ]
587+ )
588+ def test_boolop_folded (source , expected ):
589+ """
590+ Test that and/or expressions with constant operands are folded.
591+ """
592+ if sys .version_info < (3 , 4 ):
593+ pytest .skip ('NameConstant not in python < 3.4' )
594+
595+ run_test (source , expected )
596+
597+
598+ @pytest .mark .parametrize (
599+ ('source' , 'expected' ), [
600+ # Partially constant expressions must not fold, even where
601+ # short circuit evaluation makes the result predictable
602+ ('True and x' , 'True and x' ),
603+ ('True or x' , 'True or x' ),
604+ ('x and False' , 'x and False' ),
605+
606+ # fold() doesn't construct None constants, only bool and numbers
607+ ('False or None' , 'False or None' ),
608+ ]
609+ )
610+ def test_boolop_not_folded (source , expected ):
611+ """
612+ Test that and/or expressions with non-constant operands are not folded.
613+ """
614+ if sys .version_info < (3 , 4 ):
615+ pytest .skip ('NameConstant not in python < 3.4' )
616+
617+ run_test (source , expected )
618+
619+
620+ @pytest .mark .parametrize (
621+ ('source' , 'expected' ), [
622+ ('True is True' , 'True' ),
623+ ('True is not False' , 'True' ),
624+ ('None is None' , 'True' ),
625+ ('True == True' , 'True' ),
626+ ('1 < 2 < 3' , 'True' ),
627+ ('3 < 2 < 1 < 0' , 'False' ),
628+ ('1 == 1.0' , 'True' ),
629+ ]
630+ )
631+ def test_compare_folded (source , expected ):
632+ """
633+ Test that comparisons of constants are folded.
634+ """
635+ if sys .version_info < (3 , 4 ):
636+ pytest .skip ('NameConstant not in python < 3.4' )
637+
638+ run_test (source , expected )
639+
640+
641+ @pytest .mark .parametrize (
642+ ('source' , 'expected' ), [
643+ # Not smaller than the folded result
644+ ('1 < 2' , '1 < 2' ),
645+ ('1 == 1' , '1 == 1' ),
646+
647+ # Identity of non-singletons is implementation defined
648+ ('1000 is 1000' , '1000 is 1000' ),
649+ ('1000 is not 1000' , '1000 is not 1000' ),
650+ ('1 < 2 is 2' , '1 < 2 is 2' ),
651+
652+ # Raises at runtime, must be preserved
653+ ('1 in 2' , '1 in 2' ),
654+ ('1 < None' , '1 < None' ),
655+
656+ # Not constant
657+ ('x < 2' , 'x < 2' ),
658+ ('True is x' , 'True is x' ),
659+ ]
660+ )
661+ def test_compare_not_folded (source , expected ):
662+ """
663+ Test comparisons that must not be folded.
664+ """
665+ if sys .version_info < (3 , 4 ):
666+ pytest .skip ('NameConstant not in python < 3.4' )
667+
668+ run_test (source , expected )
0 commit comments