Skip to content

Commit

Permalink
Fixed Travis + Test Coverage (#11)
Browse files Browse the repository at this point in the history
boosted test coverage to 100%
  • Loading branch information
alvinwan authored Nov 19, 2017
1 parent 68c01e5 commit dbf044a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
8 changes: 3 additions & 5 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[run]
branch = True
include = tex2py/*
omit =
*__init__.py
include = */tex2py/*
omit = tests/*

[report]
# Regexes for lines to exclude from consideration
Expand All @@ -12,11 +10,11 @@ exclude_lines =

# Don't complain about missing debug-only code:
def __repr__
if self\.debug

# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError
pass

# Don't complain if non-runnable code isn't run:
if 0:
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ install:
- python3 setup.py install

script:
- coverage run setup.py test
- py.test --cov

after_success:
- coveralls
- CI=true TRAVIS=true coveralls
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts = tests tex2py --doctest-modules
addopts = tests tex2py --doctest-modules --cov=tex2py
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from setuptools.command.test import test as TestCommand

install_requires = ['TexSoup==0.1', 'pptree==2.0']
tests_require = ['pytest', 'coverage == 3.7.1', 'coveralls == 1.1']

tests_require = ['pytest', 'pytest-cov==2.5.1', 'coverage == 3.7.1', 'coveralls == 1.1']
# hack
install_requires = install_requires + tests_require

class PyTest(TestCommand):

Expand Down
24 changes: 23 additions & 1 deletion tests/test_tex2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def iscream():

def test_basic_prop(chikin):
"""tests that custom __getattr__ works"""
assert str(chikin) == '[document]'
assert str(chikin) == '[document]' == chikin.name
assert chikin.depth == 0
assert len(chikin.branches) == 2
assert isinstance(chikin.section, TreeOfContents)
Expand All @@ -39,6 +39,7 @@ def test_top_level2(iscream):
"""tests parse for top level of markdown string with only subsections"""
assert iscream.section is None
assert len(iscream.branches) == 2
assert len(list(iscream)) == 2
assert str(iscream.subsection) == 'I Scream'
assert len(list(iscream.subsections)) == 2
assert iscream.depth == 0
Expand All @@ -59,6 +60,17 @@ def test_branches_limit(chikin):
"""Tests that branches include only headings of higher depth"""
assert chikin.section.subsection.string == 'Chikin Fly'

def test_from_latex(chikin, tmpdir):
"""Tests conversion from latex to tex2py"""
p = tmpdir.mkdir("tmp").join("hello.tex")
p.write(str(chikin.source))
new_chikin = TreeOfContents.fromFile(p.open())
assert chikin.section.subsection.string == new_chikin.section.subsection.string

def test_print(chikin):
"""Tests print does not error"""
chikin.print()

#################
# UTILITY TESTS #
#################
Expand All @@ -78,3 +90,13 @@ def test_find_hierarchy(chikin):
section = TexSoup(r'\section{asdf}').section
assert chikin.getHeadingLevel(section, hie) == 1
assert chikin.parseTopDepth(chikin.descendants) == 1

###############
# ERROR TESTS #
###############

def test_invalid_attr(chikin):
"""Test getattr errors."""
print(chikin.__class__)
with pytest.raises(AttributeError):
chikin.wallawallabingbang
14 changes: 7 additions & 7 deletions tex2py/tex2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,15 @@ def parseBranches(self, descendants):
return [TOC(str(descendant), depth=i, hierarchy=self.hierarchy,
**branch) for branch in branches]

def __getattr__(self, attr, *default):
def __getattr__(self, attr):
"""Check source for attributes"""
tag = attr[:-1]
if attr in self.allowed_attrs:
if isinstance(self.source, str):
return self.source
return getattr(self.source, attr, *default)
return getattr(self.source, attr, '')
if attr in self.valid_tags:
return next(filter(lambda t: t.source.name == attr, self.branches), None)
if len(default):
return default[0]
if attr[-1] == 's' and tag in self.valid_tags:
condition = lambda t: t.source.name == tag
return filter(condition, self.branches)
Expand All @@ -212,13 +210,15 @@ def print(self):
print_tree(self, childattr='branches', nameattr='name')

@staticmethod
def fromFile(path):
def fromFile(path_or_buffer):
"""Creates abstraction using path to file
:param str path: path to tex file
:param str path_or_buffer: path to tex file or buffer
:return: TreeOfContents object
"""
return TOC.fromLatex(open(path).read())
return TOC.fromLatex(open(path_or_buffer).read()
if isinstance(path_or_buffer, str)
else path_or_buffer)

@staticmethod
def fromLatex(tex, *args, **kwargs):
Expand Down

0 comments on commit dbf044a

Please sign in to comment.