aboutsummaryrefslogtreecommitdiff
path: root/pw_ide/py/editors_test.py
blob: 1391fe9909a85592aa61b81ea9032257651964df (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Tests for pw_ide.editors"""

from collections import OrderedDict
from enum import Enum
import unittest

from pw_ide.editors import (
    dict_deep_merge,
    dict_swap_type,
    EditorSettingsFile,
    EditorSettingsManager,
    JsonFileFormat,
    Json5FileFormat,
    YamlFileFormat,
    _StructuredFileFormat,
)

from test_cases import PwIdeTestCase


class TestDictDeepMerge(unittest.TestCase):
    """Tests dict_deep_merge"""

    # pylint: disable=unnecessary-lambda
    def test_invariants_with_dict_success(self):
        dict_a = {'hello': 'world'}
        dict_b = {'foo': 'bar'}

        expected = {
            'hello': 'world',
            'foo': 'bar',
        }

        result = dict_deep_merge(dict_b, dict_a, lambda: dict())
        self.assertEqual(result, expected)

    def test_invariants_with_dict_implicit_ctor_success(self):
        dict_a = {'hello': 'world'}
        dict_b = {'foo': 'bar'}

        expected = {
            'hello': 'world',
            'foo': 'bar',
        }

        result = dict_deep_merge(dict_b, dict_a)
        self.assertEqual(result, expected)

    def test_invariants_with_dict_fails_wrong_ctor_type(self):
        dict_a = {'hello': 'world'}
        dict_b = {'foo': 'bar'}

        with self.assertRaises(TypeError):
            dict_deep_merge(dict_b, dict_a, lambda: OrderedDict())

    def test_invariants_with_ordered_dict_success(self):
        dict_a = OrderedDict({'hello': 'world'})
        dict_b = OrderedDict({'foo': 'bar'})

        expected = OrderedDict(
            {
                'hello': 'world',
                'foo': 'bar',
            }
        )

        result = dict_deep_merge(dict_b, dict_a, lambda: OrderedDict())
        self.assertEqual(result, expected)

    def test_invariants_with_ordered_dict_implicit_ctor_success(self):
        dict_a = OrderedDict({'hello': 'world'})
        dict_b = OrderedDict({'foo': 'bar'})

        expected = OrderedDict(
            {
                'hello': 'world',
                'foo': 'bar',
            }
        )

        result = dict_deep_merge(dict_b, dict_a)
        self.assertEqual(result, expected)

    def test_invariants_with_ordered_dict_fails_wrong_ctor_type(self):
        dict_a = OrderedDict({'hello': 'world'})
        dict_b = OrderedDict({'foo': 'bar'})

        with self.assertRaises(TypeError):
            dict_deep_merge(dict_b, dict_a, lambda: dict())

    # pylint: enable=unnecessary-lambda

    def test_merge_basic(self):
        dict_a = {'hello': 'world'}
        dict_b = {'hello': 'bar'}

        expected = {'hello': 'bar'}
        result = dict_deep_merge(dict_b, dict_a)
        self.assertEqual(result, expected)

    def test_merge_nested_dict(self):
        dict_a = {'hello': {'a': 'foo'}}
        dict_b = {'hello': {'b': 'bar'}}

        expected = {'hello': {'a': 'foo', 'b': 'bar'}}
        result = dict_deep_merge(dict_b, dict_a)
        self.assertEqual(result, expected)

    def test_merge_list(self):
        dict_a = {'hello': ['world']}
        dict_b = {'hello': ['bar']}

        expected = {'hello': ['world', 'bar']}
        result = dict_deep_merge(dict_b, dict_a)
        self.assertEqual(result, expected)

    def test_merge_list_no_duplicates(self):
        dict_a = {'hello': ['world']}
        dict_b = {'hello': ['world']}

        expected = {'hello': ['world']}
        result = dict_deep_merge(dict_b, dict_a)
        self.assertEqual(result, expected)

    def test_merge_nested_dict_with_lists(self):
        dict_a = {'hello': {'a': 'foo', 'c': ['lorem']}}
        dict_b = {'hello': {'b': 'bar', 'c': ['ipsum']}}

        expected = {'hello': {'a': 'foo', 'b': 'bar', 'c': ['lorem', 'ipsum']}}
        result = dict_deep_merge(dict_b, dict_a)
        self.assertEqual(result, expected)

    def test_merge_object_fails(self):
        class Strawman:
            pass

        dict_a = {'hello': 'world'}
        dict_b = {'foo': Strawman()}

        with self.assertRaises(TypeError):
            dict_deep_merge(dict_b, dict_a)

    def test_merge_copies_string(self):
        test_str = 'bar'
        dict_a = {'hello': {'a': 'foo'}}
        dict_b = {'hello': {'b': test_str}}

        result = dict_deep_merge(dict_b, dict_a)
        test_str = 'something else'

        self.assertEqual(result['hello']['b'], 'bar')


class TestDictSwapType(unittest.TestCase):
    """Tests dict_swap_type"""

    def test_ordereddict_to_dict(self):
        """Test converting an OrderedDict to a plain dict"""

        ordered_dict = OrderedDict(
            {
                'hello': 'world',
                'foo': 'bar',
                'nested': OrderedDict(
                    {
                        'lorem': 'ipsum',
                        'dolor': 'sit amet',
                    }
                ),
            }
        )

        plain_dict = dict_swap_type(ordered_dict, dict)

        expected_plain_dict = {
            'hello': 'world',
            'foo': 'bar',
            'nested': {
                'lorem': 'ipsum',
                'dolor': 'sit amet',
            },
        }

        # The returned dict has the content and type we expect
        self.assertDictEqual(plain_dict, expected_plain_dict)
        self.assertIsInstance(plain_dict, dict)
        self.assertIsInstance(plain_dict['nested'], dict)

        # The original OrderedDict is unchanged
        self.assertIsInstance(ordered_dict, OrderedDict)
        self.assertIsInstance(ordered_dict['nested'], OrderedDict)


class EditorSettingsTestType(Enum):
    SETTINGS = 'settings'


class TestCasesGenericOnFileFormat:
    """Container for tests generic on FileFormat.

    This misdirection is needed to prevent the base test class cases from being
    run as actual tests.
    """

    class EditorSettingsFileTestCase(PwIdeTestCase):
        """Test case for EditorSettingsFile with a provided FileFormat"""

        def setUp(self):
            if not hasattr(self, 'file_format'):
                self.file_format = _StructuredFileFormat()
            return super().setUp()

        def test_open_new_file_and_write(self):
            name = 'settings'
            settings_file = EditorSettingsFile(
                self.temp_dir_path, name, self.file_format
            )

            with settings_file.build() as settings:
                settings['hello'] = 'world'

            with open(
                self.temp_dir_path / f'{name}.{self.file_format.ext}'
            ) as file:
                settings_dict = self.file_format.load(file)

            self.assertEqual(settings_dict['hello'], 'world')

        def test_open_new_file_and_get(self):
            name = 'settings'
            settings_file = EditorSettingsFile(
                self.temp_dir_path, name, self.file_format
            )

            with settings_file.build() as settings:
                settings['hello'] = 'world'

            settings_dict = settings_file.get()
            self.assertEqual(settings_dict['hello'], 'world')

    class EditorSettingsManagerTestCase(PwIdeTestCase):
        """Test case for EditorSettingsManager with a provided FileFormat"""

        def setUp(self):
            if not hasattr(self, 'file_format'):
                self.file_format = _StructuredFileFormat()
            return super().setUp()

        def test_settings_merge(self):
            """Test that settings merge as expected in isolation."""
            default_settings = OrderedDict(
                {
                    'foo': 'bar',
                    'baz': 'qux',
                    'lorem': OrderedDict(
                        {
                            'ipsum': 'dolor',
                        }
                    ),
                }
            )

            types_with_defaults = {
                EditorSettingsTestType.SETTINGS: lambda _: default_settings
            }

            ide_settings = self.make_ide_settings()
            manager = EditorSettingsManager(
                ide_settings,
                self.temp_dir_path,
                self.file_format,
                types_with_defaults,
            )

            project_settings = OrderedDict(
                {
                    'alpha': 'beta',
                    'baz': 'xuq',
                    'foo': 'rab',
                }
            )

            with manager.project(
                EditorSettingsTestType.SETTINGS
            ).build() as settings:
                dict_deep_merge(project_settings, settings)

            user_settings = OrderedDict(
                {
                    'baz': 'xqu',
                    'lorem': OrderedDict(
                        {
                            'ipsum': 'sit amet',
                            'consectetur': 'adipiscing',
                        }
                    ),
                }
            )

            with manager.user(
                EditorSettingsTestType.SETTINGS
            ).build() as settings:
                dict_deep_merge(user_settings, settings)

            expected = {
                'alpha': 'beta',
                'foo': 'rab',
                'baz': 'xqu',
                'lorem': {
                    'ipsum': 'sit amet',
                    'consectetur': 'adipiscing',
                },
            }

            with manager.active(
                EditorSettingsTestType.SETTINGS
            ).build() as active_settings:
                manager.default(EditorSettingsTestType.SETTINGS).sync_to(
                    active_settings
                )
                manager.project(EditorSettingsTestType.SETTINGS).sync_to(
                    active_settings
                )
                manager.user(EditorSettingsTestType.SETTINGS).sync_to(
                    active_settings
                )

            self.assertCountEqual(
                manager.active(EditorSettingsTestType.SETTINGS).get(), expected
            )


class TestEditorSettingsFileJsonFormat(
    TestCasesGenericOnFileFormat.EditorSettingsFileTestCase
):
    """Test EditorSettingsFile with JsonFormat"""

    def setUp(self):
        self.file_format = JsonFileFormat()
        return super().setUp()


class TestEditorSettingsManagerJsonFormat(
    TestCasesGenericOnFileFormat.EditorSettingsManagerTestCase
):
    """Test EditorSettingsManager with JsonFormat"""

    def setUp(self):
        self.file_format = JsonFileFormat()
        return super().setUp()


class TestEditorSettingsFileJson5Format(
    TestCasesGenericOnFileFormat.EditorSettingsFileTestCase
):
    """Test EditorSettingsFile with Json5Format"""

    def setUp(self):
        self.file_format = Json5FileFormat()
        return super().setUp()


class TestEditorSettingsManagerJson5Format(
    TestCasesGenericOnFileFormat.EditorSettingsManagerTestCase
):
    """Test EditorSettingsManager with Json5Format"""

    def setUp(self):
        self.file_format = Json5FileFormat()
        return super().setUp()


class TestEditorSettingsFileYamlFormat(
    TestCasesGenericOnFileFormat.EditorSettingsFileTestCase
):
    """Test EditorSettingsFile with YamlFormat"""

    def setUp(self):
        self.file_format = YamlFileFormat()
        return super().setUp()


class TestEditorSettingsManagerYamlFormat(
    TestCasesGenericOnFileFormat.EditorSettingsManagerTestCase
):
    """Test EditorSettingsManager with YamlFormat"""

    def setUp(self):
        self.file_format = YamlFileFormat()
        return super().setUp()


if __name__ == '__main__':
    unittest.main()