-
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.
Refactor ConverterRegistry to use a list for storing converters and u…
…pdate related methods
- Loading branch information
1 parent
66417e3
commit 315a6a1
Showing
2 changed files
with
48 additions
and
20 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
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 |
---|---|---|
@@ -1,30 +1,55 @@ | ||
|
||
import pytest | ||
|
||
from rangy import Converter, ConverterRegistry | ||
|
||
|
||
def test_register_and_get_converter(): | ||
registry = ConverterRegistry() | ||
ConverterRegistry.clear() | ||
converter = Converter(int) | ||
registry.register(converter) | ||
assert registry.get(int) == converter | ||
ConverterRegistry.register(converter) | ||
assert ConverterRegistry.get(int) == converter | ||
|
||
def test_get_converter_for_instance(): | ||
registry = ConverterRegistry() | ||
ConverterRegistry.clear() | ||
converter = Converter(int) | ||
registry.register(converter) | ||
assert registry.get(1) == converter | ||
ConverterRegistry.register(converter) | ||
assert ConverterRegistry.get(1) == converter | ||
|
||
def test_get_nonexistent_converter(): | ||
registry = ConverterRegistry() | ||
ConverterRegistry.clear() | ||
with pytest.raises(KeyError): | ||
registry.get(str) | ||
ConverterRegistry.get(str) | ||
|
||
def test_clear_registry(): | ||
registry = ConverterRegistry() | ||
ConverterRegistry.clear() | ||
converter = Converter(int) | ||
registry.register(converter) | ||
registry.clear() | ||
ConverterRegistry.register(converter) | ||
ConverterRegistry.clear() | ||
with pytest.raises(KeyError): | ||
registry.get(int) | ||
ConverterRegistry.get(int) | ||
|
||
def test_converter_registry_order(): | ||
class CustomType1: | ||
pass | ||
|
||
class CustomType2: | ||
pass | ||
|
||
def to_numeric1(obj): | ||
return 1 | ||
|
||
def to_numeric2(obj): | ||
return 2 | ||
|
||
converter1 = Converter(CustomType1, to_numeric1) | ||
converter2 = Converter(CustomType2, to_numeric2) | ||
|
||
ConverterRegistry.clear() | ||
ConverterRegistry.register(converter1) | ||
ConverterRegistry.register(converter2) | ||
|
||
converters = list(ConverterRegistry()) | ||
assert converters[0] == converter1 | ||
assert converters[1] == converter2 | ||
|
||
if __name__ == "__main__": | ||
pytest.main() | ||
|