File: test_user_var.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (68 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import os.path
import io
import unittest
import unittest.mock

from mcstasscript.interface.instr import McStas_instr
from mcstasscript.tests.helpers_for_tests import WorkInTestDir
from mcstasscript.helper.mcstas_objects import DeclareVariable

run_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '.')

def setup_instr_no_path():
    """
    Sets up a neutron instrument without a package_path
    """

    with WorkInTestDir() as handler:
        instrument = McStas_instr("test_instrument")

    return instrument


class Test_user_vars(unittest.TestCase):
    """
    Tests of user var behavior, collisions tested in test_Instr
    """

    def test_simple_initialize(self):
        """
        Test adding basic flag
        """
        my_instrument = setup_instr_no_path()

        flag = my_instrument.add_user_var("int", "flag")

        self.assertIsInstance(flag, DeclareVariable)
        self.assertEqual(len(my_instrument.user_var_list), 1)
        self.assertIs(my_instrument.user_var_list[0], flag)

    def test_move_user_var_to_declare(self):
        """
        Test ability to move user vars to declare for compatibility
        """
        my_instrument = setup_instr_no_path()

        declare_var1 = my_instrument.add_declare_var("int", "declare_var_1")
        declare_var2 = my_instrument.add_declare_var("double", "declare_var_2", value=4)
        flag = my_instrument.add_user_var("int", "flag")
        another_user = my_instrument.add_user_var("double", "lifetime")

        self.assertEqual(len(my_instrument.declare_list), 2)
        self.assertEqual(len(my_instrument.user_var_list), 2)
        self.assertIs(my_instrument.declare_list[0], declare_var1)
        self.assertIs(my_instrument.declare_list[1], declare_var2)
        self.assertIs(my_instrument.user_var_list[0], flag)
        self.assertIs(my_instrument.user_var_list[1], another_user)

        # Moves all user vars to declare
        my_instrument.move_user_vars_to_declare()

        self.assertEqual(len(my_instrument.declare_list), 4)
        self.assertEqual(len(my_instrument.user_var_list), 0)
        self.assertIs(my_instrument.declare_list[0], declare_var1)
        self.assertIs(my_instrument.declare_list[1], declare_var2)
        self.assertIs(my_instrument.declare_list[2], flag)
        self.assertIs(my_instrument.declare_list[3], another_user)