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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
import pytest
from noseOfYeti.tokeniser.tokeniser import WITH_IT_RETURN_TYPE_ENV_NAME, Tokeniser
class Examples:
small_example = [
"""
describe "This":
before_each:
self.x = 5
describe "That":
before_each:
self.y = 6
describe "Meh":
after_each:
self.y = None
describe "Blah":pass
describe "async":
async before_each:
pass
async after_each:
pass
describe "Another":
before_each:
self.z = 8 """,
"""
class TestThis :
def setUp (self ):
__import__ ("noseOfYeti").tokeniser .TestSetup (super ()).sync_before_each ();self .x =5
class TestThis_That (TestThis ):
def setUp (self ):
__import__ ("noseOfYeti").tokeniser .TestSetup (super ()).sync_before_each ();self .y =6
class TestThis_That_Meh (TestThis_That ):
def tearDown (self ):
__import__ ("noseOfYeti").tokeniser .TestSetup (super ()).sync_after_each ();self .y =None
class TestThis_Blah (TestThis ):pass
class TestThis_Async (TestThis ):
async def setUp (self ):
await __import__ ("noseOfYeti").tokeniser .TestSetup (super ()).async_before_each ();pass
async def tearDown (self ):
await __import__ ("noseOfYeti").tokeniser .TestSetup (super ()).async_after_each ();pass
class TestAnother :
def setUp (self ):
__import__ ("noseOfYeti").tokeniser .TestSetup (super ()).sync_before_each ();self .z =8
TestThis .is_noy_spec =True # type: ignore
TestThis_That .is_noy_spec =True # type: ignore
TestThis_That_Meh .is_noy_spec =True # type: ignore
TestThis_Blah .is_noy_spec =True # type: ignore
TestThis_Async .is_noy_spec =True # type: ignore
TestAnother .is_noy_spec =True # type: ignore
""",
]
big_example = [
"""
describe "This":
before_each:
self.x = 5
it 'should':
if x:
pass
else:
x += 9
async it 'supports async its':
pass
describe "That":
before_each:
self.y = 6
describe "Meh":
after_each:
self.y = None
it "should set __testname__ for non alpha names ' $^":
pass
it 'should':
if y:
pass
else:
pass
it 'should have args', arg1, arg2:
blah |should| be_good()
describe "Blah":pass
ignore "root level $pecial-method*+":
pass
describe "Another":
before_each:
self.z = 8
it 'should':
if z:
if u:
print "hello \
there"
else:
print "no"
else:
pass
async it 'supports level 0 async its':
pass
""",
"""
class TestThis :
def setUp (self ):
__import__ ("noseOfYeti").tokeniser .TestSetup (super ()).sync_before_each ();self .x =5
def test_should (self )$RET:
if x :
pass
else :
x +=9
async def test_supports_async_its (self )$RET:
pass
class TestThis_That (TestThis ):
def setUp (self ):
__import__ ("noseOfYeti").tokeniser .TestSetup (super ()).sync_before_each ();self .y =6
class TestThis_That_Meh (TestThis_That ):
def tearDown (self ):
__import__ ("noseOfYeti").tokeniser .TestSetup (super ()).sync_after_each ();self .y =None
def test_should_set_testname_for_non_alpha_names (self )$RET:
pass
def test_should (self )$RET:
if y :
pass
else :
pass
def test_should_have_args (self ,arg1 ,arg2 )$RET:
blah |should |be_good ()
class TestThis_Blah (TestThis ):pass
def ignore__root_level_pecial_method ()$RET:
pass
class TestAnother :
def setUp (self ):
__import__ ("noseOfYeti").tokeniser .TestSetup (super ()).sync_before_each ();self .z =8
def test_should (self )$RET:
if z :
if u :
print "hello \
there"
else :
print "no"
else :
pass
async def test_supports_level_0_async_its ()$RET:
pass
TestThis .is_noy_spec =True # type: ignore
TestThis_That .is_noy_spec =True # type: ignore
TestThis_That_Meh .is_noy_spec =True # type: ignore
TestThis_Blah .is_noy_spec =True # type: ignore
TestAnother .is_noy_spec =True # type: ignore
ignore__root_level_pecial_method .__testname__ ="root level $pecial-method*+" # type: ignore
TestThis_That_Meh .test_should_set_testname_for_non_alpha_names .__testname__ ="should set __testname__ for non alpha names ' $^" # type: ignore
""",
]
comment_example = [
"""
assertTileHues(
self, tiles[0],
25.0, 25.0, 25.0, 25.0, 25.0, 25.0, # noqa
18.75, 18.75, 18.75, 18.75, 18.75, 18.75, # noqa
)
it "things":
assertTileHues(
self, tiles[1],
25.0, 25.0, 25.0, 25.0, 25.0, 25.0, # noqa
18.75, 18.75, 18.75, 18.75, 18.75, 18.75, # noqa
)
expected = {
# something
("D2", "<d"): lambda s: ("D2", "<d", s)
# something else
,
("B2", None): ("B2", None, None),
}
def t(n, f, c):
return expected[(n, f, c)]
assert True # type: ignore[valid-type]
assert True
# one two three
assert True
""",
"""
assertTileHues (
self ,tiles [0 ],
25.0 ,25.0 ,25.0 ,25.0 ,25.0 ,25.0 ,# noqa
18.75 ,18.75 ,18.75 ,18.75 ,18.75 ,18.75 ,# noqa
)
def test_things ()$RET:
assertTileHues (
self ,tiles [1 ],
25.0 ,25.0 ,25.0 ,25.0 ,25.0 ,25.0 ,# noqa
18.75 ,18.75 ,18.75 ,18.75 ,18.75 ,18.75 ,# noqa
)
expected ={
# something
("D2","<d"):lambda s :("D2","<d",s )
# something else
,
("B2",None ):("B2",None ,None ),
}
def t (n ,f ,c ):
return expected [(n ,f ,c )]
assert True # type: ignore[valid-type]
assert True
# one two three
assert True
""",
]
class Test_Tokeniser:
def test_sets_with_it_return_type_appropriately(self, monkeypatch):
monkeypatch.delenv(WITH_IT_RETURN_TYPE_ENV_NAME, raising=False)
assert not Tokeniser().with_it_return_type
monkeypatch.setenv(WITH_IT_RETURN_TYPE_ENV_NAME, "true")
assert Tokeniser().with_it_return_type
monkeypatch.setenv(WITH_IT_RETURN_TYPE_ENV_NAME, "false")
assert not Tokeniser().with_it_return_type
assert not Tokeniser().with_it_return_type
monkeypatch.setenv(WITH_IT_RETURN_TYPE_ENV_NAME, "1")
assert Tokeniser().with_it_return_type
assert Tokeniser().with_it_return_type
monkeypatch.setenv(WITH_IT_RETURN_TYPE_ENV_NAME, "0")
assert not Tokeniser().with_it_return_type
assert not Tokeniser().with_it_return_type
def test_gives_describes_noy_specific_attributes(self):
pytest.helpers.assert_example(
[
'describe "Something testable"',
"""
class TestSomethingTestable :pass
TestSomethingTestable .is_noy_spec =True # type: ignore
""",
]
)
class Test_Tokeniser_Complex:
def test_works_with_space(self):
pytest.helpers.assert_example(Examples.small_example)
def test_works_with_tabs(self):
pytest.helpers.assert_example(Examples.small_example, convert_to_tabs=True)
def test_keeps_good_indentation_in_body_with_spaces(self):
pytest.helpers.assert_example(Examples.big_example)
def test_keeps_good_indentation_in_body_with_tabs(self):
pytest.helpers.assert_example(Examples.big_example, convert_to_tabs=True)
def test_keeps_correct_indentation_with_comments(self):
pytest.helpers.assert_example(Examples.comment_example, convert_to_tabs=True)
|