forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 784841 - Part 1: Create generic container classes; r=jhammel
We create some specialized dicts that will be used in later patches.
- Loading branch information
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
# You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import unittest | ||
|
||
from mozunit import main | ||
|
||
from mozbuild.util import ( | ||
DefaultOnReadDict, | ||
ReadOnlyDefaultDict, | ||
ReadOnlyDict, | ||
) | ||
|
||
class TestReadOnlyDict(unittest.TestCase): | ||
def test_basic(self): | ||
original = {'foo': 1, 'bar': 2} | ||
|
||
test = ReadOnlyDict(original) | ||
|
||
self.assertEqual(original, test) | ||
self.assertEqual(test['foo'], 1) | ||
|
||
with self.assertRaises(KeyError): | ||
value = test['missing'] | ||
|
||
with self.assertRaises(Exception): | ||
test['baz'] = True | ||
|
||
class TestDefaultOnReadDict(unittest.TestCase): | ||
def test_no_defaults(self): | ||
original = {'foo': 1, 'bar': 2} | ||
|
||
test = DefaultOnReadDict(original) | ||
self.assertEqual(original, test) | ||
|
||
with self.assertRaises(KeyError): | ||
value = test['missing'] | ||
|
||
test['foo'] = 5 | ||
self.assertEqual(test['foo'], 5) | ||
|
||
def test_dict_defaults(self): | ||
original = {'foo': 1, 'bar': 2} | ||
|
||
test = DefaultOnReadDict(original, defaults={'baz': 3}) | ||
|
||
self.assertEqual(original, test) | ||
self.assertEqual(test['baz'], 3) | ||
|
||
with self.assertRaises(KeyError): | ||
value = test['missing'] | ||
|
||
test['baz'] = 4 | ||
self.assertEqual(test['baz'], 4) | ||
|
||
def test_global_default(self): | ||
original = {'foo': 1} | ||
|
||
test = DefaultOnReadDict(original, defaults={'bar': 2}, | ||
global_default=10) | ||
|
||
self.assertEqual(original, test) | ||
self.assertEqual(test['foo'], 1) | ||
|
||
self.assertEqual(test['bar'], 2) | ||
self.assertEqual(test['baz'], 10) | ||
|
||
test['bar'] = 3 | ||
test['baz'] = 12 | ||
test['other'] = 11 | ||
|
||
self.assertEqual(test['bar'], 3) | ||
self.assertEqual(test['baz'], 12) | ||
self.assertEqual(test['other'], 11) | ||
|
||
|
||
class TestReadOnlyDefaultDict(unittest.TestCase): | ||
def test_simple(self): | ||
original = {'foo': 1, 'bar': 2} | ||
|
||
test = ReadOnlyDefaultDict(original) | ||
|
||
self.assertEqual(original, test) | ||
|
||
self.assertEqual(test['foo'], 1) | ||
|
||
with self.assertRaises(KeyError): | ||
value = test['missing'] | ||
|
||
def test_assignment(self): | ||
test = ReadOnlyDefaultDict({}) | ||
|
||
with self.assertRaises(Exception): | ||
test['foo'] = True | ||
|
||
def test_defaults(self): | ||
test = ReadOnlyDefaultDict({}, defaults={'foo': 1}) | ||
|
||
self.assertEqual(test['foo'], 1) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters