|
2 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
3 | 3 | # You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
|
5 | | -# Imported from |
6 | | -# https://searchfox.org/mozilla-central/rev/c3ebaf6de2d481c262c04bb9657eaf76bf47e2ac/python/mozbuild/mozbuild/util.py#115-127 |
| 5 | +import copy |
| 6 | +import pickle |
7 | 7 |
|
8 | | -import unittest |
| 8 | +import pytest |
9 | 9 |
|
10 | 10 | from taskgraph.util.readonlydict import ReadOnlyDict |
11 | 11 |
|
12 | 12 |
|
13 | | -class TestReadOnlyDict(unittest.TestCase): |
14 | | - def test_basic(self): |
15 | | - original = {"foo": 1, "bar": 2} |
| 13 | +def test_basic(): |
| 14 | + original = {"foo": 1, "bar": 2} |
16 | 15 |
|
17 | | - test = ReadOnlyDict(original) |
| 16 | + test = ReadOnlyDict(original) |
18 | 17 |
|
19 | | - self.assertEqual(original, test) |
20 | | - self.assertEqual(test["foo"], 1) |
| 18 | + assert original == test |
| 19 | + assert test["foo"] == 1 |
21 | 20 |
|
22 | | - with self.assertRaises(KeyError): |
23 | | - test["missing"] |
| 21 | + with pytest.raises(KeyError): |
| 22 | + test["missing"] |
24 | 23 |
|
25 | | - with self.assertRaises(Exception): |
26 | | - test["baz"] = True |
| 24 | + with pytest.raises(Exception): |
| 25 | + test["baz"] = True |
27 | 26 |
|
28 | | - def test_update(self): |
29 | | - original = {"foo": 1, "bar": 2} |
30 | 27 |
|
31 | | - test = ReadOnlyDict(original) |
| 28 | +def test_update(): |
| 29 | + original = {"foo": 1, "bar": 2} |
32 | 30 |
|
33 | | - with self.assertRaises(Exception): |
34 | | - test.update(foo=2) |
| 31 | + test = ReadOnlyDict(original) |
35 | 32 |
|
36 | | - self.assertEqual(original, test) |
| 33 | + with pytest.raises(Exception): |
| 34 | + test.update(foo=2) |
37 | 35 |
|
38 | | - def test_del(self): |
39 | | - original = {"foo": 1, "bar": 2} |
| 36 | + assert original == test |
40 | 37 |
|
41 | | - test = ReadOnlyDict(original) |
42 | 38 |
|
43 | | - with self.assertRaises(Exception): |
44 | | - del test["foo"] |
| 39 | +def test_del(): |
| 40 | + original = {"foo": 1, "bar": 2} |
45 | 41 |
|
46 | | - self.assertEqual(original, test) |
| 42 | + test = ReadOnlyDict(original) |
| 43 | + |
| 44 | + with pytest.raises(Exception): |
| 45 | + del test["foo"] |
| 46 | + |
| 47 | + assert original == test |
| 48 | + |
| 49 | + |
| 50 | +def test_copy(): |
| 51 | + d = ReadOnlyDict(foo="bar") |
| 52 | + |
| 53 | + d_copy = d.copy() |
| 54 | + assert d == d_copy |
| 55 | + # TODO Returning a dict here feels like a bug, but there are places in-tree |
| 56 | + # relying on this behaviour. |
| 57 | + assert isinstance(d_copy, dict) |
| 58 | + |
| 59 | + d_copy = copy.copy(d) |
| 60 | + assert d == d_copy |
| 61 | + assert isinstance(d_copy, ReadOnlyDict) |
| 62 | + |
| 63 | + d_copy = copy.deepcopy(d) |
| 64 | + assert d == d_copy |
| 65 | + assert isinstance(d_copy, ReadOnlyDict) |
| 66 | + |
| 67 | + |
| 68 | +def test_pickle(): |
| 69 | + d = ReadOnlyDict(foo="bar") |
| 70 | + pickle.loads(pickle.dumps(d)) |
0 commit comments