From 674bd952cc78ccd7ce3e2ed19413334f6dfb0643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Fayzrakhmanov=20=28=D0=90=D1=80=D1=82=D1=83=D1=80?= =?UTF-8?q?=20=D0=A4=D0=B0=D0=B9=D0=B7=D1=80=D0=B0=D1=85=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=29?= Date: Mon, 18 Jan 2016 02:28:20 +0500 Subject: [PATCH] Improve haskell-utils-reduce-string. Add tests --- haskell-utils.el | 13 +++++++------ tests/haskell-utils-tests.el | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/haskell-utils.el b/haskell-utils.el index bd919cafd..6f4f9a53d 100644 --- a/haskell-utils.el +++ b/haskell-utils.el @@ -100,12 +100,13 @@ execusion." (remove-hook 'post-command-hook #'haskell-utils-async-update-post-command-flag t))) -(defun haskell-utils-reduce-string (s) - "Remove newlines and extra whitespace from S. -Removes all extra whitespace at the beginning of each line leaving -only a single space. Then removes all newlines." - (let ((s_ (replace-regexp-in-string "^\s+" " " s))) - (replace-regexp-in-string "\n" "" s_))) +(defun haskell-utils-reduce-string (str) + "Remove newlines and extra whitespace from string STR. +If line starts with a sequence of whitespaces, substitutes this +sequence with a single whitespace. Removes all newline +characters." + (let ((s (replace-regexp-in-string "^\s+" " " str))) + (replace-regexp-in-string "\r?\n" "" s))) (defun haskell-utils-repl-response-error-status (response) "Parse response REPL's RESPONSE for errors. diff --git a/tests/haskell-utils-tests.el b/tests/haskell-utils-tests.el index 8738d84f5..0bbb02355 100644 --- a/tests/haskell-utils-tests.el +++ b/tests/haskell-utils-tests.el @@ -201,4 +201,28 @@ strings will change in future." (should (equal r6 'no-error)) (should (equal r7 'no-error)))) +(ert-deftest reduce-strign () + "Test `haskell-utils-reduce-strign' command. +Whitespace sequences at beginning of lines should be replaced +with single whitespace, all newline characters should be +removed." + (should (string-equal "" (haskell-utils-reduce-string "\n"))) + (should (string-equal "" (haskell-utils-reduce-string "\r\n"))) + (should (string-equal " " (haskell-utils-reduce-string " \n"))) + (should (string-equal + "TestTest" + (haskell-utils-reduce-string "Test\nTest"))) + (should (string-equal + "Test Test" + (haskell-utils-reduce-string "Test\n Test"))) + (should (string-equal + " Test Test" + (haskell-utils-reduce-string " Test\r\n Test"))) + (should (string-equal + " TestTest" + (haskell-utils-reduce-string " Test\r\nTest"))) + (should (string-equal + " TestTest Test test" + (haskell-utils-reduce-string " Test\r\nTest\n Test test")))) + ;;; haskell-utils-tests.el ends here