TusLibros-8.st 29.6 KB
Newer Older
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
!classDefinition: #CartTest category: #TusLibros!
TestCase subclass: #CartTest
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!CartTest methodsFor: 'tests' stamp: 'HernanWilkinson 6/17/2013 17:08'!
test01NewCartsAreCreatedEmpty

	self assert: self createCart isEmpty! !

!CartTest methodsFor: 'tests' stamp: 'HernanWilkinson 6/17/2013 17:45'!
test02CanNotAddItemsThatDoNotBelongToStore

	| cart |
	
	cart := self createCart.
	
	self 
		should: [ cart add: self itemNotSellByTheStore ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: anError messageText = cart invalidItemErrorMessage.
			self assert: cart isEmpty ]! !

!CartTest methodsFor: 'tests' stamp: 'HernanWilkinson 6/17/2013 17:43'!
test03AfterAddingAnItemTheCartIsNotEmptyAnymore

	| cart |
	
	cart := self createCart.
	
	cart add: self itemSellByTheStore.
	self deny: cart isEmpty ! !

!CartTest methodsFor: 'tests' stamp: 'HernanWilkinson 6/17/2013 17:43'!
test04CanNotAddNonPositiveNumberOfItems

	| cart |
	
	cart := self createCart.
	
	self 
		should: [cart add: 0 of: self itemSellByTheStore ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: anError messageText = cart invalidQuantityErrorMessage.
			self assert: cart isEmpty ]! !

!CartTest methodsFor: 'tests' stamp: 'HernanWilkinson 6/17/2013 17:45'!
test05CanNotAddMoreThanOneItemNotSellByTheStore

	| cart |
	
	cart := self createCart.
	
	self 
		should: [cart add: 2 of: self itemNotSellByTheStore  ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: anError messageText = cart invalidItemErrorMessage.
			self assert: cart isEmpty ]! !

!CartTest methodsFor: 'tests' stamp: 'HernanWilkinson 6/17/2013 17:43'!
test06CartRemembersAddedItems

	| cart |
	
	cart := self createCart.
	
	cart add: self itemSellByTheStore.
	self assert: (cart includes: self itemSellByTheStore)! !

!CartTest methodsFor: 'tests' stamp: 'HernanWilkinson 6/17/2013 17:43'!
test07CartDoesNotHoldNotAddedItems

	| cart |
	
	cart := self createCart.
	
	self deny: (cart includes: self itemSellByTheStore)! !

!CartTest methodsFor: 'tests' stamp: 'HernanWilkinson 6/17/2013 17:45'!
test08CartRemembersTheNumberOfAddedItems

	| cart |
	
	cart := self createCart.
	
	cart add: 2 of: self itemSellByTheStore.
	self assert: (cart occurrencesOf: self itemSellByTheStore) = 2! !

!CartTest methodsFor: 'tests' stamp: 'MA 7/12/2020 21:17:54'!
test09EmptyCartShowsZeroCost

	| cart |
	
	cart := self createCart.
	
	self assert: 0 equals: cart totalValue.! !

!CartTest methodsFor: 'tests' stamp: 'MA 7/12/2020 21:16:37'!
test10CartKnowsTheTotalCostOfItsLoad

	| cart |
	
	cart := self createCart.
	cart add: 5 of: self itemSellByTheStore .
	
	self assert: 500 equals: cart totalValue.! !

!CartTest methodsFor: 'tests' stamp: 'MA 7/12/2020 23:33:34'!
test11CartBecomesEmptyAfterClearingItsContents

	| cart |
	
	cart := self createCart.
	cart add: 5 of: self itemSellByTheStore .
	cart clear.
	
	self assert: cart isEmpty.! !


!CartTest methodsFor: 'support' stamp: 'MA 7/12/2020 21:31:26'!
createCart
	
	^Cart acceptingItemsOf: self defaultCatalog .! !

!CartTest methodsFor: 'support' stamp: 'MA 7/12/2020 21:12:54'!
defaultCatalog
	
	| catalog |
	catalog _ Dictionary new.
	catalog at: (self itemSellByTheStore)  put: 100.
	^ catalog.! !

!CartTest methodsFor: 'support' stamp: 'HernanWilkinson 6/17/2013 17:44'!
itemNotSellByTheStore
	
	^'invalidBook'! !

!CartTest methodsFor: 'support' stamp: 'HernanWilkinson 6/17/2013 17:43'!
itemSellByTheStore
	
	^ 'validBook'! !


