-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Ricardaux/master
Add support for Union type and improve error management
- Loading branch information
Showing
13 changed files
with
218 additions
and
16 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
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
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
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
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,88 @@ | ||
from assertpy import assert_that | ||
|
||
from pyckson.parsers.base import ParserException, SetParser, UnionParser, BasicParserWithCast, ListParser, BasicParser | ||
|
||
|
||
class TestBasicParserWithCast: | ||
def test_should_handle_simple_type(self): | ||
parser = BasicParserWithCast(int) | ||
|
||
result = parser.parse(5) | ||
|
||
assert_that(result).is_equal_to(5) | ||
|
||
def test_should_raise_when_it_is_not_the_correct_type(self): | ||
parser = BasicParserWithCast(str) | ||
|
||
assert_that(parser.parse).raises(ParserException).when_called_with(5) | ||
|
||
|
||
class TestUnionParser: | ||
def test_should_parse_simple_union(self): | ||
parser = UnionParser([BasicParserWithCast(int)]) | ||
|
||
result = parser.parse(5) | ||
|
||
assert result == 5 | ||
|
||
def test_should_parse_list_in_union(self): | ||
parser = UnionParser([ListParser(BasicParserWithCast(int))]) | ||
|
||
result = parser.parse([5, 6]) | ||
|
||
assert result == [5, 6] | ||
|
||
def test_should_raise_if_parser_does_not_correspond_to_union_type(self): | ||
parser = UnionParser([BasicParserWithCast(int)]) | ||
|
||
assert_that(parser.parse).raises(TypeError).when_called_with("str") | ||
|
||
def test_should_not_raise_if_parser_does_not_have_cls(self): | ||
parser = UnionParser([BasicParser(), BasicParserWithCast(int)]) | ||
|
||
result = parser.parse(5) | ||
|
||
assert_that(result).is_equal_to(5) | ||
|
||
def test_should_parse_list_of_list_in_union(self): | ||
parser = UnionParser([ListParser(BasicParserWithCast(int)), ListParser(ListParser(BasicParserWithCast(int)))]) | ||
|
||
result = parser.parse([[5], [6]]) | ||
|
||
assert result == [[5], [6]] | ||
|
||
|
||
|
||
class TestListParser: | ||
def test_should_accept_list(self): | ||
parser = ListParser(BasicParserWithCast(int)) | ||
|
||
result = parser.parse([5]) | ||
|
||
assert_that(result).is_equal_to([5]) | ||
|
||
def test_should_raise_when_parse_other_than_list(self): | ||
parser = ListParser(BasicParserWithCast(int)) | ||
|
||
assert_that(parser.parse).raises(ParserException).when_called_with(5) | ||
|
||
|
||
class TestSetParser: | ||
def test_should_accept_set(self): | ||
parser = SetParser(BasicParserWithCast(int)) | ||
|
||
result = parser.parse({5}) | ||
|
||
assert_that(result).is_equal_to({5}) | ||
|
||
def test_should_accept_list_as_set(self): | ||
parser = SetParser(BasicParserWithCast(int)) | ||
|
||
result = parser.parse([5]) | ||
|
||
assert_that(result).is_equal_to({5}) | ||
|
||
def test_should_raise_when_parse_other_than_list(self): | ||
parser = SetParser(BasicParserWithCast(int)) | ||
|
||
assert_that(parser.parse).raises(ParserException).when_called_with(5) |
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
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
Oops, something went wrong.