SUnit.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. smalltalk.addPackage('SUnit', {});
  2. smalltalk.addClass('ResultAnnouncement', smalltalk.Object, ['result'], 'SUnit');
  3. smalltalk.addMethod(
  4. "_result",
  5. smalltalk.method({
  6. selector: "result",
  7. category: 'accessing',
  8. fn: function (){
  9. var self=this;
  10. var $1;
  11. $1=self["@result"];
  12. return $1;
  13. },
  14. args: [],
  15. source: "result\x0a\x09^result",
  16. messageSends: [],
  17. referencedClasses: []
  18. }),
  19. smalltalk.ResultAnnouncement);
  20. smalltalk.addMethod(
  21. "_result_",
  22. smalltalk.method({
  23. selector: "result:",
  24. category: 'accessing',
  25. fn: function (aTestResult){
  26. var self=this;
  27. self["@result"]=aTestResult;
  28. return self},
  29. args: ["aTestResult"],
  30. source: "result: aTestResult\x0a\x09result := aTestResult",
  31. messageSends: [],
  32. referencedClasses: []
  33. }),
  34. smalltalk.ResultAnnouncement);
  35. smalltalk.addClass('TestCase', smalltalk.Object, ['testSelector', 'asyncTimeout', 'context'], 'SUnit');
  36. smalltalk.TestCase.comment="A TestCase is an implementation of the command pattern to run a test. \x0a\x0a`TestCase` instances are created with the class method `#selector:`, \x0apassing the symbol that names the method to be executed when the test case runs.\x0a\x0aWhen you discover a new fixture, subclass `TestCase` and create a `#test...` method for the first test. \x0aAs that method develops and more `#test...` methods are added, you will find yourself refactoring temps \x0ainto instance variables for the objects in the fixture and overriding `#setUp` to initialize these variables. \x0aAs required, override `#tearDown` to nil references, release objects and deallocate.\x0a\x0a"
  37. smalltalk.addMethod(
  38. "_assert_",
  39. smalltalk.method({
  40. selector: "assert:",
  41. category: 'testing',
  42. fn: function (aBoolean){
  43. var self=this;
  44. smalltalk.send(self,"_assert_description_",[aBoolean,"Assertion failed"]);
  45. return self},
  46. args: ["aBoolean"],
  47. source: "assert: aBoolean\x0a\x09self assert: aBoolean description: 'Assertion failed'",
  48. messageSends: ["assert:description:"],
  49. referencedClasses: []
  50. }),
  51. smalltalk.TestCase);
  52. smalltalk.addMethod(
  53. "_assert_description_",
  54. smalltalk.method({
  55. selector: "assert:description:",
  56. category: 'testing',
  57. fn: function (aBoolean,aString){
  58. var self=this;
  59. var $1;
  60. $1=aBoolean;
  61. if(! smalltalk.assert($1)){
  62. smalltalk.send(self,"_signalFailure_",[aString]);
  63. };
  64. return self},
  65. args: ["aBoolean", "aString"],
  66. source: "assert: aBoolean description: aString\x0a\x09aBoolean ifFalse: [self signalFailure: aString]",
  67. messageSends: ["ifFalse:", "signalFailure:"],
  68. referencedClasses: []
  69. }),
  70. smalltalk.TestCase);
  71. smalltalk.addMethod(
  72. "_assert_equals_",
  73. smalltalk.method({
  74. selector: "assert:equals:",
  75. category: 'testing',
  76. fn: function (expected,actual){
  77. var self=this;
  78. var $1;
  79. $1=smalltalk.send(self,"_assert_description_",[smalltalk.send(expected,"__eq",[actual]),smalltalk.send(smalltalk.send(smalltalk.send("Expected: ","__comma",[smalltalk.send(expected,"_asString",[])]),"__comma",[" but was: "]),"__comma",[smalltalk.send(actual,"_asString",[])])]);
  80. return $1;
  81. },
  82. args: ["expected", "actual"],
  83. source: "assert: expected equals: actual\x0a\x09^ self assert: (expected = actual) description: 'Expected: ', expected asString, ' but was: ', actual asString",
  84. messageSends: ["assert:description:", "=", ",", "asString"],
  85. referencedClasses: []
  86. }),
  87. smalltalk.TestCase);
  88. smalltalk.addMethod(
  89. "_async_",
  90. smalltalk.method({
  91. selector: "async:",
  92. category: 'async',
  93. fn: function (aBlock){
  94. var self=this;
  95. var $2,$1;
  96. var c;
  97. smalltalk.send(self,"_errorIfNotAsync_",["#async"]);
  98. c=self["@context"];
  99. $1=(function(){
  100. $2=smalltalk.send(self,"_isAsync",[]);
  101. if(smalltalk.assert($2)){
  102. return smalltalk.send(c,"_execute_",[aBlock]);
  103. };
  104. });
  105. return $1;
  106. },
  107. args: ["aBlock"],
  108. source: "async: aBlock\x0a\x09| c |\x0a\x09self errorIfNotAsync: '#async'.\x0a c := context.\x0a ^ [ self isAsync ifTrue: [ c execute: aBlock ] ]",
  109. messageSends: ["errorIfNotAsync:", "ifTrue:", "execute:", "isAsync"],
  110. referencedClasses: []
  111. }),
  112. smalltalk.TestCase);
  113. smalltalk.addMethod(
  114. "_context_",
  115. smalltalk.method({
  116. selector: "context:",
  117. category: 'accessing',
  118. fn: function (aRunningTestContext){
  119. var self=this;
  120. self["@context"]=aRunningTestContext;
  121. return self},
  122. args: ["aRunningTestContext"],
  123. source: "context: aRunningTestContext\x0a\x09context := aRunningTestContext",
  124. messageSends: [],
  125. referencedClasses: []
  126. }),
  127. smalltalk.TestCase);
  128. smalltalk.addMethod(
  129. "_deny_",
  130. smalltalk.method({
  131. selector: "deny:",
  132. category: 'testing',
  133. fn: function (aBoolean){
  134. var self=this;
  135. smalltalk.send(self,"_assert_",[smalltalk.send(aBoolean,"_not",[])]);
  136. return self},
  137. args: ["aBoolean"],
  138. source: "deny: aBoolean\x0a\x09self assert: aBoolean not",
  139. messageSends: ["assert:", "not"],
  140. referencedClasses: []
  141. }),
  142. smalltalk.TestCase);
  143. smalltalk.addMethod(
  144. "_errorIfNotAsync_",
  145. smalltalk.method({
  146. selector: "errorIfNotAsync:",
  147. category: 'error handling',
  148. fn: function (aString){
  149. var self=this;
  150. var $1;
  151. $1=smalltalk.send(self,"_isAsync",[]);
  152. if(! smalltalk.assert($1)){
  153. smalltalk.send(self,"_error_",[smalltalk.send(aString,"__comma",[" used without prior #timeout:"])]);
  154. };
  155. return self},
  156. args: ["aString"],
  157. source: "errorIfNotAsync: aString\x0a\x09self isAsync ifFalse: [ \x0a \x09self error: aString, ' used without prior #timeout:' ]",
  158. messageSends: ["ifFalse:", "error:", ",", "isAsync"],
  159. referencedClasses: []
  160. }),
  161. smalltalk.TestCase);
  162. smalltalk.addMethod(
  163. "_finished",
  164. smalltalk.method({
  165. selector: "finished",
  166. category: 'async',
  167. fn: function (){
  168. var self=this;
  169. smalltalk.send(self,"_errorIfNotAsync_",["#finished"]);
  170. self["@asyncTimeout"]=nil;
  171. return self},
  172. args: [],
  173. source: "finished\x0a\x09self errorIfNotAsync: '#finished'.\x0a\x09asyncTimeout := nil",
  174. messageSends: ["errorIfNotAsync:"],
  175. referencedClasses: []
  176. }),
  177. smalltalk.TestCase);
  178. smalltalk.addMethod(
  179. "_isAsync",
  180. smalltalk.method({
  181. selector: "isAsync",
  182. category: 'testing',
  183. fn: function (){
  184. var self=this;
  185. var $1;
  186. $1=smalltalk.send(self["@asyncTimeout"],"_notNil",[]);
  187. return $1;
  188. },
  189. args: [],
  190. source: "isAsync\x0a\x09^asyncTimeout notNil",
  191. messageSends: ["notNil"],
  192. referencedClasses: []
  193. }),
  194. smalltalk.TestCase);
  195. smalltalk.addMethod(
  196. "_performTest",
  197. smalltalk.method({
  198. selector: "performTest",
  199. category: 'running',
  200. fn: function (){
  201. var self=this;
  202. self["@asyncTimeout"]=nil;
  203. smalltalk.send(self,"_perform_",[smalltalk.send(self,"_selector",[])]);
  204. return self},
  205. args: [],
  206. source: "performTest\x0a\x09asyncTimeout := nil.\x0a\x09self perform: self selector\x0a",
  207. messageSends: ["perform:", "selector"],
  208. referencedClasses: []
  209. }),
  210. smalltalk.TestCase);
  211. smalltalk.addMethod(
  212. "_runCase",
  213. smalltalk.method({
  214. selector: "runCase",
  215. category: 'running',
  216. fn: function (){
  217. var self=this;
  218. smalltalk.send(smalltalk.send((smalltalk.TestContext || TestContext),"_testCase_",[self]),"_start",[]);
  219. return self},
  220. args: [],
  221. source: "runCase\x0a\x09\x22Runs a test case in isolated context, leaking all errors.\x22\x0a\x0a\x09(TestContext testCase: self) start",
  222. messageSends: ["start", "testCase:"],
  223. referencedClasses: ["TestContext"]
  224. }),
  225. smalltalk.TestCase);
  226. smalltalk.addMethod(
  227. "_selector",
  228. smalltalk.method({
  229. selector: "selector",
  230. category: 'accessing',
  231. fn: function (){
  232. var self=this;
  233. var $1;
  234. $1=self["@testSelector"];
  235. return $1;
  236. },
  237. args: [],
  238. source: "selector\x0a\x09^testSelector",
  239. messageSends: [],
  240. referencedClasses: []
  241. }),
  242. smalltalk.TestCase);
  243. smalltalk.addMethod(
  244. "_setTestSelector_",
  245. smalltalk.method({
  246. selector: "setTestSelector:",
  247. category: 'accessing',
  248. fn: function (aSelector){
  249. var self=this;
  250. self["@testSelector"]=aSelector;
  251. return self},
  252. args: ["aSelector"],
  253. source: "setTestSelector: aSelector\x0a\x09testSelector := aSelector",
  254. messageSends: [],
  255. referencedClasses: []
  256. }),
  257. smalltalk.TestCase);
  258. smalltalk.addMethod(
  259. "_setUp",
  260. smalltalk.method({
  261. selector: "setUp",
  262. category: 'running',
  263. fn: function (){
  264. var self=this;
  265. return self},
  266. args: [],
  267. source: "setUp",
  268. messageSends: [],
  269. referencedClasses: []
  270. }),
  271. smalltalk.TestCase);
  272. smalltalk.addMethod(
  273. "_should_",
  274. smalltalk.method({
  275. selector: "should:",
  276. category: 'testing',
  277. fn: function (aBlock){
  278. var self=this;
  279. smalltalk.send(self,"_assert_",[smalltalk.send(aBlock,"_value",[])]);
  280. return self},
  281. args: ["aBlock"],
  282. source: "should: aBlock\x0a\x09self assert: aBlock value",
  283. messageSends: ["assert:", "value"],
  284. referencedClasses: []
  285. }),
  286. smalltalk.TestCase);
  287. smalltalk.addMethod(
  288. "_should_raise_",
  289. smalltalk.method({
  290. selector: "should:raise:",
  291. category: 'testing',
  292. fn: function (aBlock,anExceptionClass){
  293. var self=this;
  294. smalltalk.send(self,"_assert_",[smalltalk.send((function(){
  295. smalltalk.send(aBlock,"_value",[]);
  296. return false;
  297. }),"_on_do_",[anExceptionClass,(function(ex){
  298. return true;
  299. })])]);
  300. return self},
  301. args: ["aBlock", "anExceptionClass"],
  302. source: "should: aBlock raise: anExceptionClass\x0a\x09self assert: ([aBlock value. false] \x0a\x09\x09on: anExceptionClass \x0a\x09\x09do: [:ex | true])",
  303. messageSends: ["assert:", "on:do:", "value"],
  304. referencedClasses: []
  305. }),
  306. smalltalk.TestCase);
  307. smalltalk.addMethod(
  308. "_shouldnt_raise_",
  309. smalltalk.method({
  310. selector: "shouldnt:raise:",
  311. category: 'testing',
  312. fn: function (aBlock,anExceptionClass){
  313. var self=this;
  314. smalltalk.send(self,"_assert_",[smalltalk.send((function(){
  315. smalltalk.send(aBlock,"_value",[]);
  316. return true;
  317. }),"_on_do_",[anExceptionClass,(function(ex){
  318. return false;
  319. })])]);
  320. return self},
  321. args: ["aBlock", "anExceptionClass"],
  322. source: "shouldnt: aBlock raise: anExceptionClass\x0a\x09self assert: ([aBlock value. true] \x0a\x09\x09on: anExceptionClass \x0a\x09\x09do: [:ex | false])",
  323. messageSends: ["assert:", "on:do:", "value"],
  324. referencedClasses: []
  325. }),
  326. smalltalk.TestCase);
  327. smalltalk.addMethod(
  328. "_signalFailure_",
  329. smalltalk.method({
  330. selector: "signalFailure:",
  331. category: 'private',
  332. fn: function (aString){
  333. var self=this;
  334. var $1,$2;
  335. $1=smalltalk.send((smalltalk.TestFailure || TestFailure),"_new",[]);
  336. smalltalk.send($1,"_messageText_",[aString]);
  337. $2=smalltalk.send($1,"_signal",[]);
  338. return self},
  339. args: ["aString"],
  340. source: "signalFailure: aString\x0a\x09TestFailure new\x0a\x09\x09messageText: aString;\x0a\x09\x09signal",
  341. messageSends: ["messageText:", "new", "signal"],
  342. referencedClasses: ["TestFailure"]
  343. }),
  344. smalltalk.TestCase);
  345. smalltalk.addMethod(
  346. "_tearDown",
  347. smalltalk.method({
  348. selector: "tearDown",
  349. category: 'running',
  350. fn: function (){
  351. var self=this;
  352. return self},
  353. args: [],
  354. source: "tearDown",
  355. messageSends: [],
  356. referencedClasses: []
  357. }),
  358. smalltalk.TestCase);
  359. smalltalk.addMethod(
  360. "_timeout_",
  361. smalltalk.method({
  362. selector: "timeout:",
  363. category: 'async',
  364. fn: function (aNumber){
  365. var self=this;
  366. var $1;
  367. $1=self["@asyncTimeout"];
  368. if(($receiver = $1) == nil || $receiver == undefined){
  369. $1;
  370. } else {
  371. smalltalk.send(self["@asyncTimeout"],"_clearTimeout",[]);
  372. };
  373. self["@asyncTimeout"]=(0);
  374. self["@asyncTimeout"]=smalltalk.send(smalltalk.send(self,"_async_",[(function(){
  375. return smalltalk.send(self,"_assert_description_",[false,"SUnit grace time exhausted"]);
  376. })]),"_valueWithTimeout_",[aNumber]);
  377. return self},
  378. args: ["aNumber"],
  379. source: "timeout: aNumber\x0a\x09\x22Set a grace time timeout in milliseconds to run the test asynchronously\x22\x0a \x0a\x09asyncTimeout ifNotNil: [ asyncTimeout clearTimeout ].\x0a \x0a \x22to allow #async: message send without throwing an error\x22\x0a\x09asyncTimeout := 0.\x0a \x0a\x09asyncTimeout := (self async: [ \x0a \x09self assert: false description: 'SUnit grace time exhausted' ])\x0a \x09valueWithTimeout: aNumber",
  380. messageSends: ["ifNotNil:", "clearTimeout", "valueWithTimeout:", "async:", "assert:description:"],
  381. referencedClasses: []
  382. }),
  383. smalltalk.TestCase);
  384. smalltalk.addMethod(
  385. "_allTestSelectors",
  386. smalltalk.method({
  387. selector: "allTestSelectors",
  388. category: 'accessing',
  389. fn: function (){
  390. var self=this;
  391. var $1,$2;
  392. var selectors;
  393. selectors=smalltalk.send(self,"_testSelectors",[]);
  394. $1=smalltalk.send(self,"_shouldInheritSelectors",[]);
  395. if(smalltalk.assert($1)){
  396. smalltalk.send(selectors,"_addAll_",[smalltalk.send(smalltalk.send(self,"_superclass",[]),"_allTestSelectors",[])]);
  397. };
  398. $2=selectors;
  399. return $2;
  400. },
  401. args: [],
  402. source: "allTestSelectors\x0a\x09| selectors |\x0a\x09selectors := self testSelectors.\x0a\x09self shouldInheritSelectors ifTrue: [\x0a\x09\x09selectors addAll: self superclass allTestSelectors].\x0a\x09^selectors",
  403. messageSends: ["testSelectors", "ifTrue:", "addAll:", "allTestSelectors", "superclass", "shouldInheritSelectors"],
  404. referencedClasses: []
  405. }),
  406. smalltalk.TestCase.klass);
  407. smalltalk.addMethod(
  408. "_buildSuite",
  409. smalltalk.method({
  410. selector: "buildSuite",
  411. category: 'accessing',
  412. fn: function (){
  413. var self=this;
  414. var $1;
  415. $1=smalltalk.send(smalltalk.send(self,"_allTestSelectors",[]),"_collect_",[(function(each){
  416. return smalltalk.send(self,"_selector_",[each]);
  417. })]);
  418. return $1;
  419. },
  420. args: [],
  421. source: "buildSuite\x0a\x09^self allTestSelectors collect: [:each | self selector: each]",
  422. messageSends: ["collect:", "selector:", "allTestSelectors"],
  423. referencedClasses: []
  424. }),
  425. smalltalk.TestCase.klass);
  426. smalltalk.addMethod(
  427. "_isAbstract",
  428. smalltalk.method({
  429. selector: "isAbstract",
  430. category: 'testing',
  431. fn: function (){
  432. var self=this;
  433. var $1;
  434. $1=smalltalk.send(smalltalk.send(self,"_name",[]),"__eq",["TestCase"]);
  435. return $1;
  436. },
  437. args: [],
  438. source: "isAbstract\x0a\x09^ self name = 'TestCase'",
  439. messageSends: ["=", "name"],
  440. referencedClasses: []
  441. }),
  442. smalltalk.TestCase.klass);
  443. smalltalk.addMethod(
  444. "_lookupHierarchyRoot",
  445. smalltalk.method({
  446. selector: "lookupHierarchyRoot",
  447. category: 'accessing',
  448. fn: function (){
  449. var self=this;
  450. var $1;
  451. $1=(smalltalk.TestCase || TestCase);
  452. return $1;
  453. },
  454. args: [],
  455. source: "lookupHierarchyRoot\x0a\x09^TestCase",
  456. messageSends: [],
  457. referencedClasses: ["TestCase"]
  458. }),
  459. smalltalk.TestCase.klass);
  460. smalltalk.addMethod(
  461. "_selector_",
  462. smalltalk.method({
  463. selector: "selector:",
  464. category: 'accessing',
  465. fn: function (aSelector){
  466. var self=this;
  467. var $2,$3,$1;
  468. $2=smalltalk.send(self,"_new",[]);
  469. smalltalk.send($2,"_setTestSelector_",[aSelector]);
  470. $3=smalltalk.send($2,"_yourself",[]);
  471. $1=$3;
  472. return $1;
  473. },
  474. args: ["aSelector"],
  475. source: "selector: aSelector\x0a\x09^self new\x0a\x09\x09setTestSelector: aSelector;\x0a\x09\x09yourself",
  476. messageSends: ["setTestSelector:", "new", "yourself"],
  477. referencedClasses: []
  478. }),
  479. smalltalk.TestCase.klass);
  480. smalltalk.addMethod(
  481. "_shouldInheritSelectors",
  482. smalltalk.method({
  483. selector: "shouldInheritSelectors",
  484. category: 'testing',
  485. fn: function (){
  486. var self=this;
  487. var $1;
  488. $1=smalltalk.send(self,"_~_eq",[smalltalk.send(self,"_lookupHierarchyRoot",[])]);
  489. return $1;
  490. },
  491. args: [],
  492. source: "shouldInheritSelectors\x0a\x09^self ~= self lookupHierarchyRoot",
  493. messageSends: ["~=", "lookupHierarchyRoot"],
  494. referencedClasses: []
  495. }),
  496. smalltalk.TestCase.klass);
  497. smalltalk.addMethod(
  498. "_testSelectors",
  499. smalltalk.method({
  500. selector: "testSelectors",
  501. category: 'accessing',
  502. fn: function (){
  503. var self=this;
  504. var $1;
  505. $1=smalltalk.send(smalltalk.send(smalltalk.send(self,"_methodDictionary",[]),"_keys",[]),"_select_",[(function(each){
  506. return smalltalk.send(each,"_match_",["^test"]);
  507. })]);
  508. return $1;
  509. },
  510. args: [],
  511. source: "testSelectors\x0a\x09^self methodDictionary keys select: [:each | each match: '^test']",
  512. messageSends: ["select:", "match:", "keys", "methodDictionary"],
  513. referencedClasses: []
  514. }),
  515. smalltalk.TestCase.klass);
  516. smalltalk.addClass('TestContext', smalltalk.Object, ['testCase'], 'SUnit');
  517. smalltalk.TestContext.comment="TestContext governs running a particular test case.\x0a\x0aIt's main added value is `#execute:` method which runs a block\x0aas a part of test case (restores context, nilling it afterwards,\x0acleaning/calling tearDown as appropriate for sync/async scenario)."
  518. smalltalk.addMethod(
  519. "_execute_",
  520. smalltalk.method({
  521. selector: "execute:",
  522. category: 'running',
  523. fn: function (aBlock){
  524. var self=this;
  525. var $1,$3,$4,$2;
  526. var failed;
  527. smalltalk.send(self["@testCase"],"_context_",[self]);
  528. $1=(function(){
  529. failed=true;
  530. failed;
  531. smalltalk.send(aBlock,"_value",[]);
  532. failed=false;
  533. return failed;
  534. });
  535. $2=(function(){
  536. smalltalk.send(self["@testCase"],"_context_",[nil]);
  537. $3=smalltalk.send(failed,"_and_",[(function(){
  538. return smalltalk.send(self["@testCase"],"_isAsync",[]);
  539. })]);
  540. if(smalltalk.assert($3)){
  541. smalltalk.send(self["@testCase"],"_finished",[]);
  542. };
  543. $4=smalltalk.send(self["@testCase"],"_isAsync",[]);
  544. if(! smalltalk.assert($4)){
  545. return smalltalk.send(self["@testCase"],"_tearDown",[]);
  546. };
  547. });
  548. smalltalk.send($1,"_ensure_",[$2]);
  549. return self},
  550. args: ["aBlock"],
  551. source: "execute: aBlock\x0a\x09| failed |\x0a \x0a testCase context: self.\x0a [ \x0a \x09failed := true. \x0a aBlock value. \x0a failed := false \x0a\x09] \x0a \x09ensure: [\x0a \x09testCase context: nil.\x0a \x0a \x09(failed and: [ testCase isAsync ]) ifTrue: [ \x0a \x09testCase finished ].\x0a \x09testCase isAsync ifFalse: [ \x0a \x09\x09testCase tearDown ] ]",
  552. messageSends: ["context:", "ensure:", "ifTrue:", "finished", "and:", "isAsync", "ifFalse:", "tearDown", "value"],
  553. referencedClasses: []
  554. }),
  555. smalltalk.TestContext);
  556. smalltalk.addMethod(
  557. "_start",
  558. smalltalk.method({
  559. selector: "start",
  560. category: 'running',
  561. fn: function (){
  562. var self=this;
  563. smalltalk.send(self,"_execute_",[(function(){
  564. smalltalk.send(self["@testCase"],"_setUp",[]);
  565. return smalltalk.send(self["@testCase"],"_performTest",[]);
  566. })]);
  567. return self},
  568. args: [],
  569. source: "start\x0a\x09self execute: [ \x0a \x09testCase setUp. \x0a testCase performTest ]",
  570. messageSends: ["execute:", "setUp", "performTest"],
  571. referencedClasses: []
  572. }),
  573. smalltalk.TestContext);
  574. smalltalk.addMethod(
  575. "_testCase_",
  576. smalltalk.method({
  577. selector: "testCase:",
  578. category: 'accessing',
  579. fn: function (aTestCase){
  580. var self=this;
  581. self["@testCase"]=aTestCase;
  582. return self},
  583. args: ["aTestCase"],
  584. source: "testCase: aTestCase\x0a\x09testCase := aTestCase",
  585. messageSends: [],
  586. referencedClasses: []
  587. }),
  588. smalltalk.TestContext);
  589. smalltalk.addMethod(
  590. "_testCase_",
  591. smalltalk.method({
  592. selector: "testCase:",
  593. category: 'instance creation',
  594. fn: function (aTestCase){
  595. var self=this;
  596. var $2,$3,$1;
  597. $2=smalltalk.send(self,"_new",[]);
  598. smalltalk.send($2,"_testCase_",[aTestCase]);
  599. $3=smalltalk.send($2,"_yourself",[]);
  600. $1=$3;
  601. return $1;
  602. },
  603. args: ["aTestCase"],
  604. source: "testCase: aTestCase\x0a\x09^self new\x0a testCase: aTestCase;\x0a yourself",
  605. messageSends: ["testCase:", "new", "yourself"],
  606. referencedClasses: []
  607. }),
  608. smalltalk.TestContext.klass);
  609. smalltalk.addClass('ReportingTestContext', smalltalk.TestContext, ['finished', 'result'], 'SUnit');
  610. smalltalk.ReportingTestContext.comment="ReportingTestContext adds `TestResult` reporting\x0ato `TestContext`.\x0a\x0aErrors are caught and save into a `TestResult`,\x0aWhen test case is finished (which can be later for async tests),\x0aa callback block is executed; this is used by a `TestSuiteRunner`.\x0a"
  611. smalltalk.addMethod(
  612. "_execute_",
  613. smalltalk.method({
  614. selector: "execute:",
  615. category: 'running',
  616. fn: function (aBlock){
  617. var self=this;
  618. var $1,$3,$2;
  619. $1=(function(){
  620. return smalltalk.send(self,"_withErrorReporting_",[(function(){
  621. return smalltalk.send(self,"_execute_",[aBlock],smalltalk.TestContext);
  622. })]);
  623. });
  624. $2=(function(){
  625. $3=smalltalk.send(self["@testCase"],"_isAsync",[]);
  626. if(! smalltalk.assert($3)){
  627. smalltalk.send(self["@result"],"_increaseRuns",[]);
  628. return smalltalk.send(self["@finished"],"_value",[]);
  629. };
  630. });
  631. smalltalk.send($1,"_ensure_",[$2]);
  632. return self},
  633. args: ["aBlock"],
  634. source: "execute: aBlock\x0a [ \x0a \x09self withErrorReporting: [ super execute: aBlock ] \x0a\x09]\x0a \x09ensure: [ \x0a \x09testCase isAsync ifFalse: [ \x0a \x09result increaseRuns. finished value ] ]",
  635. messageSends: ["ensure:", "ifFalse:", "increaseRuns", "value", "isAsync", "withErrorReporting:", "execute:"],
  636. referencedClasses: []
  637. }),
  638. smalltalk.ReportingTestContext);
  639. smalltalk.addMethod(
  640. "_finished_",
  641. smalltalk.method({
  642. selector: "finished:",
  643. category: 'accessing',
  644. fn: function (aBlock){
  645. var self=this;
  646. self["@finished"]=aBlock;
  647. return self},
  648. args: ["aBlock"],
  649. source: "finished: aBlock\x0a\x09finished := aBlock",
  650. messageSends: [],
  651. referencedClasses: []
  652. }),
  653. smalltalk.ReportingTestContext);
  654. smalltalk.addMethod(
  655. "_result_",
  656. smalltalk.method({
  657. selector: "result:",
  658. category: 'accessing',
  659. fn: function (aTestResult){
  660. var self=this;
  661. self["@result"]=aTestResult;
  662. return self},
  663. args: ["aTestResult"],
  664. source: "result: aTestResult\x0a\x09result := aTestResult",
  665. messageSends: [],
  666. referencedClasses: []
  667. }),
  668. smalltalk.ReportingTestContext);
  669. smalltalk.addMethod(
  670. "_withErrorReporting_",
  671. smalltalk.method({
  672. selector: "withErrorReporting:",
  673. category: 'private',
  674. fn: function (aBlock){
  675. var self=this;
  676. smalltalk.send((function(){
  677. return smalltalk.send(aBlock,"_on_do_",[(smalltalk.TestFailure || TestFailure),(function(ex){
  678. return smalltalk.send(self["@result"],"_addFailure_",[self["@testCase"]]);
  679. })]);
  680. }),"_on_do_",[(smalltalk.Error || Error),(function(ex){
  681. return smalltalk.send(self["@result"],"_addError_",[self["@testCase"]]);
  682. })]);
  683. return self},
  684. args: ["aBlock"],
  685. source: "withErrorReporting: aBlock\x0a \x09[ aBlock\x0a\x09\x09on: TestFailure \x0a\x09\x09do: [ :ex | result addFailure: testCase ] \x0a\x09]\x0a \x09on: Error \x0a do: [ :ex | result addError: testCase ]",
  686. messageSends: ["on:do:", "addError:", "addFailure:"],
  687. referencedClasses: ["Error", "TestFailure"]
  688. }),
  689. smalltalk.ReportingTestContext);
  690. smalltalk.addMethod(
  691. "_testCase_result_finished_",
  692. smalltalk.method({
  693. selector: "testCase:result:finished:",
  694. category: 'instance creation',
  695. fn: function (aTestCase,aTestResult,aBlock){
  696. var self=this;
  697. var $2,$3,$1;
  698. $2=smalltalk.send(self,"_testCase_",[aTestCase],smalltalk.TestContext.klass);
  699. smalltalk.send($2,"_result_",[aTestResult]);
  700. smalltalk.send($2,"_finished_",[aBlock]);
  701. $3=smalltalk.send($2,"_yourself",[]);
  702. $1=$3;
  703. return $1;
  704. },
  705. args: ["aTestCase", "aTestResult", "aBlock"],
  706. source: "testCase: aTestCase result: aTestResult finished: aBlock\x0a\x09^(super testCase: aTestCase)\x0a result: aTestResult;\x0a finished: aBlock;\x0a yourself",
  707. messageSends: ["result:", "testCase:", "finished:", "yourself"],
  708. referencedClasses: []
  709. }),
  710. smalltalk.ReportingTestContext.klass);
  711. smalltalk.addClass('TestFailure', smalltalk.Error, [], 'SUnit');
  712. smalltalk.TestFailure.comment="The test framework distinguishes between failures and errors. \x0aA failure is an event whose possibiity is explicitly anticipated and checked for in an assertion, \x0awhereas an error is an unanticipated problem like a division by 0 or an index out of bounds. \x0a\x0aTestFailure is raised when the boolean parameter of an #`assert:` or `#deny:` call is the opposite of what the assertion claims."
  713. smalltalk.addClass('TestResult', smalltalk.Object, ['timestamp', 'runs', 'errors', 'failures', 'total'], 'SUnit');
  714. smalltalk.TestResult.comment="A TestResult implements the collecting parameter pattern for running a bunch of tests. \x0a\x0aA TestResult holds tests that have run, sorted into the result categories of passed, failures and errors.\x0a\x0aTestResult is an interesting object to subclass or substitute. `#runCase:` is the external protocol you need to reproduce"
  715. smalltalk.addMethod(
  716. "_addError_",
  717. smalltalk.method({
  718. selector: "addError:",
  719. category: 'accessing',
  720. fn: function (anError){
  721. var self=this;
  722. smalltalk.send(smalltalk.send(self,"_errors",[]),"_add_",[anError]);
  723. return self},
  724. args: ["anError"],
  725. source: "addError: anError\x0a\x09self errors add: anError",
  726. messageSends: ["add:", "errors"],
  727. referencedClasses: []
  728. }),
  729. smalltalk.TestResult);
  730. smalltalk.addMethod(
  731. "_addFailure_",
  732. smalltalk.method({
  733. selector: "addFailure:",
  734. category: 'accessing',
  735. fn: function (aFailure){
  736. var self=this;
  737. smalltalk.send(smalltalk.send(self,"_failures",[]),"_add_",[aFailure]);
  738. return self},
  739. args: ["aFailure"],
  740. source: "addFailure: aFailure\x0a\x09self failures add: aFailure",
  741. messageSends: ["add:", "failures"],
  742. referencedClasses: []
  743. }),
  744. smalltalk.TestResult);
  745. smalltalk.addMethod(
  746. "_errors",
  747. smalltalk.method({
  748. selector: "errors",
  749. category: 'accessing',
  750. fn: function (){
  751. var self=this;
  752. var $1;
  753. $1=self["@errors"];
  754. return $1;
  755. },
  756. args: [],
  757. source: "errors\x0a\x09^errors",
  758. messageSends: [],
  759. referencedClasses: []
  760. }),
  761. smalltalk.TestResult);
  762. smalltalk.addMethod(
  763. "_failures",
  764. smalltalk.method({
  765. selector: "failures",
  766. category: 'accessing',
  767. fn: function (){
  768. var self=this;
  769. var $1;
  770. $1=self["@failures"];
  771. return $1;
  772. },
  773. args: [],
  774. source: "failures\x0a\x09^failures",
  775. messageSends: [],
  776. referencedClasses: []
  777. }),
  778. smalltalk.TestResult);
  779. smalltalk.addMethod(
  780. "_increaseRuns",
  781. smalltalk.method({
  782. selector: "increaseRuns",
  783. category: 'accessing',
  784. fn: function (){
  785. var self=this;
  786. self["@runs"]=smalltalk.send(self["@runs"],"__plus",[(1)]);
  787. return self},
  788. args: [],
  789. source: "increaseRuns\x0a\x09runs := runs + 1",
  790. messageSends: ["+"],
  791. referencedClasses: []
  792. }),
  793. smalltalk.TestResult);
  794. smalltalk.addMethod(
  795. "_initialize",
  796. smalltalk.method({
  797. selector: "initialize",
  798. category: 'initialization',
  799. fn: function (){
  800. var self=this;
  801. smalltalk.send(self,"_initialize",[],smalltalk.Object);
  802. self["@timestamp"]=smalltalk.send((smalltalk.Date || Date),"_now",[]);
  803. self["@runs"]=(0);
  804. self["@errors"]=smalltalk.send((smalltalk.Array || Array),"_new",[]);
  805. self["@failures"]=smalltalk.send((smalltalk.Array || Array),"_new",[]);
  806. self["@total"]=(0);
  807. return self},
  808. args: [],
  809. source: "initialize\x0a\x09super initialize.\x0a\x09timestamp := Date now.\x0a\x09runs := 0.\x0a\x09errors := Array new.\x0a\x09failures := Array new.\x0a\x09total := 0",
  810. messageSends: ["initialize", "now", "new"],
  811. referencedClasses: ["Date", "Array"]
  812. }),
  813. smalltalk.TestResult);
  814. smalltalk.addMethod(
  815. "_runs",
  816. smalltalk.method({
  817. selector: "runs",
  818. category: 'accessing',
  819. fn: function (){
  820. var self=this;
  821. var $1;
  822. $1=self["@runs"];
  823. return $1;
  824. },
  825. args: [],
  826. source: "runs\x0a\x09^runs",
  827. messageSends: [],
  828. referencedClasses: []
  829. }),
  830. smalltalk.TestResult);
  831. smalltalk.addMethod(
  832. "_status",
  833. smalltalk.method({
  834. selector: "status",
  835. category: 'accessing',
  836. fn: function (){
  837. var self=this;
  838. var $2,$4,$3,$1;
  839. $2=smalltalk.send(smalltalk.send(self,"_errors",[]),"_isEmpty",[]);
  840. $3=(function(){
  841. $4=smalltalk.send(smalltalk.send(self,"_failures",[]),"_isEmpty",[]);
  842. if(smalltalk.assert($4)){
  843. return "success";
  844. } else {
  845. return "failure";
  846. };
  847. });
  848. $1=smalltalk.send($2,"_ifTrue_ifFalse_",[$3,(function(){
  849. return "error";
  850. })]);
  851. return $1;
  852. },
  853. args: [],
  854. source: "status\x0a\x09^self errors isEmpty \x0a\x09\x09ifTrue: [\x0a\x09\x09\x09self failures isEmpty \x0a\x09\x09\x09\x09ifTrue: ['success']\x0a\x09\x09\x09\x09ifFalse: ['failure']]\x0a\x09\x09ifFalse: ['error']",
  855. messageSends: ["ifTrue:ifFalse:", "isEmpty", "failures", "errors"],
  856. referencedClasses: []
  857. }),
  858. smalltalk.TestResult);
  859. smalltalk.addMethod(
  860. "_timestamp",
  861. smalltalk.method({
  862. selector: "timestamp",
  863. category: 'accessing',
  864. fn: function (){
  865. var self=this;
  866. var $1;
  867. $1=self["@timestamp"];
  868. return $1;
  869. },
  870. args: [],
  871. source: "timestamp\x0a\x09^timestamp",
  872. messageSends: [],
  873. referencedClasses: []
  874. }),
  875. smalltalk.TestResult);
  876. smalltalk.addMethod(
  877. "_total",
  878. smalltalk.method({
  879. selector: "total",
  880. category: 'accessing',
  881. fn: function (){
  882. var self=this;
  883. var $1;
  884. $1=self["@total"];
  885. return $1;
  886. },
  887. args: [],
  888. source: "total\x0a\x09^total",
  889. messageSends: [],
  890. referencedClasses: []
  891. }),
  892. smalltalk.TestResult);
  893. smalltalk.addMethod(
  894. "_total_",
  895. smalltalk.method({
  896. selector: "total:",
  897. category: 'accessing',
  898. fn: function (aNumber){
  899. var self=this;
  900. self["@total"]=aNumber;
  901. return self},
  902. args: ["aNumber"],
  903. source: "total: aNumber\x0a\x09total := aNumber",
  904. messageSends: [],
  905. referencedClasses: []
  906. }),
  907. smalltalk.TestResult);
  908. smalltalk.addClass('TestSuiteRunner', smalltalk.Object, ['suite', 'result', 'announcer', 'runNextTest'], 'SUnit');
  909. smalltalk.addMethod(
  910. "_announcer",
  911. smalltalk.method({
  912. selector: "announcer",
  913. category: 'accessing',
  914. fn: function (){
  915. var self=this;
  916. var $1;
  917. $1=self["@announcer"];
  918. return $1;
  919. },
  920. args: [],
  921. source: "announcer\x0a\x09^announcer",
  922. messageSends: [],
  923. referencedClasses: []
  924. }),
  925. smalltalk.TestSuiteRunner);
  926. smalltalk.addMethod(
  927. "_contextOf_",
  928. smalltalk.method({
  929. selector: "contextOf:",
  930. category: 'private',
  931. fn: function (anInteger){
  932. var self=this;
  933. var $1;
  934. $1=smalltalk.send((smalltalk.ReportingTestContext || ReportingTestContext),"_testCase_result_finished_",[smalltalk.send(self["@suite"],"_at_",[anInteger]),self["@result"],(function(){
  935. return smalltalk.send(self,"_resume",[]);
  936. })]);
  937. return $1;
  938. },
  939. args: ["anInteger"],
  940. source: "contextOf: anInteger\x0a \x09^ReportingTestContext testCase: (suite at: anInteger) result: result finished: [ self resume ]\x0a",
  941. messageSends: ["testCase:result:finished:", "at:", "resume"],
  942. referencedClasses: ["ReportingTestContext"]
  943. }),
  944. smalltalk.TestSuiteRunner);
  945. smalltalk.addMethod(
  946. "_initialize",
  947. smalltalk.method({
  948. selector: "initialize",
  949. category: 'initialization',
  950. fn: function (){
  951. var self=this;
  952. var $1;
  953. smalltalk.send(self,"_initialize",[],smalltalk.Object);
  954. self["@announcer"]=smalltalk.send((smalltalk.Announcer || Announcer),"_new",[]);
  955. self["@result"]=smalltalk.send((smalltalk.TestResult || TestResult),"_new",[]);
  956. self["@runNextTest"]=(function(){
  957. var runs;
  958. runs=smalltalk.send(self["@result"],"_runs",[]);
  959. runs;
  960. $1=smalltalk.send(runs,"__lt",[smalltalk.send(self["@result"],"_total",[])]);
  961. if(smalltalk.assert($1)){
  962. return smalltalk.send(smalltalk.send(self,"_contextOf_",[smalltalk.send(runs,"__plus",[(1)])]),"_start",[]);
  963. };
  964. });
  965. return self},
  966. args: [],
  967. source: "initialize\x0a\x09super initialize.\x0a\x09announcer := Announcer new.\x0a result := TestResult new.\x0a runNextTest := [ | runs | runs := result runs. runs < result total ifTrue: [ (self contextOf: runs + 1) start ]].\x0a",
  968. messageSends: ["initialize", "new", "runs", "ifTrue:", "start", "contextOf:", "+", "<", "total"],
  969. referencedClasses: ["Announcer", "TestResult"]
  970. }),
  971. smalltalk.TestSuiteRunner);
  972. smalltalk.addMethod(
  973. "_result",
  974. smalltalk.method({
  975. selector: "result",
  976. category: 'accessing',
  977. fn: function (){
  978. var self=this;
  979. var $1;
  980. $1=self["@result"];
  981. return $1;
  982. },
  983. args: [],
  984. source: "result\x0a\x09^result",
  985. messageSends: [],
  986. referencedClasses: []
  987. }),
  988. smalltalk.TestSuiteRunner);
  989. smalltalk.addMethod(
  990. "_resume",
  991. smalltalk.method({
  992. selector: "resume",
  993. category: 'actions',
  994. fn: function (){
  995. var self=this;
  996. smalltalk.send(self["@runNextTest"],"_fork",[]);
  997. smalltalk.send(self["@announcer"],"_announce_",[smalltalk.send(smalltalk.send((smalltalk.ResultAnnouncement || ResultAnnouncement),"_new",[]),"_result_",[self["@result"]])]);
  998. return self},
  999. args: [],
  1000. source: "resume\x0a\x09runNextTest fork.\x0a announcer announce: (ResultAnnouncement new result: result)\x0a",
  1001. messageSends: ["fork", "announce:", "result:", "new"],
  1002. referencedClasses: ["ResultAnnouncement"]
  1003. }),
  1004. smalltalk.TestSuiteRunner);
  1005. smalltalk.addMethod(
  1006. "_run",
  1007. smalltalk.method({
  1008. selector: "run",
  1009. category: 'actions',
  1010. fn: function (){
  1011. var self=this;
  1012. smalltalk.send(self["@result"],"_total_",[smalltalk.send(self["@suite"],"_size",[])]);
  1013. smalltalk.send(self,"_resume",[]);
  1014. return self},
  1015. args: [],
  1016. source: "run\x0a\x09result total: suite size.\x0a\x09self resume",
  1017. messageSends: ["total:", "size", "resume"],
  1018. referencedClasses: []
  1019. }),
  1020. smalltalk.TestSuiteRunner);
  1021. smalltalk.addMethod(
  1022. "_suite_",
  1023. smalltalk.method({
  1024. selector: "suite:",
  1025. category: 'accessing',
  1026. fn: function (aCollection){
  1027. var self=this;
  1028. self["@suite"]=aCollection;
  1029. return self},
  1030. args: ["aCollection"],
  1031. source: "suite: aCollection\x0a\x09suite := aCollection",
  1032. messageSends: [],
  1033. referencedClasses: []
  1034. }),
  1035. smalltalk.TestSuiteRunner);
  1036. smalltalk.addMethod(
  1037. "_new",
  1038. smalltalk.method({
  1039. selector: "new",
  1040. category: 'instance creation',
  1041. fn: function (){
  1042. var self=this;
  1043. smalltalk.send(self,"_shouldNotImplement",[]);
  1044. return self},
  1045. args: [],
  1046. source: "new\x0a\x09self shouldNotImplement",
  1047. messageSends: ["shouldNotImplement"],
  1048. referencedClasses: []
  1049. }),
  1050. smalltalk.TestSuiteRunner.klass);
  1051. smalltalk.addMethod(
  1052. "_on_",
  1053. smalltalk.method({
  1054. selector: "on:",
  1055. category: 'instance creation',
  1056. fn: function (aCollection){
  1057. var self=this;
  1058. var $1;
  1059. $1=smalltalk.send(smalltalk.send(self,"_new",[],smalltalk.Object.klass),"_suite_",[aCollection]);
  1060. return $1;
  1061. },
  1062. args: ["aCollection"],
  1063. source: "on: aCollection\x0a\x09^super new suite: aCollection",
  1064. messageSends: ["suite:", "new"],
  1065. referencedClasses: []
  1066. }),
  1067. smalltalk.TestSuiteRunner.klass);