!classDefinition: #CashierPerSaleTest category: #TusLibros!
TestCase subclass: #CashierPerSaleTest
	instanceVariableNames: 'aValidCartValue aValidDate'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!CashierPerSaleTest methodsFor: 'tests' stamp: 'MA 7/14/2020 21:27:30'!
test01CannotCheckoutAnEmptyCart

	| cashier cart creditCard aSalesBook aMerchantProcessor |
	
	cart _ self createEmptyCart.
	creditCard _ self createValidCreditCard.	
	aSalesBook _ self createSalesBook.
	aMerchantProcessor _ self createMerchantProcessor.	
		
	cashier _ CashierPerSale usingSalesBook: aSalesBook usingMerchantProcessor: aMerchantProcessor.	
	
	self 
		should: [ cashier checkout: cart payingWith: creditCard onDate: aValidDate]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cashier emptyCartErrorMessage equals: anError messageText.
			self assert: aSalesBook receivedNoMessages.
			self assert: aMerchantProcessor receivedNoMessages ].! !

!CashierPerSaleTest methodsFor: 'tests' stamp: 'MA 7/14/2020 21:27:30'!
test02CannotCheckoutWithAnInvalidCreditCard

	| cashier cart creditCard aSalesBook aMerchantProcessor |
	
	cart _ self createCartWithItems.
	aSalesBook _ self createSalesBook.
	aMerchantProcessor _ self createMerchantProcessor.
	creditCard _ self createExpiredCreditCard.
	
	cashier _ CashierPerSale usingSalesBook: aSalesBook usingMerchantProcessor: aMerchantProcessor.
	
	self 
		should: [ cashier checkout: cart payingWith: creditCard onDate: aValidDate ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cashier invalidCreditCardErrorMessage equals: anError messageText.
			self assert: aSalesBook receivedNoMessages.
			self assert: aMerchantProcessor receivedNoMessages ].! !

