12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900 |
- Smalltalk createPackage: 'Kernel-Tests'!
- TestCase subclass: #AnnouncementSubscriptionTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !AnnouncementSubscriptionTest methodsFor: 'tests'!
- testAddExtensionMethod
- | method dirty |
- dirty := self class package isDirty.
- self class package beClean.
- method := self class compile: 'doNothing' protocol: '**not-a-package'.
- self deny: self class package isDirty.
-
- self class removeCompiledMethod: method.
- dirty ifTrue: [ self class package beDirty ]
- !
- testHandlesAnnouncement
- | subscription announcementClass1 announcementClass2 classBuilder |
-
- classBuilder := ClassBuilder new.
- announcementClass1 := classBuilder basicAddSubclassOf: SystemAnnouncement named: 'TestAnnouncement1' instanceVariableNames: #() package: 'Kernel-Tests'.
-
- subscription := AnnouncementSubscription new announcementClass: SystemAnnouncement.
- "Test whether the same class triggers the announcement"
- self assert: (subscription handlesAnnouncement: SystemAnnouncement) equals: true.
- "Test whether a subclass triggers the announcement"
- self assert: (subscription handlesAnnouncement: announcementClass1) equals: true.
- "Test whether an unrelated class does not trigger the announcement"
- self assert: (subscription handlesAnnouncement: Object) equals: false.
-
- classBuilder basicRemoveClass: announcementClass1.
- ! !
- TestCase subclass: #AnnouncerTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !AnnouncerTest methodsFor: 'tests'!
- testOnDo
- | counter announcer |
-
- counter := 0.
- announcer := Announcer new.
- announcer on: SystemAnnouncement do: [ counter := counter + 1 ].
- announcer announce: (SystemAnnouncement new).
- self assert: counter equals: 1.
- announcer announce: (SystemAnnouncement new).
- self assert: counter equals: 2.
- !
- testOnDoFor
- | counter announcer |
-
- counter := 0.
- announcer := Announcer new.
- announcer on: SystemAnnouncement do: [ counter := counter + 1 ] for: self.
- announcer announce: (SystemAnnouncement new).
- self assert: counter equals: 1.
- announcer announce: (SystemAnnouncement new).
- self assert: counter equals: 2.
-
- announcer unsubscribe: self.
-
- announcer announce: (SystemAnnouncement new).
- self assert: counter equals: 2.
- !
- testOnDoOnce
- | counter announcer |
-
- counter := 0.
- announcer := Announcer new.
- announcer on: SystemAnnouncement doOnce: [ counter := counter + 1 ].
- announcer announce: (SystemAnnouncement new).
- self assert: counter equals: 1.
- announcer announce: (SystemAnnouncement new).
- self assert: counter equals: 1.
- ! !
- TestCase subclass: #BlockClosureTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !BlockClosureTest methodsFor: 'fixture'!
- localReturnOnDoCatch
- [ ^ 2 ] on: Error do: [].
- ^ 3
- !
- localReturnOnDoMiss
- [ ^ 2 ] on: Class do: [].
- ^ 3
- ! !
- !BlockClosureTest methodsFor: 'tests'!
- testCanClearInterval
- self shouldnt: [ ([ Error new signal ] valueWithInterval: 0) clearInterval ] raise: Error
- !
- testCanClearTimeout
- self shouldnt: [ ([ Error new signal ] valueWithTimeout: 0) clearTimeout ] raise: Error
- !
- testCompiledSource
- self assert: ([ 1+1 ] compiledSource includesSubString: 'function')
- !
- testCurrySelf
- | curriedMethod array |
- curriedMethod := [ :selfarg :x | selfarg at: x ] currySelf asCompiledMethod: 'foo:'.
- array := #(3 1 4).
- ClassBuilder new installMethod: curriedMethod forClass: Array protocol: '**test helper'.
- [ self assert: (array foo: 2) equals: 1 ]
- ensure: [ Array removeCompiledMethod: curriedMethod ]
- !
- testEnsure
- self assert: ([ 3 ] ensure: [ 4 ]) equals: 3
- !
- testEnsureRaises
- self should: [ [Error new signal ] ensure: [ true ]] raise: Error
- !
- testExceptionSemantics
- "See https://lolg.it/amber/amber/issues/314"
- self timeout: 100.
-
- (self async: [
- [
- self assert: true.
- Error signal.
- "The following should *not* be run"
- self deny: true.
- self finished.
- ] on: Error do: [ :ex | self finished ]
- ]) valueWithTimeout: 0
- !
- testLocalReturnOnDoCatch
- self assert: self localReturnOnDoCatch equals: 2
- !
- testLocalReturnOnDoMiss
- self assert: self localReturnOnDoMiss equals: 2
- !
- testNewWithValues
- <inlineJS: '
- function TestConstructor(arg1, arg2, arg3) {}
- TestConstructor.prototype.name = "theTestPrototype";
- var wrappedConstructor = $recv(TestConstructor);
- var result = wrappedConstructor._newWithValues_([1, 2, 3]);
- self._assert_(result instanceof TestConstructor);
- self._assert_equals_(result.name, "theTestPrototype");
- /* newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made. */
- self._should_raise_(function () {wrappedConstructor._newWithValues_("single argument");}, $globals.Error);
- '>
- !
- testNumArgs
- self assert: [] numArgs equals: 0.
- self assert: [ :a :b | ] numArgs equals: 2
- !
- testOnDo
- self assert: ([ Error new signal ] on: Error do: [ :ex | true ])
- !
- testValue
- self assert: ([ 1+1 ] value) equals: 2.
- self assert: ([ :x | x +1 ] value: 2) equals: 3.
- self assert: ([ :x :y | x*y ] value: 2 value: 4) equals: 8.
- "Arguments are optional in Amber. This isn't ANSI compliant."
- self assert: ([ :a :b :c | 1 ] value) equals: 1
- !
- testValueWithPossibleArguments
- self assert: ([ 1 ] valueWithPossibleArguments: #(3 4)) equals: 1.
- self assert: ([ :a | a + 4 ] valueWithPossibleArguments: #(3 4)) equals: 7.
- self assert: ([ :a :b | a + b ] valueWithPossibleArguments: #(3 4 5)) equals: 7.
- !
- testWhileFalse
- | i |
- i := 0.
- [ i > 5 ] whileFalse: [ i := i + 1 ].
- self assert: i equals: 6.
- i := 0.
- [ i := i + 1. i > 5 ] whileFalse.
- self assert: i equals: 6
- !
- testWhileTrue
- | i |
- i := 0.
- [ i < 5 ] whileTrue: [ i := i + 1 ].
- self assert: i equals: 5.
- i := 0.
- [ i := i + 1. i < 5 ] whileTrue.
- self assert: i equals: 5
- ! !
- TestCase subclass: #BooleanTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !BooleanTest methodsFor: 'tests'!
- testEquality
- "We're on top of JS...just be sure to check the basics!!"
- self deny: 0 = false.
- self deny: false = 0.
- self deny: '' = false.
- self deny: false = ''.
- self assert: (true = true).
- self deny: false = true.
- self deny: true = false.
- self assert: (false = false).
- "JS may do some type coercing after sending a message"
- self assert: (true yourself = true).
- self assert: (true yourself = true yourself)
- !
- testIdentity
- "We're on top of JS...just be sure to check the basics!!"
- self deny: 0 == false.
- self deny: false == 0.
- self deny: '' == false.
- self deny: false == ''.
- self assert: true == true.
- self deny: false == true.
- self deny: true == false.
- self assert: false == false.
- "JS may do some type coercing after sending a message"
- self assert: true yourself == true.
- self assert: true yourself == true yourself
- !
- testIfTrueIfFalse
- self assert: (true ifTrue: [ 'alternative block' ]) equals: 'alternative block'.
- self assert: (true ifFalse: [ 'alternative block' ]) equals: nil.
- self assert: (false ifTrue: [ 'alternative block' ]) equals: nil.
- self assert: (false ifFalse: [ 'alternative block' ]) equals: 'alternative block'.
- self assert: (false ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block2'.
- self assert: (false ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block'.
- self assert: (true ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block'.
- self assert: (true ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block2'.
- !
- testIfTrueIfFalseWithBoxing
- self assert: (true yourself ifTrue: [ 'alternative block' ]) equals: 'alternative block'.
- self assert: (true yourself ifFalse: [ 'alternative block' ]) equals: nil.
- self assert: (false yourself ifTrue: [ 'alternative block' ]) equals: nil.
- self assert: (false yourself ifFalse: [ 'alternative block' ]) equals: 'alternative block'.
- self assert: (false yourself ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block2'.
- self assert: (false yourself ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block'.
- self assert: (true yourself ifTrue: [ 'alternative block' ] ifFalse: [ 'alternative block2' ]) equals: 'alternative block'.
- self assert: (true yourself ifFalse: [ 'alternative block' ] ifTrue: [ 'alternative block2' ]) equals: 'alternative block2'.
- !
- testLogic
- "Trivial logic table"
- self assert: (true & true);
- deny: (true & false);
- deny: (false & true);
- deny: (false & false).
- self assert: (true | true);
- assert: (true | false);
- assert: (false | true);
- deny: (false | false).
- "Checking that expressions work fine too"
- self assert: (true & (1 > 0));
- deny: ((1 > 0) & false);
- deny: ((1 > 0) & (1 > 2)).
- self assert: (false | (1 > 0));
- assert: ((1 > 0) | false);
- assert: ((1 > 0) | (1 > 2))
- !
- testLogicKeywords
- "Trivial logic table"
- self
- assert: (true and: [ true ]);
- deny: (true and: [ false ]);
- deny: (false and: [ true ]);
- deny: (false and: [ false ]).
- self
- assert: (true or: [ true ]);
- assert: (true or: [ false ]);
- assert: (false or: [ true ]);
- deny: (false or: [ false ]).
-
- "Checking that expressions work fine too"
- self
- assert: (true and: [ 1 > 0 ]);
- deny: ((1 > 0) and: [ false ]);
- deny: ((1 > 0) and: [ 1 > 2 ]).
- self
- assert: (false or: [ 1 > 0 ]);
- assert: ((1 > 0) or: [ false ]);
- assert: ((1 > 0) or: [ 1 > 2 ])
- !
- testNonBooleanError
- self should: [ '' ifTrue: [] ifFalse: [] ] raise: NonBooleanReceiver
- ! !
- TestCase subclass: #ClassBuilderTest
- instanceVariableNames: 'builder theClass'
- package: 'Kernel-Tests'!
- !ClassBuilderTest methodsFor: 'running'!
- setUp
- builder := ClassBuilder new
- !
- tearDown
- theClass ifNotNil: [ Smalltalk removeClass: theClass. theClass := nil ]
- ! !
- !ClassBuilderTest methodsFor: 'tests'!
- testClassCopy
- theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
- self assert: theClass superclass == ObjectMock superclass.
- self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
- self assert: theClass name equals: 'ObjectMock2'.
- self assert: theClass package == ObjectMock package.
- self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
- !
- testClassMigration
- | instance oldClass |
-
- oldClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
- instance := (Smalltalk globals at: 'ObjectMock2') new.
-
- "Change the superclass of ObjectMock2"
- ObjectMock subclass: (Smalltalk globals at: 'ObjectMock2')
- instanceVariableNames: ''
- package: 'Kernel-Tests'.
-
- self deny: oldClass == ObjectMock2.
-
- self assert: ObjectMock2 superclass == ObjectMock.
- self assert: ObjectMock2 instanceVariableNames isEmpty.
- self assert: ObjectMock2 selectors equals: oldClass selectors.
- self assert: ObjectMock2 comment equals: oldClass comment.
- self assert: ObjectMock2 package name equals: 'Kernel-Tests'.
-
- self deny: instance class == ObjectMock2.
- "Commeting this out. Tests implementation detail."
- "self assert: instance class name equals: 'OldObjectMock2'."
-
- self assert: (Smalltalk globals at: instance class name) isNil.
-
- Smalltalk removeClass: ObjectMock2
- !
- testClassMigrationWithClassInstanceVariables
-
- builder copyClass: ObjectMock named: 'ObjectMock2'.
- ObjectMock2 class instanceVariableNames: 'foo bar'.
-
- "Change the superclass of ObjectMock2"
- ObjectMock subclass: (Smalltalk globals at: 'ObjectMock2')
- instanceVariableNames: ''
- package: 'Kernel-Tests'.
-
- self assert: ObjectMock2 class instanceVariableNames equals: #('foo' 'bar').
-
- Smalltalk removeClass: ObjectMock2
- !
- testClassMigrationWithSubclasses
-
- builder copyClass: ObjectMock named: 'ObjectMock2'.
- ObjectMock2 subclass: 'ObjectMock3' instanceVariableNames: '' package: 'Kernel-Tests'.
- ObjectMock3 subclass: 'ObjectMock4' instanceVariableNames: '' package: 'Kernel-Tests'.
-
- "Change the superclass of ObjectMock2"
- ObjectMock subclass: (Smalltalk globals at: 'ObjectMock2')
- instanceVariableNames: ''
- package: 'Kernel-Tests'.
-
- self assert: (ObjectMock subclasses includes: ObjectMock2).
- self assert: (ObjectMock2 subclasses includes: ObjectMock3).
- self assert: (ObjectMock3 subclasses includes: ObjectMock4).
-
- ObjectMock allSubclasses do: [ :each | Smalltalk removeClass: each ]
- !
- testInstanceVariableNames
- self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
- ! !
- TestCase subclass: #ClassTest
- instanceVariableNames: 'builder theClass'
- package: 'Kernel-Tests'!
- !ClassTest methodsFor: 'running'!
- jsConstructor
- <inlineJS: '
- function Foo(){}
- Foo.prototype.valueOf = function () {return 4;};
- return Foo;
- '>
- !
- setUp
- builder := ClassBuilder new
- !
- tearDown
- theClass ifNotNil: [ Smalltalk removeClass: theClass. theClass := nil ]
- !
- trickyJsConstructor
- <inlineJS: '
- function Foo(){}
- Foo.prototype.valueOf = function () {return 4;};
- Foo.prototype._foo = function () {return "bar";};
- return Foo;
- '>
- ! !
- !ClassTest methodsFor: 'tests'!
- testAllSubclasses
- | subclasses index |
- subclasses := Object subclasses.
- index := 1.
- [ index > subclasses size ]
- whileFalse: [ subclasses addAll: (subclasses at: index) subclasses.
- index := index + 1 ].
- self assert: Object allSubclasses equals: subclasses
- !
- testMetaclassSubclasses
- | subclasses |
- subclasses := (Object class instanceClass subclasses
- select: [ :each | each isMetaclass not ])
- collect: [ :each | each theMetaClass ].
- self assert: Object class subclasses equals: subclasses
- !
- testSetJavaScriptConstructor
- | instance |
- theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
- theClass javascriptConstructor: self jsConstructor.
- "part took from copy class test"
- self assert: theClass superclass == ObjectMock superclass.
- self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
- self assert: theClass name equals: 'ObjectMock2'.
- self assert: theClass package == ObjectMock package.
- self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys.
- "testing specific to late-coupled detached root class"
- instance := theClass new.
- self assert: instance class == theClass.
- self assert: instance value equals: 4.
- self shouldnt: [ instance foo: 9 ] raise: Error.
- self assert: instance foo equals: 9
- !
- testTrickySetJavaScriptConstructor
- | instance |
- theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
- theClass javascriptConstructor: self trickyJsConstructor.
- "part took from copy class test"
- self assert: theClass superclass == ObjectMock superclass.
- self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
- self assert: theClass name equals: 'ObjectMock2'.
- self assert: theClass package == ObjectMock package.
- self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys.
- "testing specific to late-coupled detached root class"
- instance := theClass new.
- self assert: instance class == theClass.
- self assert: instance value equals: 4.
- self shouldnt: [ instance foo: 9 ] raise: Error.
- self assert: instance foo equals: 9
- ! !
- TestCase subclass: #CollectionTest
- instanceVariableNames: 'sampleBlock'
- package: 'Kernel-Tests'!
- !CollectionTest methodsFor: 'convenience'!
- assertSameContents: aCollection as: anotherCollection
- self assert: (aCollection size = anotherCollection size).
- aCollection do: [ :each |
- self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
- ! !
- !CollectionTest methodsFor: 'fixture'!
- collection
- "Answers pre-filled collection of type tested."
- self subclassResponsibility
- !
- collectionClass
- "Answers class of collection type tested"
- ^ self class collectionClass
- !
- collectionOfPrintStrings
- "Answers self collection but with values
- changed to their printStrings"
- self subclassResponsibility
- !
- collectionSize
- "Answers size of self collection."
- self subclassResponsibility
- !
- collectionWithDuplicates
- "Answers pre-filled collection of type tested,
- with exactly five distinct elements,
- some of them appearing multiple times, if possible."
- self subclassResponsibility
- !
- collectionWithNewValue
- "Answers a collection which shows how
- self collection would look after adding
- self sampleNewValue"
-
- self subclassResponsibility
- !
- sampleNewValue
- "Answers a value that is not yet there
- and can be put into a tested collection"
-
- ^ 'N'
- !
- sampleNewValueAsCollection
- "Answers self sampleNewValue
- wrapped in single element collection
- of tested type"
-
- ^ self collectionClass with: self sampleNewValue
- ! !
- !CollectionTest methodsFor: 'initialization'!
- initialize
- super initialize.
- sampleBlock := []
- ! !
- !CollectionTest methodsFor: 'testing'!
- isCollectionReadOnly
- ^ false
- ! !
- !CollectionTest methodsFor: 'tests'!
- testAddAll
- self assert: (self collection addAll: self collectionClass new; yourself) equals: self collection.
- self assert: (self collectionClass new addAll: self collection; yourself) equals: self collection.
- self assert: (self collectionClass new addAll: self collectionClass new; yourself) equals: self collectionClass new.
- self assert: (self collection addAll: self sampleNewValueAsCollection; yourself) equals: self collectionWithNewValue.
- self assertSameContents: (self sampleNewValueAsCollection addAll: self collection; yourself) as: self collectionWithNewValue
- !
- testAllSatisfy
- | collection anyOne |
- collection := self collection.
- anyOne := collection anyOne.
- self assert: (collection allSatisfy: [ :each | collection includes: each ]).
- self deny: (collection allSatisfy: [ :each | each ~= anyOne ])
- !
- testAnyOne
- self should: [ self collectionClass new anyOne ] raise: Error.
- self assert: (self collection includes: self collection anyOne)
- !
- testAnySatisfy
- | anyOne |
- anyOne := self collection anyOne.
- self assert: (self collection anySatisfy: [ :each | each = anyOne ]).
- self deny: (self collection anySatisfy: [ :each | each = Object new ])
- !
- testAsArray
- self
- assertSameContents: self collection
- as: self collection asArray
- !
- testAsOrderedCollection
- self
- assertSameContents: self collection
- as: self collection asOrderedCollection
- !
- testAsSet
- | c set |
- c := self collectionWithDuplicates.
- set := c asSet.
- self assert: set size equals: 6.
- c do: [ :each |
- self assert: (set includes: each) ]
- !
- testCollect
- self assert: (self collection collect: [ :each | each ]) equals: self collection.
- self assert: (self collectionWithNewValue collect: [ :each | each ]) equals: self collectionWithNewValue.
- self assert: (self collectionClass new collect: [ :each | each printString ]) equals: self collectionClass new.
- self assert: ((self collection collect: [ self sampleNewValue ]) detect: [ true ]) equals: self sampleNewValue.
- self assert: (self collection collect: [ :each | each printString ]) equals: self collectionOfPrintStrings
- !
- testComma
- self assert: self collection, self collectionClass new equals: self collection.
- self assert: self collectionClass new, self collection equals: self collection.
- self assert: self collectionClass new, self collectionClass new equals: self collectionClass new.
- self assert: self collection, self sampleNewValueAsCollection equals: self collectionWithNewValue.
- self assertSameContents: self sampleNewValueAsCollection, self collection as: self collectionWithNewValue
- !
- testCopy
- self assert: self collectionClass new copy equals: self collectionClass new.
- self assert: self collection copy equals: self collection.
- self assert: self collectionWithNewValue copy equals: self collectionWithNewValue.
-
- self deny: self collectionClass new copy = self collection.
- self deny: self collection copy = self collectionClass new.
- self deny: self collection copy = self collectionWithNewValue
- !
- testCopySeparates
- | original copy |
- original := self collection.
- copy := original copy.
- copy addAll: self sampleNewValueAsCollection.
- self assert: original = self collection
- !
- testDetect
- self
- shouldnt: [ self collection detect: [ true ] ]
- raise: Error.
- self
- should: [ self collection detect: [ false ] ]
- raise: Error.
- self assert: (self sampleNewValueAsCollection detect: [ true ]) equals: self sampleNewValue.
- self assert: (self collectionWithNewValue detect: [ :each | each = self sampleNewValue ]) equals: self sampleNewValue.
- self
- should: [ self collection detect: [ :each | each = self sampleNewValue ] ]
- raise: Error
- !
- testDetectIfNone
- | sentinel |
- sentinel := Object new.
- self assert: (self collection detect: [ true ] ifNone: [ sentinel ]) ~= sentinel.
- self assert: (self collection detect: [ false ] ifNone: [ sentinel ]) equals: sentinel.
- self assert: (self sampleNewValueAsCollection detect: [ true ] ifNone: [ sentinel ]) equals: self sampleNewValue.
- self assert: (self collectionWithNewValue detect: [ :each | each = self sampleNewValue ] ifNone: [ sentinel ]) equals: self sampleNewValue.
- self assert: (self collection detect: [ :each | each = self sampleNewValue ] ifNone: [ sentinel ]) equals: sentinel
- !
- testDo
- | newCollection |
- newCollection := OrderedCollection new.
- self collection do: [ :each |
- newCollection add: each ].
- self
- assertSameContents: self collection
- as: newCollection.
- newCollection := OrderedCollection new.
- self collectionWithDuplicates do: [ :each |
- newCollection add: each ].
- self
- assertSameContents: self collectionWithDuplicates
- as: newCollection
- !
- testEquality
- self assert: self collectionClass new equals: self collectionClass new.
- self assert: self collection equals: self collection.
- self assert: self collectionWithNewValue equals: self collectionWithNewValue.
-
- self deny: self collectionClass new = self collection.
- self deny: self collection = self collectionClass new.
- self deny: self collection = self collectionWithNewValue
- !
- testIfEmptyFamily
- self assert: (self collectionClass new ifEmpty: [ 42 ]) equals: 42.
- self assert: (self collection ifEmpty: [ 42 ]) equals: self collection.
- self assert: (self collectionClass new ifNotEmpty: [ 42 ]) equals: self collectionClass new.
- self assert: (self collection ifNotEmpty: [ 42 ]) equals: 42.
- self assert: (self collection ifNotEmpty: [ :col | col ]) equals: self collection.
-
- self assert: (self collectionClass new ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 42.
- self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 999.
- self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ :col | col ]) equals: self collection.
- self assert: (self collectionClass new ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 999.
- self assert: (self collection ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 42.
- self assert: (self collection ifNotEmpty: [ :col | col ] ifEmpty: [ 999 ]) equals: self collection.
- !
- testIsEmpty
- self assert: self collectionClass new isEmpty.
- self deny: self collection isEmpty
- !
- testNoneSatisfy
- | anyOne |
- anyOne := self collection anyOne.
- self deny: (self collection noneSatisfy: [ :each | each = anyOne ]).
- self assert: (self collection noneSatisfy: [ :each | each = Object new ])
- !
- testRemoveAll
- self assert: (self collection removeAll; yourself) equals: self collectionClass new
- !
- testSelect
- self assert: (self collection select: [ false ]) equals: self collectionClass new.
- self assert: (self collection select: [ true ]) equals: self collection.
- self assert: (self collectionWithNewValue select: [ :each | each = self sampleNewValue ]) equals: self sampleNewValueAsCollection.
- self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ]) equals: self collection.
- self assert: (self collection select: [ :each | each = self sampleNewValue ]) equals: self collectionClass new.
- self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ]) equals: self collection
- !
- testSingle
- self should: [ self collectionClass new single ] raise: Error.
- self should: [ self collection single ] raise: Error.
- self assert: self sampleNewValueAsCollection single equals: self sampleNewValue
- !
- testSize
- self assert: self collectionClass new size equals: 0.
- self assert: self sampleNewValueAsCollection size equals: 1.
- self assert: self collection size equals: self collectionSize
- ! !
- !CollectionTest class methodsFor: 'fixture'!
- collectionClass
- "Answers class of collection type tested,
- or nil if test is abstract"
- ^ nil
- ! !
- !CollectionTest class methodsFor: 'testing'!
- isAbstract
- ^ self collectionClass isNil
- ! !
- CollectionTest subclass: #IndexableCollectionTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !IndexableCollectionTest methodsFor: 'fixture'!
- collectionWithNewValue
- "Answers a collection which shows how
- self collection would look after adding
- self sampleNewValue at self sampleNewIndex"
-
- self subclassResponsibility
- !
- sampleNewIndex
- "Answers a value that can be used as index in at:put: or at:ifAbsentPut:"
-
- self subclassResponsibility
- !
- sampleNonIndexesDo: aBlock
- "Executes block a few times,
- each time passing value that is known
- not to be an index, as the first parameter"
-
- self subclassResponsibility
- !
- samplesDo: aBlock
- "Executes block a few times,
- each time passing known index and value stored
- under that index as the parameters"
-
- self subclassResponsibility
- ! !
- !IndexableCollectionTest methodsFor: 'tests'!
- testAt
- self nonIndexesDo: [ :each |
- self should: [ self collection at: each ] raise: Error ].
- self samplesDo: [ :index :value |
- self assert: (self collection at: index) equals: value ]
- !
- testAtIfAbsent
- self nonIndexesDo: [ :each |
- self assert: (self collection at: each ifAbsent: [ self sampleNewValue ]) equals: self sampleNewValue ].
- self samplesDo: [ :index :value |
- self assert: (self collection at: index ifAbsent: [ self sampleNewValue ]) equals: value ].
- !
- testAtIfAbsentPut
- | newCollection |
- newCollection := self collection.
- self samplesDo: [ :index :value |
- self assert: (newCollection at: index ifAbsentPut: [ self sampleNewValue ]) equals: value ].
- self assert: newCollection equals: self collection.
- self assert: (newCollection at: self sampleNewIndex ifAbsentPut: [ self sampleNewValue ]) equals: self sampleNewValue.
- self assert: newCollection equals: self collectionWithNewValue
- !
- testAtIfPresent
- | visited sentinel |
- sentinel := Object new.
- self nonIndexesDo: [ :each |
- visited := nil.
- self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ]) equals: nil.
- self assert: visited isNil ].
- self samplesDo: [ :index :value |
- visited := nil.
- self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ]) equals: sentinel.
- self assert: visited equals: (self collection at: index) ]
- !
- testAtIfPresentIfAbsent
- | visited sentinel |
- sentinel := Object new.
- self nonIndexesDo: [ :each |
- visited := nil.
- self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ] ifAbsent: [ self sampleNewValue ] ) equals: self sampleNewValue.
- self assert: visited isNil ].
- self samplesDo: [ :index :value |
- visited := nil.
- self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ] ifAbsent: [ self sampleNewValue ]) equals: sentinel.
- self assert: visited equals: (self collection at: index) ]
- !
- testAtPut
- | newCollection |
- newCollection := self collection.
- self samplesDo: [ :index :value |
- newCollection at: index put: value ].
- self assert: newCollection equals: self collection.
- newCollection at: self sampleNewIndex put: self sampleNewValue.
- self assert: newCollection equals: self collectionWithNewValue
- !
- testIndexOf
- self should: [ self collection indexOf: self sampleNewValue ] raise: Error.
- self samplesDo: [ :index :value |
- self assert: (self collection indexOf: value) equals: index ]
- !
- testIndexOfWithNull
- | jsNull |
- jsNull := JSON parse: 'null'.
- self samplesDo: [ :index :value |
- self assert: (self collection at: index put: jsNull; indexOf: jsNull) equals: index ]
- !
- testWithIndexDo
- | collection |
- collection := self collection.
-
- self collection withIndexDo: [ :each :index |
- self assert: (collection at: index) equals: each ]
- ! !
- IndexableCollectionTest subclass: #AssociativeCollectionTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !AssociativeCollectionTest methodsFor: 'fixture'!
- collectionKeys
- self subclassResponsibility
- !
- collectionValues
- self subclassResponsibility
- !
- nonIndexesDo: aBlock
- aBlock value: 5.
- aBlock value: [].
- aBlock value: Object new.
- aBlock value: 'z'
- !
- sampleNewIndex
- ^ 'new'
- !
- samplesDo: aBlock
- aBlock value: 'a' value: 2
- ! !
- !AssociativeCollectionTest methodsFor: 'tests'!
- testAddAll
- super testAddAll.
- self assert: (self collection addAll: self collection; yourself) equals: self collection.
- self assert: (self collection addAll: self collectionWithNewValue; yourself) equals: self collectionWithNewValue.
- self assert: (self collectionWithNewValue addAll: self collection; yourself) equals: self collectionWithNewValue
- !
- testAsDictionary
- self assert: ( self collectionClass new asDictionary isMemberOf: Dictionary ).
- !
- testAsHashedCollection
- self assert: ( self collectionClass new asHashedCollection isMemberOf: HashedCollection ).
- !
- testComma
- super testComma.
- self assert: self collection, self collection equals: self collection.
- self assert: self collection, self collectionWithNewValue equals: self collectionWithNewValue.
- self assert: self collectionWithNewValue, self collection equals: self collectionWithNewValue
- !
- testFrom
- "Accept a collection of associations."
- | associations |
- associations := { 'a' -> 1. 'b' -> 2 }.
- self assertSameContents: ( self class collectionClass from: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
- !
- testKeys
- self assert:self collectionClass new keys isEmpty.
- self assertSameContents:self collection keys as: self collectionKeys.
- self assertSameContents:self collectionWithNewValue keys as: self collectionKeys, { self sampleNewIndex }
- !
- testNewFromPairs
- "Accept an array in which all odd indexes are keys and evens are values."
- | flattenedAssociations |
- flattenedAssociations := { 'a'. 1. 'b'. 2 }.
- self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
- !
- testPrintString
- self
- assert: (self collectionClass new
- at:'firstname' put: 'James';
- at:'lastname' put: 'Bond';
- printString)
- equals: 'a ', self collectionClass name, ' (''firstname'' -> ''James'' , ''lastname'' -> ''Bond'')'
- !
- testRemoveKey
- self nonIndexesDo: [ :each |
- | collection |
- collection := self collection.
- self should: [ collection removeKey: each ] raise: Error.
- self assert: collection equals: self collection ].
- self samplesDo: [ :index :value |
- | collection |
- collection := self collection.
- self assert: (collection removeKey: index) equals: value.
- self deny: collection = self collection ].
- self
- assert: (self collectionWithNewValue removeKey: self sampleNewIndex; yourself)
- equals: self collection
- !
- testRemoveKeyIfAbsent
- self nonIndexesDo: [ :each |
- | collection |
- collection := self collection.
- self assert: (collection removeKey: each ifAbsent: [ self sampleNewValue ]) equals: self sampleNewValue.
- self assert: collection equals: self collection ].
- self samplesDo: [ :index :value |
- | collection |
- collection := self collection.
- self assert: (collection removeKey: index ifAbsent: [ self sampleNewValue ]) equals: value.
- self deny: collection = self collection ].
- self
- assert: (self collectionWithNewValue removeKey: self sampleNewIndex ifAbsent: [ self assert: false ]; yourself)
- equals: self collection
- !
- testValues
- self assert:self collectionClass new values isEmpty.
- self assertSameContents:self collection values as: self collectionValues.
- self assertSameContents:self collectionWithNewValue values as: self collectionValues, { self sampleNewValue }
- ! !
- AssociativeCollectionTest subclass: #DictionaryTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !DictionaryTest methodsFor: 'fixture'!
- collection
- ^ Dictionary new
- at: 1 put: 1;
- at: 'a' put: 2;
- at: true put: 3;
- at: 1@3 put: -4;
- at: sampleBlock put: 9;
- yourself
- !
- collectionKeys
- ^ {1. 'a'. true. 1@3. sampleBlock}
- !
- collectionOfPrintStrings
- ^ Dictionary new
- at: 1 put: '1';
- at: 'a' put: '2';
- at: true put: '3';
- at: 1@3 put: '-4';
- at: sampleBlock put: '9';
- yourself
- !
- collectionSize
- ^ 5
- !
- collectionValues
- ^ {1. 2. 3. -4. 9}
- !
- collectionWithDuplicates
- ^ Dictionary new
- at: 1 put: 1;
- at: 'a' put: 2;
- at: true put: 3;
- at: 4 put: -4;
- at: sampleBlock put: 9;
- at: 'b' put: 1;
- at: 3 put: 3;
- at: false put: 12;
- yourself
- !
- collectionWithNewValue
- ^ Dictionary new
- at: 1 put: 1;
- at: 'a' put: 2;
- at: true put: 3;
- at: 1@3 put: -4;
- at: sampleBlock put: 9;
- at: 'new' put: 'N';
- yourself
- !
- sampleNewValueAsCollection
- ^ Dictionary new
- at: 'new' put: 'N';
- yourself
- !
- samplesDo: aBlock
- super samplesDo: aBlock.
- aBlock value: true value: 3.
- aBlock value: 1@3 value: -4.
- aBlock value: sampleBlock value: 9
- ! !
- !DictionaryTest methodsFor: 'tests'!
- testAccessing
- | d |
- d := Dictionary new.
- d at: 'hello' put: 'world'.
- self assert: (d at: 'hello') equals: 'world'.
- self assert: (d at: 'hello' ifAbsent: [ nil ]) equals: 'world'.
- self deny: (d at: 'foo' ifAbsent: [ nil ]) = 'world'.
- self assert: (d includesKey: 'hello').
- self deny: (d includesKey: 'foo').
- d at: 1 put: 2.
- self assert: (d at: 1) equals: 2.
- d at: 1@3 put: 3.
- self assert: (d at: 1@3) equals: 3.
- self assert: (d includesKey: 1@3).
- self deny: (d includesKey: 3@1)
- !
- testDynamicDictionaries
- self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
- ! !
- !DictionaryTest class methodsFor: 'fixture'!
- collectionClass
- ^ Dictionary
- ! !
- AssociativeCollectionTest subclass: #HashedCollectionTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !HashedCollectionTest methodsFor: 'fixture'!
- collection
- ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4 }
- !
- collectionKeys
- ^ { 'b'. 'a'. 'c'. 'd' }
- !
- collectionOfPrintStrings
- ^ #{ 'b' -> '1'. 'a' -> '2'. 'c' -> '3'. 'd' -> '-4' }
- !
- collectionSize
- ^ 4
- !
- collectionValues
- ^ { 1. 2. 3. -4 }
- !
- collectionWithDuplicates
- ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10. 'h' -> 0 }
- !
- collectionWithNewValue
- ^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'new' -> 'N' }
- !
- sampleNewValueAsCollection
- ^ #{ 'new' -> 'N' }
- ! !
- !HashedCollectionTest methodsFor: 'tests'!
- testDynamicDictionaries
- self assert: #{'hello' -> 1} asHashedCollection equals: (HashedCollection with: 'hello' -> 1)
- ! !
- !HashedCollectionTest class methodsFor: 'fixture'!
- collectionClass
- ^ HashedCollection
- ! !
- IndexableCollectionTest subclass: #SequenceableCollectionTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !SequenceableCollectionTest methodsFor: 'fixture'!
- collectionFirst
- self subclassResponsibility
- !
- collectionFirstTwo
- self subclassResponsibility
- !
- collectionLast
- self subclassResponsibility
- !
- collectionLastTwo
- self subclassResponsibility
- !
- nonIndexesDo: aBlock
- aBlock value: 0.
- aBlock value: self collectionSize + 1.
- aBlock value: 'z'
- !
- samplesDo: aBlock
- aBlock value: 1 value: self collectionFirst.
- aBlock value: self collectionSize value: self collectionLast
- ! !
- !SequenceableCollectionTest methodsFor: 'tests'!
- testBeginsWith
- self assert: (self collection beginsWith: self collectionClass new).
- self assert: (self collection beginsWith: self collection).
- self assert: (self collection beginsWith: self collectionFirstTwo).
- self deny: (self collection beginsWith: self collectionLastTwo)
- !
- testEndsWith
- self assert: (self collection endsWith: self collectionClass new).
- self assert: (self collection endsWith: self collection).
- self assert: (self collection endsWith: self collectionLastTwo).
- self deny: (self collection endsWith: self collectionFirstTwo)
- !
- testFirst
- self assert: self collection first equals: self collectionFirst
- !
- testFirstN
- self
- assert: (self collection first: 2)
- equals: self collectionFirstTwo.
-
- self
- assert: (self collection first: 0)
- equals: self collectionClass new.
-
- self
- assert: (self collection first: self collectionSize)
- equals: self collection.
-
- self should: [ self collection first: 33 ] raise: Error
- !
- testFourth
- self assert: (self collection fourth) equals: (self collection at: 4)
- !
- testIndexOfStartingAt
- | jsNull |
- jsNull := JSON parse: 'null'.
- self samplesDo: [ :index :value |
- self assert: (self collection indexOf: value startingAt: 1) equals: index.
- self assert: (self collection indexOf: value startingAt: index) equals: index.
- self assert: (self collection indexOf: value startingAt: index+1) equals: 0 ]
- !
- testIndexOfStartingAtWithNull
- | jsNull |
- jsNull := JSON parse: 'null'.
- self samplesDo: [ :index :value | | collection |
- collection := self collection.
- collection at: index put: jsNull.
- self assert: (collection indexOf: jsNull startingAt: 1) equals: index.
- self assert: (collection indexOf: jsNull startingAt: index) equals: index.
- self assert: (collection indexOf: jsNull startingAt: index+1) equals: 0 ]
- !
- testLast
- self assert: self collection last equals: self collectionLast
- !
- testLastN
- self
- assert: (self collection last: 2)
- equals: self collectionLastTwo.
-
- self
- assert: (self collection last: 0)
- equals: self collectionClass new.
- self
- assert: (self collection last: self collectionSize)
- equals: self collection.
- self should: [ self collection last: 33 ] raise: Error
- !
- testSecond
- self assert: (self collection second) equals: (self collection at: 2)
- !
- testThird
- self assert: (self collection third) equals: (self collection at: 3)
- ! !
- SequenceableCollectionTest subclass: #ArrayTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !ArrayTest methodsFor: 'fixture'!
- collection
- ^ #(1 2 3 -4)
- !
- collectionFirst
- ^ 1
- !
- collectionFirstTwo
- ^ #(1 2)
- !
- collectionLast
- ^ -4
- !
- collectionLastTwo
- ^ #(3 -4)
- !
- collectionOfPrintStrings
- ^ #('1' '2' '3' '-4')
- !
- collectionSize
- ^ 4
- !
- collectionWithDuplicates
- ^ #('a' 'b' 'c' 1 2 1 'a' ())
- !
- collectionWithNewValue
- ^ #(1 2 3 -4 'N')
- !
- sampleNewIndex
- ^ 5
- !
- samplesDo: aBlock
- super samplesDo: aBlock.
- aBlock value: 3 value: 3.
- ! !
- !ArrayTest methodsFor: 'tests'!
- testAdd
- | array |
- array := self collection.
- array add: 6.
-
- self assert: array last equals: 6
- !
- testAddFirst
- self assert: (self collection addFirst: 0; yourself) first equals: 0
- !
- testPrintString
- | array |
- array := Array new.
- self assert: array printString equals: 'an Array ()'.
- array add: 1; add: 3.
- self assert: array printString equals: 'an Array (1 3)'.
- array add: 'foo'.
- self assert: array printString equals: 'an Array (1 3 ''foo'')'.
- array remove: 1; remove: 3.
- self assert: array printString equals: 'an Array (''foo'')'.
- array addLast: 3.
- self assert: array printString equals: 'an Array (''foo'' 3)'.
- array addLast: 3.
- self assert: array printString equals: 'an Array (''foo'' 3 3)'.
- !
- testRemove
- | array |
- array := #(1 2 3 4 5).
- array remove: 3.
- self assert: array equals: #(1 2 4 5).
- self should: [ array remove: 3 ] raise: Error
- !
- testRemoveFromTo
-
- self assert: (#(1 2 3 4) removeFrom: 1 to: 3) equals: #(4).
- self assert: (#(1 2 3 4) removeFrom: 2 to: 3) equals: #(1 4).
- self assert: (#(1 2 3 4) removeFrom: 2 to: 4) equals: #(1)
- !
- testRemoveIndex
-
- self assert: (#(1 2 3 4) removeIndex: 2) equals: #(1 3 4).
- self assert: (#(1 2 3 4) removeIndex: 1) equals: #(2 3 4).
- self assert: (#('hello') removeIndex: 1) equals: #()
- !
- testRemoveLast
- | array |
- array := #(1 2).
- array removeLast.
-
- self assert: array last equals: 1
- !
- testReversed
- |array|
- array := #(5 4 3 2 1).
- self assert: (array reversed) equals: #(1 2 3 4 5)
- !
- testSort
- | array |
- array := #(10 1 5).
- array sort.
- self assert: array equals: #(1 5 10)
- ! !
- !ArrayTest class methodsFor: 'fixture'!
- collectionClass
- ^ Array
- ! !
- SequenceableCollectionTest subclass: #StringTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !StringTest methodsFor: 'fixture'!
- collection
- ^ 'helLo'
- !
- collectionFirst
- ^ 'h'
- !
- collectionFirstTwo
- ^ 'he'
- !
- collectionLast
- ^ 'o'
- !
- collectionLastTwo
- ^ 'Lo'
- !
- collectionOfPrintStrings
- ^ '''h''''e''''l''''L''''o'''
- !
- collectionSize
- ^ 5
- !
- collectionWithDuplicates
- ^ 'abbaerten'
- !
- collectionWithNewValue
- ^ 'helLoN'
- !
- sampleNewValueAsCollection
- ^ 'N'
- !
- samplesDo: aBlock
- super samplesDo: aBlock.
- aBlock value: 3 value: 'l'
- ! !
- !StringTest methodsFor: 'tests'!
- testAddAll
- "String instances are read-only"
- self should: [ self collection addAll: self collection ] raise: Error
- !
- testAddRemove
- self should: [ 'hello' add: 'a' ] raise: Error.
- self should: [ 'hello' remove: 'h' ] raise: Error
- !
- testAsArray
- self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
- !
- testAsLowerCase
- self assert: 'JACKIE' asLowercase equals: 'jackie'.
- !
- testAsNumber
- self assert: '3' asNumber equals: 3.
- self assert: '-3' asNumber equals: -3.
- self assert: '-1.5' asNumber equals: -1.5.
- !
- testAsUpperCase
- self assert: 'jackie' asUppercase equals: 'JACKIE'.
- !
- testAsciiValue
- | characterA characterU |
- characterA := 'A'.
- characterU := 'U'.
- self assert: (characterA asciiValue) equals:65.
- self assert: (characterU asciiValue) equals:85
- !
- testAtIfAbsentPut
- "String instances are read-only"
- self should: [ 'hello' at: 6 ifAbsentPut: [ 'a' ] ] raise: Error
- !
- testAtPut
- "String instances are read-only"
- self should: [ 'hello' at: 1 put: 'a' ] raise: Error
- !
- testCapitalized
- self assert: 'test' capitalized equals: 'Test'.
- self assert: 'Test' capitalized equals: 'Test'.
- self assert: '' capitalized equals: ''.
- self assert: 'Test' isCapitalized equals: true.
- self assert: 'test' isCapitalized equals: false.
- !
- testCharCodeAt
- self assert: ('jackie' charCodeAt:1) equals: 106.
- self assert: ('jackie' charCodeAt:2) equals: 97.
- self assert: ('jackie' charCodeAt:3) equals: 99.
- self assert: ('jackie' charCodeAt:4) equals: 107.
- self assert: ('jackie' charCodeAt:5) equals: 105.
- self assert: ('jackie' charCodeAt:6) equals: 101
- !
- testCopyFromTo
- self assert: ('jackie' copyFrom: 1 to: 3) equals: 'jac'.
- self assert: ('jackie' copyFrom: 4 to: 6) equals: 'kie'.
- !
- testCopySeparates
- "String instances are immutable"
- self assert: self collection copy == self collection
- !
- testCopyWithoutAll
- self
- assert: ('*hello* *world*' copyWithoutAll: '*')
- equals: 'hello world'
- !
- testEquality
- self assert: 'hello' equals: 'hello'.
- self deny: 'hello' = 'world'.
-
- "Test for issue 459"
- self deny: 'hello' = (#() at: 1 ifAbsent: [ ]).
- self assert: 'hello' equals: 'hello' yourself.
- self assert: 'hello' yourself equals: 'hello'.
- "test JS falsy value"
- self deny: '' = 0
- !
- testIdentity
- self assert: 'hello' == 'hello'.
- self deny: 'hello' == 'world'.
- self assert: 'hello' == 'hello' yourself.
- self assert: 'hello' yourself == 'hello'.
- "test JS falsy value"
- self deny: '' == 0
- !
- testIncludesSubString
- self assert: ('amber' includesSubString: 'ber').
- self deny: ('amber' includesSubString: 'zork').
- !
- testIndexOfStartingAtWithNull
- "String cannot hold JS null"
- !
- testIndexOfWithNull
- "String cannot hold JS null"
- !
- testIsVowel
- |vowel consonant|
- vowel := 'u'.
- consonant := 'z'.
- self assert: vowel isVowel equals: true.
- self assert: consonant isVowel equals: false
- !
- testJoin
- self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
- !
- testRemoveAll
- self should: [ self collection removeAll ] raise: Error
- !
- testReversed
- self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
- !
- testStreamContents
- self
- assert: (String streamContents: [ :aStream |
- aStream
- nextPutAll: 'hello'; space;
- nextPutAll: 'world' ])
- equals: 'hello world'
- !
- testSubStrings
- self assert: ('jackiechan' subStrings: 'ie') equals: #( 'jack' 'chan' ).
- !
- testTrim
- self assert: ' jackie' trimLeft equals: 'jackie'.
- self assert: 'jackie ' trimRight equals: 'jackie'.
- !
- testValue
- self assert: (#asString value: 1) equals: '1'.
- "Which (since String and BlockClosure are now polymorphic) enables the nice idiom..."
- self assert: (#(1 2 3) collect: #asString) equals: #('1' '2' '3')
- ! !
- !StringTest class methodsFor: 'fixture'!
- collectionClass
- ^ String
- ! !
- CollectionTest subclass: #SetTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !SetTest methodsFor: 'fixture'!
- collection
- ^ Set new
- add: Smalltalk;
- add: nil;
- add: 3@3;
- add: false;
- add: sampleBlock;
- yourself
- !
- collectionOfPrintStrings
- ^ Set new
- add: 'a SmalltalkImage';
- add: 'nil';
- add: '3@3';
- add: 'false';
- add: 'a BlockClosure';
- yourself
- !
- collectionSize
- ^ 5
- !
- collectionWithDuplicates
- "Set has no duplicates"
- ^ self collection add: 0; yourself
- !
- collectionWithNewValue
- ^ Set new
- add: Smalltalk;
- add: nil;
- add: 3@3;
- add: 'N';
- add: false;
- add: sampleBlock;
- yourself
- ! !
- !SetTest methodsFor: 'tests'!
- testAddAll
- super testAddAll.
- self assert: (self collection addAll: self collection; yourself) equals: self collection.
- self assert: (self collection addAll: self collectionWithNewValue; yourself) equals: self collectionWithNewValue.
- self assert: (self collectionWithNewValue addAll: self collection; yourself) equals: self collectionWithNewValue
- !
- testAddRemove
- | set |
- set := Set new.
-
- self assert: set isEmpty.
- set add: 3.
- self assert: (set includes: 3).
- set add: 5.
- self assert: (set includes: 5).
- set remove: 3.
- self deny: (set includes: 3)
- !
- testAt
- self should: [ Set new at: 1 put: 2 ] raise: Error
- !
- testCollect
- super testCollect.
- self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
- !
- testComma
- super testComma.
- self assert: self collection, self collection equals: self collection.
- self assert: self collection, self collectionWithNewValue equals: self collectionWithNewValue.
- self assert: self collectionWithNewValue, self collection equals: self collectionWithNewValue
- !
- testComparing
- self assert: #(0 2) asSet equals: #(0 2) asSet.
- self assert: #(2 0) asSet equals: #(0 2) asSet.
- self deny: #(0 2 3) asSet = #(0 2) asSet.
- self deny: #(1 2) asSet = #(0 2) asSet
- !
- testPrintString
- | set |
- set := Set new.
- self assert: set printString equals: 'a Set ()'.
- set add: 1; add: 3.
- self assert: set printString equals: 'a Set (1 3)'.
- set add: 'foo'.
- self assert: set printString equals: 'a Set (1 3 ''foo'')'.
- set remove: 1; remove: 3.
- self assert: set printString equals: 'a Set (''foo'')'.
- set add: 3.
- self assert: set printString equals: 'a Set (3 ''foo'')'.
- set add: 3.
- self assert: set printString equals: 'a Set (3 ''foo'')'
- !
- testUnboxedObjects
- self assert: {'foo' yourself. 'foo' yourself} asSet asArray equals: #('foo')
- !
- testUnicity
- | set |
- set := Set new.
- set add: 21.
- set add: 'hello'.
- set add: 21.
- self assert: set size equals: 2.
-
- set add: 'hello'.
- self assert: set size equals: 2.
- self assert: set asArray equals: #(21 'hello')
- ! !
- !SetTest class methodsFor: 'fixture'!
- collectionClass
- ^ Set
- ! !
- TestCase subclass: #ConsoleTranscriptTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !ConsoleTranscriptTest methodsFor: 'tests'!
- testShow
- | originalTranscript |
- originalTranscript := Transcript current.
- Transcript register: ConsoleTranscript new.
- self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
- self shouldnt: [ Transcript show: console ] raise: Error.
- Transcript register: originalTranscript.
- ! !
- TestCase subclass: #DateTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !DateTest methodsFor: 'tests'!
- testEquality
- | now |
- now := Date new.
- self assert: now = now.
- self deny: now = (Date fromMilliseconds: 0).
- self assert: (Date fromMilliseconds: 12345678) = (Date fromMilliseconds: 12345678).
- self assert: now = (Date fromMilliseconds: now asMilliseconds).
- self assert: (Date fromMilliseconds: now asMilliseconds) = now
- !
- testIdentity
- | now |
- now := Date new.
- self assert: now == now.
- self deny: now == (Date fromMilliseconds: 0).
- self deny: (Date fromMilliseconds: 12345678) == (Date fromMilliseconds: 12345678).
- self deny: now == (Date fromMilliseconds: now asMilliseconds).
- self deny: (Date fromMilliseconds: now asMilliseconds) == now
- ! !
- TestCase subclass: #JSObjectProxyTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !JSObjectProxyTest methodsFor: 'accessing'!
- jsNull
- <inlineJS: 'return null'>
- !
- jsObject
- <inlineJS:
- 'return {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: "", "e": null, "f": void 0}'>
- !
- jsUndefined
- <inlineJS: 'return'>
- ! !
- !JSObjectProxyTest methodsFor: 'tests'!
- testAtIfAbsent
- | testObject |
- testObject := self jsObject.
- self assert: (testObject at: 'abc' ifAbsent: [ 'Property does not exist' ]) equals: 'Property does not exist'.
- self assert: (testObject at: 'e' ifAbsent: [ 'Property does not exist' ]) equals: nil.
- self assert: (testObject at: 'a' ifAbsent: [ 'Property does not exist' ]) equals: 1.
- self assert: (testObject at: 'f' ifAbsent: [ 'Property does not exist' ]) equals: nil.
- !
- testAtIfPresent
- | testObject |
-
- testObject := self jsObject.
-
- self assert: (testObject at: 'abc' ifPresent: [ :x | 'hello ',x asString ]) equals: nil.
- self assert: (testObject at: 'e' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
- self assert: (testObject at: 'a' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello 1'.
- self assert: (testObject at: 'f' ifPresent: [ :x | 'hello ',x asString ]) equals: 'hello nil'.
- !
- testAtIfPresentIfAbsent
- | testObject |
- testObject := self jsObject.
- self assert: (testObject at: 'abc' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'not present'.
- self assert: (testObject at: 'e' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
- self assert: (testObject at: 'a' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello 1'.
- self assert: (testObject at: 'f' ifPresent: [ :x|'hello ',x asString ] ifAbsent: [ 'not present' ]) equals: 'hello nil'.
- !
- testAtPut
- | testObject |
- testObject := self jsObject.
-
- self assert: (testObject at: 'abc') ~= 'xyz'.
- self assert: (testObject at: 'abc' put: 'xyz') equals: 'xyz'.
- self assert: (testObject at: 'abc') equals: 'xyz'
- !
- testComparison
- self assert: ({ console. 2 } indexOf: console) equals: 1.
- self assert: console = console.
- self deny: console = Object new.
- self deny: console = self jsObject
- !
- testDNU
- self should: [ self jsObject foo ] raise: MessageNotUnderstood
- !
- testDNURegression1057
- | jsObject |
- jsObject := #().
- jsObject basicAt: 'allowJavaScriptCalls' put: true.
- jsObject basicAt: 'foo' put: 3.
- self shouldnt: [ jsObject foo ] raise: Error.
- self assert: jsObject foo equals: 3.
- self shouldnt: [ jsObject foo: 4 ] raise: Error.
- self assert: jsObject foo equals: 4
- !
- testDNURegression1059
- | jsObject |
- jsObject := #().
- jsObject basicAt: 'allowJavaScriptCalls' put: true.
- jsObject basicAt: 'x' put: 3.
- jsObject basicAt: 'x:' put: [ self error ].
- self shouldnt: [ jsObject x: 4 ] raise: Error.
- self assert: jsObject x equals: 4
- !
- testDNURegression1062
- | jsObject stored |
- jsObject := #().
- jsObject basicAt: 'allowJavaScriptCalls' put: true.
- jsObject basicAt: 'x' put: [ :v | stored := v ].
- self shouldnt: [ jsObject x: 4 ] raise: Error.
- self assert: stored equals: 4
- !
- testDNUWithAllowJavaScriptCalls
- | jsObject |
- jsObject := #().
- jsObject basicAt: 'allowJavaScriptCalls' put: true.
- self should: [ jsObject foo ] raise: MessageNotUnderstood
- !
- testMessageSend
- self assert: self jsObject a equals: 1.
- self assert: self jsObject b equals: 2.
- self assert: (self jsObject c: 3) equals: 3
- !
- testMethodWithArguments
- self assert: (self jsObject c: 1) equals: 1
- !
- testPrinting
- self assert: self jsObject printString equals: '[object Object]'
- !
- testPropertyThatReturnsEmptyString
- | object |
- object := self jsObject.
- self assert: object d equals: ''.
- object d: 'hello'.
- self assert: object d equals: 'hello'
- !
- testPropertyThatReturnsUndefined
- | object |
- object := self jsObject.
- self shouldnt: [ object e ] raise: MessageNotUnderstood.
- self assert: object e isNil
- !
- testSetPropertyWithFalsyValue
- | jsObject |
- jsObject := self jsObject.
- self assert: (jsObject a) equals: 1.
- jsObject a: self jsNull.
- self assert: (jsObject a) equals: nil.
- jsObject a: 0.
- self assert: (jsObject a) equals: 0.
- jsObject a: self jsUndefined.
- self assert: (jsObject a) equals: nil.
- jsObject a: ''.
- self assert: (jsObject a) equals: ''.
- jsObject a: false.
- self assert: (jsObject a) equals: false
- !
- testValue
- | testObject |
- testObject := self jsObject.
- testObject at: 'value' put: 'aValue'.
- self assert: testObject value equals: 'aValue'
- !
- testYourself
- | object |
- object := self jsObject
- d: 'test';
- yourself.
- self assert: object d equals: 'test'
- ! !
- TestCase subclass: #JavaScriptExceptionTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !JavaScriptExceptionTest methodsFor: 'helpers'!
- throwException
- <inlineJS: 'throw "test"'>
- ! !
- !JavaScriptExceptionTest methodsFor: 'tests'!
- testCatchingException
- [ self throwException ]
- on: Error
- do: [ :error |
- self assert: error exception = 'test' ]
- !
- testRaisingException
- self should: [ self throwException ] raise: JavaScriptException
- ! !
- TestCase subclass: #MessageSendTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !MessageSendTest methodsFor: 'tests'!
- testValue
- | messageSend |
-
- messageSend := MessageSend new
- receiver: Object new;
- selector: #asString;
- yourself.
-
- self assert: messageSend value equals: 'an Object'
- !
- testValueWithArguments
- | messageSend |
-
- messageSend := MessageSend new
- receiver: 2;
- selector: '+';
- yourself.
-
- self assert: (messageSend value: 3) equals: 5.
-
- self assert: (messageSend valueWithPossibleArguments: #(4)) equals: 6
- ! !
- TestCase subclass: #MethodInheritanceTest
- instanceVariableNames: 'receiverTop receiverMiddle receiverBottom method performBlock'
- package: 'Kernel-Tests'!
- !MethodInheritanceTest methodsFor: 'accessing'!
- codeGeneratorClass
- ^ CodeGenerator
- !
- targetClassBottom
- ^ JavaScriptException
- !
- targetClassMiddle
- ^ Error
- !
- targetClassTop
- ^ Object
- ! !
- !MethodInheritanceTest methodsFor: 'factory'!
- compiler
- ^ Compiler new
- codeGeneratorClass: self codeGeneratorClass;
- yourself
- ! !
- !MethodInheritanceTest methodsFor: 'initialization'!
- setUp
- receiverTop := self targetClassTop new.
- receiverMiddle := self targetClassMiddle new.
- receiverBottom := self targetClassBottom new.
- method := nil.
- performBlock := [ self error: 'performBlock not initialized' ]
- !
- tearDown
- [ self deinstallTop ] on: Error do: [ ].
- [ self deinstallMiddle ] on: Error do: [ ].
- [ self deinstallBottom ] on: Error do: [ ]
- ! !
- !MethodInheritanceTest methodsFor: 'testing'!
- deinstallBottom
- self targetClassBottom removeCompiledMethod: method
- !
- deinstallMiddle
- self targetClassMiddle removeCompiledMethod: method
- !
- deinstallTop
- self targetClassTop removeCompiledMethod: method
- !
- installBottom: aString
- method := self compiler install: aString forClass: self targetClassBottom protocol: 'tests'
- !
- installMiddle: aString
- method := self compiler install: aString forClass: self targetClassMiddle protocol: 'tests'
- !
- installTop: aString
- method := self compiler install: aString forClass: self targetClassTop protocol: 'tests'
- !
- shouldMNU
- self shouldMNUTop.
- self shouldMNUMiddle.
- self shouldMNUBottom
- !
- shouldMNUBottom
- self should: [ performBlock value: receiverBottom ] raise: MessageNotUnderstood
- !
- shouldMNUMiddle
- self should: [ performBlock value: receiverMiddle ] raise: MessageNotUnderstood
- !
- shouldMNUTop
- self should: [ performBlock value: receiverTop ] raise: MessageNotUnderstood
- !
- shouldReturn: anObject
- | result |
- result := performBlock value: receiverTop.
- self assert: { 'top'. anObject } equals: { 'top'. result }.
- result := performBlock value: receiverMiddle.
- self assert: { 'middle'. anObject } equals: { 'middle'. result }.
- result := performBlock value: receiverBottom.
- self assert: { 'bottom'. anObject } equals: { 'bottom'. result }
- !
- shouldReturn: anObject and: anObject2 and: anObject3
- | result |
- result := performBlock value: receiverTop.
- self assert: { 'top'. anObject } equals: { 'top'. result }.
- result := performBlock value: receiverMiddle.
- self assert: { 'middle'. anObject2 } equals: { 'middle'. result }.
- result := performBlock value: receiverBottom.
- self assert: { 'bottom'. anObject3 } equals: { 'bottom'. result }
- ! !
- !MethodInheritanceTest methodsFor: 'tests'!
- testMNU11
- performBlock := [ :x | x foo ].
- self shouldMNU.
- self installTop: 'foo ^ false'.
- self installTop: 'foo ^ true'.
- self deinstallTop.
- self shouldMNU
- !
- testMNU22
- performBlock := [ :x | x foo ].
- self shouldMNU.
- self installMiddle: 'foo ^ false'.
- self installMiddle: 'foo ^ true'.
- self deinstallMiddle.
- self shouldMNU
- !
- testReturns1
- performBlock := [ :x | x foo ].
- self installTop: 'foo ^ false'.
- self shouldReturn: false.
- self installTop: 'foo ^ true'.
- self shouldReturn: true
- ! !
- TestCase subclass: #NumberTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !NumberTest methodsFor: 'tests'!
- testAbs
- self assert: 4 abs equals: 4.
- self assert: -4 abs equals: 4
- !
- testArithmetic
-
- "We rely on JS here, so we won't test complex behavior, just check if
- message sends are corrects"
- self assert: 1.5 + 1 equals: 2.5.
- self assert: 2 - 1 equals: 1.
- self assert: -2 - 1 equals: -3.
- self assert: 12 / 2 equals: 6.
- self assert: 3 * 4 equals: 12.
- self assert: 7 // 2 equals: 3.
- self assert: 7 \\ 2 equals: 1.
- "Simple parenthesis and execution order"
- self assert: 1 + 2 * 3 equals: 9.
- self assert: 1 + (2 * 3) equals: 7
- !
- testAsNumber
- self assert: 3 asNumber equals: 3.
- !
- testCeiling
- self assert: 1.2 ceiling equals: 2.
- self assert: -1.2 ceiling equals: -1.
- self assert: 1.0 ceiling equals: 1.
- !
- testComparison
- self assert: 3 > 2.
- self assert: 2 < 3.
-
- self deny: 3 < 2.
- self deny: 2 > 3.
- self assert: 3 >= 3.
- self assert: 3.1 >= 3.
- self assert: 3 <= 3.
- self assert: 3 <= 3.1
- !
- testCopying
- self assert: 1 copy == 1.
- self assert: 1 deepCopy == 1
- !
- testEquality
- self assert: (1 = 1).
- self assert: (0 = 0).
- self deny: (1 = 0).
- self assert: (1 yourself = 1).
- self assert: (1 = 1 yourself).
- self assert: (1 yourself = 1 yourself).
-
- self deny: 0 = false.
- self deny: false = 0.
- self deny: '' = 0.
- self deny: 0 = ''
- !
- testFloor
- self assert: 1.2 floor equals: 1.
- self assert: -1.2 floor equals: -2.
- self assert: 1.0 floor equals: 1.
- !
- testHexNumbers
- self assert: 16r9 equals: 9.
- self assert: 16rA truncated equals: 10.
- self assert: 16rB truncated equals: 11.
- self assert: 16rC truncated equals: 12.
- self assert: 16rD truncated equals: 13.
- self assert: 16rE truncated equals: 14.
- self assert: 16rF truncated equals: 15
- !
- testIdentity
- self assert: 1 == 1.
- self assert: 0 == 0.
- self deny: 1 == 0.
- self assert: 1 yourself == 1.
- self assert: 1 == 1 yourself.
- self assert: 1 yourself == 1 yourself.
-
- self deny: 1 == 2
- !
- testInvalidHexNumbers
- self should: [ 16rG ] raise: MessageNotUnderstood.
- self should: [ 16rg ] raise: MessageNotUnderstood.
- self should: [ 16rH ] raise: MessageNotUnderstood.
- self should: [ 16rh ] raise: MessageNotUnderstood.
- self should: [ 16rI ] raise: MessageNotUnderstood.
- self should: [ 16ri ] raise: MessageNotUnderstood.
- self should: [ 16rJ ] raise: MessageNotUnderstood.
- self should: [ 16rj ] raise: MessageNotUnderstood.
- self should: [ 16rK ] raise: MessageNotUnderstood.
- self should: [ 16rk ] raise: MessageNotUnderstood.
- self should: [ 16rL ] raise: MessageNotUnderstood.
- self should: [ 16rl ] raise: MessageNotUnderstood.
- self should: [ 16rM ] raise: MessageNotUnderstood.
- self should: [ 16rm ] raise: MessageNotUnderstood.
- self should: [ 16rN ] raise: MessageNotUnderstood.
- self should: [ 16rn ] raise: MessageNotUnderstood.
- self should: [ 16rO ] raise: MessageNotUnderstood.
- self should: [ 16ro ] raise: MessageNotUnderstood.
- self should: [ 16rP ] raise: MessageNotUnderstood.
- self should: [ 16rp ] raise: MessageNotUnderstood.
- self should: [ 16rQ ] raise: MessageNotUnderstood.
- self should: [ 16rq ] raise: MessageNotUnderstood.
- self should: [ 16rR ] raise: MessageNotUnderstood.
- self should: [ 16rr ] raise: MessageNotUnderstood.
- self should: [ 16rS ] raise: MessageNotUnderstood.
- self should: [ 16rs ] raise: MessageNotUnderstood.
- self should: [ 16rT ] raise: MessageNotUnderstood.
- self should: [ 16rt ] raise: MessageNotUnderstood.
- self should: [ 16rU ] raise: MessageNotUnderstood.
- self should: [ 16ru ] raise: MessageNotUnderstood.
- self should: [ 16rV ] raise: MessageNotUnderstood.
- self should: [ 16rv ] raise: MessageNotUnderstood.
- self should: [ 16rW ] raise: MessageNotUnderstood.
- self should: [ 16rw ] raise: MessageNotUnderstood.
- self should: [ 16rX ] raise: MessageNotUnderstood.
- self should: [ 16rx ] raise: MessageNotUnderstood.
- self should: [ 16rY ] raise: MessageNotUnderstood.
- self should: [ 16ry ] raise: MessageNotUnderstood.
- self should: [ 16rZ ] raise: MessageNotUnderstood.
- self should: [ 16rz ] raise: MessageNotUnderstood.
- self should: [ 16rABcdEfZ ] raise: MessageNotUnderstood.
- !
- testLog
- self assert: 10000 log equals: 4.
- self assert: (512 log: 2) equals: 9.
- self assert: Number e ln equals: 1.
- !
- testMinMax
-
- self assert: (2 max: 5) equals: 5.
- self assert: (2 min: 5) equals: 2
- !
- testNegated
- self assert: 3 negated equals: -3.
- self assert: -3 negated equals: 3
- !
- testPrintShowingDecimalPlaces
- self assert: (23 printShowingDecimalPlaces: 2) equals: '23.00'.
- self assert: (23.5698 printShowingDecimalPlaces: 2) equals: '23.57'.
- self assert: (234.567 negated printShowingDecimalPlaces: 5) equals: '-234.56700'.
- self assert: (23.4567 printShowingDecimalPlaces: 0) equals: '23'.
- self assert: (23.5567 printShowingDecimalPlaces: 0) equals: '24'.
- self assert: (23.4567 negated printShowingDecimalPlaces: 0) equals: '-23'.
- self assert: (23.5567 negated printShowingDecimalPlaces: 0) equals: '-24'.
- self assert: (100000000 printShowingDecimalPlaces: 1) equals: '100000000.0'.
- self assert: (0.98 printShowingDecimalPlaces: 5) equals: '0.98000'.
- self assert: (0.98 negated printShowingDecimalPlaces: 2) equals: '-0.98'.
- self assert: (2.567 printShowingDecimalPlaces: 2) equals: '2.57'.
- self assert: (-2.567 printShowingDecimalPlaces: 2) equals: '-2.57'.
- self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
- !
- testRaisedTo
- self assert: (2 raisedTo: 4) equals: 16.
- self assert: (2 raisedTo: 0) equals: 1.
- self assert: (2 raisedTo: -3) equals: 0.125.
- self assert: (4 raisedTo: 0.5) equals: 2.
-
- self assert: 2 ** 4 equals: 16.
- !
- testRounded
-
- self assert: 3 rounded equals: 3.
- self assert: 3.212 rounded equals: 3.
- self assert: 3.51 rounded equals: 4
- !
- testSign
- self assert: 5 sign equals: 1.
- self assert: 0 sign equals: 0.
- self assert: -1.4 sign equals: -1.
- !
- testSqrt
-
- self assert: 4 sqrt equals: 2.
- self assert: 16 sqrt equals: 4
- !
- testSquared
-
- self assert: 4 squared equals: 16
- !
- testTimesRepeat
- | i |
- i := 0.
- 0 timesRepeat: [ i := i + 1 ].
- self assert: i equals: 0.
- 5 timesRepeat: [ i := i + 1 ].
- self assert: i equals: 5
- !
- testTo
- self assert: (1 to: 5) equals: #(1 2 3 4 5)
- !
- testToBy
- self assert: (0 to: 6 by: 2) equals: #(0 2 4 6).
- self should: [ 1 to: 4 by: 0 ] raise: Error
- !
- testTrigonometry
- self assert: 0 cos equals: 1.
- self assert: 0 sin equals: 0.
- self assert: 0 tan equals: 0.
- self assert: 1 arcCos equals: 0.
- self assert: 0 arcSin equals: 0.
- self assert: 0 arcTan equals: 0.
- !
- testTruncated
-
- self assert: 3 truncated equals: 3.
- self assert: 3.212 truncated equals: 3.
- self assert: 3.51 truncated equals: 3
- ! !
- Object subclass: #ObjectMock
- instanceVariableNames: 'foo bar'
- package: 'Kernel-Tests'!
- !ObjectMock commentStamp!
- ObjectMock is there only to perform tests on classes.!
- !ObjectMock methodsFor: 'not yet classified'!
- foo
- ^ foo
- !
- foo: anObject
- foo := anObject
- ! !
- TestCase subclass: #ObjectTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !ObjectTest methodsFor: 'tests'!
- notDefined
- <inlineJS: 'return void 0;'>
- !
- testBasicAccess
- | o |
- o := Object new.
- o basicAt: 'a' put: 1.
- self assert: (o basicAt: 'a') equals: 1.
- self assert: (o basicAt: 'b') equals: nil
- !
- testBasicPerform
- | o |
- o := Object new.
- o basicAt: 'func' put: [ 'hello' ].
- o basicAt: 'func2' put: [ :a | a + 1 ].
- self assert: (o basicPerform: 'func') equals: 'hello'.
- self assert: (o basicPerform: 'func2' withArguments: #(3)) equals: 4
- !
- testDNU
- self should: [ Object new foo ] raise: MessageNotUnderstood
- !
- testEquality
- | o |
- o := Object new.
- self deny: o = Object new.
- self assert: (o = o).
- self assert: (o yourself = o).
- self assert: (o = o yourself)
- !
- testHalt
- self should: [ Object new halt ] raise: Error
- !
- testIdentity
- | o |
- o := Object new.
- self deny: o == Object new.
- self assert: o == o.
- self assert: o yourself == o.
- self assert: o == o yourself
- !
- testIfNil
- self deny: Object new isNil.
- self deny: (Object new ifNil: [ true ]) = true.
- self assert: (Object new ifNotNil: [ true ]) equals: true.
- self assert: (Object new ifNil: [ false ] ifNotNil: [ true ]) equals: true.
- self assert: (Object new ifNotNil: [ true ] ifNil: [ false ]) equals: true
- !
- testInstVars
- | o |
- o := ObjectMock new.
- self assert: (o instVarAt: #foo) equals: nil.
- o instVarAt: #foo put: 1.
- self assert: (o instVarAt: #foo) equals: 1.
- self assert: (o instVarAt: 'foo') equals: 1
- !
- testNilUndefined
- "nil in Smalltalk is the undefined object in JS"
- self assert: self notDefined equals: nil
- !
- testYourself
- | o |
- o := ObjectMock new.
- self assert: o yourself == o
- !
- testidentityHash
- | o1 o2 |
-
- o1 := Object new.
- o2 := Object new.
- self assert: o1 identityHash == o1 identityHash.
- self deny: o1 identityHash == o2 identityHash
- ! !
- TestCase subclass: #PointTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !PointTest methodsFor: 'tests'!
- testAccessing
- self assert: (Point x: 3 y: 4) x equals: 3.
- self assert: (Point x: 3 y: 4) y equals: 4.
- self assert: (Point new x: 3) x equals: 3.
- self assert: (Point new y: 4) y equals: 4
- !
- testArithmetic
- self assert: 3@4 * (3@4 ) equals: (Point x: 9 y: 16).
- self assert: 3@4 + (3@4 ) equals: (Point x: 6 y: 8).
- self assert: 3@4 - (3@4 ) equals: (Point x: 0 y: 0).
- self assert: 6@8 / (3@4 ) equals: (Point x: 2 y: 2)
- !
- testAt
- self assert: 3@4 equals: (Point x: 3 y: 4)
- !
- testComparison
- self assert: 3@4 < (4@5).
- self deny: 3@4 < (4@4).
-
- self assert: 4@5 <= (4@5).
- self deny: 4@5 <= (3@5).
-
- self assert: 5@6 > (4@5).
- self deny: 5@6 > (6@6).
-
- self assert: 4@5 >= (4@5).
- self deny: 4@5 >= (5@5)
- !
- testEgality
- self assert: (3@4 = (3@4)).
- self deny: 3@5 = (3@6)
- !
- testNew
- self assert: (Point new x: 3) y equals: nil.
- self deny: (Point new x: 3) x = 0.
- self assert: (Point new y: 4) x equals: nil.
- self deny: (Point new y: 4) y = 0
- !
- testTranslateBy
- self assert: (3@3 translateBy: 0@1) equals: 3@4.
- self assert: (3@3 translateBy: 0@1 negated) equals: 3@2.
- self assert: (3@3 translateBy: 2@3) equals: 5@6.
- self assert: (3@3 translateBy: 3 negated @0) equals: 0@3.
- ! !
- TestCase subclass: #QueueTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !QueueTest methodsFor: 'tests'!
- testNextIfAbsent
- | queue |
- queue := Queue new.
- queue nextPut: 'index1'.
- self assert: (queue nextIfAbsent: 'empty') = 'index1'.
- self deny: (queue nextIfAbsent: 'empty') = 'index1'
- !
- testQueueNext
- | queue |
- queue := Queue new.
- queue
- nextPut: 'index1';
- nextPut: 'index2'.
- self assert: queue next = 'index1'.
- self deny: queue next = 'index'.
- self should: [ queue next ] raise: Error
- ! !
- TestCase subclass: #RandomTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !RandomTest methodsFor: 'tests'!
- testAtRandomNumber
- |val|
- 100 timesRepeat: [
- val := 10 atRandom.
- self assert: (val > 0).
- self assert: (val <11)
- ]
- !
- testAtRandomSequenceableCollection
- |val|
-
- 100 timesRepeat: [
- val := 'abc' atRandom.
- self assert: ((val = 'a') | (val = 'b') | (val = 'c' )).
- ].
- !
- textNext
- 10000 timesRepeat: [
- | current next |
- next := Random new next.
- self assert: (next >= 0).
- self assert: (next < 1).
- self deny: current = next.
- next = current ]
- ! !
- TestCase subclass: #StreamTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !StreamTest methodsFor: 'accessing'!
- collectionClass
- ^ self class collectionClass
- !
- newCollection
- ^ self collectionClass new
- !
- newStream
- ^ self collectionClass new stream
- ! !
- !StreamTest methodsFor: 'tests'!
- testAtStartAtEnd
- | stream |
-
- stream := self newStream.
- self assert: stream atStart.
- self assert: stream atEnd.
-
- stream nextPutAll: self newCollection.
- self assert: stream atEnd.
- self deny: stream atStart.
-
- stream position: 1.
- self deny: stream atEnd.
- self deny: stream atStart
- !
- testContents
- | stream |
-
- stream := self newStream.
- stream nextPutAll: self newCollection.
-
- self assert: stream contents equals: self newCollection
- !
- testIsEmpty
- | stream |
-
- stream := self newStream.
- self assert: stream isEmpty.
-
- stream nextPutAll: self newCollection.
- self deny: stream isEmpty
- !
- testPosition
- | collection stream |
-
- collection := self newCollection.
- stream := self newStream.
-
- stream nextPutAll: collection.
- self assert: stream position equals: collection size.
-
- stream position: 0.
- self assert: stream position equals: 0.
-
- stream next.
- self assert: stream position equals: 1.
-
- stream next.
- self assert: stream position equals: 2
- !
- testReading
- | stream collection |
-
- collection := self newCollection.
- stream := self newStream.
-
- stream
- nextPutAll: collection;
- position: 0.
-
- collection do: [ :each |
- self assert: stream next equals: each ].
-
- self assert: stream next isNil
- !
- testStreamContents
- !
- testWrite
- | stream collection |
-
- collection := self newCollection.
- stream := self newStream.
-
- collection do: [ :each | stream << each ].
- self assert: stream contents equals: collection
- !
- testWriting
- | stream collection |
-
- collection := self newCollection.
- stream := self newStream.
-
- collection do: [ :each | stream nextPut: each ].
- self assert: stream contents equals: collection.
-
- stream := self newStream.
- stream nextPutAll: collection.
- self assert: stream contents equals: collection
- ! !
- !StreamTest class methodsFor: 'accessing'!
- collectionClass
- ^ nil
- ! !
- !StreamTest class methodsFor: 'testing'!
- isAbstract
- ^ self collectionClass isNil
- ! !
- StreamTest subclass: #ArrayStreamTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !ArrayStreamTest methodsFor: 'accessing'!
- newCollection
- ^ { true. 1. 3@4. 'foo' }
- ! !
- !ArrayStreamTest class methodsFor: 'accessing'!
- collectionClass
- ^ Array
- ! !
- StreamTest subclass: #StringStreamTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !StringStreamTest methodsFor: 'accessing'!
- newCollection
- ^ 'hello world'
- ! !
- !StringStreamTest class methodsFor: 'accessing'!
- collectionClass
- ^ String
- ! !
- TestCase subclass: #UndefinedTest
- instanceVariableNames: ''
- package: 'Kernel-Tests'!
- !UndefinedTest methodsFor: 'tests'!
- testCopying
- self assert: nil copy equals: nil
- !
- testDeepCopy
- self assert: nil deepCopy = nil
- !
- testIfNil
- self assert: (nil ifNil: [ true ]) equals: true.
- self deny: (nil ifNotNil: [ true ]) = true.
- self assert: (nil ifNil: [ true ] ifNotNil: [ false ]) equals: true.
- self deny: (nil ifNotNil: [ true ] ifNil: [ false ]) = true
- !
- testIsNil
- self assert: nil isNil.
- self deny: nil notNil.
- ! !
|