# Path: test/conftest.py
# def assert_no_expr_in_log(flog, expr='error'):
#     """Raise if `expr` is found in flog"""
#     assert os.path.isfile(flog)
# 
#     matched_lines = [line for line in open(flog, 'r')
#                      if re.search(expr, line.lower())]
# 
#     if matched_lines:
#         print(matched_lines)
#         assert len(matched_lines) == 0


import os
import pytest
import abkhazia.decode as decode
import abkhazia.utils as utils
from .conftest import assert_no_expr_in_log
# You should have received a copy of the GNU General Public License
# along with abkhazia. If not, see <http://www.gnu.org/licenses/>.
"""Test of the abkhazia.decode module"""





#
# Basic tests, decode a single utterance from the train corpus. Uses
# all the levels of acoustic models.
#


def wrapper_test_decode(name, options, corpus, lm, feats, am, tmpdir,
                        skip_scoring=False):
    # decode only the first utterance (just make the test faster)
    corpus = corpus.subcorpus([corpus.utts()[0]])
    output_dir = str(tmpdir.mkdir('decode-{}'.format(name)))
    flog = os.path.join(output_dir, 'decode-{}.log'.format(name))

    decoder = decode.Decode(
        corpus, lm, feats, am, output_dir, log=utils.logger.get_log(flog))
    for k, v in options.items():
        decoder.decode_opts[k].value = v
    if skip_scoring:
        decoder.score_opts['skip-scoring'] = True
    decoder.compute()

    # check if we have no error in log

# Path: test/conftest.py
# def assert_no_expr_in_log(flog, expr='error'):
#     """Raise if `expr` is found in flog"""
#     assert os.path.isfile(flog)
# 
#     matched_lines = [line for line in open(flog, 'r')
#                      if re.search(expr, line.lower())]
# 
#     if matched_lines:
#         print(matched_lines)
#         assert len(matched_lines) == 0


# Path: test/test_decode.py
import os
import pytest
import abkhazia.decode as decode
import abkhazia.utils as utils
from .conftest import assert_no_expr_in_log

# You should have received a copy of the GNU General Public License
# along with abkhazia. If not, see <http://www.gnu.org/licenses/>.
"""Test of the abkhazia.decode module"""





#
# Basic tests, decode a single utterance from the train corpus. Uses
# all the levels of acoustic models.
#


def wrapper_test_decode(name, options, corpus, lm, feats, am, tmpdir,
                        skip_scoring=False):
    # decode only the first utterance (just make the test faster)
    corpus = corpus.subcorpus([corpus.utts()[0]])
    output_dir = str(tmpdir.mkdir('decode-{}'.format(name)))
    flog = os.path.join(output_dir, 'decode-{}.log'.format(name))

    decoder = decode.Decode(
        corpus, lm, feats, am, output_dir, log=utils.logger.get_log(flog))
    for k, v in options.items():
        decoder.decode_opts[k].value = v
    if skip_scoring:
        decoder.score_opts['skip-scoring'] = True
    decoder.compute()

    # check if we have no error in log

    assert_no_expr_in_log(flog, 'error')