Skip to content

Commit

Permalink
Fixed tests and code
Browse files Browse the repository at this point in the history
  • Loading branch information
csparpa committed Mar 13, 2020
1 parent 4f09706 commit fdfd136
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 63 deletions.
24 changes: 14 additions & 10 deletions fluentcheck/assertions/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,37 +255,41 @@ def is_not_json(check_obj):
def is_yaml(check_obj):
check_obj.is_string()
try:
yaml.loads(check_obj._val)
return check_obj
except ValueError:
yaml.load(check_obj._val)
except Exception:
raise CheckError('{} is not valid YAML'.format(check_obj._val))
else:
return check_obj


def is_not_yaml(check_obj):
check_obj.is_string()
try:
yaml.loads(check_obj._val)
raise CheckError('{} is valid YAML'.format(check_obj._val))
except ValueError:
yaml.load(check_obj._val)
except Exception:
return check_obj
else:
raise CheckError('{} is valid YAML'.format(check_obj._val))


def is_xml(check_obj):
check_obj.is_string()
try:
ElementTree.fromstring(check_obj._val)
return check_obj
except ValueError:
except Exception:
raise CheckError('{} is not valid XML'.format(check_obj._val))
else:
return check_obj


def is_not_xml(check_obj):
check_obj.is_string()
try:
ElementTree.fromstring(check_obj._val)
raise CheckError('{} is valid XML'.format(check_obj._val))
except ValueError:
except Exception:
return check_obj
else:
raise CheckError('{} is valid XML'.format(check_obj._val))


def matches(check_obj, regex):
Expand Down
43 changes: 42 additions & 1 deletion fluentcheck/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,45 @@ def test_has_not_length(self):
self.fail()
except CheckError:
pass


def test_is_yaml(self):
res = Check("hello").is_yaml()
self.assertIsInstance(res, Check)
try:
Check(123).is_yaml()
self.fail()
except CheckError:
pass

def test_is_not_yaml(self):
res = Check("xxx: {").is_not_yaml()
self.assertIsInstance(res, Check)
try:
Check("valid_yaml").is_not_yaml()
self.fail()
except CheckError:
pass

def test_is_xml(self):
obj = """<Agenda>
<type>gardening</type>
<Activity>
<type>cooking</type>
</Activity>
</Agenda>"""
res = Check(obj).is_xml()
self.assertIsInstance(res, Check)
try:
Check(123).is_xml()
self.fail()
except CheckError:
pass

def test_is_not_xml(self):
res = Check('[123]').is_not_xml()
self.assertIsInstance(res, Check)
try:
Check("<Agenda>ok</Agenda>").is_not_xml()
self.fail()
except CheckError:
pass
91 changes: 39 additions & 52 deletions fluentcheck/tests/test_strings_is.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,60 +254,47 @@ def test_is_not_json_fail(self):
with self.assertRaises(CheckError):
Is(obj).not_json

# TODO: FAILING WRONG:
# AttributeError: module 'yaml' has no attribute 'loads'
# def test_is_yaml_pass(self):
# obj = "Hello world"
# self.assertIsInstance(Is(obj).yaml, Is)

# TODO: FAILING WRONG:
# AttributeError: module 'yaml' has no attribute 'loads'
# def test_is_yaml_fail(self):
# obj = "goodbye"
# with self.assertRaises(CheckError):
# Is(obj).yaml
def test_is_yaml_pass(self):
obj = "Hello world"
self.assertIsInstance(Is(obj).yaml, Is)

# TODO: FAILING WRONG:
# AttributeError: module 'yaml' has no attribute 'loads'
# def test_is_not_yaml_pass(self):
# obj = "Hello world"
# self.assertIsInstance(Is(obj).not_yaml, Is)

# TODO: FAILING WRONG:
# AttributeError: module 'yaml' has no attribute 'loads'
# def test_is_not_yaml_fail(self):
# obj = """
# ---
# doe: "a deer, a female deer"
# calling-birds:
# - louie
# - fred
# """.strip()
# with self.assertRaises(CheckError):
# Is(obj).not_yaml
#
# def test_is_xml_pass(self):
# obj = """<Agenda>
# <type>gardening</type>
# <Activity>
# <type>cooking</type>
# </Activity>
# </Agenda>"""
# self.assertIsInstance(Is(obj).xml, Is)

# TODO: This is broken in check:
# xml.etree.ElementTree.ParseError: syntax error: line 1, column 0
# def test_is_xml_fail(self):
# obj = "not xml"
# with self.assertRaises(CheckError):
# Is(obj).xml
def test_is_yaml_fail(self):
obj = "xxx: {"
with self.assertRaises(CheckError):
Is(obj).yaml

def test_is_not_yaml_pass(self):
obj = "xxx: {"
self.assertIsInstance(Is(obj).not_yaml, Is)

def test_is_not_yaml_fail(self):
obj = """
---
doe: "a deer, a female deer"
calling-birds:
- louie
- fred
""".strip()
with self.assertRaises(CheckError):
Is(obj).not_yaml

def test_is_xml_pass(self):
obj = """<Agenda>
<type>gardening</type>
<Activity>
<type>cooking</type>
</Activity>
</Agenda>"""
self.assertIsInstance(Is(obj).xml, Is)

def test_is_xml_fail(self):
obj = "not xml"
with self.assertRaises(CheckError):
Is(obj).xml

# TODO: This is broken in check:
# xml.etree.ElementTree.ParseError: syntax error: line 1, column 0
# Rather than check error
# def test_is_not_xml_pass(self):
# obj = "Hello world"
# self.assertIsInstance(Is(obj).not_xml, Is)
def test_is_not_xml_pass(self):
obj = "Hello world"
self.assertIsInstance(Is(obj).not_xml, Is)

def test_is_not_xml_fail(self):
obj = """<Agenda></Agenda>"""
Expand Down

0 comments on commit fdfd136

Please sign in to comment.