Use ABCMeta for abstract classes

Signed-off-by: Werner Lewis <werner.lewis@arm.com>
This commit is contained in:
Werner Lewis 2022-08-24 12:18:25 +01:00
parent 169034ae63
commit 699e126942
2 changed files with 12 additions and 13 deletions

View File

@ -24,7 +24,7 @@ import os
import posixpath import posixpath
import re import re
from abc import abstractmethod from abc import ABCMeta, abstractmethod
from typing import Callable, Dict, Iterable, List, Type, TypeVar from typing import Callable, Dict, Iterable, List, Type, TypeVar
from mbedtls_dev import build_tree from mbedtls_dev import build_tree
@ -33,7 +33,7 @@ from mbedtls_dev import test_case
T = TypeVar('T') #pylint: disable=invalid-name T = TypeVar('T') #pylint: disable=invalid-name
class BaseTarget: class BaseTarget(metaclass=ABCMeta):
"""Base target for test case generation. """Base target for test case generation.
Attributes: Attributes:
@ -94,13 +94,12 @@ class BaseTarget:
def generate_tests(cls): def generate_tests(cls):
"""Generate test cases for the target subclasses. """Generate test cases for the target subclasses.
Classes will iterate over its subclasses, calling this method in each. During generation, each class will iterate over any subclasses, calling
In abstract classes, no further changes are needed, as there is no this method in each.
In abstract classes, no tests will be generated, as there is no
function to generate tests for. function to generate tests for.
In classes which do implement a test function, this should be overrided In classes which do implement a test function, this should be overridden
and a means to use `create_test_case()` should be added. In most cases and a means to use `create_test_case()` should be added.
the subclasses can still be iterated over, as either the class will
have none, or it may continue.
""" """
for subclass in sorted(cls.__subclasses__(), key=lambda c: c.__name__): for subclass in sorted(cls.__subclasses__(), key=lambda c: c.__name__):
yield from subclass.generate_tests() yield from subclass.generate_tests()

View File

@ -48,7 +48,7 @@ of BaseTarget in test_generation.py.
import itertools import itertools
import sys import sys
from abc import abstractmethod from abc import ABCMeta, abstractmethod
from typing import Callable, Dict, Iterator, List, Optional, Tuple, TypeVar from typing import Callable, Dict, Iterator, List, Optional, Tuple, TypeVar
import scripts_path # pylint: disable=unused-import import scripts_path # pylint: disable=unused-import
@ -64,12 +64,12 @@ def quote_str(val):
return "\"{}\"".format(val) return "\"{}\"".format(val)
class BignumTarget(test_generation.BaseTarget): class BignumTarget(test_generation.BaseTarget, metaclass=ABCMeta):
"""Target for bignum (mpi) test case generation.""" """Target for bignum (mpi) test case generation."""
target_basename = 'test_suite_mpi.generated' target_basename = 'test_suite_mpi.generated'
class BignumOperation(BignumTarget): class BignumOperation(BignumTarget, metaclass=ABCMeta):
"""Common features for test cases covering binary bignum operations. """Common features for test cases covering binary bignum operations.
This adds functionality common in binary operation tests. This includes This adds functionality common in binary operation tests. This includes
@ -118,7 +118,7 @@ class BignumOperation(BignumTarget):
return super().description() return super().description()
@abstractmethod @abstractmethod
def result(self) -> Optional[str]: def result(self) -> str:
"""Get the result of the operation. """Get the result of the operation.
This may be calculated during initialization and stored as `_result`, This may be calculated during initialization and stored as `_result`,
@ -131,7 +131,7 @@ class BignumOperation(BignumTarget):
"""Generate a description of the argument val. """Generate a description of the argument val.
This produces a simple description of the value, which are used in test This produces a simple description of the value, which are used in test
case naming, to avoid most generated cases only being numbered. case naming, to add context to the test cases.
""" """
if val == "": if val == "":
return "0 (null)" return "0 (null)"