Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save futuremint/98744 to your computer and use it in GitHub Desktop.
Save futuremint/98744 to your computer and use it in GitHub Desktop.
Translates RSpec to Shoulda in your current Emacs buffer (tested in GNU Emacs only)
(defun rspec-to-shoulda ()
"Translates RSpec code to Shoulda
NOTE: You still have to wrap the results in a Test::Unit class"
(interactive)
(dolist (pair '(("\\(require\s.*/\\)\\([a-z_]+\\)'$" "\\1test_helper'") ;; require
("describe\s" "context ") ;; RSpec -> Shoulda syntax
("it\s" "should ")
("\s\"should\s" " \"")
("before\s" "setup ")
("before(:each)\s" "setup ")
("\\(^\s+\\)\\([@a-zA-Z1-9\\._=\\?]+\\)\\.should\s==\s\\(.*\\)\s$" "\\1assert_equals(\\3, \\2)") ;; == matcher
("\\(^\s+\\)\\([@a-zA-Z1-9\\._=\\?]+\\)\\.should\sbe_?(?\\([a-zA-Z1-9_]+\\))?\s$" "\\1assert \\2.\\3?") ;; be_p matcher
("\\.should_receive" ".expects") ;; Flexmock -> Mocha
("\\.should_not_receive(\\(:[a-zA-Z1-9_=\\?]+\\))" ".expects(\\1).never")
("\\.stub!" ".stubs")
("\\.and_return" ".returns")))
(save-excursion
(while (re-search-forward (car pair) nil t)
(replace-match (cadr pair))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment