PageRenderTime 74ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/local-pkgs/ruby-templates.el

http://github.com/jimweirich/emacs-setup
Emacs Lisp | 561 lines | 446 code | 93 blank | 22 comment | 3 complexity | 84f1b9bff9c9f3c9128bd256d0c37041 MD5 | raw file
Possible License(s): GPL-2.0
  1. ;;; *****************************************************************
  2. ;;; Ruby Code Templates
  3. ;;; Date: 3/Dec/98
  4. ;;; Author: Jim Weirich
  5. ;;; Purpose: This file provides several templates for Ruby programming.
  6. ;;; *****************************************************************
  7. (provide 'ruby-templates)
  8. (require 'jw-templates)
  9. (setq tempo-interactive t)
  10. ;;; ==================================================================
  11. ;;; Ruby Specific helper functions
  12. ;;; ==================================================================
  13. ;;; ==================================================================
  14. ;;; Declare template package. Name should be a the major mode that
  15. ;;; gets the C-C I binding.
  16. ;;; ==================================================================
  17. (jw-template-package "ruby")
  18. ;;; ==================================================================
  19. ;;; Templates
  20. ;;; ==================================================================
  21. (setq jw-ruby-copyright-list
  22. '(
  23. "# Copyright " (current-year-string) " by " (jw-get-author-name)
  24. " (" (jw-get-author-email) "). All rights reserved." n
  25. "# Permission is granted for use, modification and distribution as" n
  26. "# long as the above copyright notice is included." n
  27. ))
  28. (jw-template "copyright" jw-ruby-copyright-list)
  29. (defun jw-ruby-file-start ()
  30. (if (string-match "\\.rb$" (buffer-file-name))
  31. '("#!/usr/bin/env ruby" n n)
  32. '("#!/usr/bin/env ruby" n
  33. "# -*- ruby -*-" n n) ))
  34. (setq jw-test-case-code
  35. '(
  36. "class " (p "Test Class Name: " tcname) " < Test::Unit::TestCase" n
  37. n
  38. " def test_initial_conditions" n
  39. p " assert false" n
  40. " end" n
  41. "end" n
  42. n
  43. ))
  44. (jw-template
  45. "test-file"
  46. (append
  47. (jw-ruby-file-start)
  48. '(
  49. "require 'test/unit'" n
  50. n
  51. )
  52. jw-test-case-code) )
  53. ;;; Testing ==========================================================
  54. (jw-template
  55. "test-case"
  56. jw-test-case-code )
  57. (jw-template
  58. "def-test"
  59. '(
  60. > "def test_" (p "Test Name: ") n
  61. > p > n
  62. "end" > n
  63. n
  64. )
  65. )
  66. (jw-template
  67. "assert-block"
  68. '( > "assert_block do" n
  69. > p n
  70. "end" > n
  71. )
  72. )
  73. (jw-template
  74. "assert-true"
  75. '( > "assert " (p "Condition: ") > )
  76. )
  77. (jw-template
  78. "assert-equal"
  79. '( > "assert_equal " (p "Expected: ") ", " (p "Actual: ") > )
  80. )
  81. (jw-template
  82. "assert-raise"
  83. '( > "assert_raise " (p "Exception: ") " do" > n
  84. > p n
  85. "end" > n
  86. )
  87. )
  88. (jw-template
  89. "assert-instance-of"
  90. '( > "assert_instance_of " (p "Class: ") ", " (p "Actual: ") > )
  91. )
  92. (jw-template
  93. "assert-nil"
  94. '( > "assert_nil " (p "Actual: ") > )
  95. )
  96. (jw-template
  97. "assert-kind-of"
  98. '( > "assert_kind_of " (p "Root class: ") ", " (p "Actual: ") > )
  99. )
  100. (jw-template
  101. "assert-respond-to"
  102. '( > "assert_respond_to " (p "Object: ") ", :" (p "Method: :") > )
  103. )
  104. (jw-template
  105. "assert-match"
  106. '( > "assert_match /" (p "Pattern: /") "/, " (p "Actual: ") > )
  107. )
  108. (jw-template
  109. "assert-same"
  110. '( > "assert_same " (p "Expected: ") ", " (p "Actual: ") > )
  111. )
  112. (jw-template
  113. "assert-operator"
  114. '( > "assert_operator " (p "Expected: ") ", :" (p "Operator: :") ", " (p "Actual: ") > )
  115. )
  116. (jw-template
  117. "assert-nothing-raised"
  118. '( > "assert_nothing_raised do" > n
  119. > p n
  120. "end" > n
  121. )
  122. )
  123. (jw-template
  124. "flunk"
  125. '( > "flunk" > )
  126. )
  127. (jw-template
  128. "assert-not-same"
  129. '( > "assert_not_same " (p "Expected: ") ", " (p "Actual: ") > )
  130. )
  131. (jw-template
  132. "assert-not-equal"
  133. '( > "assert_not_equal " (p "Expected: ") ", " (p "Actual: ") > )
  134. )
  135. (jw-template
  136. "assert-not-nil"
  137. '( > "assert_not_nil " (p "Actual: ") > )
  138. )
  139. (jw-template
  140. "assert-not-match"
  141. '( > "assert_no_match /" (p "Pattern: /") "/, " (p "Actual: ") > )
  142. )
  143. (jw-template
  144. "assert-throws"
  145. '( > "assert_throws :" (p "Expected symbol: :") ", do " > n
  146. > p n
  147. "end" > n
  148. )
  149. )
  150. (jw-template
  151. "assert-not-thrown"
  152. '( > "assert_nothing_thrown do " > n
  153. > p n
  154. "end" > n
  155. )
  156. )
  157. (jw-template
  158. "assert-in-delta"
  159. '( > "assert_in_delta " (p "Expected float: ") ", " (p "Actual float: ") ", " (p "Delta: ") > )
  160. )
  161. (jw-template
  162. "assert-send"
  163. '( > "assert_send [" (p "Receiver: ")
  164. ", :" (p "Method symbol: :")
  165. ", " (p "List of args (comma separated): ") "]" >
  166. )
  167. )
  168. ;;; RSpec ============================================================
  169. (jw-template
  170. "describe-description"
  171. '( > "describe \"" (p "Context: \"") "\" do" > n
  172. > p n
  173. "end" > n
  174. )
  175. )
  176. (jw-template
  177. "describe-class"
  178. '( > "describe " (p "Class: ") ", \"" (p "When: when \"") "\" do" > n
  179. > p n
  180. "end" > n
  181. )
  182. )
  183. (jw-template
  184. "describe-shared"
  185. '( > "describe \"" (p "Shared behavior name: \"") "\", :shared => true do" > n
  186. > p n
  187. "end" > n
  188. )
  189. )
  190. (jw-template
  191. "before-each"
  192. '( > "before(:each) do" > n
  193. > p n
  194. "end" > n
  195. )
  196. )
  197. (jw-template
  198. "before-all"
  199. '( > "before(:all) do" > n
  200. > p n
  201. "end" > n
  202. )
  203. )
  204. (jw-template
  205. "before-all"
  206. '( > "before(:all) do" > n
  207. > p n
  208. "end" > n
  209. )
  210. )
  211. (jw-template
  212. "after-each"
  213. '( > "after(:each) do" > n
  214. > p n
  215. "end" > n
  216. )
  217. )
  218. (jw-template
  219. "after-all"
  220. '( > "after(:all) do" > n
  221. > p n
  222. "end" > n
  223. )
  224. )
  225. (jw-template
  226. "it-should"
  227. '( > "it \"should " (p "Description: it \"should ") "\" do" > n
  228. > p n
  229. "end" > n
  230. )
  231. )
  232. (jw-template
  233. "it-should-behave-like"
  234. '( > "it_should_behave_like \"" (p "Shared behavior: \"") "\"" > )
  235. )
  236. (jw-template
  237. "should-be"
  238. '( > (p "Target: ") ".should be_" (p "Predicate: ") > )
  239. )
  240. (jw-template
  241. "should-not-be"
  242. '( > (p "Target: ") ".should_not be_" (p "Predicate: ") > )
  243. )
  244. (jw-template
  245. "should-have"
  246. '( > (p "Target: ") ".should have_" (p "Predicate: have_") > )
  247. )
  248. (jw-template
  249. "should-not-have"
  250. '( > (p "Target: ") ".should_not have_" (p "Predicate: have_") > )
  251. )
  252. (jw-template
  253. "should-be-true"
  254. '( > (p "Target: ") ".should be_true" > )
  255. )
  256. (jw-template
  257. "should-be-false"
  258. '( > (p "Target: ") ".should be_false" > )
  259. )
  260. (jw-template
  261. "should-be-nil"
  262. '( > (p "Target: ") ".should be_nil" > )
  263. )
  264. (jw-template
  265. "should-not-be-nil"
  266. '( > (p "Target: ") ".should_not be_nil" > )
  267. )
  268. (jw-template
  269. "should-be-close"
  270. '( > (p "Target: ") ".should be_close(" (p "Expected float: ") ", " (p "Delta: ") ")" > )
  271. )
  272. (jw-template
  273. "should-not-be-close"
  274. '( > (p "Target: ") ".should_not be_close(" (p "Expected float: ") ", " (p "Delta: ") ")" > )
  275. )
  276. (jw-template
  277. "should-change"
  278. '( > "lambda {" > n
  279. (p "Statment: ") > n
  280. > "}.should change(" (p "Change target: ") ", :" (p "Attribute symbol: :")
  281. ").from(" (p "From: ") ").to(" (p "To: ") ")" > n
  282. )
  283. )
  284. (jw-template
  285. "should-not-change"
  286. '( > "lambda {" > n
  287. (p "Statment: ") > n
  288. > "}.should_not change(" (p "Change target: ") ", :" (p "Attribute symbol: :") ")" > n
  289. )
  290. )
  291. (jw-template
  292. "should-="
  293. '( > (p "Target: ") ".should == " (p "Expected value: ") > )
  294. )
  295. (jw-template
  296. "should-equal-value"
  297. '( > (p "Target: ") ".should eql(" (p "Expected value: ") ")" > )
  298. )
  299. (jw-template
  300. "should-not-equal-value"
  301. '( > (p "Target: ") ".should_not eql(" (p "Expected value: ") ")" > )
  302. )
  303. (jw-template
  304. "should-be-same-object"
  305. '( > (p "Target: ") ".should equal(" (p "Expected object: ") ")" > )
  306. )
  307. (jw-template
  308. "should-not-be-same-object"
  309. '( > (p "Target: ") ".should_not equal(" (p "Expected object: ") ")" > )
  310. )
  311. (jw-template
  312. "should-have-items"
  313. '( > (p "Target: ") ".should have(" (p "Expected count: ") ")." (p "Item name: ") > )
  314. )
  315. (jw-template
  316. "should-not-have-items"
  317. '( > (p "Target: ") ".should_not have(" (p "Expected count: ") ")." (p "Item name: ") > )
  318. )
  319. (jw-template
  320. "should-have-at-least-items"
  321. '( > (p "Target: ") ".should have_at_least(" (p "Minimum count: ") ")." (p "Item name: ") > )
  322. )
  323. (jw-template
  324. "should-have-at-most-items"
  325. '( > (p "Target: ") ".should have_at_most(" (p "Maximum count: ") ")." (p "Item name: ") > )
  326. )
  327. (jw-template
  328. "should-include"
  329. '( > (p "Target: ") ".should include(" (p "Should include: ") ")" > )
  330. )
  331. (jw-template
  332. "should-not-include"
  333. '( > (p "Target: ") ".should_not include(" (p "Should not include: ") ")" > )
  334. )
  335. (jw-template
  336. "should-match"
  337. '( > (p "Target: ") ".should match(/" (p "Should match: /") "/)" > )
  338. )
  339. (jw-template
  340. "should-not-match"
  341. '( > (p "Target: ") ".should_not match(/" (p "Should not match: /") "/)" > )
  342. )
  343. (jw-template
  344. "should-raise"
  345. '( > "lambda {" > n
  346. > (p "Target statement: ") > n
  347. > "}.should raise(" (p "Expected exception: ")
  348. ", " (p "Message string or regex: ") ")" > )
  349. )
  350. (jw-template
  351. "should-not-raise"
  352. '( > "lambda {" > n
  353. > (p "Target statement: ") > n
  354. > "}.should raise(" (p "Expected exception: ") ")" > )
  355. )
  356. (jw-template
  357. "should-respond-to"
  358. '( > (p "Target: ") ".should respond_to(:" (p "Method name(s): :") ")" > )
  359. )
  360. (jw-template
  361. "should-not-respond-to"
  362. '( > (p "Target: ") ".should_not respond_to(:" (p "Method name(s): :") ")" > )
  363. )
  364. (jw-template
  365. "should-satisfy"
  366. '( > (p "Target: ") ".should satisfy { |" (p "Args: ") "|" > n
  367. > (p "Condition: ") > n
  368. > "}" > n
  369. )
  370. )
  371. (jw-template
  372. "should-not-satisfy"
  373. '( > (p "Target: ") ".should_not satisfy { |" (p "Args: ") "|" > n
  374. > (p "Condition: ") > n
  375. > "}" > n
  376. )
  377. )
  378. (jw-template
  379. "should-throw"
  380. '( > "lambda {" > n
  381. > (p "Target statement: ") > n
  382. > "}.should throw_symbol(:" (p "Symbol: :") ")" > )
  383. )
  384. (jw-template
  385. "should-not-throw"
  386. '( > "lambda {" > n
  387. > (p "Target statement: ") > n
  388. > "}.should_not throw_symbol(:" (p "Symbol: :") ")" > )
  389. )
  390. ;;; File Structure ===================================================
  391. (setq jw-ruby-main-test
  392. '(
  393. "if __FILE__ == $0 then" n
  394. " main(ARGV)" n
  395. "end" n
  396. ))
  397. (jw-template "preface" (jw-ruby-file-start))
  398. (jw-template "ifmain" jw-ruby-main-test)
  399. (jw-template
  400. "mainfile"
  401. (append
  402. (jw-ruby-file-start)
  403. '(
  404. n
  405. "# Main Program =======================================================" n
  406. n
  407. "def main(args)" n
  408. "end" n
  409. n
  410. )
  411. jw-ruby-main-test
  412. )
  413. )
  414. ;;; Debugging ========================================================
  415. (jw-template
  416. "breakpoint"
  417. '( "require 'breakpoint'; breakpoint" > n ))
  418. ;;; Objects and classes ==============================================
  419. (jw-template
  420. "class"
  421. '(
  422. n
  423. "class " (p "Class Name: ") n
  424. p n
  425. "end" n
  426. n
  427. n
  428. )
  429. )
  430. ;;; Option Parsing ===================================================
  431. (jw-template
  432. "optparse"
  433. '(
  434. "def handle_options(args)" > n>
  435. "options = OpenStruct.new" > n>
  436. n
  437. "opts = OptionParser.new do |opts|" > n>
  438. "opts.banner = \"Usage: #$0 [options]\"" > n>
  439. "opts.separator \"\"" > n>
  440. "opts.separator \"Where:\"" > n>
  441. n
  442. "opts.on_tail('-h', '--help', 'Show this message') do" > n>
  443. "puts opts" > n>
  444. "exit" > n>
  445. "end " > n>
  446. n
  447. "opts.on_tail('--version', 'Show version') do" > n>
  448. "puts \"Version: #{" (p "Version Expression: ") "}\"" > n>
  449. "exit" > n>
  450. "end " > n>
  451. "end " > n>
  452. n
  453. "opts.parse!(args)" > n
  454. "options" > n>
  455. "end " > n>
  456. )
  457. )
  458. (jw-template
  459. "option"
  460. '(
  461. "opts.on('-" (p "Option Char: ") "', "
  462. "'--" (p "Option Name: " name) "', "
  463. "'" (p "Description: " desc) "') do |value|" > n>
  464. " options." (s name) " = value" > n>
  465. " # Handle option" > n>
  466. "end " > n>
  467. )
  468. )