Skip to content

Commit

Permalink
Modified Context in ParseResults to verify that _commands does not co…
Browse files Browse the repository at this point in the history
…ntain multiple Context or no Context. Doing so will raise a LookupError with a description of the problem. Two test cases are also included in tests/test_kerngen.py to verify that the correct error is raised.
  • Loading branch information
christopherngutierrez committed Aug 2, 2024
1 parent bfeecf9 commit 2b8112a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
25 changes: 21 additions & 4 deletions kerngen/high_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,32 @@ class ParseResults:
"""Queryable class about parse results"""

def __init__(self, iterable, symbols_map):
self._commands = list(iterable)
self._commands = ParseResults._validate_commands(list(iterable))
self._symbols_map = symbols_map

@staticmethod
def _get_context_from_commands_list(commands):
"""Validates that the commands list contains a single context"""
context_list = [context for context in commands if isinstance(context, Context)]
if not context_list:
raise LookupError("No Context found for commands list for ParseResults")
if len(context_list) > 1:
raise LookupError(
"Multiple Context found in commands list for ParseResults"
)
return context_list[0]

@staticmethod
def _validate_commands(commands):
"""Validate commands. Raises a LookupError if context is missing."""
ParseResults._get_context_from_commands_list(commands)
# Todo: add other checks here
return commands

@property
def context(self):
"""Return found context"""
return next(
context for context in self._commands if isinstance(context, Context)
)
return ParseResults._get_context_from_commands_list(self._commands)

@property
def commands(self):
Expand Down
25 changes: 25 additions & 0 deletions kerngen/tests/test_kerngen.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

# Copyright (C) 2024 Intel Corporation

"""Test the expected behaviour of the kerngen script"""

from enum import Enum
from pathlib import Path
from subprocess import run
from high_parser.parser import ParseResults
from high_parser.types import Context
import pytest


Expand Down Expand Up @@ -93,6 +98,26 @@ def test_invalid_scheme(kerngen_path):
assert result.returncode != 0


def test_parse_results_missing_context_in_constructor():
"""Test ParseResults constructor for missing context"""
with pytest.raises(LookupError) as e:
ParseResults([], {})
assert "No Context found for commands list for ParseResults" in str(e.value)


def test_parse_results_multiple_context_in_constructor():
"""Test ParseResults constructor for multiple context"""
with pytest.raises(LookupError) as e:
ParseResults(
[
Context(scheme="BGV", poly_order=8192, max_rns=1),
Context(scheme="CKKS", poly_order=8192, max_rns=1),
],
{},
)
assert "Multiple Context found in commands list for ParseResults" in str(e.value)


@pytest.fixture(name="gen_op_data")
def fixture_gen_op_data(request):
"""Given an op name, return both the input and expected output strings"""
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = kerngen

0 comments on commit 2b8112a

Please sign in to comment.