Skip to content

Commit

Permalink
Fixed style name always being "Style" (#117)
Browse files Browse the repository at this point in the history
Styles used to be derived from the `Style` class and their class name was used in verbose output. Styles are now dynamic objects and there is no inheritance so this lead to all styles being called "Style".

This changes the way Styles are added to name them as per the configuration file or to give them a sensible default name when initialised through another means.
  • Loading branch information
MatthewHambley authored Aug 22, 2023
1 parent 8d60d06 commit ba209b6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions source/stylist/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def load_configuration(config_file: Path) -> Configuration:
if isinstance(var, FilePipe):
configuration.add_pipe(name, var)
elif isinstance(var, Style):
var.name = name
configuration.add_style(name, var)

return configuration
Expand Down
20 changes: 16 additions & 4 deletions source/stylist/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,29 @@ class Style(ABC):
"""
Abstract parent of all style lists.
"""
__unnamed_tally = 1

def __init__(self, *rules: Rule) -> None:
"""
:param *args: Rules which make up this style.
"""
self._rules = list(rules)
self.__rules = list(rules)
self.__name = f"Unnamed style {self.__unnamed_tally}"
self.__unnamed_tally += 1

@property
def name(self) -> str:
return self.__name

@name.setter
def name(self, name: str):
self.__name = name

def list_rules(self) -> List[Rule]:
"""
Gets a list of the rules which make up this style.
"""
return self._rules
return self.__rules

def check(self,
source: stylist.source.SourceTree) -> List[stylist.issue.Issue]:
Expand All @@ -40,9 +52,9 @@ def check(self,
:param source: Source code to inspect.
:return: All issues found in the source.
"""
logging.getLogger(__name__).info('Style: ' + self.__class__.__name__)
logging.getLogger(__name__).info(f"Style: {self.name}")
issues: List[stylist.issue.Issue] = []
for rule in self._rules:
for rule in self.__rules:
additional_issues = rule.examine(source)
issues.extend(additional_issues)
result = "Failed" if additional_issues else "Passed"
Expand Down
15 changes: 15 additions & 0 deletions unit-tests/configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@


class DummySource(SourceTree):
@staticmethod
def get_name() -> str:
return "Dummy source"

def get_tree(self):
pass

Expand All @@ -26,11 +30,19 @@ def get_tree_error(self) -> Optional[str]:


class DummyProcOne(TextProcessor):
@staticmethod
def get_name() -> str:
return "Dummy processor one"

def get_text(self) -> str:
return "dummy text one"


class DummyProcTwo(TextProcessor):
@staticmethod
def get_name() -> str:
return "Dummy processor two"

def get_text(self) -> str:
return "dummy text two"

Expand Down Expand Up @@ -126,6 +138,7 @@ def test_only_rule(self, tmp_path: Path):
assert configuration.file_pipes == {}
assert list(configuration.styles.keys()) == ['only_rules']
style = configuration.styles['only_rules']
assert style.name == "only_rules"
assert len(style.list_rules()) == 1
assert isinstance(style.list_rules()[0], DummyRuleZero)

Expand All @@ -140,6 +153,7 @@ def test_only_multi_rule(self, tmp_path: Path):
assert configuration.file_pipes == {}
assert list(configuration.styles.keys()) == ['only_multi_rules']
style = configuration.styles['only_multi_rules']
assert style.name == "only_multi_rules"
assert len(style.list_rules()) == 2
assert isinstance(style.list_rules()[0], DummyRuleOne)
assert cast(DummyRuleOne, style.list_rules()[0]).first == 1
Expand All @@ -159,6 +173,7 @@ def test_regex_rule(self, tmp_path: Path):
assert configuration.file_pipes == {}
assert list(configuration.styles.keys()) == ['regex_rule']
style = configuration.styles['regex_rule']
assert style.name == "regex_rule"
assert len(style.list_rules()) == 1
assert isinstance(style.list_rules()[0], DummyRuleOne)
assert cast(DummyRuleOne, style.list_rules()[0]).first.pattern == r'.*'
11 changes: 11 additions & 0 deletions unit-tests/style_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ def test_constructor(self, initials):
assert [rule.__class__.__name__
for rule in unit_under_test.list_rules()] == initials[1]

def test_naming(self):
"""
Checks a style can be named and renamed.
"""
test_unit = stylist.style.Style()
assert test_unit.name == "Unnamed style 1"
test_unit.name = "Cheese style"
assert test_unit.name == "Cheese style"
test_unit.name = "Fish style"
assert test_unit.name == "Fish style"

class _RuleHarness(stylist.rule.Rule):
def __init__(self):
self.examined = []
Expand Down

0 comments on commit ba209b6

Please sign in to comment.