Beispiel #1
0
    def test_Component_basic_init_set_comment(self):
        """
        Testing set_comment method
        """
        comp = Component("test_component", "Arm")

        comp.set_comment("test comment")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.comment, "test comment")
Beispiel #2
0
    def test_Component_basic_init_set_GROUP(self):
        """
        Testing set_GROUP method
        """

        comp = Component("test_component", "Arm")

        comp.set_GROUP("test group")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.GROUP, "test group")
Beispiel #3
0
    def test_Component_basic_init_set_JUMP(self):
        """
        Testing set_JUMP method
        """

        comp = Component("test_component", "Arm")

        comp.set_JUMP("test jump")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.JUMP, "test jump")
Beispiel #4
0
    def test_Component_basic_init_set_SPLIT(self):
        """
        Testing set_SPLIT method
        """

        comp = Component("test_component", "Arm")

        comp.set_SPLIT(500)

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.SPLIT, 500)
Beispiel #5
0
    def test_Component_basic_init_set_AT(self):
        """
        Testing set_AT method
        """
        comp = Component("test_component", "Arm")

        comp.set_AT([12.124, 214.0, 2], RELATIVE="monochromator")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.AT_data, [12.124, 214.0, 2])
        self.assertEqual(comp.AT_relative, "RELATIVE monochromator")
Beispiel #6
0
    def test_Component_basic_init_set_WHEN(self):
        """
        Testing WHEN method
        """

        comp = Component("test_component", "Arm")

        comp.set_WHEN("1 != 2")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.WHEN, "WHEN (1 != 2)")
Beispiel #7
0
    def test_Component_basic_init_set_RELATIVE(self):
        """
        Testing set_RELATIVE method with string
        """

        comp = Component("test_component", "Arm")

        comp.set_RELATIVE("sample")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.AT_relative, "RELATIVE sample")
        self.assertEqual(comp.ROTATED_relative, "RELATIVE sample")
Beispiel #8
0
    def test_Component_basic_init_set_ROTATED(self):
        """
        Testing set_ROTATED method med relative as string
        """

        comp = Component("test_component", "Arm")

        comp.set_ROTATED([1204.8, 8490.1, 129], RELATIVE="analyzer")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.ROTATED_data, [1204.8, 8490.1, 129])
        self.assertEqual(comp.ROTATED_relative, "RELATIVE analyzer")
Beispiel #9
0
    def test_Component_basic_init_set_ROTATED_Component(self):
        """
        Testing set_ROTATED method with relative as Component object
        """

        prev_component = Component("relative_base", "Arm")
        comp = Component("test_component", "Arm")

        comp.set_ROTATED([1204.8, 8490.1, 129], RELATIVE=prev_component)

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.ROTATED_data, [1204.8, 8490.1, 129])
        self.assertEqual(comp.ROTATED_relative, "RELATIVE relative_base")
Beispiel #10
0
    def test_Component_basic_init_set_AT_Component(self):
        """
        Testing set_AT method using Component object and method
        """

        prev_component = Component("relative_base", "Arm")
        comp = Component("test_component", "Arm")

        comp.set_AT([12.124, 214.0, 2], RELATIVE=prev_component)

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.AT_data, [12.124, 214.0, 2])
        self.assertEqual(comp.AT_relative, "RELATIVE relative_base")
Beispiel #11
0
    def test_Component_basic_object_ref_init_set_RELATIVE(self):
        """
        Testing set_RELATIVE method with Component object input
        """

        prev_component = Component("relative_base", "Arm")
        comp = Component("test_component", "Arm")

        comp.set_RELATIVE(prev_component)

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.AT_relative, "RELATIVE relative_base")
        self.assertEqual(comp.ROTATED_relative, "RELATIVE relative_base")
Beispiel #12
0
    def test_Component_basic_init_set_ROTATED_Component_keyword(self):
        """
        Testing setting ROTATION with keyword and Component object input
        """

        prev_component = Component("relative_base", "Arm")
        comp = Component("test_component",
                         "Arm",
                         ROTATED=[1, 2, 3.0],
                         ROTATED_RELATIVE=prev_component)

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.ROTATED_data, [1, 2, 3.0])
        self.assertEqual(comp.ROTATED_relative, "RELATIVE relative_base")
Beispiel #13
0
    def test_Component_basic_init_set_AT_Component_keyword(self):
        """
        Testing set_AT method using Component object and keyword argument
        """

        prev_component = Component("relative_base", "Arm")
        comp = Component("test_component",
                         "Arm",
                         AT=[1, 2, 3.0],
                         AT_RELATIVE=prev_component)

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.AT_data, [1, 2, 3.0])
        self.assertEqual(comp.AT_relative, "RELATIVE relative_base")
Beispiel #14
0
    def test_Component_basic_init(self):
        """
        Testing basic initialization

        """

        comp = Component("test_component", "Arm")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
