Skip to content

Commit

Permalink
fix: exclude by default _ fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniele-Tentoni committed Feb 8, 2022
1 parent ce7cbb3 commit aee199c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
39 changes: 21 additions & 18 deletions cc_codechecker/configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

class Configurable:
"""Abstract class for yaml objects.
This class expose two methods for two different presentation modes useful for
an yaml object: dump generate dictionaries and repr generate a string. At the
moment is not possible to generate the yaml file properly using only the
representation method, since we have to main both presentations.
"""
def __init__(self, **kwargs) -> None:
"""Create a new configurable yaml object.
"""
"""Create a new configurable yaml object."""
self._locals = Context().options()

# Check if verbose override is needed.
Expand All @@ -21,27 +25,26 @@ def __init__(self, **kwargs) -> None:
self._locals.verbose = verbose

def dump(self) -> dict[str, Any]:
"""Dump the configurable object to a dictionary.
"""
result = {}
for key, value in self.valued_items():
result[key] = value

return result
"""Dump the configurable object to a dictionary."""
return dict(self.valued_items())

def valued_items(self) -> list[tuple[str, Any]]:
def _excluded(key, value) -> bool:
return key and value
"""Gets valued items in the object.
Useful when you want to represent the object without default values. Child
objects need to inherit _excluded method to hide private fields useless for
end user or for yaml configuration.
Returns:
list[tuple[str, Any]]: items not
"""
def _excluded(key: str, value) -> bool:
return bool(key) and bool(value) and not key.startswith('_')

items = self.__dict__.items()
return [(k,v) for k, v in items if _excluded(k, v)]

def __repr__(self) -> str:
args: list[str] = []

for key, value in self.valued_items():
args.append(f'{key}={value}')

args_string = ','.join(args)

res = [f'{k}={v}' for k, v in self.valued_items()]
args_string = ','.join(res)
return f'{self.__class__.__name__}({args_string})'
3 changes: 3 additions & 0 deletions tests/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ Manual for developers and contributors
--------------------------------------

This command permit to run all tests and produce a very good report::

pytest --cov-report term-missing:skip-covered --cov-fail-under=67 --cov=cc_codechecker tests/

Produce human readable reports with this command::

pytest --cov-report html --cov=cc_codechecker tests/

This is the command used in ci, since we don't need terminal reports::

pytest --cov-report= --cov=cc_codechecker tests/
45 changes: 45 additions & 0 deletions tests/configurable_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Unit tests for base configurables."""

import pytest

# Codechecker
from cc_codechecker.configurable import Configurable


@pytest.fixture(name='base_configurable')
def fixture_base_configurable() -> Configurable:
return Configurable()

class VerboseConfigurable(Configurable):

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)

self.verbose = self._locals.verbose

@pytest.mark.parametrize('value', [True, False])
def test_verbose_value_set(value: bool):
configurable = VerboseConfigurable(verbose = value)
assert configurable.verbose == value

def test_repr_nothing(base_configurable: Configurable):
assert str(base_configurable) == 'Configurable()'

def test_dump_nothing(base_configurable: Configurable):
assert base_configurable.dump() == {}

class ExampleConfigurable(Configurable):

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self.example = True

@pytest.fixture(name='example_configurable')
def fixture_example_configurable() -> Configurable:
return ExampleConfigurable()

def test_repr_something(example_configurable: ExampleConfigurable):
assert str(example_configurable) == 'ExampleConfigurable(example=True)'

def test_dump_something(example_configurable: ExampleConfigurable):
assert example_configurable.dump() == { 'example': True }

0 comments on commit aee199c

Please # to comment.