File: test_options.py

package info (click to toggle)
f3d 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 23,504 kB
  • sloc: cpp: 99,106; python: 758; sh: 342; xml: 223; java: 101; javascript: 95; makefile: 25
file content (175 lines) | stat: -rw-r--r-- 4,705 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from typing import Any
import pytest

import f3d


def test_closest_option():
    option, distance = f3d.Options().get_closest_option("scene_direction")
    assert option == "scene.up_direction"
    assert distance == 3


def test_setitem():
    options = f3d.Options()
    options["ui.axis"] = False
    options["render.background.blur.coc"] = 33.33
    options["scene.animation.speed_factor"] = 3.3
    options["render.raytracing.samples"] = 5
    options["render.grid.color"] = [1.0, 1.0, 1.0]
    options["model.point_sprites.type"] = "sphere"


def test_getitem():
    engine = f3d.Engine.create_none()
    options = engine.options

    assert options["ui.axis"] is False
    assert options["render.background.blur.coc"] == 20.0
    assert options["scene.animation.speed_factor"] == 1.0
    assert options["render.raytracing.samples"] == 5
    assert options["render.grid.color"] == [0.0, 0.0, 0.0]
    assert options["model.point_sprites.type"] == "sphere"


def test_get_non_existent_key():
    options = f3d.Options()
    with pytest.raises(KeyError):
        assert options["hello"] == "world"


def test_set_non_existent_key():
    options = f3d.Options()
    with pytest.raises(KeyError):
        options["hello"] = "world"


def test_set_invalid_str_value():
    options = f3d.Options()
    with pytest.raises(ValueError):
        options["ui.axis"] = "world"


def test_set_incompatible_value_type():
    options = f3d.Options()
    with pytest.raises(AttributeError):
        options["ui.axis"] = 1.12


def test_len():
    options = f3d.Options()
    assert len(options) == len(options.keys())


def test_iter():
    options = f3d.Options()
    assert list(options) == options.keys()


def test_contains():
    options = f3d.Options()
    assert "render.grid.color" in options
    assert "hello.world" not in options


def test_set_options():
    options = f3d.Options()
    options["ui.axis"] = True
    options["model.material.roughness"] = 0.7
    options["scene.animation.speed_factor"] = 3.7
    options["render.raytracing.samples"] = 2
    options["model.color.rgb"] = [0.0, 1.0, 1.0]
    options["model.point_sprites.type"] = "other"

    engine = f3d.Engine.create_none()
    engine.options = options
    assert engine.options["ui.axis"] is True
    assert engine.options["model.material.roughness"] == 0.7
    assert engine.options["scene.animation.speed_factor"] == 3.7
    assert engine.options["render.raytracing.samples"] == 2
    assert engine.options["model.color.rgb"] == [0.0, 1.0, 1.0]
    assert engine.options["model.point_sprites.type"] == "other"


def test_set_options_from_string():
    options = f3d.Options()
    options["ui.axis"] = False
    assert not options["ui.axis"]

    options["ui.axis"] = "on"
    assert options["ui.axis"]

    options["ui.axis"] = "off"
    assert not options["ui.axis"]


def test_to_dict():
    options = f3d.Options()
    options_dict = dict(options)

    assert len(options) > 0
    assert len(options) == len(options_dict)
    for k, v in options_dict.items():
        assert options[k] == v


def test_update_from_dict():
    options = f3d.Options()

    d: dict[str, Any] = {
        "ui.axis": True,
        "model.material.roughness": 0.8,
        "scene.animation.speed_factor": 3.8,
        "render.raytracing.samples": 8,
        "model.color.rgb": [0.1, 0.2, 1.3],
        "model.point_sprites.type": "-X",
    }
    options.update(d)
    for k, v in d.items():
        assert options[k] == v, f"{k} was not set correctly"


def test_update_from_kv_pairs():
    options = f3d.Options()

    d: dict[str, Any] = {
        "ui.axis": True,
        "model.material.roughness": 0.8,
        "scene.animation.speed_factor": 3.8,
        "render.raytracing.samples": 8,
        "model.color.rgb": [0.1, 0.2, 1.3],
        "model.point_sprites.type": "-X",
    }
    options.update(d.items())
    for k, v in d.items():
        assert options[k] == v, f"{k} was not set correctly"


def test_update_from_invalid_kv_pairs():
    options = f3d.Options()

    items = (
        ("ui.axis", True),
        ("model.material.roughness", 0.8),
        ("a", "b", 3),
    )
    with pytest.raises(ValueError):
        options.update(items)  # type: ignore


def test_is_same():
    options1 = f3d.Options()
    options2 = f3d.Options()
    options1["ui.axis"] = True
    options2["ui.axis"] = False
    assert not options2.is_same(options1, "ui.axis")


def test_is_copy():
    options1 = f3d.Options()
    options2 = f3d.Options()
    options1["ui.axis"] = True
    options2["ui.axis"] = False
    assert not options2.is_same(options1, "ui.axis")
    options2.copy(options1, "ui.axis")
    assert options2.is_same(options1, "ui.axis")