SUnit.js 16 KB

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