<commit_before># Copyright (c) 2015 <NAME>.V.
# Uranium is released under the terms of the AGPLv3 or higher.

import unittest
import collections

from UM.Signal import Signal
from UM.i18n import i18nCatalog

from UM.Settings.MachineManager import MachineManager
from UM.Settings.Setting import Setting

class TestMachineManager():
    def __init__(self):
        self.activeProfileChanged = Signal()

    def getActiveProfile(self):
        return None

class TestProfile():
    def __init__(self, root_setting):
        self._root_setting = root_setting

    def getSettingValue(self, key):
        setting = self._root_setting.getSetting(key)
        if setting:
            return setting.getDefaultValue()

        return None

class TestSetting(unittest.TestCase):
    def setUp(self):
        # Called before the first testfunction is executed
        self._machine_manager = TestMachineManager()
        self._catalog = i18nCatalog("TestSetting")

    def tearDown(self):
        # Called after the last testfunction was executed
        self._machine_manager = None
        self._catalog = None

    def test_construct(self):
        # Most basic construction, only required arguments.
        setting = Setting(self._machine_manager, "test", self._catalog)

        # This check is mostly to see if the object was constructed properly
        self.assertIsInstance(setting, Setting)
        self.assertEqual("test", setting.getKey())

        # Construct with keyword arguments
        setting = Setting(
            self._machine_manager, "test", self._catalog,
            label = "Test Setting",
            type = "string",
        )

        self.assertIsInstance(setting, Setting)
        self.assertEqual("test", setting.getKey())
        self.assertEqual("Test Setting", setting.getLabel())
        self.assertEqual("string", setting.getType())

    def test_fillByDict(self):
        setting = Setting(self._machine_manager, "test", self._catalog)

        data = {
            "type": "int",
            "default": 4,
            "label": "Test Setting",
            "description": "A Test Setting",
            "unit": "furlongs per fortnight",
            "visible": True
        }

        setting.fillByDict(data)

        self.assertEqual("int", setting.getType())
        self.assertEqual(4, setting.getDefaultValue())
        self.assertEqual("Test Setting", setting.getLabel())
        self.assertEqual("A Test Setting", setting.getDescription())
        self.assertEqual("furlongs per fortnight", setting.getUnit())
        self.assertTrue(setting.isVisible())

    def test_fillByDictWithChildren(self):
        setting = Setting(self._machine_manager, "test", self._catalog)

        data = {
            "type": "int",
            "default": 4,
            "label": "Test Setting",
            "description": "A Test Setting",
            "unit": "furlongs per fortnight",
            "visible": True,

            # Since python's dict is unordered but we want an ordered dict we
            # have to go through a bit of magic to make sure we define it as an
            # ordered dict.
            "children": collections.OrderedDict(sorted({
                "test_child1": {
                    "type": "int",
                    "default": 9,
                    "label": "Test Child 1",
                    "description": "Test Setting Child 1",
                },
                "test_child2": {
                    "type": "int",
                    "default": 5,
                    "label": "Test Child 2",
                    "description": "Test Setting Child 2",
                    "inherit": False
                },
                "test_child3": {
                    "type": "int",
                    "default": 99,
                    "label": "Test Child 3",
                    "description": "Test Setting Child 3",
                    "inherit_function": "parent_value * 10"
                }
            }.items(), key = lambda k: k[0]))
        }

        setting.fillByDict(data)

        profile = TestProfile(setting)

        self.assertEqual(len(setting.getChildren()), 3)

        child1 = setting.getChildren()[0]
        child2 = setting.getChildren()[1]
        child3 = setting.getChildren()[2]
        # Children should keep the order in which they were defined
        self.assertEqual("test_child1", child1.getKey())
        self.assertEqual("test_child2", child2.getKey())
        self.assertEqual("test_child3", child3.getKey())

        # All children should have "setting" as their parent.
        self.assertEqual(setting, child1.getParent())
        self.assertEqual(setting, child2.getParent())
        self.assertEqual(setting, child3.getParent())

        # Child visibility should not affect parent visibility.
        self.assertTrue(setting.isVisible())

        # Child 1 uses default inheritance so should inherit its parent value
        self.assertEqual(4, child1.getDefaultValue(profile))

        # Child 2 does not inherit so should return its default value
        self.assertEqual(5, child2.getDefaultValue(profile))

        # Child 3 uses an inherit function and should return parent's value * 10.
        self.assertEqual(40, child3.getDefaultValue(profile))

    def test_parseValue(self):
        setting = Setting(self._machine_manager, "test", self._catalog, type = "int")

        self.assertEqual(setting.parseValue("1"), 1)
        self.assertEqual(setting.parseValue(1), 1)
        self.assertEqual(setting.parseValue(True), 1)
        self.assertEqual(setting.parseValue(False), 0)
        self.assertEqual(setting.parseValue(1.1), 1)

        # An error value should return a default value valid for the setting type.
        self.assertEqual(setting.parseValue("something"), 0)

        setting = Setting(self._machine_manager, "test", self._catalog, type = "bool")<commit_msg>Use the right type for the Boolean setting in TestSetting<commit_after># Copyright (c) 2015 <NAME>.
# Uranium is released under the terms of the AGPLv3 or higher.

