Commit 97941839 authored by Simon Will's avatar Simon Will
Browse files

Add tests for meters.py

parent 3f68e9fb
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ from .model import Reading, Position
def caesurae_together(position_specs, reward):
    def get_reward(meter: Meter, reading: Reading):
        for spec in position_specs:
            position = Position.after(spec[0], reading, meter, spec[1])
            position = Position.after(spec[0], reading, spec[1], meter)
            if not position.word_boundary:
                return 0
        else:
@@ -46,8 +46,9 @@ class Meter:
        return sum(cond(reading) for cond in self.conditions)


ALL_METERS = [
    Meter(
ALL_METERS = {

    'Catalectic Dactylic Hexameter': Meter(
        'Catalectic Dactylic Hexameter',
        r'(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(⏑|–)',
        conditions={
@@ -59,7 +60,7 @@ ALL_METERS = [
        },
        short_name='6da‸'
    ),
    Meter(
    'Iambic Trimeter': Meter(
        'Iambic Trimeter',
        r'(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑⏑|–)(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑⏑|–)(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑|–)',
        conditions={
@@ -68,4 +69,4 @@ ALL_METERS = [
        },
        short_name='3ia'
    ),
]
}

tests/test_meters.py

0 → 100644
+65 −0
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-

import os.path
import re

import pytest

import allzweckmesser as azm

TEST_DIR = os.path.dirname(__file__)
RE_MATCH_TYPE = type(re.match('a', 'a'))


@pytest.fixture
def verse_models():
    verse_filepath = os.path.join(TEST_DIR, 'verses.json')
    verse_models = azm.model.from_json(verse_filepath)
    return verse_models


@pytest.fixture
def pentameter_verse(verse_models):
    return verse_models[2]


@pytest.fixture
def hexameter_verse(verse_models):
    return verse_models[3]


@pytest.fixture
def hexameter():
    return azm.meters.ALL_METERS['Catalectic Dactylic Hexameter']


def test_caesura_together(hexameter, hexameter_verse):
    reading = hexameter_verse.readings[0]
    penthemimeral_reward = azm.meters.caesurae_together(
        [('mora', 10, 'Penthemimeral')], 1)
    assert penthemimeral_reward(hexameter, reading) == 1

    trithemimeral_reward = azm.meters.caesurae_together(
        [('mora', 6, 'Trithemimeral')], 1
    )
    assert trithemimeral_reward(hexameter, reading) == 0

    hephthemimeral_reward = azm.meters.caesurae_together(
        [('mora', 14, 'Hephthemimeral')], 1
    )
    assert hephthemimeral_reward(hexameter, reading) == 1

    trit_and_hepht_hemimeral_reward = azm.meters.caesurae_together(
        [('mora', 6, 'Hephthemimeral'), ('mora', 14, 'Hephthemimeral')], 1
    )
    assert trit_and_hepht_hemimeral_reward(hexameter, reading) == 0


def test_match_reading_success(hexameter, hexameter_verse):
    match = hexameter.match_reading(hexameter_verse.readings[0])
    assert isinstance(match, RE_MATCH_TYPE)


def test_match_reading_fail(hexameter, pentameter_verse):
    match = hexameter.match_reading(pentameter_verse.readings[0])
    assert match is None
+1 −1

File changed.

Contains only whitespace changes.