| 1 |
## #!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# <sure - utility belt for automated testing in python> |
| 4 |
# Copyright (C) <2010-2023> Gabriel Falcão <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
from __future__ import unicode_literals |
| 19 |
import re |
| 20 |
import mock |
| 21 |
from collections import OrderedDict |
| 22 |
|
| 23 |
from datetime import datetime, timedelta |
| 24 |
from sure import this, these, those, it, expect, anything, AssertionBuilder |
| 25 |
from six import PY2 |
| 26 |
from sure.compat import compat_repr |
| 27 |
|
| 28 |
|
| 29 |
def test_assertion_builder_synonyms(): |
| 30 |
("this, it, these and those are all synonyms") |
| 31 |
|
| 32 |
assert isinstance(it, AssertionBuilder) |
| 33 |
assert isinstance(this, AssertionBuilder) |
| 34 |
assert isinstance(these, AssertionBuilder) |
| 35 |
assert isinstance(those, AssertionBuilder) |
| 36 |
|
| 37 |
|
| 38 |
def test_4_equal_2p2(): |
| 39 |
("this(4).should.equal(2 + 2)") |
| 40 |
|
| 41 |
time = datetime.now() - timedelta(0, 60) |
| 42 |
|
| 43 |
assert this(4).should.equal(2 + 2) |
| 44 |
assert this(time).should_not.equal(datetime.now()) |
| 45 |
|
| 46 |
def opposite(): |
| 47 |
assert this(4).should.equal(8) |
| 48 |
|
| 49 |
def opposite_not(): |
| 50 |
assert this(4).should_not.equal(4) |
| 51 |
|
| 52 |
expect(opposite).when.called.to.throw(AssertionError) |
| 53 |
expect(opposite).when.called.to.throw("X is 4 whereas Y is 8") |
| 54 |
|
| 55 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 56 |
expect(opposite_not).when.called.to.throw( |
| 57 |
"4 should differ from 4, but is the same thing") |
| 58 |
|
| 59 |
|
| 60 |
def test_2_within_0a2(): |
| 61 |
("this(1).should.be.within(0, 2)") |
| 62 |
|
| 63 |
assert this(1).should.be.within(0, 2) |
| 64 |
assert this(4).should_not.be.within(0, 2) |
| 65 |
|
| 66 |
def opposite(): |
| 67 |
assert this(1).should.be.within(2, 4) |
| 68 |
|
| 69 |
def opposite_not(): |
| 70 |
assert this(1).should_not.be.within(0, 2) |
| 71 |
|
| 72 |
expect(opposite).when.called.to.throw(AssertionError) |
| 73 |
expect(opposite).when.called.to.throw("expected 1 to be within 2 and 4") |
| 74 |
|
| 75 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 76 |
expect(opposite_not).when.called.to.throw("expected 1 to NOT be within 0 and 2") |
| 77 |
|
| 78 |
|
| 79 |
def test_true_be_ok(): |
| 80 |
("this(True).should.be.ok") |
| 81 |
|
| 82 |
assert this(True).should.be.ok |
| 83 |
assert this(False).should_not.be.ok |
| 84 |
|
| 85 |
def opposite(): |
| 86 |
assert this(False).should.be.ok |
| 87 |
|
| 88 |
def opposite_not(): |
| 89 |
assert this(True).should_not.be.ok |
| 90 |
|
| 91 |
expect(opposite).when.called.to.throw(AssertionError) |
| 92 |
expect(opposite).when.called.to.throw("expected `False` to be truthy") |
| 93 |
|
| 94 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 95 |
expect(opposite_not).when.called.to.throw("expected `True` to be falsy") |
| 96 |
|
| 97 |
|
| 98 |
def test_false_be_falsy(): |
| 99 |
("this(False).should.be.false") |
| 100 |
|
| 101 |
assert this(False).should.be.falsy |
| 102 |
assert this(True).should_not.be.falsy |
| 103 |
|
| 104 |
def opposite(): |
| 105 |
assert this(True).should.be.falsy |
| 106 |
|
| 107 |
def opposite_not(): |
| 108 |
assert this(False).should_not.be.falsy |
| 109 |
|
| 110 |
expect(opposite).when.called.to.throw(AssertionError) |
| 111 |
expect(opposite).when.called.to.throw("expected `True` to be falsy") |
| 112 |
|
| 113 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 114 |
expect(opposite_not).when.called.to.throw("expected `False` to be truthy") |
| 115 |
|
| 116 |
|
| 117 |
def test_none(): |
| 118 |
("this(None).should.be.none") |
| 119 |
|
| 120 |
assert this(None).should.be.none |
| 121 |
assert this(not None).should_not.be.none |
| 122 |
|
| 123 |
def opposite(): |
| 124 |
assert this("cool").should.be.none |
| 125 |
|
| 126 |
def opposite_not(): |
| 127 |
assert this(None).should_not.be.none |
| 128 |
|
| 129 |
expect(opposite).when.called.to.throw(AssertionError) |
| 130 |
expect(opposite).when.called.to.throw("expected `cool` to be None") |
| 131 |
|
| 132 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 133 |
expect(opposite_not).when.called.to.throw("expected `None` to not be None") |
| 134 |
|
| 135 |
|
| 136 |
def test_should_be_a(): |
| 137 |
("this(None).should.be.none") |
| 138 |
|
| 139 |
assert this(1).should.be.an(int) |
| 140 |
assert this([]).should.be.a('sure.compat.Iterable') |
| 141 |
assert this({}).should_not.be.a(list) |
| 142 |
|
| 143 |
def opposite(): |
| 144 |
assert this(1).should_not.be.an(int) |
| 145 |
|
| 146 |
def opposite_not(): |
| 147 |
assert this([]).should_not.be.a('list') |
| 148 |
|
| 149 |
expect(opposite).when.called.to.throw(AssertionError) |
| 150 |
expect(opposite).when.called.to.throw("expected `1` to not be an int") |
| 151 |
|
| 152 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 153 |
expect(opposite_not).when.called.to.throw("expected `[]` to not be a list") |
| 154 |
|
| 155 |
|
| 156 |
def test_should_be_callable(): |
| 157 |
("this(function).should.be.callable") |
| 158 |
|
| 159 |
assert this(lambda: None).should.be.callable |
| 160 |
assert this("aa").should_not.be.callable |
| 161 |
|
| 162 |
def opposite(): |
| 163 |
assert this("foo").should.be.callable |
| 164 |
|
| 165 |
def opposite_not(): |
| 166 |
assert this(opposite).should_not.be.callable |
| 167 |
|
| 168 |
expect(opposite).when.called.to.throw(AssertionError) |
| 169 |
expect(opposite).when.called.to.throw(compat_repr( |
| 170 |
"expected 'foo' to be callable")) |
| 171 |
|
| 172 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 173 |
expect(opposite_not).when.called.to.throw( |
| 174 |
"expected `{0}` to not be callable but it is".format(repr(opposite))) |
| 175 |
|
| 176 |
|
| 177 |
def test_iterable_should_be_empty(): |
| 178 |
("this(iterable).should.be.empty") |
| 179 |
|
| 180 |
assert this([]).should.be.empty |
| 181 |
assert this([1, 2, 3]).should_not.be.empty |
| 182 |
|
| 183 |
def opposite(): |
| 184 |
assert this([3, 2, 1]).should.be.empty |
| 185 |
|
| 186 |
def opposite_not(): |
| 187 |
assert this({}).should_not.be.empty |
| 188 |
|
| 189 |
expect(opposite).when.called.to.throw(AssertionError) |
| 190 |
expect(opposite).when.called.to.throw( |
| 191 |
"expected `[3, 2, 1]` to be empty but it has 3 items") |
| 192 |
|
| 193 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 194 |
expect(opposite_not).when.called.to.throw("expected `{}` to not be empty") |
| 195 |
|
| 196 |
|
| 197 |
def test_iterable_should_have_length_of(): |
| 198 |
("this(iterable).should.have.length_of(N)") |
| 199 |
|
| 200 |
assert this({'foo': 'bar', 'a': 'b'}).should.have.length_of(2) |
| 201 |
assert this([1, 2, 3]).should_not.have.length_of(4) |
| 202 |
|
| 203 |
def opposite(): |
| 204 |
assert this(('foo', 'bar', 'a', 'b')).should.have.length_of(1) |
| 205 |
|
| 206 |
def opposite_not(): |
| 207 |
assert this([1, 2, 3]).should_not.have.length_of(3) |
| 208 |
|
| 209 |
expect(opposite).when.called.to.throw(AssertionError) |
| 210 |
expect(opposite).when.called.to.throw(compat_repr( |
| 211 |
"the length of ('foo', 'bar', 'a', 'b') should be 1, but is 4")) |
| 212 |
|
| 213 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 214 |
expect(opposite_not).when.called.to.throw( |
| 215 |
"the length of [1, 2, 3] should not be 3") |
| 216 |
|
| 217 |
|
| 218 |
def test_greater_than(): |
| 219 |
("this(X).should.be.greater_than(Y)") |
| 220 |
|
| 221 |
assert this(5).should.be.greater_than(4) |
| 222 |
assert this(1).should_not.be.greater_than(2) |
| 223 |
|
| 224 |
def opposite(): |
| 225 |
assert this(4).should.be.greater_than(5) |
| 226 |
|
| 227 |
def opposite_not(): |
| 228 |
assert this(2).should_not.be.greater_than(1) |
| 229 |
|
| 230 |
expect(opposite).when.called.to.throw(AssertionError) |
| 231 |
expect(opposite).when.called.to.throw( |
| 232 |
"expected `4` to be greater than `5`") |
| 233 |
|
| 234 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 235 |
expect(opposite_not).when.called.to.throw( |
| 236 |
"expected `2` to not be greater than `1`") |
| 237 |
|
| 238 |
|
| 239 |
def test_greater_than_or_equal_to(): |
| 240 |
("this(X).should.be.greater_than_or_equal_to(Y)") |
| 241 |
|
| 242 |
assert this(4).should.be.greater_than_or_equal_to(4) |
| 243 |
assert this(1).should_not.be.greater_than_or_equal_to(2) |
| 244 |
|
| 245 |
def opposite(): |
| 246 |
assert this(4).should.be.greater_than_or_equal_to(5) |
| 247 |
|
| 248 |
def opposite_not(): |
| 249 |
assert this(2).should_not.be.greater_than_or_equal_to(1) |
| 250 |
|
| 251 |
expect(opposite).when.called.to.throw(AssertionError) |
| 252 |
expect(opposite).when.called.to.throw( |
| 253 |
"expected `4` to be greater than or equal to `5`") |
| 254 |
|
| 255 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 256 |
expect(opposite_not).when.called.to.throw( |
| 257 |
"expected `2` to not be greater than or equal to `1`") |
| 258 |
|
| 259 |
|
| 260 |
def test_lower_than(): |
| 261 |
("this(X).should.be.lower_than(Y)") |
| 262 |
|
| 263 |
assert this(4).should.be.lower_than(5) |
| 264 |
assert this(2).should_not.be.lower_than(1) |
| 265 |
|
| 266 |
def opposite(): |
| 267 |
assert this(5).should.be.lower_than(4) |
| 268 |
|
| 269 |
def opposite_not(): |
| 270 |
assert this(1).should_not.be.lower_than(2) |
| 271 |
|
| 272 |
expect(opposite).when.called.to.throw(AssertionError) |
| 273 |
expect(opposite).when.called.to.throw( |
| 274 |
"expected `5` to be lower than `4`") |
| 275 |
|
| 276 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 277 |
expect(opposite_not).when.called.to.throw( |
| 278 |
"expected `1` to not be lower than `2`") |
| 279 |
|
| 280 |
|
| 281 |
def test_lower_than_or_equal_to(): |
| 282 |
("this(X).should.be.lower_than_or_equal_to(Y)") |
| 283 |
|
| 284 |
assert this(5).should.be.lower_than_or_equal_to(5) |
| 285 |
assert this(2).should_not.be.lower_than_or_equal_to(1) |
| 286 |
|
| 287 |
def opposite(): |
| 288 |
assert this(5).should.be.lower_than_or_equal_to(4) |
| 289 |
|
| 290 |
def opposite_not(): |
| 291 |
assert this(1).should_not.be.lower_than_or_equal_to(2) |
| 292 |
|
| 293 |
expect(opposite).when.called.to.throw(AssertionError) |
| 294 |
expect(opposite).when.called.to.throw( |
| 295 |
"expected `5` to be lower than or equal to `4`") |
| 296 |
|
| 297 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 298 |
expect(opposite_not).when.called.to.throw( |
| 299 |
"expected `1` to not be lower than or equal to `2`") |
| 300 |
|
| 301 |
|
| 302 |
def test_be(): |
| 303 |
("this(X).should.be(X) when X is a reference to the same object") |
| 304 |
|
| 305 |
d1 = {} |
| 306 |
d2 = d1 |
| 307 |
d3 = {} |
| 308 |
|
| 309 |
assert isinstance(this(d2).should.be(d1), bool) |
| 310 |
assert this(d2).should.be(d1) |
| 311 |
assert this(d3).should_not.be(d1) |
| 312 |
|
| 313 |
def wrong_should(): |
| 314 |
return this(d3).should.be(d1) |
| 315 |
|
| 316 |
def wrong_should_not(): |
| 317 |
return this(d2).should_not.be(d1) |
| 318 |
|
| 319 |
expect(wrong_should_not).when.called.should.throw( |
| 320 |
AssertionError, |
| 321 |
'{} should not be the same object as {}, but it is', |
| 322 |
) |
| 323 |
expect(wrong_should).when.called.should.throw( |
| 324 |
AssertionError, |
| 325 |
'{} should be the same object as {}, but it is not', |
| 326 |
) |
| 327 |
|
| 328 |
|
| 329 |
def test_have_property(): |
| 330 |
("this(instance).should.have.property(property_name)") |
| 331 |
|
| 332 |
class Person(object): |
| 333 |
name = "John Doe" |
| 334 |
|
| 335 |
def __repr__(self): |
| 336 |
return r"Person()" |
| 337 |
|
| 338 |
jay = Person() |
| 339 |
|
| 340 |
assert this(jay).should.have.property("name") |
| 341 |
assert this(jay).should_not.have.property("age") |
| 342 |
|
| 343 |
def opposite(): |
| 344 |
assert this(jay).should_not.have.property("name") |
| 345 |
|
| 346 |
def opposite_not(): |
| 347 |
assert this(jay).should.have.property("age") |
| 348 |
|
| 349 |
expect(opposite).when.called.to.throw(AssertionError) |
| 350 |
expect(opposite).when.called.to.throw(compat_repr( |
| 351 |
"Person() should not have the property `name`, but it is 'John Doe'")) |
| 352 |
|
| 353 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 354 |
expect(opposite_not).when.called.to.throw( |
| 355 |
"Person() should have the property `age` but does not") |
| 356 |
|
| 357 |
|
| 358 |
def test_have_property_with_value(): |
| 359 |
("this(instance).should.have.property(property_name).being or " |
| 360 |
".with_value should allow chain up") |
| 361 |
|
| 362 |
class Person(object): |
| 363 |
name = "John Doe" |
| 364 |
|
| 365 |
def __repr__(self): |
| 366 |
return r"Person()" |
| 367 |
|
| 368 |
jay = Person() |
| 369 |
|
| 370 |
assert this(jay).should.have.property("name").being.equal("John Doe") |
| 371 |
assert this(jay).should.have.property("name").not_being.equal("Foo") |
| 372 |
|
| 373 |
def opposite(): |
| 374 |
assert this(jay).should.have.property("name").not_being.equal( |
| 375 |
"John Doe") |
| 376 |
|
| 377 |
def opposite_not(): |
| 378 |
assert this(jay).should.have.property("name").being.equal( |
| 379 |
"Foo") |
| 380 |
|
| 381 |
expect(opposite).when.called.to.throw(AssertionError) |
| 382 |
expect(opposite).when.called.to.throw(compat_repr( |
| 383 |
"'John Doe' should differ from 'John Doe', but is the same thing")) |
| 384 |
|
| 385 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 386 |
expect(opposite_not).when.called.to.throw(compat_repr( |
| 387 |
"X is 'John Doe' whereas Y is 'Foo'")) |
| 388 |
|
| 389 |
|
| 390 |
def test_have_key(): |
| 391 |
("this(dictionary).should.have.key(key_name)") |
| 392 |
|
| 393 |
jay = {'name': "John Doe"} |
| 394 |
|
| 395 |
assert this(jay).should.have.key("name") |
| 396 |
assert this(jay).should_not.have.key("age") |
| 397 |
|
| 398 |
def opposite(): |
| 399 |
assert this(jay).should_not.have.key("name") |
| 400 |
|
| 401 |
def opposite_not(): |
| 402 |
assert this(jay).should.have.key("age") |
| 403 |
|
| 404 |
expect(opposite).when.called.to.throw(AssertionError) |
| 405 |
expect(opposite).when.called.to.throw(compat_repr( |
| 406 |
"{'name': 'John Doe'} should not have the key `name`, " |
| 407 |
"but it is 'John Doe'")) |
| 408 |
|
| 409 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 410 |
expect(opposite_not).when.called.to.throw(compat_repr( |
| 411 |
"{'name': 'John Doe'} should have the key `age` but does not")) |
| 412 |
|
| 413 |
|
| 414 |
def test_have_key_with_value(): |
| 415 |
("this(dictionary).should.have.key(key_name).being or " |
| 416 |
".with_value should allow chain up") |
| 417 |
|
| 418 |
jay = dict(name="John Doe") |
| 419 |
|
| 420 |
assert this(jay).should.have.key("name").being.equal("John Doe") |
| 421 |
assert this(jay).should.have.key("name").not_being.equal("Foo") |
| 422 |
|
| 423 |
def opposite(): |
| 424 |
assert this(jay).should.have.key("name").not_being.equal( |
| 425 |
"John Doe") |
| 426 |
|
| 427 |
def opposite_not(): |
| 428 |
assert this(jay).should.have.key("name").being.equal( |
| 429 |
"Foo") |
| 430 |
|
| 431 |
expect(opposite).when.called.to.throw(AssertionError) |
| 432 |
expect(opposite).when.called.to.throw(compat_repr( |
| 433 |
"'John Doe' should differ from 'John Doe', but is the same thing")) |
| 434 |
|
| 435 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 436 |
expect(opposite_not).when.called.to.throw(compat_repr( |
| 437 |
"X is 'John Doe' whereas Y is 'Foo'")) |
| 438 |
|
| 439 |
|
| 440 |
def test_look_like(): |
| 441 |
("this(' aa \n ').should.look_like('aa')") |
| 442 |
|
| 443 |
assert this(' \n aa \n ').should.look_like('AA') |
| 444 |
assert this(' \n bb \n ').should_not.look_like('aa') |
| 445 |
|
| 446 |
def opposite(): |
| 447 |
assert this('\n aa \n').should.look_like('bb') |
| 448 |
|
| 449 |
def opposite_not(): |
| 450 |
assert this('\n aa \n').should_not.look_like('aa') |
| 451 |
|
| 452 |
expect(opposite).when.called.to.throw(AssertionError) |
| 453 |
expect(opposite).when.called.to.throw(compat_repr(r"'\n aa \n' does not look like 'bb'")) |
| 454 |
|
| 455 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 456 |
expect(opposite_not).when.called.to.throw(compat_repr(r"'\n aa \n' should not look like 'aa' but does")) |
| 457 |
|
| 458 |
|
| 459 |
def test_equal_with_repr_of_complex_types_and_unicode(): |
| 460 |
("test usage of repr() inside expect(complex1).to.equal(complex2)") |
| 461 |
|
| 462 |
class Y(object): |
| 463 |
def __init__(self, x): |
| 464 |
self.x = x |
| 465 |
|
| 466 |
def __repr__(self): |
| 467 |
if PY2: |
| 468 |
# PY2 should return the regular (unicode) string |
| 469 |
return self.x.encode('utf-8') |
| 470 |
else: |
| 471 |
return self.x |
| 472 |
|
| 473 |
def __eq__(self, other): |
| 474 |
return self.x == other.x |
| 475 |
|
| 476 |
y1 = dict( |
| 477 |
a=2, |
| 478 |
b=Y('Gabriel Falcão'), |
| 479 |
c='Foo', |
| 480 |
) |
| 481 |
|
| 482 |
expect(y1).to.equal(dict( |
| 483 |
a=2, |
| 484 |
b=Y('Gabriel Falcão'), |
| 485 |
c='Foo', |
| 486 |
)) |
| 487 |
|
| 488 |
|
| 489 |
def test_equal_with_repr_of_complex_types_and_repr(): |
| 490 |
("test usage of repr() inside expect(complex1).to.equal(complex2)") |
| 491 |
|
| 492 |
class Y(object): |
| 493 |
def __init__(self, x): |
| 494 |
self.x = x |
| 495 |
|
| 496 |
def __repr__(self): |
| 497 |
if PY2: |
| 498 |
# PY2 should return the regular (unicode) string |
| 499 |
return self.x.encode('utf-8') |
| 500 |
else: |
| 501 |
return self.x |
| 502 |
|
| 503 |
def __eq__(self, other): |
| 504 |
return self.x == other.x |
| 505 |
|
| 506 |
y1 = { |
| 507 |
'a': 2, |
| 508 |
'b': Y('Gabriel Falcão'), |
| 509 |
'c': 'Foo', |
| 510 |
} |
| 511 |
|
| 512 |
expect(y1).to.equal({ |
| 513 |
'a': 2, |
| 514 |
'b': Y('Gabriel Falcão'), |
| 515 |
'c': 'Foo', |
| 516 |
}) |
| 517 |
|
| 518 |
expect(y1).to_not.equal({ |
| 519 |
'a': 2, |
| 520 |
'b': Y('Gabriel Falçao'), |
| 521 |
'c': 'Foo', |
| 522 |
}) |
| 523 |
|
| 524 |
def opposite(): |
| 525 |
expect(y1).to.equal({ |
| 526 |
'a': 2, |
| 527 |
'b': Y('Gabriel Falçao'), |
| 528 |
'c': 'Foo', |
| 529 |
}) |
| 530 |
|
| 531 |
def opposite_not(): |
| 532 |
expect(y1).to_not.equal({ |
| 533 |
'a': 2, |
| 534 |
'b': Y('Gabriel Falcão'), |
| 535 |
'c': 'Foo', |
| 536 |
}) |
| 537 |
|
| 538 |
expect(opposite).when.called.to.throw(AssertionError) |
| 539 |
expect(opposite).when.called.to.throw(compat_repr("X['b'] != Y['b']")) |
| 540 |
|
| 541 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 542 |
expect(opposite_not).when.called.to.throw(compat_repr( |
| 543 |
"{'a': 2, 'b': Gabriel Falcão, 'c': 'Foo'} should differ from {'a': 2, 'b': Gabriel Falcão, 'c': 'Foo'}, but is the same thing")) |
| 544 |
|
| 545 |
|
| 546 |
def test_match_regex(): |
| 547 |
("expect('some string').to.match(r'\w{4} \w{6}') matches regex") |
| 548 |
|
| 549 |
assert this("some string").should.match(r"\w{4} \w{6}") |
| 550 |
assert this("some string").should_not.match(r"^\d*$") |
| 551 |
|
| 552 |
def opposite(): |
| 553 |
assert this("some string").should.match(r"\d{2} \d{4}") |
| 554 |
|
| 555 |
def opposite_not(): |
| 556 |
assert this("some string").should_not.match(r"some string") |
| 557 |
|
| 558 |
expect(opposite).when.called.to.throw( |
| 559 |
AssertionError, |
| 560 |
"'some string' doesn't match the regular expression /\d{2} \d{4}/") |
| 561 |
|
| 562 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 563 |
expect(opposite_not).when.called.to.throw( |
| 564 |
"'some string' should not match the regular expression /some string/") |
| 565 |
|
| 566 |
|
| 567 |
def test_match_contain(): |
| 568 |
("expect('some string').to.contain('tri')") |
| 569 |
|
| 570 |
assert this("some string").should.contain("tri") |
| 571 |
assert this("some string").should_not.contain('foo') |
| 572 |
|
| 573 |
def opposite(): |
| 574 |
assert this("some string").should.contain("bar") |
| 575 |
|
| 576 |
def opposite_not(): |
| 577 |
assert this("some string").should_not.contain(r"string") |
| 578 |
|
| 579 |
expect(opposite).when.called.to.throw(AssertionError) |
| 580 |
if PY2: |
| 581 |
expect(opposite).when.called.to.throw( |
| 582 |
"u'bar' should be in u'some string'") |
| 583 |
else: |
| 584 |
expect(opposite).when.called.to.throw( |
| 585 |
"'bar' should be in 'some string'") |
| 586 |
|
| 587 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 588 |
if PY2: |
| 589 |
expect(opposite_not).when.called.to.throw( |
| 590 |
"u'string' should NOT be in u'some string'") |
| 591 |
else: |
| 592 |
expect(opposite_not).when.called.to.throw( |
| 593 |
"'string' should NOT be in 'some string'") |
| 594 |
|
| 595 |
|
| 596 |
def test_catching_exceptions(): |
| 597 |
|
| 598 |
# Given that I have a function that raises an exceptiont that does *not* |
| 599 |
# inherit from the `Exception` class |
| 600 |
def blah(): |
| 601 |
raise SystemExit(2) |
| 602 |
|
| 603 |
# When I call it testing which exception it's raising, Then it should be |
| 604 |
# successful |
| 605 |
expect(blah).when.called_with().should.throw(SystemExit) |
| 606 |
|
| 607 |
|
| 608 |
def test_catching_exceptions_with_params(): |
| 609 |
|
| 610 |
# Given that I have a function that raises an exceptiont that does *not* |
| 611 |
# inherit from the `Exception` class |
| 612 |
def blah(foo): |
| 613 |
raise SystemExit(2) |
| 614 |
|
| 615 |
# When I call it testing which exception it's raising, Then it should be |
| 616 |
# successful |
| 617 |
expect(blah).when.called_with(0).should.throw(SystemExit) |
| 618 |
|
| 619 |
|
| 620 |
def test_success_with_params(): |
| 621 |
def blah(foo): |
| 622 |
pass |
| 623 |
|
| 624 |
expect(blah).when.called_with(0).should_not.throw(TypeError) |
| 625 |
|
| 626 |
|
| 627 |
def test_success_with_params_exception(): |
| 628 |
def blah(): |
| 629 |
pass |
| 630 |
|
| 631 |
expect(blah).when.called_with(0).should.throw(TypeError) |
| 632 |
|
| 633 |
|
| 634 |
def test_throw_matching_regex(): |
| 635 |
def blah(num): |
| 636 |
if num == 1: |
| 637 |
msg = 'this message' |
| 638 |
else: |
| 639 |
msg = 'another thing' |
| 640 |
|
| 641 |
raise ValueError(msg) |
| 642 |
|
| 643 |
expect(blah).when.called_with(1).should.throw(ValueError, 'this message') |
| 644 |
expect(blah).when.called_with(1).should.throw(re.compile(r'(this message|another thing)')) |
| 645 |
expect(blah).when.called_with(2).should.throw(ValueError, 'another thing') |
| 646 |
expect(blah).when.called_with(2).should.throw(ValueError, re.compile(r'(this message|another thing)')) |
| 647 |
|
| 648 |
try: |
| 649 |
expect(blah).when.called_with(1).should.throw(re.compile(r'invalid regex')) |
| 650 |
raise RuntimeError('should not have reached here') |
| 651 |
|
| 652 |
except AssertionError as e: |
| 653 |
if PY2: |
| 654 |
expect(str(e)).to.equal("When calling 'blah [tests/test_assertion_builder.py line 635]' the exception message does not match. Expected to match regex: u'invalid regex'\n against:\n u'this message'") |
| 655 |
else: |
| 656 |
expect(str(e)).to.equal("When calling b'blah [tests/test_assertion_builder.py line 635]' the exception message does not match. Expected to match regex: 'invalid regex'\n against:\n 'this message'") |
| 657 |
|
| 658 |
try: |
| 659 |
expect(blah).when.called_with(1).should.throw(ValueError, re.compile(r'invalid regex')) |
| 660 |
raise RuntimeError('should not have reached here') |
| 661 |
except AssertionError as e: |
| 662 |
if PY2: |
| 663 |
expect(str(e)).to.equal("When calling 'blah [tests/test_assertion_builder.py line 635]' the exception message does not match. Expected to match regex: u'invalid regex'\n against:\n u'this message'") |
| 664 |
else: |
| 665 |
expect(str(e)).to.equal("When calling b'blah [tests/test_assertion_builder.py line 635]' the exception message does not match. Expected to match regex: 'invalid regex'\n against:\n 'this message'") |
| 666 |
|
| 667 |
def test_should_not_be_different(): |
| 668 |
("'something'.should_not.be.different('SOMETHING'.lower())") |
| 669 |
|
| 670 |
part1 = '''<root> |
| 671 |
<a-tag with-attribute="one">AND A VALUE</a-tag> |
| 672 |
</root>''' |
| 673 |
|
| 674 |
part2 = '''<root> |
| 675 |
<a-tag with-attribute="two">AND A VALUE</a-tag> |
| 676 |
</root>''' |
| 677 |
|
| 678 |
assert this(part1).should.be.different_of(part2) |
| 679 |
assert this(part2).should_not.be.different_of(part2) |
| 680 |
|
| 681 |
def opposite(): |
| 682 |
assert this(part2).should.be.different_of(part2) |
| 683 |
|
| 684 |
def opposite_not(): |
| 685 |
assert this(part1).should_not.be.different_of(part2) |
| 686 |
|
| 687 |
expect(opposite).when.called.to.throw(AssertionError) |
| 688 |
expect(opposite).when.called.to.throw('''<root> |
| 689 |
<a-tag with-attribute="two">AND A VALUE</a-tag> |
| 690 |
</root> should be different of <root> |
| 691 |
<a-tag with-attribute="two">AND A VALUE</a-tag> |
| 692 |
</root>''') |
| 693 |
|
| 694 |
expect(opposite_not).when.called.to.throw(AssertionError) |
| 695 |
expect(opposite_not).when.called.to.throw('''Difference: |
| 696 |
|
| 697 |
<root> |
| 698 |
- <a-tag with-attribute="one">AND A VALUE</a-tag> |
| 699 |
? -- |
| 700 |
+ <a-tag with-attribute="two">AND A VALUE</a-tag> |
| 701 |
? ++ |
| 702 |
</root>''') |
| 703 |
|
| 704 |
|
| 705 |
def test_equals_handles_mock_call_list(): |
| 706 |
".equal() Should convert mock._CallList instances to lists" |
| 707 |
|
| 708 |
# Given the following mocked callback |
| 709 |
callback = mock.Mock() |
| 710 |
|
| 711 |
# When I call the callback with a few parameters twice |
| 712 |
callback(a=1, b=2) |
| 713 |
callback(a=3, b=4) |
| 714 |
|
| 715 |
# Then I see I can compare the call list without manually |
| 716 |
# converting anything |
| 717 |
|
| 718 |
expect(callback.call_args_list).should.equal([ |
| 719 |
mock.call(a=1, b=2), |
| 720 |
mock.call(a=3, b=4), |
| 721 |
]) |
| 722 |
|
| 723 |
|
| 724 |
def test_equals_handles_float_with_epsilon(): |
| 725 |
".equal(what, epsilon=XXX) should check for equality with an epsilon for float values" |
| 726 |
float1 = 4.242423 |
| 727 |
float2 = 4.242420 |
| 728 |
|
| 729 |
expect(float1).should_not.be.equal(float2) |
| 730 |
expect(float1).should.be.equal(float2, epsilon=0.000005) |
| 731 |
|
| 732 |
float_list1 = [4.242421, 4.242422, 4.242423, 4.242424, 4.242425] |
| 733 |
float_list2 = [4.242420, 4.242420, 4.242420, 4.242420, 4.242420] |
| 734 |
|
| 735 |
expect(float_list1).should_not.be.equal(float_list2) |
| 736 |
expect(float_list1).should.be.equal(float_list2, epsilon=0.000005) |
| 737 |
|
| 738 |
float_dict1 = {"f1": 4.242421, "f2": 4.242422, "f3": 4.242423, "f4": 4.242424, "f5": 4.242425} |
| 739 |
float_dict2 = {"f1": 4.242420, "f2": 4.242420, "f3": 4.242420, "f4": 4.242420, "f5": 4.242420} |
| 740 |
|
| 741 |
expect(float_dict1).should_not.be.equal(float_dict2) |
| 742 |
expect(float_dict1).should.be.equal(float_dict2, epsilon=0.000005) |
| 743 |
|
| 744 |
|
| 745 |
def test_equals_dictionaries_with_tuple_keys(): |
| 746 |
('.equal() with dict containing tuples as keys should work') |
| 747 |
|
| 748 |
X = { |
| 749 |
("0.0.0.0", 3478): "chuck norris", |
| 750 |
} |
| 751 |
|
| 752 |
Y = { |
| 753 |
("0.0.0.0", 400): "chuck norris", |
| 754 |
} |
| 755 |
|
| 756 |
expect(X).should_not.equal(Y) |
| 757 |
expect(Y).should_not.equal(X) |
| 758 |
|
| 759 |
|
| 760 |
def test_ordereddict_comparison(): |
| 761 |
".equal(OrderedDict) should check if two ordered dicts are the same" |
| 762 |
result = { |
| 763 |
"fields": OrderedDict([ |
| 764 |
("name", "John"), |
| 765 |
("age", "22"), |
| 766 |
]), |
| 767 |
"children": OrderedDict([]), |
| 768 |
} |
| 769 |
|
| 770 |
expectation = { |
| 771 |
"fields": OrderedDict([ |
| 772 |
("age", "22"), |
| 773 |
("name", "John"), |
| 774 |
]), |
| 775 |
"children": OrderedDict([]), |
| 776 |
} |
| 777 |
|
| 778 |
expect(result).shouldnt.be.equal(expectation) |
| 779 |
expect(result).should.be.equal(result) |
| 780 |
|
| 781 |
try: |
| 782 |
expect(result).should.equal(expectation) |
| 783 |
raise RuntimeError("should not have reached here") |
| 784 |
except AssertionError as error: |
| 785 |
if PY2: |
| 786 |
expect(str(error)).should.be.equal("""given |
| 787 |
X = {u'children': {}, u'fields': {u'age': u'22', u'name': u'John'}} |
| 788 |
and |
| 789 |
Y = {u'children': {}, u'fields': {u'age': u'22', u'name': u'John'}} |
| 790 |
X[u'fields'] and Y[u'fields'] are in a different order""") |
| 791 |
else: |
| 792 |
expect(str(error)).should.be.equal("""given |
| 793 |
X = {'children': {}, 'fields': {'age': '22', 'name': 'John'}} |
| 794 |
and |
| 795 |
Y = {'children': {}, 'fields': {'age': '22', 'name': 'John'}} |
| 796 |
X['fields'] and Y['fields'] are in a different order""") |
| 797 |
|
| 798 |
|
| 799 |
def test_equals_anything(): |
| 800 |
val = 1 |
| 801 |
expect(val).to.be.equal(anything) |
| 802 |
|
| 803 |
val = {'key': 1, 'other_key': 4} |
| 804 |
expect(val).to.be.equal({'key': 1, 'other_key': anything}) |
| 805 |
|
| 806 |
val = {'key': 1, 'other_key': 'yo!'} |
| 807 |
expect(val).to.be.equal({'key': 1, 'other_key': anything}) |
| 808 |
|
| 809 |
val = [1, 2, 3, 4] |
| 810 |
expect(val).to.be.equal([1, anything, 3, anything]) |
| 811 |
|
| 812 |
def test_equals_crosstype(): |
| 813 |
class MyFloat(float): |
| 814 |
pass |
| 815 |
expect(MyFloat(1.23)).to.be.equal(1.23) |