Skip to content

Commit

Permalink
updated tests to pass on CPython/Jython/IronPython/PyPy/Stackless
Browse files Browse the repository at this point in the history
  • Loading branch information
mzipay committed Aug 27, 2018
1 parent b0fddd8 commit 42b77cf
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 60 deletions.
20 changes: 10 additions & 10 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,32 @@

from autologging import TRACE, __version__

try:
import clr
clr.AddReference("System")
is_ironpython = True
except:
is_ironpython = False

__all__ = [
"dummy_module_logger",
"get_lineno",
"get_dummy_lineno",
"has_co_lnotab",
"is_ironpython",
"list_handler",
"named_logger",
"named_tracer",
"suite",
]


def get_dummy_lineno(marker):
def get_lineno(filename, marker):
fn = os.path.join(
os.path.dirname(get_dummy_lineno.__code__.co_filename), "dummy.py")
os.path.dirname(get_lineno.__code__.co_filename),
os.path.basename(filename))
with open(fn) as fp:
for (i, line) in enumerate(fp):
if marker in line:
return i + 1


def get_dummy_lineno(marker):
return get_lineno("dummy.py", marker)


has_co_lnotab = hasattr(get_dummy_lineno.__code__, "co_lnotab")


Expand Down Expand Up @@ -113,6 +111,7 @@ def suite():
test_make_traceable_instancemethod,
test_make_traceable_classmethod,
test_make_traceable_staticmethod,
test_find_lastlineno,
test_FunctionTracingProxy,
test_GeneratorIteratorTracingProxy,
functest_logged,
Expand Down Expand Up @@ -141,6 +140,7 @@ def suite():
suite.addTest(test_make_traceable_instancemethod.suite())
suite.addTest(test_make_traceable_classmethod.suite())
suite.addTest(test_make_traceable_staticmethod.suite())
suite.addTest(test_find_lastlineno.suite())
suite.addTest(test_FunctionTracingProxy.suite())
suite.addTest(test_GeneratorIteratorTracingProxy.suite())
suite.addTest(functest_logged.suite())
Expand Down
5 changes: 2 additions & 3 deletions test/functest_logged.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
import logging
import unittest

from autologging import __version__
from autologging import __version__, _is_ironpython

