SUnit.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. smalltalk.addPackage('SUnit', {});
  2. smalltalk.addClass('TestCase', smalltalk.Object, ['testSelector'], 'SUnit');
  3. smalltalk.addMethod(
  4. "_assert_",
  5. smalltalk.method({
  6. selector: "assert:",
  7. category: 'testing',
  8. fn: function (aBoolean){
  9. var self=this;
  10. smalltalk.send(self,"_assert_description_",[aBoolean,"Assertion failed"]);
  11. return self},
  12. args: ["aBoolean"],
  13. source: "assert: aBoolean\x0a\x09self assert: aBoolean description: 'Assertion failed'",
  14. messageSends: ["assert:description:"],
  15. referencedClasses: []
  16. }),
  17. smalltalk.TestCase);
  18. smalltalk.addMethod(
  19. "_assert_description_",
  20. smalltalk.method({
  21. selector: "assert:description:",
  22. category: 'testing',
  23. fn: function (aBoolean,aString){
  24. var self=this;
  25. if(! smalltalk.assert(aBoolean)){
  26. smalltalk.send(self,"_signalFailure_",[aString]);
  27. };
  28. return self},
  29. args: ["aBoolean", "aString"],
  30. source: "assert: aBoolean description: aString\x0a\x09aBoolean ifFalse: [self signalFailure: aString]",
  31. messageSends: ["ifFalse:", "signalFailure:"],
  32. referencedClasses: []
  33. }),
  34. smalltalk.TestCase);
  35. smalltalk.addMethod(
  36. "_assert_equals_",
  37. smalltalk.method({
  38. selector: "assert:equals:",
  39. category: 'testing',
  40. fn: function (expected,actual){
  41. var self=this;
  42. var $1;
  43. $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",[])])]);
  44. return $1;
  45. },
  46. args: ["expected", "actual"],
  47. source: "assert: expected equals: actual\x0a\x09^ self assert: (expected = actual) description: 'Expected: ', expected asString, ' but was: ', actual asString",
  48. messageSends: ["assert:description:", "=", ",", "asString"],
  49. referencedClasses: []
  50. }),
  51. smalltalk.TestCase);
  52. smalltalk.addMethod(
  53. "_deny_",
  54. smalltalk.method({
  55. selector: "deny:",
  56. category: 'testing',
  57. fn: function (aBoolean){
  58. var self=this;
  59. smalltalk.send(self,"_assert_",[smalltalk.send(aBoolean,"_not",[])]);
  60. return self},
  61. args: ["aBoolean"],
  62. source: "deny: aBoolean\x0a\x09self assert: aBoolean not",
  63. messageSends: ["assert:", "not"],
  64. referencedClasses: []
  65. }),
  66. smalltalk.TestCase);
  67. smalltalk.addMethod(
  68. "_performTestFor_",
  69. smalltalk.method({
  70. selector: "performTestFor:",
  71. category: 'running',
  72. fn: function (aResult){
  73. var self=this;
  74. smalltalk.send((function(){
  75. return smalltalk.send((function(){
  76. return smalltalk.send(self,"_perform_",[smalltalk.send(self,"_selector",[])]);
  77. }),"_on_do_",[(smalltalk.TestFailure || TestFailure),(function(ex){
  78. return smalltalk.send(aResult,"_addFailure_",[self]);
  79. })]);
  80. }),"_on_do_",[(smalltalk.Error || Error),(function(ex){
  81. return smalltalk.send(aResult,"_addError_",[self]);
  82. })]);
  83. return self},
  84. args: ["aResult"],
  85. source: "performTestFor: aResult\x0a\x09[[self perform: self selector]\x0a\x09\x09on: TestFailure do: [:ex | aResult addFailure: self]]\x0a\x09\x09on: Error do: [:ex | aResult addError: self]",
  86. messageSends: ["on:do:", "addError:", "addFailure:", "perform:", "selector"],
  87. referencedClasses: ["Error", "TestFailure"]
  88. }),
  89. smalltalk.TestCase);
  90. smalltalk.addMethod(
  91. "_runCaseFor_",
  92. smalltalk.method({
  93. selector: "runCaseFor:",
  94. category: 'running',
  95. fn: function (aTestResult){
  96. var self=this;
  97. smalltalk.send(self,"_setUp",[]);
  98. smalltalk.send(aTestResult,"_increaseRuns",[]);
  99. smalltalk.send(self,"_performTestFor_",[aTestResult]);
  100. smalltalk.send(self,"_tearDown",[]);
  101. return self},
  102. args: ["aTestResult"],
  103. source: "runCaseFor: aTestResult\x0a\x09self setUp.\x0a\x09aTestResult increaseRuns.\x0a\x09self performTestFor: aTestResult.\x0a\x09self tearDown",
  104. messageSends: ["setUp", "increaseRuns", "performTestFor:", "tearDown"],
  105. referencedClasses: []
  106. }),
  107. smalltalk.TestCase);
  108. smalltalk.addMethod(
  109. "_selector",
  110. smalltalk.method({
  111. selector: "selector",
  112. category: 'accessing',
  113. fn: function (){
  114. var self=this;
  115. return self["@testSelector"];
  116. },
  117. args: [],
  118. source: "selector\x0a\x09^testSelector",
  119. messageSends: [],
  120. referencedClasses: []
  121. }),
  122. smalltalk.TestCase);
  123. smalltalk.addMethod(
  124. "_setTestSelector_",
  125. smalltalk.method({
  126. selector: "setTestSelector:",
  127. category: 'accessing',
  128. fn: function (aSelector){
  129. var self=this;
  130. self["@testSelector"]=aSelector;
  131. return self},
  132. args: ["aSelector"],
  133. source: "setTestSelector: aSelector\x0a\x09testSelector := aSelector",
  134. messageSends: [],
  135. referencedClasses: []
  136. }),
  137. smalltalk.TestCase);
  138. smalltalk.addMethod(
  139. "_setUp",
  140. smalltalk.method({
  141. selector: "setUp",
  142. category: 'running',
  143. fn: function (){
  144. var self=this;
  145. return self},
  146. args: [],
  147. source: "setUp",
  148. messageSends: [],
  149. referencedClasses: []
  150. }),
  151. smalltalk.TestCase);
  152. smalltalk.addMethod(
  153. "_should_",
  154. smalltalk.method({
  155. selector: "should:",
  156. category: 'testing',
  157. fn: function (aBlock){
  158. var self=this;
  159. smalltalk.send(self,"_assert_",[smalltalk.send(aBlock,"_value",[])]);
  160. return self},
  161. args: ["aBlock"],
  162. source: "should: aBlock\x0a\x09self assert: aBlock value",
  163. messageSends: ["assert:", "value"],
  164. referencedClasses: []
  165. }),
  166. smalltalk.TestCase);
  167. smalltalk.addMethod(
  168. "_should_raise_",
  169. smalltalk.method({
  170. selector: "should:raise:",
  171. category: 'testing',
  172. fn: function (aBlock,anExceptionClass){
  173. var self=this;
  174. smalltalk.send(self,"_assert_",[smalltalk.send((function(){
  175. smalltalk.send(aBlock,"_value",[]);
  176. return false;
  177. }),"_on_do_",[anExceptionClass,(function(ex){
  178. return true;
  179. })])]);
  180. return self},
  181. args: ["aBlock", "anExceptionClass"],
  182. source: "should: aBlock raise: anExceptionClass\x0a\x09self assert: ([aBlock value. false] \x0a\x09\x09on: anExceptionClass \x0a\x09\x09do: [:ex | true])",
  183. messageSends: ["assert:", "on:do:", "value"],
  184. referencedClasses: []
  185. }),
  186. smalltalk.TestCase);
  187. smalltalk.addMethod(
  188. "_shouldnt_raise_",
  189. smalltalk.method({
  190. selector: "shouldnt:raise:",
  191. category: 'testing',
  192. fn: function (aBlock,anExceptionClass){
  193. var self=this;
  194. smalltalk.send(self,"_assert_",[smalltalk.send((function(){
  195. smalltalk.send(aBlock,"_value",[]);
  196. return true;
  197. }),"_on_do_",[anExceptionClass,(function(ex){
  198. return false;
  199. })])]);
  200. return self},
  201. args: ["aBlock", "anExceptionClass"],
  202. source: "shouldnt: aBlock raise: anExceptionClass\x0a\x09self assert: ([aBlock value. true] \x0a\x09\x09on: anExceptionClass \x0a\x09\x09do: [:ex | false])",
  203. messageSends: ["assert:", "on:do:", "value"],
  204. referencedClasses: []
  205. }),
  206. smalltalk.TestCase);
  207. smalltalk.addMethod(
  208. "_signalFailure_",
  209. smalltalk.method({
  210. selector: "signalFailure:",
  211. category: 'private',
  212. fn: function (aString){
  213. var self=this;
  214. var $1,$2;
  215. $1=smalltalk.send((smalltalk.TestFailure || TestFailure),"_new",[]);
  216. smalltalk.send($1,"_messageText_",[aString]);
  217. $2=smalltalk.send($1,"_signal",[]);
  218. return self},
  219. args: ["aString"],
  220. source: "signalFailure: aString\x0a\x09TestFailure new\x0a\x09\x09messageText: aString;\x0a\x09\x09signal",
  221. messageSends: ["messageText:", "new", "signal"],
  222. referencedClasses: ["TestFailure"]
  223. }),
  224. smalltalk.TestCase);
  225. smalltalk.addMethod(
  226. "_tearDown",
  227. smalltalk.method({
  228. selector: "tearDown",
  229. category: 'running',
  230. fn: function (){
  231. var self=this;
  232. return self},
  233. args: [],
  234. source: "tearDown",
  235. messageSends: [],
  236. referencedClasses: []
  237. }),
  238. smalltalk.TestCase);
  239. smalltalk.addMethod(
  240. "_allTestSelectors",
  241. smalltalk.method({
  242. selector: "allTestSelectors",
  243. category: 'accessing',
  244. fn: function (){
  245. var self=this;
  246. var $1;
  247. var selectors;
  248. selectors=smalltalk.send(self,"_testSelectors",[]);
  249. $1=smalltalk.send(self,"_shouldInheritSelectors",[]);
  250. if(smalltalk.assert($1)){
  251. smalltalk.send(selectors,"_addAll_",[smalltalk.send(smalltalk.send(self,"_superclass",[]),"_allTestSelectors",[])]);
  252. };
  253. return selectors;
  254. },
  255. args: [],
  256. source: "allTestSelectors\x0a\x09| selectors |\x0a\x09selectors := self testSelectors.\x0a\x09self shouldInheritSelectors ifTrue: [\x0a\x09\x09selectors addAll: self superclass allTestSelectors].\x0a\x09^selectors",
  257. messageSends: ["testSelectors", "ifTrue:", "addAll:", "allTestSelectors", "superclass", "shouldInheritSelectors"],
  258. referencedClasses: []
  259. }),
  260. smalltalk.TestCase.klass);
  261. smalltalk.addMethod(
  262. "_buildSuite",
  263. smalltalk.method({
  264. selector: "buildSuite",
  265. category: 'accessing',
  266. fn: function (){
  267. var self=this;
  268. var $1;
  269. $1=smalltalk.send(smalltalk.send(self,"_allTestSelectors",[]),"_collect_",[(function(each){
  270. return smalltalk.send(self,"_selector_",[each]);
  271. })]);
  272. return $1;
  273. },
  274. args: [],
  275. source: "buildSuite\x0a\x09^self allTestSelectors collect: [:each | self selector: each]",
  276. messageSends: ["collect:", "selector:", "allTestSelectors"],
  277. referencedClasses: []
  278. }),
  279. smalltalk.TestCase.klass);
  280. smalltalk.addMethod(
  281. "_isAbstract",
  282. smalltalk.method({
  283. selector: "isAbstract",
  284. category: 'testing',
  285. fn: function (){
  286. var self=this;
  287. var $1;
  288. $1=smalltalk.send(smalltalk.send(self,"_name",[]),"__eq",["TestCase"]);
  289. return $1;
  290. },
  291. args: [],
  292. source: "isAbstract\x0a\x09^ self name = 'TestCase'",
  293. messageSends: ["=", "name"],
  294. referencedClasses: []
  295. }),
  296. smalltalk.TestCase.klass);
  297. smalltalk.addMethod(
  298. "_lookupHierarchyRoot",
  299. smalltalk.method({
  300. selector: "lookupHierarchyRoot",
  301. category: 'accessing',
  302. fn: function (){
  303. var self=this;
  304. return (smalltalk.TestCase || TestCase);
  305. },
  306. args: [],
  307. source: "lookupHierarchyRoot\x0a\x09^TestCase",
  308. messageSends: [],
  309. referencedClasses: ["TestCase"]
  310. }),
  311. smalltalk.TestCase.klass);
  312. smalltalk.addMethod(
  313. "_selector_",
  314. smalltalk.method({
  315. selector: "selector:",
  316. category: 'accessing',
  317. fn: function (aSelector){
  318. var self=this;
  319. var $2,$3,$1;
  320. $2=smalltalk.send(self,"_new",[]);
  321. smalltalk.send($2,"_setTestSelector_",[aSelector]);
  322. $3=smalltalk.send($2,"_yourself",[]);
  323. $1=$3;
  324. return $1;
  325. },
  326. args: ["aSelector"],
  327. source: "selector: aSelector\x0a\x09^self new\x0a\x09\x09setTestSelector: aSelector;\x0a\x09\x09yourself",
  328. messageSends: ["setTestSelector:", "new", "yourself"],
  329. referencedClasses: []
  330. }),
  331. smalltalk.TestCase.klass);
  332. smalltalk.addMethod(
  333. "_shouldInheritSelectors",
  334. smalltalk.method({
  335. selector: "shouldInheritSelectors",
  336. category: 'testing',
  337. fn: function (){
  338. var self=this;
  339. var $1;
  340. $1=smalltalk.send(self,"_~_eq",[smalltalk.send(self,"_lookupHierarchyRoot",[])]);
  341. return $1;
  342. },
  343. args: [],
  344. source: "shouldInheritSelectors\x0a\x09^self ~= self lookupHierarchyRoot",
  345. messageSends: ["~=", "lookupHierarchyRoot"],
  346. referencedClasses: []
  347. }),
  348. smalltalk.TestCase.klass);
  349. smalltalk.addMethod(
  350. "_testSelectors",
  351. smalltalk.method({
  352. selector: "testSelectors",
  353. category: 'accessing',
  354. fn: function (){
  355. var self=this;
  356. var $1;
  357. $1=smalltalk.send(smalltalk.send(smalltalk.send(self,"_methodDictionary",[]),"_keys",[]),"_select_",[(function(each){
  358. return smalltalk.send(each,"_match_",["^test"]);
  359. })]);
  360. return $1;
  361. },
  362. args: [],
  363. source: "testSelectors\x0a\x09^self methodDictionary keys select: [:each | each match: '^test']",
  364. messageSends: ["select:", "match:", "keys", "methodDictionary"],
  365. referencedClasses: []
  366. }),
  367. smalltalk.TestCase.klass);
  368. smalltalk.addClass('TestFailure', smalltalk.Error, [], 'SUnit');
  369. smalltalk.addClass('TestResult', smalltalk.Object, ['timestamp', 'runs', 'errors', 'failures', 'total'], 'SUnit');
  370. smalltalk.addMethod(
  371. "_addError_",
  372. smalltalk.method({
  373. selector: "addError:",
  374. category: 'accessing',
  375. fn: function (anError){
  376. var self=this;
  377. smalltalk.send(smalltalk.send(self,"_errors",[]),"_add_",[anError]);
  378. return self},
  379. args: ["anError"],
  380. source: "addError: anError\x0a\x09self errors add: anError",
  381. messageSends: ["add:", "errors"],
  382. referencedClasses: []
  383. }),
  384. smalltalk.TestResult);
  385. smalltalk.addMethod(
  386. "_addFailure_",
  387. smalltalk.method({
  388. selector: "addFailure:",
  389. category: 'accessing',
  390. fn: function (aFailure){
  391. var self=this;
  392. smalltalk.send(smalltalk.send(self,"_failures",[]),"_add_",[aFailure]);
  393. return self},
  394. args: ["aFailure"],
  395. source: "addFailure: aFailure\x0a\x09self failures add: aFailure",
  396. messageSends: ["add:", "failures"],
  397. referencedClasses: []
  398. }),
  399. smalltalk.TestResult);
  400. smalltalk.addMethod(
  401. "_errors",
  402. smalltalk.method({
  403. selector: "errors",
  404. category: 'accessing',
  405. fn: function (){
  406. var self=this;
  407. return self["@errors"];
  408. },
  409. args: [],
  410. source: "errors\x0a\x09^errors",
  411. messageSends: [],
  412. referencedClasses: []
  413. }),
  414. smalltalk.TestResult);
  415. smalltalk.addMethod(
  416. "_failures",
  417. smalltalk.method({
  418. selector: "failures",
  419. category: 'accessing',
  420. fn: function (){
  421. var self=this;
  422. return self["@failures"];
  423. },
  424. args: [],
  425. source: "failures\x0a\x09^failures",
  426. messageSends: [],
  427. referencedClasses: []
  428. }),
  429. smalltalk.TestResult);
  430. smalltalk.addMethod(
  431. "_increaseRuns",
  432. smalltalk.method({
  433. selector: "increaseRuns",
  434. category: 'accessing',
  435. fn: function (){
  436. var self=this;
  437. self["@runs"]=smalltalk.send(self["@runs"],"__plus",[(1)]);
  438. return self},
  439. args: [],
  440. source: "increaseRuns\x0a\x09runs := runs + 1",
  441. messageSends: ["+"],
  442. referencedClasses: []
  443. }),
  444. smalltalk.TestResult);
  445. smalltalk.addMethod(
  446. "_initialize",
  447. smalltalk.method({
  448. selector: "initialize",
  449. category: 'initialization',
  450. fn: function (){
  451. var self=this;
  452. smalltalk.send(self,"_initialize",[],smalltalk.Object);
  453. self["@timestamp"]=smalltalk.send((smalltalk.Date || Date),"_now",[]);
  454. self["@runs"]=(0);
  455. self["@errors"]=smalltalk.send((smalltalk.Array || Array),"_new",[]);
  456. self["@failures"]=smalltalk.send((smalltalk.Array || Array),"_new",[]);
  457. self["@total"]=(0);
  458. return self},
  459. args: [],
  460. source: "initialize\x0a\x09super initialize.\x0a\x09timestamp := Date now.\x0a\x09runs := 0.\x0a\x09errors := Array new.\x0a\x09failures := Array new.\x0a\x09total := 0",
  461. messageSends: ["initialize", "now", "new"],
  462. referencedClasses: ["Date", "Array"]
  463. }),
  464. smalltalk.TestResult);
  465. smalltalk.addMethod(
  466. "_runs",
  467. smalltalk.method({
  468. selector: "runs",
  469. category: 'accessing',
  470. fn: function (){
  471. var self=this;
  472. return self["@runs"];
  473. },
  474. args: [],
  475. source: "runs\x0a\x09^runs",
  476. messageSends: [],
  477. referencedClasses: []
  478. }),
  479. smalltalk.TestResult);
  480. smalltalk.addMethod(
  481. "_status",
  482. smalltalk.method({
  483. selector: "status",
  484. category: 'accessing',
  485. fn: function (){
  486. var self=this;
  487. var $2,$3,$1;
  488. $2=smalltalk.send(smalltalk.send(self,"_errors",[]),"_isEmpty",[]);
  489. if(smalltalk.assert($2)){
  490. $3=smalltalk.send(smalltalk.send(self,"_failures",[]),"_isEmpty",[]);
  491. if(smalltalk.assert($3)){
  492. $1="success";
  493. } else {
  494. $1="failure";
  495. };
  496. } else {
  497. $1="error";
  498. };
  499. return $1;
  500. },
  501. args: [],
  502. 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']",
  503. messageSends: ["ifTrue:ifFalse:", "isEmpty", "failures", "errors"],
  504. referencedClasses: []
  505. }),
  506. smalltalk.TestResult);
  507. smalltalk.addMethod(
  508. "_timestamp",
  509. smalltalk.method({
  510. selector: "timestamp",
  511. category: 'accessing',
  512. fn: function (){
  513. var self=this;
  514. return self["@timestamp"];
  515. },
  516. args: [],
  517. source: "timestamp\x0a\x09^timestamp",
  518. messageSends: [],
  519. referencedClasses: []
  520. }),
  521. smalltalk.TestResult);
  522. smalltalk.addMethod(
  523. "_total",
  524. smalltalk.method({
  525. selector: "total",
  526. category: 'accessing',
  527. fn: function (){
  528. var self=this;
  529. return self["@total"];
  530. },
  531. args: [],
  532. source: "total\x0a\x09^total",
  533. messageSends: [],
  534. referencedClasses: []
  535. }),
  536. smalltalk.TestResult);
  537. smalltalk.addMethod(
  538. "_total_",
  539. smalltalk.method({
  540. selector: "total:",
  541. category: 'accessing',
  542. fn: function (aNumber){
  543. var self=this;
  544. self["@total"]=aNumber;
  545. return self},
  546. args: ["aNumber"],
  547. source: "total: aNumber\x0a\x09total := aNumber",
  548. messageSends: [],
  549. referencedClasses: []
  550. }),
  551. smalltalk.TestResult);