Debian Codegraph @

features/steps/behave_tag_expression_steps.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 Provides step definitions that test tag expressions (and tag logic).
4
5 .. code-block:: gherkin
6
7 Given the default tags "-@foo"
8 And the tag expression "@foo"
9 Then the tag expression selects elements with tags:
10 | tags | selected? |
11 | @foo | yes |
12 | @other | no |
13
14 .. code-block:: gherkin
15
16 Given the named model elements with tags:
17 | name | tags |
18 | S1 | @foo |
19 Then the tag expression select model elements with:
20 | tag expression | selected? |
21 | @foo | S1, S3 |
22 | -@foo | S0, S2, S3 |
23 """
24
25 from __future__ import absolute_import
26 from behave import given, then, register_type
27 from behave.tag_expression import TagExpression
28 from behave_model_util import convert_comma_list, convert_model_element_tags
29 from hamcrest import assert_that, equal_to
30
31
32 # -----------------------------------------------------------------------------
33 # TEST DOMAIN, FIXTURES, STEP UTILS:
34 # -----------------------------------------------------------------------------
35 class ModelElement(object):
36 def __init__(self, name, tags=None):
37 self.name = name
38 self.tags = tags or []
39
40 # -----------------------------------------------------------------------------
41 # TYPE CONVERTERS:
42 # -----------------------------------------------------------------------------
43 def convert_tag_expression(text):
44 parts = text.strip().split()
45 return TagExpression(parts)
46 register_type(TagExpression=convert_tag_expression)
47
48 def convert_yesno(text):
49 text = text.strip().lower()
50 assert text in convert_yesno.choices
51 return text in convert_yesno.true_choices
52 convert_yesno.choices = ("yes", "no", "true", "false")
53 convert_yesno.true_choices = ("yes", "true")
54
55
56 # -----------------------------------------------------------------------------
57 # STEP DEFINITIONS:
58 # -----------------------------------------------------------------------------
59 @given('the tag expression "{tag_expression:TagExpression}"')
60 def step_given_the_tag_expression(context, tag_expression):
61 """
62 Define a tag expression that is used later-on.
63
64 .. code-block:: gherkin
65
66 Given the tag expression "@foo"
67 """
68 context.tag_expression = tag_expression
69
70 @given('the default tags "{default_tags:TagExpression}"')
71 def step_given_the_tag_expression(context, default_tags):
72 """
73 Define a tag expression that is used later-on.
74
75 .. code-block:: gherkin
76
77 Given the tag expression "@foo"
78 """
79 context.default_tags = default_tags
80 tag_expression = getattr(context, "tag_expression", None)
81 if tag_expression is None:
82 context.tag_expression = default_tags
83
84 @then('the tag expression selects elements with tags')
85 def step_then_tag_expression_selects_elements_with_tags(context):
86 """
87 Checks if a tag expression selects an element with the given tags.
88
89 .. code-block:: gherkin
90 Then the tag expression selects elements with tags:
91 | tags | selected? |
92 | @foo | yes |
93 | @other | no |
94 """
95 assert context.tag_expression, "REQUIRE: context.tag_expression"
96 context.table.require_columns(["tags", "selected?"])
97 tag_expression = context.tag_expression
98 expected = []
99 actual = []
100 for row in context.table.rows:
101 element_tags = convert_model_element_tags(row["tags"])
102 expected_element_selected = convert_yesno(row["selected?"])
103 actual_element_selected = tag_expression.check(element_tags)
104 expected.append((element_tags, expected_element_selected))
105 actual.append((element_tags, actual_element_selected))
106
107 # -- PERFORM CHECK:
108 assert_that(actual, equal_to(expected))
109
110
111 @given('the model elements with name and tags')
112 def step_given_named_model_elements_with_tags(context):
113 """
114 .. code-block:: gherkin
115
116 Given the model elements with name and tags:
117 | name | tags |
118 | S1 | @foo |
119 Then the tag expression select model elements with:
120 | tag expression | selected? |
121 | @foo | S1, S3 |
122 | -@foo | S0, S2, S3 |
123 """
124 assert context.table, "REQUIRE: context.table"
125 context.table.require_columns(["name", "tags"])
126
127 # -- PREPARE:
128 model_element_names = set()
129 model_elements = []
130 for row in context.table.rows:
131 name = row["name"].strip()
132 tags = convert_model_element_tags(row["tags"])
133 assert name not in model_element_names, "DUPLICATED: name=%s" % name
134 model_elements.append(ModelElement(name, tags=tags))
135 model_element_names.add(name)
136
137 # -- SETUP:
138 context.model_elements = model_elements
139
140
141 @then('the tag expression selects model elements with')
142 def step_given_named_model_elements_with_tags(context):
143 """
144 .. code-block:: gherkin
145
146 Then the tag expression select model elements with:
147 | tag expression | selected? |
148 | @foo | S1, S3 |
149 | -@foo | S0, S2, S3 |
150 """
151 assert context.model_elements, "REQUIRE: context attribute"
152 assert context.table, "REQUIRE: context.table"
153 context.table.require_columns(["tag expression", "selected?"])
154
155 for row_index, row in enumerate(context.table.rows):
156 tag_expression_text = row["tag expression"]
157 tag_expression = convert_tag_expression(tag_expression_text)
158 expected_selected_names = convert_comma_list(row["selected?"])
159
160 actual_selected = []
161 for model_element in context.model_elements:
162 if tag_expression.check(model_element.tags):
163 actual_selected.append(model_element.name)
164
165 assert_that(actual_selected, equal_to(expected_selected_names),
166 "tag_expression=%s (row=%s)" % (tag_expression_text, row_index))