from test import (
dummy_module_logger,
get_dummy_lineno,
is_ironpython,
list_handler,
named_logger,
)
Expand All @@ -65,7 +64,7 @@ def _assert_log_record(
self.assertEqual(logging.INFO, info_record.levelno)
# IronPython doesn't handle frames or code objects fully (even with
# -X:FullFrames)
if not is_ironpython:
if not _is_ironpython:
self.assertEqual(
logged_function.__code__.co_filename, info_record.pathname)
self.assertEqual(
Expand Down
5 changes: 2 additions & 3 deletions test/functest_traced.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@
import logging
import unittest

from autologging import TRACE, __version__
from autologging import TRACE, __version__, _is_ironpython

from test import (
dummy_module_logger,
get_dummy_lineno,
has_co_lnotab,
is_ironpython,
list_handler,
named_tracer,
)
Expand Down Expand Up @@ -83,7 +82,7 @@ def _assert_trace_record(
self.assertEqual(TRACE, trace_record.levelno)
# IronPython doesn't handle frames or code objects fully (even with
# -X:FullFrames)
if not is_ironpython:
if not _is_ironpython:
self.assertEqual(
traced_function.__code__.co_filename, trace_record.pathname)
self.assertEqual(expected_lineno, trace_record.lineno)
Expand Down
55 changes: 22 additions & 33 deletions test/test_FunctionTracingProxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,52 @@
__version__,
)

from test import has_co_lnotab, is_ironpython, list_handler
from test import get_lineno, has_co_lnotab, list_handler

# suppress messages to the console
logging.getLogger().setLevel(logging.FATAL + 1)


def sample_function(arg, keyword=None):
def sample_function(arg, keyword=None): #s_f:L1
x = arg.upper()
return "%s %s" % (x, keyword)
return "%s %s" % (x, keyword) #s_f:LN


_expected_function_filename = sample_function.__code__.co_filename
_expected_function_firstlineno = 50
_expected_function_lastlineno = \
52 if has_co_lnotab else _expected_function_firstlineno
_expected_function_firstlineno = get_lineno(
_expected_function_filename, "#s_f:L1")
_expected_function_lastlineno = (get_lineno(
_expected_function_filename, "#s_f:LN")
if has_co_lnotab else _expected_function_firstlineno)


def sample_generator(count):
def sample_generator(count): #s_g:L1
for i in range(count):
yield i + 1
yield i + 1 #s_g:LY


_expected_generator_filename = sample_generator.__code__.co_filename
_expected_generator_firstlineno = 61
_expected_generator_lastlineno = \
63 if has_co_lnotab else _expected_generator_firstlineno
_expected_generator_firstlineno = get_lineno(
_expected_generator_filename, "#s_g:L1")
_expected_generator_lastlineno = (get_lineno(
_expected_generator_filename, "#s_g:LY")
if has_co_lnotab else _expected_generator_firstlineno)


class SampleClass(object):

def method(self, arg, keyword=None):
def method(self, arg, keyword=None): #SC.m:L1
x = arg.upper()
return "%s %s" % (x, keyword)
return "%s %s" % (x, keyword) #SC.m:LN


_method = SampleClass.__dict__["method"]
_expected_method_filename = _method.__code__.co_filename
_expected_method_firstlineno = 74
_expected_method_lastlineno = \
76 if has_co_lnotab else _expected_method_firstlineno
_expected_method_firstlineno = get_lineno(
_expected_method_filename, "#SC.m:L1")
_expected_method_lastlineno = (get_lineno(
_expected_method_filename, "#SC.m:LN")
if has_co_lnotab else _expected_method_firstlineno)

_module_logger = logging.getLogger(__name__)
_module_logger.setLevel(TRACE)
Expand All @@ -106,23 +112,6 @@ def setUpClass(cls):
def setUp(self):
list_handler.reset()

def test_find_last_line_number_of_function(self):
self.assertEqual(
_expected_function_lastlineno,
_FunctionTracingProxy._find_last_line_number(
sample_function.__code__))

def test_find_last_line_number_of_generator(self):
self.assertEqual(
_expected_generator_lastlineno,
_FunctionTracingProxy._find_last_line_number(
sample_generator.__code__))

def test_find_last_line_number_of_method(self):
self.assertEqual(
_expected_method_lastlineno,
_FunctionTracingProxy._find_last_line_number(_method.__code__))

def test_init_sets_expected_log_record_attributes_for_function(self):
self.assertEqual(
_expected_function_filename,
Expand Down
30 changes: 19 additions & 11 deletions test/test_GeneratorIteratorTracingProxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,47 @@
import unittest

from autologging import (
_is_ironpython,
TRACE,
_GeneratorIteratorTracingProxy,
__version__,
)

from test import has_co_lnotab, is_ironpython, list_handler
from test import get_lineno, has_co_lnotab, list_handler

# suppress messages to the console
logging.getLogger().setLevel(logging.FATAL + 1)


def sample_generator(count):
def sample_generator(count): #s_g:L1
for i in range(count):
yield i + 1
yield i + 1 #s_g:LY


_expected_function_filename = sample_generator.__code__.co_filename
# generator iterators have a built-in reference to a frame object, so the
# line number *should* be consistent
_expected_function_lineno = 51
# line number *should* be consistent... except for IronPython
_expected_function_lineno = (
get_lineno(_expected_function_filename, "#s_g:LY")
if not _is_ironpython else
get_lineno(_expected_function_filename, "#s_g:L1"))


class SampleClass(object):

def method(self, count):
def method(self, count): #SC.m:L1
for i in range(count):
yield i + 1
yield i + 1 #SC.m:LY


_method = SampleClass.__dict__["method"]
_expected_method_filename = _method.__code__.co_filename
# generator iterators have a built-in reference to a frame object, so the
# line number *should* be consistent
_expected_method_lineno = 64
# line number *should* be consistent... except for IronPython
_expected_method_lineno = (
get_lineno(_expected_function_filename, "#SC.m:LY")
if not _is_ironpython else
get_lineno(_expected_function_filename, "#SC.m:L1"))

_module_logger = logging.getLogger(__name__)
_module_logger.setLevel(TRACE)
Expand All @@ -90,9 +97,10 @@ class GeneratorIteratorTracingProxyTest(unittest.TestCase):
def setUpClass(cls):
# note: generator iterators cannot be "rewound"
cls._function_proxy = _GeneratorIteratorTracingProxy(
sample_generator(2), _module_logger)
sample_generator, sample_generator(2), _module_logger)
cls._method_proxy = _GeneratorIteratorTracingProxy(
SampleClass().method(2), _class_logger)
SampleClass.__dict__["method"], SampleClass().method(2),
_class_logger)

def setUp(self):
list_handler.reset()
Expand Down
86 changes: 86 additions & 0 deletions test/test_find_lastlineno.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2013, 2015, 2016, 2018 Matthew Zipay.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

"""Test case and runner for :func:`autologging._find_lastlineno`."""

__author__ = "Matthew Zipay <mattz@ninthtest.info>"

from inspect import isgenerator
import logging
import unittest

from autologging import TRACE, _find_lastlineno, __version__

from test import get_lineno, has_co_lnotab

# suppress messages to the console
logging.getLogger().setLevel(logging.FATAL + 1)


def sample_function(arg, keyword=None): #s_f:L1
pass #s_f:LN


_f_filename = sample_function.__code__.co_filename
_expected_function_firstlineno = get_lineno(_f_filename, "#s_f:L1")
_expected_function_lastlineno = (
get_lineno(_f_filename, "#s_f:LN")
if has_co_lnotab else _expected_function_firstlineno)


class SampleClass(object):

def method(self, arg, keyword=None): #SC.m:L1
pass #SC.m:LN


_method = SampleClass.__dict__["method"]
_m_filename = _method.__code__.co_filename
_expected_method_firstlineno = get_lineno(_m_filename, "#SC.m:L1")
_expected_method_lastlineno = (
get_lineno(_m_filename, "#SC.m:LN")
if has_co_lnotab else _expected_method_firstlineno)


class FindLastLineNumberTest(unittest.TestCase):
"""Test the :func:`autologging._find_lastlineno` function."""

def test_find_lastlineno_of_function(self):
self.assertEqual(
_expected_function_lastlineno,
_find_lastlineno(sample_function.__code__))

def test_find_lastlineno_of_method(self):
self.assertEqual(
_expected_method_lastlineno, _find_lastlineno(_method.__code__))


def suite():
return unittest.makeSuite(FindLastLineNumberTest)


if __name__ == "__main__":
unittest.TextTestRunner().run(suite())

0 comments on commit 42b77cf

Please sign in to comment.