Beispiel #15
0
def setup_Component_relative():
    """
    Sets up a Component with the relative keyword used
    """
    return Component("test_component",
                     "Arm",
                     AT=[0.124, 183.9, 157],
                     ROTATED=[482, 1240.2, 0.185],
                     RELATIVE="source",
                     WHEN="1==2",
                     EXTEND="nscat = 8;",
                     GROUP="developers",
                     JUMP="myself 37",
                     SPLIT=7,
                     comment="test comment")
Beispiel #16
0
    def test_Component_freeze(self):
        """
        Testing frozen Component cant have new attributes, and that
        _unfreeze / _freeze works correctly.
        """
        comp = Component("test_component", "Arm")

        with self.assertRaises(AttributeError):
            comp.new_parameter = 5

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")

        comp._unfreeze()
        comp.new_parameter = 5

        self.assertEqual(comp.new_parameter, 5)

        comp._freeze()
        with self.assertRaises(AttributeError):
            comp.another_parameter = 5
Beispiel #17
0
def setup_Component_all_keywords():
    """
    Sets up a Component by using all initialize keywords
    """

    return Component("test_component",
                     "Arm",
                     AT=[0.124, 183.9, 157],
                     AT_RELATIVE="home",
                     ROTATED=[482, 1240.2, 0.185],
                     ROTATED_RELATIVE="etc",
                     WHEN="1==2",
                     EXTEND="nscat = 8;",
                     GROUP="developers",
                     JUMP="myself 37",
                     SPLIT=7,
                     comment="test comment")
Beispiel #18
0
    def test_Component_basic_init_defaults(self):
        """
        Testing basic initialization sets the correct defaults
        """

        comp = Component("test_component", "Arm")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.AT_data, [0, 0, 0])
        self.assertEqual(comp.AT_relative, "ABSOLUTE")
        self.assertEqual(comp.ROTATED_data, [0, 0, 0])
        self.assertEqual(comp.ROTATED_relative, "ABSOLUTE")
        self.assertEqual(comp.WHEN, "")
        self.assertEqual(comp.EXTEND, "")
        self.assertEqual(comp.GROUP, "")
        self.assertEqual(comp.JUMP, "")
        self.assertEqual(comp.comment, "")
Beispiel #19
0
    def test_Component_basic_new_attribute_error(self):
        """
        The Component class is frozen after initialize in order to
        prevent the user accidentilly misspelling an attribute name,
        or at least be able to report an error when they do so.
        """

        comp = Component("test_component", "Arm")
        with self.assertRaises(AttributeError):
            comp.new_attribute = 1

        # If unfreeze does not work, this would cause an error
        comp._unfreeze()
        comp.new_attribute = 1
Beispiel #20
0
    def test_Component_basic_init_set_EXTEND(self):
        """
        Testing set_EXTEND method
        """

        comp = Component("test_component", "Arm")

        comp.append_EXTEND("test code")

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.EXTEND, "test code\n")

        comp.append_EXTEND("new code")

        self.assertEqual(comp.EXTEND, "test code\nnew code\n")
Beispiel #21
0
    def test_Component_write_to_file_include(self, mock_f):
        """
        Testing that a Component can be written to file with the
        expected output. Here with simple input.
        """
        comp = Component("test_component",
                         "Arm",
                         c_code_before="%include \"test.instr\"")

        comp.set_c_code_after("%include \"after.instr\"")

        comp._unfreeze()
        # Need to set up attribute parameters
        # Also need to categorize them as when created
        comp.parameter_names = []
        comp.parameter_defaults = {}
        comp.parameter_types = {}
        comp._freeze()

        with mock_f('test.txt', 'w') as m_fo:
            comp.write_component(m_fo)

        my_call = unittest.mock.call
        expected_writes = [
            my_call("%include \"test.instr\" // From" +
                    " component named test_component\n"),
            my_call("\n"),
            my_call("COMPONENT test_component = Arm("),
            my_call(")\n"),
            my_call("AT (0,0,0)"),
            my_call(" ABSOLUTE\n"),
            my_call("\n"),
            my_call("%include \"after.instr\" // From" +
                    " component named test_component\n"),
            my_call("\n")
        ]

        mock_f.assert_called_with('test.txt', 'w')
        handle = mock_f()
        handle.write.assert_has_calls(expected_writes, any_order=False)
Beispiel #22
0
    def test_component_basic_init_set_parameters(self):
        """
        Testing set_parameters method. Need to set some attribute
        parameters manually to test this.
        """

        comp = Component("test_component", "Arm")

        # Need to add some parameters to this bare component
        # Parameters are usually added by McStas_Instr
        comp._unfreeze()
        comp.new_par1 = 1
        comp.new_par2 = 3
        comp.this_par = 1492.2

        comp.set_parameters({
            "new_par1": 37.0,
            "new_par2": 12.0,
            "this_par": 1
        })

        self.assertEqual(comp.name, "test_component")
        self.assertEqual(comp.component_name, "Arm")
        self.assertEqual(comp.new_par1, 37.0)
        self.assertEqual(comp.new_par2, 12.0)
        self.assertEqual(comp.this_par, 1)

        with self.assertRaises(NameError):
            comp.set_parameters({"new_par3": 37.0})