import unittest
import collections

from UM.Signal import Signal
from UM.i18n import i18nCatalog

from UM.Settings.MachineManager import MachineManager
from UM.Settings.Setting import Setting

class TestMachineManager():
    def __init__(self):
        self.activeProfileChanged = Signal()

    def getActiveProfile(self):
        return None

class TestProfile():
    def __init__(self, root_setting):
        self._root_setting = root_setting

    def getSettingValue(self, key):
        setting = self._root_setting.getSetting(key)
        if setting:
            return setting.getDefaultValue()

        return None

class TestSetting(unittest.TestCase):
    def setUp(self):
        # Called before the first testfunction is executed
        self._machine_manager = TestMachineManager()
        self._catalog = i18nCatalog("TestSetting")

    def tearDown(self):
        # Called after the last testfunction was executed
        self._machine_manager = None
        self._catalog = None

    def test_construct(self):
        # Most basic construction, only required arguments.
        setting = Setting(self._machine_manager, "test", self._catalog)

        # This check is mostly to see if the object was constructed properly
        self.assertIsInstance(setting, Setting)
        self.assertEqual("test", setting.getKey())

        # Construct with keyword arguments
        setting = Setting(
            self._machine_manager, "test", self._catalog,
            label = "Test Setting",
            type = "string",
        )

        self.assertIsInstance(setting, Setting)
        self.assertEqual("test", setting.getKey())
        self.assertEqual("Test Setting", setting.getLabel())
        self.assertEqual("string", setting.getType())

    def test_fillByDict(self):
        setting = Setting(self._machine_manager, "test", self._catalog)

        data = {
            "type": "int",
            "default": 4,
            "label": "Test Setting",
            "description": "A Test Setting",
            "unit": "furlongs per fortnight",
            "visible": True
        }

        setting.fillByDict(data)

        self.assertEqual("int", setting.getType())
        self.assertEqual(4, setting.getDefaultValue())
        self.assertEqual("Test Setting", setting.getLabel())
        self.assertEqual("A Test Setting", setting.getDescription())
        self.assertEqual("furlongs per fortnight", setting.getUnit())
        self.assertTrue(setting.isVisible())

    def test_fillByDictWithChildren(self):
        setting = Setting(self._machine_manager, "test", self._catalog)

        data = {
            "type": "int",
            "default": 4,
            "label": "Test Setting",
            "description": "A Test Setting",
            "unit": "furlongs per fortnight",
            "visible": True,

            # Since python's dict is unordered but we want an ordered dict we
            # have to go through a bit of magic to make sure we define it as an
            # ordered dict.
            "children": collections.OrderedDict(sorted({
                "test_child1": {
                    "type": "int",
                    "default": 9,
                    "label": "Test Child 1",
                    "description": "Test Setting Child 1",
                },
                "test_child2": {
                    "type": "int",
                    "default": 5,
                    "label": "Test Child 2",
                    "description": "Test Setting Child 2",
                    "inherit": False
                },
                "test_child3": {
                    "type": "int",
                    "default": 99,
                    "label": "Test Child 3",
                    "description": "Test Setting Child 3",
                    "inherit_function": "parent_value * 10"
                }
            }.items(), key = lambda k: k[0]))
        }

        setting.fillByDict(data)

        profile = TestProfile(setting)

        self.assertEqual(len(setting.getChildren()), 3)

        child1 = setting.getChildren()[0]
        child2 = setting.getChildren()[1]
        child3 = setting.getChildren()[2]
        # Children should keep the order in which they were defined
        self.assertEqual("test_child1", child1.getKey())
        self.assertEqual("test_child2", child2.getKey())
        self.assertEqual("test_child3", child3.getKey())

        # All children should have "setting" as their parent.
        self.assertEqual(setting, child1.getParent())
        self.assertEqual(setting, child2.getParent())
        self.assertEqual(setting, child3.getParent())

        # Child visibility should not affect parent visibility.
        self.assertTrue(setting.isVisible())

        # Child 1 uses default inheritance so should inherit its parent value
        self.assertEqual(4, child1.getDefaultValue(profile))

        # Child 2 does not inherit so should return its default value
        self.assertEqual(5, child2.getDefaultValue(profile))

        # Child 3 uses an inherit function and should return parent's value * 10.
        self.assertEqual(40, child3.getDefaultValue(profile))

    def test_parseValue(self):
        setting = Setting(self._machine_manager, "test", self._catalog, type = "int")

        self.assertEqual(setting.parseValue("1"), 1)
        self.assertEqual(setting.parseValue(1), 1)
        self.assertEqual(setting.parseValue(True), 1)
        self.assertEqual(setting.parseValue(False), 0)
        self.assertEqual(setting.parseValue(1.1), 1)

        # An error value should return a default value valid for the setting type.
        self.assertEqual(setting.parseValue("something"), 0)

        setting = Setting(self._machine_manager, "test", self._catalog, type = "boolean")
{'fraction_non_alphanumeric': 0.10439420878841758, 'fraction_numerical': 0.01151468969604606, 'mean_word_length': 2.238826432684398, 'pattern_counts': {'":': 60, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2242956', 'n_tokens_mistral': 3085, 'n_tokens_neox': 2928, 'n_words': 848}