def test_enable_incompatible_rule_knockout_is_saved(self):
        from castervoice.lib import utilities
        from castervoice.lib.ctrl.rules_config import RulesConfig
        from castervoice.rules.ccr.java_rules import java
        from castervoice.rules.ccr.python_rules import python

        # "write" the rules.toml file:
        self._setup_rules_config_file(loadable_true=["Java", "Python"],
                                      enabled=["Java"])

        # check that the mock file changes were written
        self.assertEqual(
            1, len(self._rule_config._config[RulesConfig._ENABLED_ORDERED]))

        # initialize the gm
        a, b = java.get_rule(), python.get_rule()
        self._initialize(FullContentSet([a, b], [], []))

        # simulate a spoken "enable" command from the GrammarActivator:
        self._gm._change_rule_enabled("Python", True)
        # afterwards, the config should have Python enabled but not Java
        config = utilities.load_toml_file(
            TestGrammarManager._MOCK_PATH_RULES_CONFIG)
        self.assertNotIn("Java", config[RulesConfig._ENABLED_ORDERED])
        self.assertIn("Python", config[RulesConfig._ENABLED_ORDERED])
    def test_knockout_with_companions_saves_correctly(self):
        from castervoice.lib import utilities
        from castervoice.lib.ctrl.rules_config import RulesConfig
        from castervoice.rules.ccr.java_rules import java
        from castervoice.rules.ccr.java_rules import java2
        from castervoice.rules.ccr.python_rules import python
        from castervoice.rules.ccr.python_rules import python2
        from castervoice.rules.core.alphabet_rules import alphabet
        from castervoice.rules.apps.microsoft_office import outlook

        # "write" the companion config file
        self._setup_config_file(utilities, ["paths", "COMPANION_CONFIG_PATH"],
                                TestGrammarManager._MOCK_PATH_COMPANION_CONFIG,
                                {
                                    "Java": ["JavaNon"],
                                    "Python": ["PythonNon"]
                                })

        # "write" the rules.toml file:
        self._setup_rules_config_file(
            loadable_true=[
                "Alphabet", "OutlookRule", "Java", "JavaNon", "Python",
                "PythonNon"
            ],
            enabled=["Alphabet", "OutlookRule", "Java", "JavaNon"])

        # check that the mock file changes were written
        self.assertEqual(
            4, len(self._rule_config._config[RulesConfig._ENABLED_ORDERED]))

        # initialize the gm
        alphabet_rule, outlook_rule = alphabet.get_rule(), outlook.get_rule()
        python_rule, pythonnon_rule = python.get_rule(), python2.get_rule()
        java_rule, javanon_rule = java.get_rule(), java2.get_rule()
        self._initialize(
            FullContentSet([
                alphabet_rule, outlook_rule, python_rule, pythonnon_rule,
                java_rule, javanon_rule
            ], [], []))

        # simulate a spoken "enable" command from the GrammarActivator:
        self._gm._change_rule_enabled("Python", True)
        """
        Afterwards, all of the following should be true:
        1. Python and PythonNon should each be in rules.toml exactly once
        2. Java and JavaNon should be in rules.toml zero times
        3. Alphabet and Outlook should still be in rules.toml
        """
        config = utilities.load_toml_file(
            TestGrammarManager._MOCK_PATH_RULES_CONFIG)
        enabled_ordered = config[RulesConfig._ENABLED_ORDERED]
        self.assertEqual(1, enabled_ordered.count("Python"))
        self.assertEqual(1, enabled_ordered.count("PythonNon"))
        self.assertEqual(0, enabled_ordered.count("Java"))
        self.assertEqual(0, enabled_ordered.count("JavaNon"))
        self.assertEqual(1, enabled_ordered.count("Alphabet"))
        self.assertEqual(1, enabled_ordered.count("OutlookRule"))
    def test_disable_non_enabled_doesnt_crash(self):
        from castervoice.lib.ctrl.rules_config import RulesConfig
        from castervoice.rules.ccr.java_rules import java
        from castervoice.rules.ccr.python_rules import python

        # "write" the rules.toml file:
        self._setup_rules_config_file(loadable_true=["Java", "Python"],
                                      enabled=["Java"])

        # check that the mock file changes were written
        self.assertEqual(
            1, len(self._rule_config._config[RulesConfig._ENABLED_ORDERED]))

        # initialize the gm
        a, b = java.get_rule(), python.get_rule()
        self._initialize(FullContentSet([a, b], [], []))

        # simulate a spoken "enable" command from the GrammarActivator:
        self._gm._change_rule_enabled("Python", False)