Skip to content

Commit

Permalink
Add some test for process_instruction_hook
Browse files Browse the repository at this point in the history
See also: #33
  • Loading branch information
wildlarva committed Jun 1, 2020
1 parent 3974d6c commit 7bbe429
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
33 changes: 28 additions & 5 deletions src/mcdecoder/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@

# region External classes

# region Exceptions

class LoadError(Exception):
"""Raised when an error occurs while loading"""
message: str
"""explanation of the error"""

def __init__(self, message) -> None:
self.message = message

# endregion Exceptions

# region MC description models loaded from yaml files


Expand Down Expand Up @@ -612,6 +624,7 @@ def create_mcdecoder_model(mcfile: str) -> McDecoder:
:param mcfile: Path to an MC description file
:return: Created McDecoder
:raises LoadError: if there's an invalid or inconsistent information
"""
# Load MC description
mc_desc_model = load_mc_description_model(mcfile)
Expand Down Expand Up @@ -651,11 +664,19 @@ def create_mcdecoder_model(mcfile: str) -> McDecoder:
# Process model
process_instruction_hook: Optional[Callable[[
InstructionDecoder], None]] = None
if config_module is not None:
if decoder_desc_model is not None:
if 'process_instruction_hook' in decoder_desc_model:
process_instruction_hook = getattr(config_module, cast(
str, decoder_desc_model['process_instruction_hook']))
if decoder_desc_model is not None:
if 'process_instruction_hook' in decoder_desc_model:
process_instruction_hook_name = cast(
str, decoder_desc_model['process_instruction_hook'])
if config_module is None:
raise LoadError(
"There must be config.py for the attribute decoder.process_instruction_hook.")
if not hasattr(config_module, process_instruction_hook_name):
raise LoadError(
f"You must define '{process_instruction_hook_name}' in config.py")

process_instruction_hook = getattr(
config_module, process_instruction_hook_name)

if process_instruction_hook is not None:
for instruction_decoder in instruction_decoders:
Expand Down Expand Up @@ -1517,6 +1538,8 @@ def _setbit_count(value_vec: np.ndarray) -> np.ndarray:
'setbit_count': _setbit_count,
}
"""
Dictionary to define built-in functions for an instruction condition.
Its entry is a pair of a function name and a function.
"""
Expand Down
20 changes: 17 additions & 3 deletions src/mcdecoder/test/test_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import pytest

from ..core import (
EqualityIdCondition, FieldIdConditionObject, ImmediateIdConditionObject, InRangeIdCondition,
create_mcdecoder_model, load_mc_description_model)
EqualityIdCondition,
FieldIdConditionObject,
ImmediateIdConditionObject,
InRangeIdCondition,
LoadError,
create_mcdecoder_model,
load_mc_description_model,
)


def test_create_mcdecoder_model_namespace() -> None:
Expand All @@ -12,12 +20,18 @@ def test_create_mcdecoder_model_namespace() -> None:

def test_create_mcdecoder_model_with_config() -> None:
mcdecoder_model = create_mcdecoder_model(
'test/with_config.yaml')
'test/process_instruction_hook_with_config/process_instruction_hook.yaml')

instruction = mcdecoder_model.instructions[0]
assert instruction.extras['extra_attribute'] == 'extra_content'


def test_create_mcdecoder_model_without_config() -> None:
with pytest.raises(LoadError):
create_mcdecoder_model(
'test/process_instruction_hook_without_config/process_instruction_hook.yaml')


def test_create_mcdecoder_model_extras() -> None:
mcdecoder_model = create_mcdecoder_model(
'test/arm.yaml')
Expand Down
5 changes: 5 additions & 0 deletions test/process_instruction_hook_with_config/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from mcdecoder.core import InstructionDecoder


def process_instruction(instruction: InstructionDecoder) -> None:
instruction.extras = {'extra_attribute': 'extra_content'}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
machine:
byteorder: little
instructions:
- name: add_1
format: xxxx:cond|00|1|0100|x:S|xxxx:Rn|xxxx:Rd|xxxx xxxx xxxx:imm12
decoder:
process_instruction_hook: process_instruction
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
machine:
byteorder: little
instructions:
- name: add_1
format: xxxx:cond|00|1|0100|x:S|xxxx:Rn|xxxx:Rd|xxxx xxxx xxxx:imm12
decoder:
process_instruction_hook: process_instruction

0 comments on commit 7bbe429

Please sign in to comment.