test/unittest_parser.py
author David Douard <david.douard@logilab.fr>
Sun, 26 Oct 2014 18:30:36 +0100
changeset 1736 72ec6c7e956c
parent 1515 2a12802efe66
permissions -rw-r--r--
[apycotlib] port to narval 4.2 The connection handler is now a CWProxy (cwclientlib), and there is no CreateSubEntity view any more since we can now create File entity using cwclientlib and rqlcontroller.

#!/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()