-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyword.py
97 lines (78 loc) · 3.19 KB
/
keyword.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Robot Lint Rules - Lint rules for Robot Framework data files.
# Copyright (c) 2014, 2015, 2016 Richard Huang <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Robot Lint Rules - Lint rules for Robot Framework data files.
"""
from rflint.common import KeywordRule, WARNING
def _get_count(steps):
"""Returns total breaklines."""
count = 0
steps = list(steps)
steps.reverse()
for step in steps:
if _is_breakline(step):
count += 1
else:
break
return count
def _get_total(test):
"""Returns total steps."""
return len(test.statements)
def _is_breakline(statement):
"""Returns True if statement is a breakline, False otherwise."""
return len(statement) == 1 and statement[0].strip() == ''
def _last_keyword(keyword):
"""Returns True if given keyword is the last one."""
parent_keywords = tuple(keyword.parent.keywords)
return parent_keywords.index(keyword) == (len(parent_keywords) - 1)
class TooFewKeywordBlankLines(KeywordRule):
"""Warn about keyword without blank lines between each other.
"""
max_allowed = 1
message = 'Too few trailing blank lines in "%s" keyword.'
severity = WARNING
def apply(self, keyword):
"""Apply the rule to given keyword."""
if not _last_keyword(keyword):
count = _get_count(keyword.steps)
total = _get_total(keyword)
if count < self.max_allowed:
linenumber = keyword.linenumber + total
self.report(keyword, self.message % keyword.name,
linenumber + self.max_allowed, 0)
def configure(self, max_allowed):
"""Configures the rule."""
self.max_allowed = int(max_allowed)
class TooManyKeywordBlankLines(KeywordRule):
"""Warn about keyword with extra blank lines between each other.
"""
max_allowed = 1
message = 'Too many trailing blank lines in "%s" keyword.'
severity = WARNING
def apply(self, keyword):
"""Apply the rule to given keyword."""
if not _last_keyword(keyword):
count = _get_count(keyword.steps)
total = _get_total(keyword)
if count > self.max_allowed:
linenumber = (keyword.linenumber + total) - count
self.report(keyword, self.message % keyword.name,
linenumber + self.max_allowed, 0)
def configure(self, max_allowed):
"""Configures the rule."""
self.max_allowed = int(max_allowed)