From 91593a653d643e9492e791894dd44b4bf3735596 Mon Sep 17 00:00:00 2001 From: Tom ten Thij Date: Wed, 14 May 2008 07:08:47 +0100 Subject: [PATCH] Adding some functionality to rails-lib, experimenting with elisp BDD framework, first test driven bug fix I felt that tests were missing from the emacs rails package so I have been experimenting a bit. There is now a behave-rails.el file which contains contexts that can be used by Technomancy's behave BDD framework. Although behave is old and no longer maintained it has some nice features. I may go for a different framework in the end. To use behave you can get it from git://github.com/tomtt/elisp_behave.git or alternatively clone my entire emacslisp dir from git://github.com/tomtt/tomtt_emacslisp.git. The most interesting other framework out there seems to be el-expectations: http://www.emacswiki.org/cgi-bin/wiki/EmacsLispExpectations --- behave-rails.el | 44 ++++++++++++++++++++++++++++++++++++++++++++ rails-lib.el | 17 ++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 behave-rails.el diff --git a/behave-rails.el b/behave-rails.el new file mode 100644 index 0000000..ba2e13a --- /dev/null +++ b/behave-rails.el @@ -0,0 +1,44 @@ +(require 'behave) + +(context "Testing if string is camelized" + (tag rails rails-lib camelized-p) + (specify "should be nil if there are no capital characters" + (expect (camelized-p "horse") equal nil)) + (specify "should be 0 for a word with first character being a capital" + (expect (camelized-p "Horse") equal 0)) + (specify "should be nil if the first character is not a capital" + (expect (camelized-p "dragonFly") equal nil)) + (specify "should be 0 if there are multiple capitals" + (expect (camelized-p "DragonFly") equal 0)) + (specify "should be 0 if it has numbers" + (expect (camelized-p "DragonFly69") equal 0)) + (specify "should be nil if all its characters are capital" + (expect (camelized-p "DONKEY") equal nil))) + +(context "Testing if string is underscored" + (tag rails rails-lib underscored-p) + (specify "should be nil if there is a capital character" + (expect (underscored-p "horSe") equal nil)) + (specify "should be 0 if all characters are lowercase" + (expect (underscored-p "horse") equal 0)) + (specify "should be 0 if some characters are numbers" + (expect (underscored-p "horse12") equal 0)) + (specify "should be 0 if some characters are underscores" + (expect (underscored-p "dragon_fly") equal 0)) + (specify "should be nil if the first character is not a letter" + (expect (underscored-p "5_gold_rings") equal nil)) +) + +(context "Decamelizing" + (tag rails rails-lib decamelize) + (specify "should return a string of all lower case characters unchanged" + (expect (decamelize "horse") equal "horse")) + (specify "should replace an initial capital with a lower case character" + (expect (decamelize "Horse") equal "horse")) + (specify "should insert underscores before capital letters" + (expect (decamelize "AntEaterTongue") equal "ant_eater_tongue")) + (specify "should insert one underscore after last character of serie of capitals" + (expect (decamelize "SMSMessage") equal "sms_message")) + (specify "should insert an underscore between a digit and a capital" + (expect (decamelize "Dalmatien101Movie") equal "dalmatien101_movie")) +) diff --git a/rails-lib.el b/rails-lib.el index 4f3f2f6..d18e95f 100644 --- a/rails-lib.el +++ b/rails-lib.el @@ -109,7 +109,7 @@ If EXPR is not nil exeutes BODY. (replace-regexp-in-string "\\([A-Z]+\\)\\([A-Z][a-z]\\)" "\\1_\\2" (replace-regexp-in-string - "\\([a-z\\d]\\)\\([A-Z]\\)" "\\1_\\2" + "\\([a-z0-9]\\)\\([A-Z]\\)" "\\1_\\2" string))))) (defun string-not-empty (str) ;(+) @@ -346,6 +346,21 @@ as the value of the symbol, and the hook as the function definition." history ; hist (car history-value))))) ;def +;; railsy-replace +(defun camelized-p (string) + "Return nil unless string is in camelized format (first character is capital, there is at least on lower capital and all characters are letters of numbers" + (let ((case-fold-search nil)) + (string-match "^[A-Z][A-Za-z0-9]*[a-z]+[A-Za-z0-9]*$" string))) + +(defun underscored-p (string) + "Return nil unless string is in underscored format (containing only lower case characters, numbers or underscores)" + (let ((case-fold-search nil)) + (string-match "^[a-z][a-z0-9_]*$" string))) + +(defun replace-rails-variable () + (interactive) +) + ;; MMM ;; (defvar mmm-indent-sandbox-finish-position nil)