#!/usr/bin/python
import logging
from logilab.common.testlib import TestCase, unittest_main
import cubicweb.devtools
import cubes.apycot.testutils # import this first
from apycotlib import SimpleOutputParser
from apycotlib.writer import AbstractLogWriter
from checkers.apycot.jslint import JsLintParser
class SimpleLogWriter(AbstractLogWriter):
"""Simple writer only able to handle log/debug/info/warning/error/fatal methods"""
def __init__(self):
self.messages = []
def _log(self, severity, path, line, msg):
self.messages.append((severity, path, line, msg))
class ParserTC(TestCase):
parser_class = None
def validate_parser(self, text_input, msg_output, *args, **kwargs):
fake_stream = text_input.splitlines(True)
writer = SimpleLogWriter()
assert self.parser_class is not None, 'No parser_class defined'
parser = self.parser_class(writer, *args, **kwargs)
parser.parse(fake_stream)
self.assertListEqual(writer.messages, msg_output)
return parser
class SimpleOutputParserTC(ParserTC):
parser_class = SimpleOutputParser
def test_input(self):
text_input = """
E:toto bob
W:Warning toto
W:machin
F:Urg arg
"""
msg_output = [
(logging.ERROR, None, None, 'toto bob'),
(logging.WARNING, None, None, 'Warning toto'),
(logging.WARNING, None, None, 'machin'),
(logging.FATAL, None, None, 'Urg arg'),
]
parser = self.validate_parser(text_input, msg_output)
class JsLintParserTC(ParserTC):
parser_class = JsLintParser
def test_input(self):
text_input = """
Lint at line 8 character 1: 'CubicWeb' is not defined.
CubicWeb.require('htmlhelpers.js');
Lint at line 8 character 6: Expected 'updateMessage' to have an indentation at 9 instead at 6.
updateMessage(_("bookmark has been removed"));
Lint at line 8 character 6: 'updateMessage' is not defined.
updateMessage(_("bookmark has been removed"));
Lint at line 8 character 20: Unexpected dangling '_' in '_'.
updateMessage(_("bookmark has been removed"));
Lint at line 8 character 20: '_' is not defined.
updateMessage(_("bookmark has been removed"));
Lint at line 8 character 1: Mixed spaces and tabs.
updateMessage(_("bookmark has been removed"));
Lint at line 41 character 23: Missing space after 'function'.
tabbable: function(a, i, m) {
Lint at line 42 character 2: Confusing plusses.
++a;
Lint at line 45 character 8: Expected an operator and instead saw 'in'.
singing in the rain
"""
msg_output = [
(logging.ERROR, 'toto.js', u'8:1', u"'CubicWeb' is not defined."),
(logging.WARNING, 'toto.js', u'8:6',
u"Expected 'updateMessage' to have an indentation at 9 instead at 6."),
(logging.ERROR, 'toto.js', u'8:6', u"'updateMessage' is not defined."),
(logging.ERROR, 'toto.js', u'8:20', u"Unexpected dangling '_' in '_'."),
(logging.ERROR, 'toto.js', u'8:20', u"'_' is not defined."),
(logging.ERROR, 'toto.js', u'8:1', u'Mixed spaces and tabs.'),
(logging.ERROR, 'toto.js', u'41:23', u"Missing space after 'function'."),
(logging.INFO, 'toto.js', u'42:2', u'Confusing plusses.'),
(logging.WARNING, 'toto.js', u'45:8', u"Expected an operator and instead saw 'in'."),
]
parser = self.validate_parser(text_input, msg_output, path='toto.js')
if __name__ == '__main__':
unittest_main()