Skip to content
Snippets Groups Projects
Commit 97941839 authored by Simon Will's avatar Simon Will
Browse files

Add tests for meters.py

parent 3f68e9fb
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,7 @@ from .model import Reading, Position ...@@ -9,7 +9,7 @@ from .model import Reading, Position
def caesurae_together(position_specs, reward): def caesurae_together(position_specs, reward):
def get_reward(meter: Meter, reading: Reading): def get_reward(meter: Meter, reading: Reading):
for spec in position_specs: 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: if not position.word_boundary:
return 0 return 0
else: else:
...@@ -46,8 +46,9 @@ class Meter: ...@@ -46,8 +46,9 @@ class Meter:
return sum(cond(reading) for cond in self.conditions) return sum(cond(reading) for cond in self.conditions)
ALL_METERS = [ ALL_METERS = {
Meter(
'Catalectic Dactylic Hexameter': Meter(
'Catalectic Dactylic Hexameter', 'Catalectic Dactylic Hexameter',
r'(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(⏑|–)', r'(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(–)(⏑⏑|–)(⏑|–)',
conditions={ conditions={
...@@ -59,7 +60,7 @@ ALL_METERS = [ ...@@ -59,7 +60,7 @@ ALL_METERS = [
}, },
short_name='6da‸' short_name='6da‸'
), ),
Meter( 'Iambic Trimeter': Meter(
'Iambic Trimeter', 'Iambic Trimeter',
r'(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑⏑|–)(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑⏑|–)(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑|–)', r'(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑⏑|–)(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑⏑|–)(⏑|⏑⏑|–)(⏑⏑|–)(⏑)(⏑|–)',
conditions={ conditions={
...@@ -68,4 +69,4 @@ ALL_METERS = [ ...@@ -68,4 +69,4 @@ ALL_METERS = [
}, },
short_name='3ia' short_name='3ia'
), ),
] }
...@@ -499,7 +499,7 @@ class Position: ...@@ -499,7 +499,7 @@ class Position:
@classmethod @classmethod
def after(cls, type: str, reading: Reading, position_number: int, def after(cls, type: str, reading: Reading, position_number: int,
meter: '.meters.Meter') -> 'Position': meter: '.meters.Meter') -> 'Position':
if type == 'mora': if type == 'mora':
return cls.after_mora(reading, position_number) return cls.after_mora(reading, position_number)
elif type == 'element': elif type == 'element':
......
# -*- 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment