@@ -2640,3 +2640,140 @@ def test_combine_expressions_with_duplicated_elements(self):
26402640
26412641 def test_combine_expressions_with_or_relationship (self ):
26422642 assert str (combine_expressions (["mit" , "apache-2.0" ], "OR" )) == "mit OR apache-2.0"
2643+
2644+
2645+ class LicensingRemoveTest (TestCase ):
2646+ def setUp (self ):
2647+ self .licensing = Licensing ()
2648+
2649+ def test_remove_from_and_expression (self ):
2650+ result = self .licensing .remove ("MIT AND Apache-2.0" , "Apache-2.0" )
2651+ assert result .render () == "MIT"
2652+
2653+ def test_remove_from_or_expression (self ):
2654+ result = self .licensing .remove ("MIT OR Apache-2.0" , "Apache-2.0" )
2655+ assert result .render () == "MIT"
2656+
2657+ def test_remove_first_term_from_and (self ):
2658+ result = self .licensing .remove ("MIT AND Apache-2.0" , "MIT" )
2659+ assert result .render () == "Apache-2.0"
2660+
2661+ def test_remove_first_term_from_or (self ):
2662+ result = self .licensing .remove ("MIT OR Apache-2.0" , "MIT" )
2663+ assert result .render () == "Apache-2.0"
2664+
2665+ def test_remove_nested_and_or_expressions (self ):
2666+ expr = "(MIT AND Apache-2.0) OR GPL-2.0"
2667+ result = self .licensing .remove (expr , "Apache-2.0" )
2668+ assert result .render () == "MIT OR GPL-2.0"
2669+
2670+ result = self .licensing .remove (expr , "GPL-2.0" )
2671+ assert result .render () == "MIT AND Apache-2.0"
2672+
2673+ def test_remove_complete_subexpression_string (self ):
2674+ expr = "(MIT AND Apache-2.0) OR GPL-2.0"
2675+ result = self .licensing .remove (expr , "MIT AND Apache-2.0" )
2676+ assert result .render () == "GPL-2.0"
2677+
2678+ def test_remove_complete_subexpression_object (self ):
2679+ expr = self .licensing .parse ("(MIT AND Apache-2.0) OR GPL-2.0" )
2680+ subexpr = self .licensing .parse ("MIT AND Apache-2.0" )
2681+ result = self .licensing .remove (expr , subexpr )
2682+ assert result .render () == "GPL-2.0"
2683+
2684+ def test_remove_nonexistent_license (self ):
2685+ expr = "MIT AND Apache-2.0"
2686+ result = self .licensing .remove (expr , "GPL-2.0" )
2687+ assert result .render () == "MIT AND Apache-2.0"
2688+
2689+ def test_remove_all_terms_single_license (self ):
2690+ result = self .licensing .remove ("MIT" , "MIT" )
2691+ assert result is None
2692+
2693+ def test_remove_all_terms_from_and (self ):
2694+ result = self .licensing .remove ("MIT AND Apache-2.0" , ["MIT" , "Apache-2.0" ])
2695+ assert result is None
2696+
2697+ def test_remove_all_terms_from_or (self ):
2698+ result = self .licensing .remove ("MIT OR Apache-2.0" , ["MIT" , "Apache-2.0" ])
2699+ assert result is None
2700+
2701+ def test_remove_all_terms_from_nested (self ):
2702+ expr = "(MIT AND Apache-2.0) OR GPL-2.0"
2703+ result = self .licensing .remove (expr , ["MIT" , "Apache-2.0" , "GPL-2.0" ])
2704+ assert result is None
2705+
2706+ def test_remove_with_duplicate_terms (self ):
2707+ expr = "MIT AND MIT AND Apache-2.0"
2708+ result = self .licensing .remove (expr , "Apache-2.0" )
2709+ assert result .render () == "MIT"
2710+
2711+ def test_remove_with_parenthesized_complex_expressions (self ):
2712+ expr = "(MIT OR BSD) AND (Apache-2.0 OR GPL-2.0)"
2713+ result = self .licensing .remove (expr , "BSD" )
2714+ assert result .render () == "MIT AND (Apache-2.0 OR GPL-2.0)"
2715+
2716+ result = self .licensing .remove (expr , ["BSD" , "GPL-2.0" ])
2717+ assert result .render () == "MIT AND Apache-2.0"
2718+
2719+ def test_remove_with_expression_exact_composite_string (self ):
2720+ expr = "GPL-2.0 WITH Classpath-exception OR MIT"
2721+ result = self .licensing .remove (expr , "GPL-2.0 WITH Classpath-exception" )
2722+ assert result .render () == "MIT"
2723+
2724+ def test_remove_with_expression_exact_composite_object (self ):
2725+ lic_sym = LicenseSymbol ("GPL-2.0" )
2726+ exc_sym = LicenseSymbol ("Classpath-exception" )
2727+ with_sym = LicenseWithExceptionSymbol (lic_sym , exc_sym )
2728+ expr = "GPL-2.0 WITH Classpath-exception OR MIT"
2729+ result = self .licensing .remove (expr , with_sym )
2730+ assert result .render () == "MIT"
2731+
2732+ def test_remove_with_expression_exact_composite_object_with_known_symbols (self ):
2733+ gpl = LicenseSymbol ("GPL-2.0" )
2734+ exc = LicenseSymbol ("Classpath-exception" , is_exception = True )
2735+ mit = LicenseSymbol ("MIT" )
2736+ licensing = Licensing ([gpl , exc , mit ])
2737+ with_sym = LicenseWithExceptionSymbol (gpl , exc )
2738+ expr = "GPL-2.0 WITH Classpath-exception OR MIT"
2739+ result = licensing .remove (expr , with_sym )
2740+ assert result .render () == "MIT"
2741+
2742+ def test_remove_with_expression_all_removed (self ):
2743+ expr = "GPL-2.0 WITH Classpath-exception"
2744+ result = self .licensing .remove (expr , "GPL-2.0 WITH Classpath-exception" )
2745+ assert result is None
2746+
2747+ def test_remove_base_license_does_not_affect_composite_with_expression (self ):
2748+ expr = "GPL-2.0 WITH Classpath-exception OR GPL-2.0"
2749+ result = self .licensing .remove (expr , "GPL-2.0" )
2750+ assert result .render () == "GPL-2.0 WITH Classpath-exception"
2751+
2752+ def test_remove_with_symbol_object_target (self ):
2753+ expr = "MIT AND Apache-2.0"
2754+ result = self .licensing .remove (expr , LicenseSymbol ("Apache-2.0" ))
2755+ assert result .render () == "MIT"
2756+
2757+ def test_remove_with_parsed_expression_input (self ):
2758+ expr = self .licensing .parse ("MIT AND Apache-2.0" )
2759+ result = self .licensing .remove (expr , "Apache-2.0" )
2760+ assert result .render () == "MIT"
2761+
2762+ def test_remove_empty_or_none_expression (self ):
2763+ assert self .licensing .remove (None , "MIT" ) is None
2764+ assert self .licensing .remove ("" , "MIT" ) is None
2765+ assert self .licensing .remove (" " , "MIT" ) is None
2766+
2767+ def test_remove_empty_or_none_targets (self ):
2768+ expr = "MIT AND Apache-2.0"
2769+ assert self .licensing .remove (expr , None ).render () == "MIT AND Apache-2.0"
2770+ assert self .licensing .remove (expr , []).render () == "MIT AND Apache-2.0"
2771+ assert self .licensing .remove (expr , "" ).render () == "MIT AND Apache-2.0"
2772+
2773+ def test_remove_with_known_symbols_and_aliases (self ):
2774+ gpl2 = LicenseSymbol ("GPL-2.0" , aliases = ["gpl v2" , "gpl2" ])
2775+ mit = LicenseSymbol ("MIT" , aliases = ["mit license" ])
2776+ licensing = Licensing ([gpl2 , mit ])
2777+ expr = "gpl v2 OR mit license"
2778+ result = licensing .remove (expr , "gpl2" )
2779+ assert result .render () == "MIT"
0 commit comments