Skip to content

Commit

Permalink
testcase.py: determine testdata_yaml_validator_flags by validator
Browse files Browse the repository at this point in the history
  • Loading branch information
thorehusfeldt committed Mar 8, 2024
1 parent d02bd2e commit 2af1276
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
16 changes: 10 additions & 6 deletions bin/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ def get_testdata_yaml(p, path, key, name=None) -> str | None:
if key == 'grading':
raise NotImplementedError(key)
if key != 'input_validator_flags' and name is not None:
raise ValueError(f"Only input validators support flags by validator name, got {key} and {name}")
raise ValueError(
f"Only input validators support flags by validator name, got {key} and {name}"
)

for dir in [path] + list(path.parents):
f = dir / 'testdata.yaml'
Expand All @@ -211,21 +213,23 @@ def get_testdata_yaml(p, path, key, name=None) -> str | None:
if isinstance(flags[key], str):
continue
for name in flags[key]:
input_validator_names = list(val.name for val in p.validators(validate.InputValidator))
input_validator_names = list(
val.name for val in p.validators(validate.InputValidator)
)
if not name in input_validator_names:
warn(f'Unknown input validator {name}; expected {input_validator_names}')
warn(
f'Unknown input validator {name}; expected {input_validator_names}'
)
case 'grading' | 'run_samples':
warn(f'{key} not implemented in BAPCtools')
case _:
warn(f'Unknown testdata.yaml key: {key}')



# Store testdata.yaml files in a cache.
if f not in p._testdata_yamls:
p._testdata_yamls[f] = read_yaml(f, plain=True)

#flags = p._testdata_yamls[f]
# flags = p._testdata_yamls[f]
if key in flags:
if isinstance(flags[key], str):
return flags[key]
Expand Down
32 changes: 17 additions & 15 deletions bin/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ def __init__(self, base_problem, path: Path, *, short_path=None):
# Read using the short_path instead of the in_path, because during
# generate the testcase will live in a temporary directory, where
# testdata.yaml doesn't exist.
self.testdata_yaml = self.problem.get_testdata_yaml(
self.problem.path / 'data' / self.short_path
)

# self.testdata_yaml = self.problem.get_testdata_yaml( TODO Thore
# self.problem.path / 'data' / self.short_path
# )

def __repr__(self):
return self.name
Expand All @@ -128,7 +129,7 @@ def with_suffix(self, ext):

def testdata_yaml_validator_flags(self, validator) -> list[str] | None | Literal[False]:
"""
The flags specified in testdata.yaml for the given validator,
The flags specified in testdata.yaml for the given validator applying to this testcase.
Arguments
---------
Expand All @@ -150,19 +151,20 @@ def testdata_yaml_validator_flags(self, validator) -> list[str] | None | Literal
if self.problem.settings.validation == 'default' and isinstance(validator, OutputValidator):
return None

if self.testdata_yaml is None:
return None
key = (
'input_validator_flags'
if isinstance(validator, InputValidator)
else 'output_validator_flags'
)
if key not in self.testdata_yaml:
return None
flags = self.testdata_yaml[key]
if isinstance(validator, InputValidator):
key = 'input_validator_flags'
name = validator.name
else:
key = 'output_validator_flags'
name = None

path = self.problem.path / 'data' / self.short_path
flags = self.problem.get_testdata_yaml(path, key, name=name)
# Note: support for lists/dicts for was removed in #259.
if flags is None:
return None
if not isinstance(flags, str):
fatal(f'{key} must be a string in testdata.yaml')
fatal(f'{key} must be a string in testdata.yaml, got {flags}')
return flags.split()

def validator_hashes(self, cls: Type[Validator]):
Expand Down

0 comments on commit 2af1276

Please sign in to comment.