|
| 1 | +# coding=utf-8 |
1 | 2 | """Test code generation command."""
|
2 | 3 | import os
|
3 | 4 | import sys
|
4 | 5 | import textwrap
|
5 | 6 |
|
| 7 | +import six |
| 8 | + |
6 | 9 | from pytest_bdd.scripts import main
|
7 | 10 |
|
8 | 11 | PATH = os.path.dirname(__file__)
|
@@ -60,3 +63,144 @@ def my_list_should_be_1():
|
60 | 63 | u"'", u"'"
|
61 | 64 | )
|
62 | 65 | )
|
| 66 | + |
| 67 | + |
| 68 | +def test_generate_with_quotes(testdir): |
| 69 | + """Test that code generation escapes quote characters properly.""" |
| 70 | + testdir.makefile( |
| 71 | + ".feature", |
| 72 | + generate_with_quotes=textwrap.dedent( |
| 73 | + '''\ |
| 74 | + Feature: Handling quotes in code generation |
| 75 | +
|
| 76 | + Scenario: A step definition with quotes should be escaped as needed |
| 77 | + Given I have a fixture with 'single' quotes |
| 78 | + And I have a fixture with "double" quotes |
| 79 | + And I have a fixture with single-quote \'\'\'triple\'\'\' quotes |
| 80 | + And I have a fixture with double-quote """triple""" quotes |
| 81 | + |
| 82 | + When I generate the code |
| 83 | + |
| 84 | + Then The generated string should be written |
| 85 | + ''' |
| 86 | + ), |
| 87 | + ) |
| 88 | + |
| 89 | + result = testdir.run("pytest-bdd", "generate", "generate_with_quotes.feature") |
| 90 | + assert result.stdout.str() == textwrap.dedent( |
| 91 | + '''\ |
| 92 | + # coding=utf-8 |
| 93 | + """Handling quotes in code generation feature tests.""" |
| 94 | +
|
| 95 | + from pytest_bdd import ( |
| 96 | + given, |
| 97 | + scenario, |
| 98 | + then, |
| 99 | + when, |
| 100 | + ) |
| 101 | +
|
| 102 | +
|
| 103 | + @scenario('generate_with_quotes.feature', 'A step definition with quotes should be escaped as needed') |
| 104 | + def test_a_step_definition_with_quotes_should_be_escaped_as_needed(): |
| 105 | + """A step definition with quotes should be escaped as needed.""" |
| 106 | +
|
| 107 | +
|
| 108 | + @given('I have a fixture with "double" quotes') |
| 109 | + def i_have_a_fixture_with_double_quotes(): |
| 110 | + """I have a fixture with "double" quotes.""" |
| 111 | + raise NotImplementedError |
| 112 | +
|
| 113 | +
|
| 114 | + @given('I have a fixture with \\'single\\' quotes') |
| 115 | + def i_have_a_fixture_with_single_quotes(): |
| 116 | + """I have a fixture with 'single' quotes.""" |
| 117 | + raise NotImplementedError |
| 118 | +
|
| 119 | +
|
| 120 | + @given('I have a fixture with double-quote """triple""" quotes') |
| 121 | + def i_have_a_fixture_with_doublequote_triple_quotes(): |
| 122 | + """I have a fixture with double-quote \\"\\"\\"triple\\"\\"\\" quotes.""" |
| 123 | + raise NotImplementedError |
| 124 | +
|
| 125 | +
|
| 126 | + @given('I have a fixture with single-quote \\'\\'\\'triple\\'\\'\\' quotes') |
| 127 | + def i_have_a_fixture_with_singlequote_triple_quotes(): |
| 128 | + """I have a fixture with single-quote \'\'\'triple\'\'\' quotes.""" |
| 129 | + raise NotImplementedError |
| 130 | +
|
| 131 | +
|
| 132 | + @when('I generate the code') |
| 133 | + def i_generate_the_code(): |
| 134 | + """I generate the code.""" |
| 135 | + raise NotImplementedError |
| 136 | +
|
| 137 | +
|
| 138 | + @then('The generated string should be written') |
| 139 | + def the_generated_string_should_be_written(): |
| 140 | + """The generated string should be written.""" |
| 141 | + raise NotImplementedError |
| 142 | + ''' |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +def test_unicode_characters(testdir): |
| 147 | + """Test generating code with unicode characters. |
| 148 | +
|
| 149 | + Primary purpose is to ensure compatibility with Python2. |
| 150 | + """ |
| 151 | + |
| 152 | + testdir.makefile( |
| 153 | + ".feature", |
| 154 | + unicode_characters=textwrap.dedent( |
| 155 | + """\ |
| 156 | + # coding=utf-8 |
| 157 | + Feature: Generating unicode characters |
| 158 | +
|
| 159 | + Scenario: Calculating the circumference of a circle |
| 160 | + Given We have a circle |
| 161 | + When We want to know its circumference |
| 162 | + Then We calculate 2 * ℼ * 𝑟 |
| 163 | + """ |
| 164 | + ), |
| 165 | + ) |
| 166 | + |
| 167 | + result = testdir.run("pytest-bdd", "generate", "unicode_characters.feature") |
| 168 | + expected_output = textwrap.dedent( |
| 169 | + u'''\ |
| 170 | + # coding=utf-8 |
| 171 | + """Generating unicode characters feature tests.""" |
| 172 | +
|
| 173 | + from pytest_bdd import ( |
| 174 | + given, |
| 175 | + scenario, |
| 176 | + then, |
| 177 | + when, |
| 178 | + ) |
| 179 | +
|
| 180 | +
|
| 181 | + @scenario('unicode_characters.feature', 'Calculating the circumference of a circle') |
| 182 | + def test_calculating_the_circumference_of_a_circle(): |
| 183 | + """Calculating the circumference of a circle.""" |
| 184 | +
|
| 185 | +
|
| 186 | + @given('We have a circle') |
| 187 | + def we_have_a_circle(): |
| 188 | + """We have a circle.""" |
| 189 | + raise NotImplementedError |
| 190 | +
|
| 191 | +
|
| 192 | + @when('We want to know its circumference') |
| 193 | + def we_want_to_know_its_circumference(): |
| 194 | + """We want to know its circumference.""" |
| 195 | + raise NotImplementedError |
| 196 | +
|
| 197 | +
|
| 198 | + @then('We calculate 2 * ℼ * 𝑟') |
| 199 | + def {function_with_unicode_chars}(): |
| 200 | + """We calculate 2 * ℼ * 𝑟.""" |
| 201 | + raise NotImplementedError |
| 202 | + '''.format( |
| 203 | + function_with_unicode_chars=u"we_calculate_2__ℼ__𝑟" if not six.PY2 else u"we_calculate_2____" |
| 204 | + ) |
| 205 | + ) |
| 206 | + assert result.stdout.str() == expected_output |
0 commit comments