!CashierPerSaleTest methodsFor: 'tests' stamp: 'MA 7/14/2020 21:27:30'!
test03AfterValidCheckoutSalesBookIsGivenANewSale

	| cashier cart creditCard aSalesBook aMerchantProcessor totalValue |
	
	cart _ self createCartWithItems.
	aSalesBook _ self createSalesBook.
	aMerchantProcessor _ self createMerchantProcessor.
	creditCard _ self createValidCreditCard.
	
	totalValue _ cart totalValue.

	cashier _ CashierPerSale usingSalesBook: aSalesBook usingMerchantProcessor: aMerchantProcessor.
	
	cashier checkout: cart payingWith: creditCard onDate: aValidDate.
	
	self assert: 1 equals: (aSalesBook timesReceivedSelector: #add: withArgs: { totalValue }).! !

!CashierPerSaleTest methodsFor: 'tests' stamp: 'MA 7/14/2020 21:27:30'!
test04AfterValidCheckoutCartGetsEmpty

	| cashier cart creditCard aSalesBook aMerchantProcessor |
	
	cart _ self createCartWithItems.
	aSalesBook _ self createSalesBook.
	aMerchantProcessor _ self createMerchantProcessor.
	creditCard _ self createValidCreditCard.
	
	cashier _ CashierPerSale usingSalesBook: aSalesBook usingMerchantProcessor: aMerchantProcessor.
	
	cashier checkout: cart payingWith: creditCard onDate: aValidDate.
	
	self assert: 1 equals: (cart timesReceivedSelector: #clear).! !

!CashierPerSaleTest methodsFor: 'tests' stamp: 'MA 7/14/2020 21:27:30'!
test05DuringAValidCheckoutCashierForwardsDebitToMerchantProcessor

	| cashier cart creditCard aSalesBook aMerchantProcessor cartTotalValue |
		
	cart _ self createCartWithItems.
	aSalesBook _ self createSalesBook.
	aMerchantProcessor _ self createMerchantProcessor.
	creditCard _ self createValidCreditCard.
	
	cartTotalValue _ cart totalValue.
	
	cashier _ CashierPerSale usingSalesBook: aSalesBook usingMerchantProcessor: aMerchantProcessor.
	
	cashier checkout: cart payingWith: creditCard onDate: aValidDate.
	
	self assert: 1 equals: (aMerchantProcessor timesReceivedSelector: #debit:from: withArgs: { cartTotalValue. creditCard. }).! !

!CashierPerSaleTest methodsFor: 'tests' stamp: 'MA 7/14/2020 21:30:13'!
test06IfMerchantProcessorDetectsInvalidCardCheckoutIsNotFullfilled

	| cashier cart creditCard aSalesBook aMerchantProcessor merchantProcessorErrorMessage |
	
	merchantProcessorErrorMessage _ 'card is stolen or has no funds'.
		
	cart _ self createCartWithItems.
	aSalesBook _ self createSalesBook.
	aMerchantProcessor _ self createMerchantProcessorThatRejectsCardWithErrorMessage: merchantProcessorErrorMessage .
	creditCard _ self createValidCreditCard.
	
	cashier _ CashierPerSale usingSalesBook: aSalesBook usingMerchantProcessor: aMerchantProcessor.
	
	self 
		should: [ cashier checkout: cart payingWith: creditCard onDate: aValidDate. ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: merchantProcessorErrorMessage equals: anError messageText.
			self assert: aSalesBook receivedNoMessages.
			self assert: 0 equals: (cart timesReceivedSelector: #clear) ].
! !


!CashierPerSaleTest methodsFor: 'support' stamp: 'MA 7/16/2020 17:07:12'!
createCartWithItems
	| cart |
	cart _ TestDouble new.
	cart when: #isEmpty do: [ false ].
	cart when: #totalValue do: [ aValidCartValue ].
	cart when: #clear do: [ ].	
	^ Spy on: cart! !

!CashierPerSaleTest methodsFor: 'support' stamp: 'MA 7/16/2020 17:07:25'!
createEmptyCart
	| cart |
	cart _ TestDouble new.
	cart when: #isEmpty do: [ true ].
	^ Spy on: cart! !

!CashierPerSaleTest methodsFor: 'support' stamp: 'MA 7/14/2020 21:13:36'!
createExpiredCreditCard
	| cc |
	cc := TestDouble new.
	cc when: #isValidOn: do: [ :aDate | false ].
	^ cc
! !

!CashierPerSaleTest methodsFor: 'support' stamp: 'MA 7/16/2020 17:07:37'!
createMerchantProcessor
	| mp |
	mp _ TestDouble new.
	mp when: #debit:from: do: [ :amount :card | ].
	^ Spy on: mp! !

!CashierPerSaleTest methodsFor: 'support' stamp: 'MA 7/16/2020 17:07:42'!
createMerchantProcessorThatRejectsCardWithErrorMessage: anErrorMessage
	| mp |
	mp _ TestDouble new.
	mp when: #debit:from: do: [ :amount :card | Error signal: anErrorMessage ].
	^ Spy on: mp! !

!CashierPerSaleTest methodsFor: 'support' stamp: 'MA 7/16/2020 17:07:48'!
createSalesBook
	| sb |
	sb _ TestDouble new.
	sb when: #add: do: [:amount | ].
	^ Spy on: sb.! !

!CashierPerSaleTest methodsFor: 'support' stamp: 'MA 7/14/2020 21:11:52'!
createValidCreditCard
	| cc |
	cc := TestDouble new.
	cc when: #isValidOn: do: [ :aDate | true ].
	^ cc
! !


!CashierPerSaleTest methodsFor: 'setUp/tearDown' stamp: 'MA 7/14/2020 21:20:05'!
setUp
	aValidCartValue _ 100.
	aValidDate _ '07/01/20'! !


!classDefinition: #CreditCardBuilderTest category: #TusLibros!
TestCase subclass: #CreditCardBuilderTest
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!CreditCardBuilderTest methodsFor: 'tests' stamp: 'MA 7/13/2020 15:02:04'!
test01CardCannotHaveAnEmptyOwnerName

	| cardBuilder |
	cardBuilder _ CreditCard builder.
	
	self
		should: [ cardBuilder setOwner: '' ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cardBuilder invalidOwnerErrorMessage equals: anError messageText ]! !

!CreditCardBuilderTest methodsFor: 'tests' stamp: 'MA 7/13/2020 15:36:01'!
test01CardCannotHaveAnOwnerNameLongerThan30Characters

	| cardBuilder |
	cardBuilder _ CreditCard builder.
	
	self
		should: [ cardBuilder setOwner: 'un nombre de mas de 30 caracteres' ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cardBuilder invalidOwnerErrorMessage equals: anError messageText ]! !

!CreditCardBuilderTest methodsFor: 'tests' stamp: 'MA 7/13/2020 15:02:24'!
test02ValidFromMustBeAValidDate

	| cardBuilder |
	cardBuilder _ CreditCard builder.
	
	self
		should: [ cardBuilder setValidFrom: 'abcd' ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cardBuilder invalidDateFormatErrorMessage equals: anError messageText ]! !

!CreditCardBuilderTest methodsFor: 'tests' stamp: 'MA 7/13/2020 15:02:41'!
test03ValidUntilMustBeAValidDate

	| cardBuilder |
	cardBuilder _ CreditCard builder.
	
	self
		should: [ cardBuilder setValidUntil: 'abcd' ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cardBuilder invalidDateFormatErrorMessage equals: anError messageText ]! !

!CreditCardBuilderTest methodsFor: 'tests' stamp: 'MA 7/13/2020 15:02:55'!
test04CardNumberMustBeANumber

	| cardBuilder |
	cardBuilder _ CreditCard builder.
	
	self
		should: [ cardBuilder setNumber: 'abcdefghijklmnop' ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cardBuilder invalidCardNumberFormatErrorMessage equals: anError messageText ]! !

!CreditCardBuilderTest methodsFor: 'tests' stamp: 'MA 7/13/2020 15:03:08'!
test05CardNumberMustBe16CharactersLong

	| cardBuilder |
	cardBuilder _ CreditCard builder.
	
	self
		should: [ cardBuilder setNumber: '1234' ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cardBuilder invalidCardNumberLengthErrorMessage equals: anError messageText ].! !

!CreditCardBuilderTest methodsFor: 'tests' stamp: 'MA 7/13/2020 17:51:53'!
test06CardCannotBeBuiltUntilAllFieldsHaveBeenAssigned

	| cardBuilder |
	cardBuilder _ CreditCard builder.

	cardBuilder setNumber: '1234567812345678'.
	cardBuilder setOwner: 'nombre apellido'.
	cardBuilder setValidFrom: '07/20'.

	self
		should: [ cardBuilder build ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: cardBuilder missingFieldsErrorMessage equals: anError messageText ].! !

!CreditCardBuilderTest methodsFor: 'tests' stamp: 'MA 7/13/2020 16:13:38'!
test07CardIsOnlyValidWithinValidityPeriod

	| card cardBuilder aDate |
	cardBuilder _ CreditCard builder.

	cardBuilder setNumber: '1234567812345678'.
	cardBuilder setOwner: 'nombre apellido'.
	cardBuilder setValidFrom: '07/20'.
	cardBuilder setValidUntil: '07/24'.
	card _ cardBuilder build.
	
	aDate _ '07/01/20' asDate.	
	self assert: (card isValidOn: aDate).
	
	aDate _ '07/31/24' asDate.	
	self assert: (card isValidOn: aDate).
	
	aDate _ '06/30/20' asDate.	
	self deny: (card isValidOn: aDate).
	
	aDate _ '08/01/24' asDate.	
	self deny: (card isValidOn: aDate).

! !


!classDefinition: #SpyTest category: #TusLibros!
TestCase subclass: #SpyTest
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!SpyTest methodsFor: 'tests' stamp: 'MA 7/16/2020 15:54:45'!
test00SpyForwardsEveryMessageReceivedToItsSpiedObject

	| aCollection aSpiedCollection anElement |
	
	anElement _ 'xxx'.

	aCollection _ OrderedCollection new.

	aSpiedCollection _ Spy on: aCollection.

	self
		should: [ aSpiedCollection burn: anElement ]
		raise: MessageNotUnderstood.
		
	self
		shouldnt: [ aSpiedCollection add: anElement ]
		raise: MessageNotUnderstood.
		
	
	self assert: aCollection includes: anElement. ! !

!SpyTest methodsFor: 'tests' stamp: 'MA 7/16/2020 15:58:41'!
test01SpyKnowsIfItHasReceivedNoMessages

	| aCollection aSpiedCollection |
	
	aCollection _ OrderedCollection new.

	aSpiedCollection _ Spy on: aCollection.
	
	self assert: aSpiedCollection receivedNoMessages.
	
	aSpiedCollection add: '1'.
	
	self deny: aSpiedCollection receivedNoMessages.
! !

!SpyTest methodsFor: 'tests' stamp: 'MA 7/16/2020 16:08:39'!
test02SpyRemembersHowManyTotalMessagesItReceived

	| aCollection aSpiedCollection |
	
	aCollection _ OrderedCollection new.

	aSpiedCollection _ Spy on: aCollection.
	
	aSpiedCollection add: '1'.
	aSpiedCollection add: '2'.
	aSpiedCollection add: '3'.
	aSpiedCollection first.
	aSpiedCollection last.
	
	self assert: 5 equals: (aSpiedCollection messagesReceivedCount).! !

!SpyTest methodsFor: 'tests' stamp: 'MA 7/16/2020 16:11:56'!
test03SpyRemembersHowManyTimesItReceivedASpecificMessage

	| aCollection aSpiedCollection |
	
	aCollection _ OrderedCollection new.

	aSpiedCollection _ Spy on: aCollection.
	
	self assert: 0 equals: (aSpiedCollection timesReceivedSelector: #add:).
		
	aSpiedCollection add: '1'.
	aSpiedCollection add: '2'.
	aSpiedCollection add: '3'.
	aSpiedCollection first.
	aSpiedCollection last.
	
	self assert: 3 equals: (aSpiedCollection timesReceivedSelector: #add:).! !

!SpyTest methodsFor: 'tests' stamp: 'MA 7/16/2020 16:26:13'!
test04SpyRemembersForEachMessageWhatArgumentsItReceived

	| aCollection aSpiedCollection elemX elemY elemZ |
	
	aCollection _ OrderedCollection new.
	
	elemX _ 'x'.
	elemY _ 'y'.
	elemZ _ 'z'.

	aSpiedCollection _ Spy on: aCollection.
		
	aSpiedCollection add: '1'.
	aSpiedCollection add: '2'.
	aSpiedCollection add: '3'.
	aSpiedCollection at: 1 put: elemX.
	aSpiedCollection at: 2 put: elemY.
	aSpiedCollection at: 3 put: elemZ.
	
	self assert: 3 equals: (aSpiedCollection timesReceivedSelector: #at:put:).
	
	self assert: 1 equals: (aSpiedCollection timesReceivedSelector: #at:put: withArgs: { 1. elemX. } ).! !


!classDefinition: #TestDoubleTest category: #TusLibros!
TestCase subclass: #TestDoubleTest
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!TestDoubleTest methodsFor: 'tests' stamp: 'MA 7/16/2020 15:17:03'!
test01TestDoubleCannotHandleMessagesThatHaveNotBeenDefined

	| aCreditCard |
	aCreditCard _ TestDouble new.

	self
		should: [ aCreditCard isValidOn: 'xxx' ]
		raise: Error - MessageNotUnderstood
		withExceptionDo: [ :anError |
			self assert: TestDouble behaviourNotDefinedErrorDescription equals: anError messageText ]! !

!TestDoubleTest methodsFor: 'tests' stamp: 'MA 7/16/2020 15:37:11'!
test02TestDoubleCanHandleMessagesOnceTheyHaveBeenDefined

	| aCreditCard theArg result |
	
	aCreditCard _ TestDouble new.
	
	theArg _ 'xxxxx'.

	aCreditCard when: #isValidOn: do: [ :anArg | anArg ].
	
	result _ aCreditCard isValidOn: theArg.
	
	self assert: theArg equals: result.! !


!classDefinition: #Cart category: #TusLibros!
Object subclass: #Cart
	instanceVariableNames: 'catalog items'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!Cart methodsFor: 'error messages' stamp: 'HernanWilkinson 6/17/2013 17:45'!
invalidItemErrorMessage
	
	^'Item is not in catalog'! !

!Cart methodsFor: 'error messages' stamp: 'HernanWilkinson 6/17/2013 17:45'!
invalidQuantityErrorMessage
	
	^'Invalid number of items'! !


!Cart methodsFor: 'assertions' stamp: 'MA 7/12/2020 21:12:28'!
assertIsValidItem: anItem

	(catalog includesKey: anItem) ifFalse: [ self error: self invalidItemErrorMessage ]! !

!Cart methodsFor: 'assertions' stamp: 'HernanWilkinson 6/17/2013 17:51'!
assertIsValidQuantity: aQuantity

	aQuantity strictlyPositive ifFalse: [ self error: self invalidQuantityErrorMessage ]! !


!Cart methodsFor: 'initialization' stamp: 'HernanWilkinson 6/17/2013 17:48'!
initializeAcceptingItemsOf: aCatalog

	catalog := aCatalog.
	items := OrderedCollection new.! !


!Cart methodsFor: 'queries' stamp: 'HernanWilkinson 6/17/2013 17:45'!
occurrencesOf: anItem

	^items occurrencesOf: anItem  ! !


!Cart methodsFor: 'testing' stamp: 'HernanWilkinson 6/17/2013 17:44'!
includes: anItem

	^items includes: anItem ! !

!Cart methodsFor: 'testing' stamp: 'HernanWilkinson 6/17/2013 17:44'!
isEmpty
	
	^items isEmpty ! !


!Cart methodsFor: 'operations' stamp: 'HernanWilkinson 6/17/2013 17:44'!
add: anItem

	^ self add: 1 of: anItem ! !

!Cart methodsFor: 'operations' stamp: 'HernanWilkinson 6/17/2013 17:51'!
add: aQuantity of: anItem

	self assertIsValidQuantity: aQuantity.
	self assertIsValidItem: anItem.

	1 to: aQuantity do: [ :aNumber | items add: anItem ]! !

!Cart methodsFor: 'operations' stamp: 'MA 7/12/2020 23:36:41'!
clear
	[ items isEmpty ] whileFalse: [ items removeFirst ]! !


!Cart methodsFor: 'accessing' stamp: 'MA 7/12/2020 21:24:32'!
totalValue
	^ items inject: 0 into: [ :sum :each | sum + (catalog at: each) ]! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'Cart class' category: #TusLibros!
Cart class
	instanceVariableNames: ''!

!Cart class methodsFor: 'instance creation' stamp: 'HernanWilkinson 6/17/2013 17:48'!
acceptingItemsOf: aCatalog

	^self new initializeAcceptingItemsOf: aCatalog ! !


!classDefinition: #CashierPerSale category: #TusLibros!
Object subclass: #CashierPerSale
	instanceVariableNames: 'catalog salesCount earnings salesBook merchantProcessor'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!CashierPerSale methodsFor: 'initialization' stamp: 'MA 7/13/2020 21:55:44'!
initializeUsingSalesBook: aSalesBook merchantProcessor: aMerchantProcessor  
	salesBook _ aSalesBook.
	merchantProcessor _ aMerchantProcessor.! !


!CashierPerSale methodsFor: 'accessing' stamp: 'MA 7/13/2020 20:55:35'!
earnings
	^ earnings.! !

!CashierPerSale methodsFor: 'accessing' stamp: 'MA 7/13/2020 20:55:35'!
salesCount
	^ salesCount.! !


!CashierPerSale methodsFor: 'operations' stamp: 'MA 7/13/2020 22:24:43'!
checkout: aCart payingWith: aCreditCard onDate: aDate

	| amountToDebit |
	
	self assertValidCart: aCart.
	self assertValidCard: aCreditCard onDate: aDate.
	
	amountToDebit  _ aCart totalValue.
	
	merchantProcessor debit: amountToDebit from: aCreditCard. 
	
	salesBook add: amountToDebit.
	
	aCart clear.! !


!CashierPerSale methodsFor: 'error messages' stamp: 'MA 7/13/2020 20:55:35'!
emptyCartErrorMessage
	^ 'cannot checkout on an empty cart'! !

!CashierPerSale methodsFor: 'error messages' stamp: 'MA 7/13/2020 20:55:35'!
invalidCreditCardErrorMessage
	^ 'cannot checkout using an invalid credit card'.! !


!CashierPerSale methodsFor: 'assertions' stamp: 'MA 7/14/2020 18:42:49'!
assertValidCard: aCreditCard onDate: aDate

	(aCreditCard isValidOn: aDate) ifFalse: [ self error: self invalidCreditCardErrorMessage ]! !

!CashierPerSale methodsFor: 'assertions' stamp: 'MA 7/14/2020 20:28:57'!
assertValidCart: aCart
	(aCart isEmpty) ifTrue: [ self error: self emptyCartErrorMessage ]! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'CashierPerSale class' category: #TusLibros!
CashierPerSale class
	instanceVariableNames: 'catalog'!

!CashierPerSale class methodsFor: 'instance creation' stamp: 'MA 7/13/2020 21:55:06'!
usingSalesBook: aSalesBook usingMerchantProcessor: aMerchantProcessor  
	^self new initializeUsingSalesBook: aSalesBook merchantProcessor: aMerchantProcessor ! !


!classDefinition: #CreditCard category: #TusLibros!
Object subclass: #CreditCard
	instanceVariableNames: 'number owner validFrom validUntil'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!CreditCard methodsFor: 'validation' stamp: 'MA 7/13/2020 16:16:08'!
isValidOn: aDate
	| start end |
	
	start _ Date newDay: 1 month: (validFrom monthName) year: (validFrom yearNumber).
	end _ Date newDay: 1 month: (validUntil next monthName) year: (validUntil next yearNumber).
	
	^ aDate next > start and: aDate < end.
	! !


!CreditCard methodsFor: 'initialization' stamp: 'MA 7/13/2020 15:41:14'!
initializeWithNumber: aNumberAsString withOwner: aName withDateFrom: aMonthYearFromAsString withDateUntil: aMonthYearUntilAsString 
	number := aNumberAsString.
	owner := aName.
	validFrom := aMonthYearFromAsString.
	validUntil := aMonthYearUntilAsString.! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'CreditCard class' category: #TusLibros!
CreditCard class
	instanceVariableNames: ''!

!CreditCard class methodsFor: 'builder' stamp: 'MA 7/12/2020 22:49:49'!
builder
	^ CreditCardBuilder new.! !


!CreditCard class methodsFor: 'instance creation' stamp: 'MA 7/13/2020 15:40:05'!
withNumber: aNumberAsString withOwner: aName withDateFrom: aMonthYearFromAsString withDateUntil: aMonthYearUntilAsString 
	^self new initializeWithNumber: aNumberAsString withOwner: aName withDateFrom: aMonthYearFromAsString withDateUntil: aMonthYearUntilAsString. ! !


!classDefinition: #CreditCardBuilder category: #TusLibros!
Object subclass: #CreditCardBuilder
	instanceVariableNames: 'number owner dateFrom dateUntil'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!CreditCardBuilder methodsFor: 'setters' stamp: 'MA 7/13/2020 15:41:39'!
setNumber: aNumberAsString

	self assertValidCardNumberLength: aNumberAsString.
	
	self assertValidCardNumberFormat: aNumberAsString .
	
	number _ aNumberAsString! !

!CreditCardBuilder methodsFor: 'setters' stamp: 'MA 7/13/2020 15:42:33'!
setOwner: aName
	self assertValidOwner: aName.
	
	owner _ aName.! !

!CreditCardBuilder methodsFor: 'setters' stamp: 'MA 7/13/2020 15:58:02'!
setValidFrom: aDateAsString
	self assertValidDateFormat: aDateAsString.
	
	dateFrom _ aDateAsString asMonth.! !

!CreditCardBuilder methodsFor: 'setters' stamp: 'MA 7/13/2020 15:58:12'!
setValidUntil: aDateAsString 
	self assertValidDateFormat: aDateAsString.
	
	dateUntil _ aDateAsString asMonth.! !


!CreditCardBuilder methodsFor: 'error messages' stamp: 'MA 7/13/2020 14:32:54'!
invalidCardNumberFormatErrorMessage
	^ 'card number must only contain numeric characters'! !

!CreditCardBuilder methodsFor: 'error messages' stamp: 'MA 7/13/2020 14:36:18'!
invalidCardNumberLengthErrorMessage
	^ 'card number must be 16 digits long'. ! !

!CreditCardBuilder methodsFor: 'error messages' stamp: 'MA 7/13/2020 13:31:48'!
invalidDateFormatErrorMessage
	^ 'string provided cannot be parsed into a valid date'! !

!CreditCardBuilder methodsFor: 'error messages' stamp: 'MA 7/13/2020 14:46:02'!
invalidOwnerErrorMessage
	^ 'owner name cannot be empty'! !

!CreditCardBuilder methodsFor: 'error messages' stamp: 'MA 7/13/2020 15:36:42'!
missingFieldsErrorMessage
	^ 'cannot create card with missing fields'.! !


!CreditCardBuilder methodsFor: 'assertions' stamp: 'MA 7/13/2020 20:29:15'!
assertNothingIsMissing
	(owner isNil
		or: dateFrom isNil
		or: dateUntil isNil
		or: number isNil)
			ifTrue: [ Error signal: self missingFieldsErrorMessage ]! !

!CreditCardBuilder methodsFor: 'assertions' stamp: 'MA 7/13/2020 15:25:42'!
assertValidCardNumberFormat: aNumberAsString

	aNumberAsString do: [ :char | char isDigit ifFalse: [ Error signal: self invalidCardNumberFormatErrorMessage ] ].! !

!CreditCardBuilder methodsFor: 'assertions' stamp: 'MA 7/13/2020 14:43:03'!
assertValidCardNumberLength: aNumberAsString

	^ aNumberAsString size = 16 ifFalse: [ Error signal: self invalidCardNumberLengthErrorMessage ]! !

!CreditCardBuilder methodsFor: 'assertions' stamp: 'MA 7/13/2020 14:44:41'!
assertValidDateFormat: aString

	^ [ aString asMonth ]
	on: Error
	do: [ Error signal: self invalidDateFormatErrorMessage. ]! !

!CreditCardBuilder methodsFor: 'assertions' stamp: 'MA 7/13/2020 15:31:18'!
assertValidOwner: aString 
	(aString size between: 1 and: 30) ifFalse: [ Error signal: self invalidOwnerErrorMessage ].! !


!CreditCardBuilder methodsFor: 'building' stamp: 'MA 7/13/2020 17:45:32'!
build
	self assertNothingIsMissing.
	
	^ CreditCard
		withNumber: number
		withOwner: owner
		withDateFrom: dateFrom
		withDateUntil: dateUntil.! !


!classDefinition: #MerchantProcessor category: #TusLibros!
Object subclass: #MerchantProcessor
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!MerchantProcessor methodsFor: 'operations' stamp: 'MA 7/13/2020 21:28:42'!
debit: anAmount from: aCreditCard
	self subclassResponsibility .! !


!classDefinition: #TestDouble category: #TusLibros!
Object subclass: #TestDouble
	instanceVariableNames: 'customMethodDictionary methodCalls'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!TestDouble methodsFor: 'initialization' stamp: 'MA 7/16/2020 17:00:07'!
initialize
	customMethodDictionary  _ Dictionary new.
! !


!TestDouble methodsFor: 'operations' stamp: 'MA 7/14/2020 20:25:55'!
actUpon: aMessage
	| block result |
	block _ customMethodDictionary at: aMessage selector.
	result _ block valueWithArguments: aMessage arguments.
	^ result.! !

!TestDouble methodsFor: 'operations' stamp: 'MA 7/16/2020 16:59:53'!
doesNotUnderstand: aMessage
	self assertBehaviourHasBeenSetFor: aMessage.
	^ self actUpon: aMessage.
! !

!TestDouble methodsFor: 'operations' stamp: 'MA 7/14/2020 17:17:14'!
when: aSelector do: aBlock
	customMethodDictionary at: aSelector put: aBlock.! !


!TestDouble methodsFor: 'assertions' stamp: 'MA 7/14/2020 17:14:36'!
assertBehaviourHasBeenSetFor: aMessage
	(customMethodDictionary includesKey: aMessage selector) ifFalse: [
		self error: self class behaviourNotDefinedErrorDescription.
	].! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'TestDouble class' category: #TusLibros!
TestDouble class
	instanceVariableNames: ''!

!TestDouble class methodsFor: 'error handling' stamp: 'MA 7/14/2020 16:55:13'!
behaviourNotDefinedErrorDescription

	^ 'selector has no associated behaviour in mock object'! !


!classDefinition: #Spy category: #TusLibros!
ProtoObject subclass: #Spy
	instanceVariableNames: 'methodCalls spiedObject'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'TusLibros'!

!Spy methodsFor: 'initialization' stamp: 'MA 7/16/2020 15:43:33'!
initialize
	methodCalls _ Dictionary new.! !

!Spy methodsFor: 'initialization' stamp: 'MA 7/16/2020 15:46:19'!
initializeOn: anObject
	spiedObject _ anObject.! !


!Spy methodsFor: 'operations' stamp: 'MA 7/16/2020 16:53:37'!
doesNotUnderstand: aMessage
	self countMessage: aMessage.
	^ aMessage sendTo: spiedObject.! !


!Spy methodsFor: 'accessing' stamp: 'MA 7/16/2020 15:35:52'!
messagesReceivedCount
	^ methodCalls keys inject: 0 into: [ :accum :key | accum + (methodCalls at: key) size ].! !

!Spy methodsFor: 'accessing' stamp: 'MA 7/16/2020 15:35:52'!
receivedNoMessages
	^ self messagesReceivedCount = 0.! !

!Spy methodsFor: 'accessing' stamp: 'MA 7/16/2020 15:35:52'!
timesReceivedSelector: aSelector
	^ (methodCalls at: aSelector ifAbsent: [ #() ]) size.! !

!Spy methodsFor: 'accessing' stamp: 'MA 7/16/2020 16:36:21'!
timesReceivedSelector: aSelector withArgs: anArrayOfArgs
	| argsReceived |
	argsReceived _ methodCalls at: aSelector ifAbsent: [ OrderedCollection new ].
	^ (argsReceived select: [ :arrOfArgs | self elemsIn: arrOfArgs matchElemsIn: anArrayOfArgs ]) size.
! !


!Spy methodsFor: 'private' stamp: 'MA 7/16/2020 16:52:32'!
countMessage: aMessage
	(methodCalls includesKey: aMessage selector) ifFalse: [ methodCalls at: aMessage selector put: OrderedCollection new ].
	
	(methodCalls at: aMessage selector) add: aMessage arguments.! !

!Spy methodsFor: 'private' stamp: 'MA 7/16/2020 15:35:52'!
elemsIn: collection1 matchElemsIn: collection2

	(collection1 size = collection2 size) ifFalse: [ ^ false ].
	
	(1 to: collection1 size) do: [ :index | ((collection1 at: index) = (collection2 at: index)) ifFalse: [^ false ] ].
	
	^ true.
	
	! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'Spy class' category: #TusLibros!
Spy class
	instanceVariableNames: ''!

!Spy class methodsFor: 'instance creation' stamp: 'MA 7/16/2020 15:45:45'!
on: anObject
	^ self new initializeOn: anObject.
! !