Back

split-lines (clj)

(source)

function

(split-lines s)
Splits s on \n or \r\n. Trailing empty lines are not returned.

Examples

clojure
(ns clojure.test-clojure.string
  (:require [clojure.string :as s])
  (:use clojure.test))

(deftest char-sequence-handling
  (are [result f args] (let [[^CharSequence s & more] args]
                         (= result (apply f (StringBuffer. s) more)))
       "paz" s/reverse ["zap"]
       "foo:bar" s/replace ["foo-bar" \- \:]
       "ABC" s/replace ["abc" #"\w" s/upper-case]
       "faa" s/replace ["foo" #"o" (StringBuffer. "a")]
       "baz::quux" s/replace-first ["baz--quux" #"--" "::"]
       "baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
       "zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
       "\\\\ \\$" s/re-quote-replacement ["\\ $"]
       "Pow" s/capitalize ["POW"]
       "BOOM" s/upper-case ["boom"]
       "whimper" s/lower-case ["whimPER"]
       ["foo" "bar"] s/split ["foo-bar" #"-"]
       "calvino" s/trim ["  calvino  "]
       "calvino  " s/triml ["  calvino  "]
       "  calvino" s/trimr ["  calvino  "]
       "the end" s/trim-newline ["the end\r\n\r\r\n"]
       true s/blank? [" "]
       ["a" "b"] s/split-lines ["a\nb"]
       "fa la la" s/escape ["fo lo lo" {\o \a}]))

(deftest t-split-lines
  (let [result (s/split-lines "one\ntwo\r\nthree")]
    (is (= ["one" "two" "three"] result))
    (is (vector? result)))
  (is (= (list "foo") (s/split-lines "foo"))))
clojure
(ns clojure.test-clojure.repl
  (:use clojure.test
        clojure.repl
        [clojure.test-helper :only [platform-newlines]]
        clojure.test-clojure.repl.example)
  (:require [clojure.string :as str]))

(deftest test-doc
  (testing "with namespaces"
    (is (= "clojure.pprint"
           (second (str/split-lines (with-out-str (doc clojure.pprint)))))))
  (testing "with special cases"
    (is (= (with-out-str (doc catch)) (with-out-str (doc try))))))
clojure
  "(ns autodoc.build-html
  \"This is the namespace that builds the HTML pages themselves.
It is implemented with a number of custom enlive templates.\"
  {:skip-wiki true, :author \"Tom Faulhaber\"}
  (:refer-clojure :exclude [empty complement])
  (:import [java.util.jar JarFile]
           [java.io File FileWriter BufferedWriter StringReader
                    BufferedInputStream BufferedOutputStream
                    ByteArrayOutputStream FileReader FileInputStream]
           [java.util.regex Pattern])
  (:require [clojure.string :as str])
  (:use [net.cgrand.enlive-html :exclude (deftemplate)]
        [clojure.java.io :only (as-file file writer)]
        [clojure.java.shell :only (sh)]
        [clojure.pprint :only (pprint cl-format pprint-ident
                               pprint-logical-block set-pprint-dispatch
                               get-pretty-writer fresh-line)]
        [clojure.data.json :only (pprint-json)]
        [autodoc.collect-info :only (contrib-info)]
        [autodoc.params :only (params expand-classpath)])
  (:use clojure.set clojure.java.io clojure.data clojure.java.browse
        clojure.inspector clojure.zip clojure.stacktrace))")

(deftest test-pprint-calendar
  (let [calendar (doto (java.util.GregorianCalendar. 2014 3 29 14 0 0)
                   (.setTimeZone (java.util.TimeZone/getTimeZone "GMT")))
        calendar-str (with-out-str (pprint calendar))]
    (is (= (str/split-lines calendar-str)
           ["#inst \"2014-04-29T14:00:00.000+00:00\""])
        "calendar object pretty prints")))
functional-koans/clojure-koans
(ns koans.02-strings
  (:require [koan-engine.core :refer :all]
            [clojure.string :as string]))

  "Maybe you want to separate out all your lines"
  (= [__ __ __] (string/split-lines "1\n2\n3"))
nextjournal/clerk
;; # 🕳 Deep Data
^{:nextjournal.clerk/visibility {:code :hide}}
(ns ^:nextjournal.clerk/no-cache deep
  (:require [clojure.string :as str]
            [hickory.core :as hick]
            [nextjournal.clerk :as clerk]
            [nextjournal.clerk.viewer :as viewer]))

(def letters->words
  (->> "/usr/share/dict/words"
       slurp
       str/split-lines
       (group-by (comp keyword str/lower-case first))
       (into (sorted-map))))
squint-cljs/squint
(ns squint.resource
  (:require [clojure.edn :as edn]
            [clojure.java.io :as io]
            [clojure.string :as str]))

(defmacro version []
  (->> (slurp "package.json")
       (str/split-lines)
       (some #(when (str/includes? % "version") %))
       (re-find #"\d+\.\d+\.\d+")))
slagyr/speclj
(ns speclj.report.progress-spec
  (#?(:clj :require :cljs :require-macros)
   [speclj.core :refer [around describe it should should= with -new-exception -new-failure -new-pending]])
  (:require [clojure.string :as str]
            #?(:cljs [goog.string])                         ;cljs bug?
            [speclj.components :refer [new-description new-characteristic install]]
            [speclj.config :refer [*color?* *full-stack-trace?* *omit-pending?*]]
            [speclj.platform :refer [format-seconds]]
            [speclj.report.progress :refer [new-progress-reporter full-name print-summary print-pendings print-errors]]
            [speclj.reporting :refer [report-description report-pass report-pending
                                      report-fail report-error red green yellow grey report-runs]]
            [speclj.results :refer [pass-result fail-result pending-result error-result]]
            [speclj.run.standard :refer [run-specs]]))

  (it "reports passing run results"
    (binding [*color?* true]
      (let [result1 (pass-result nil 0.1)
            result2 (pass-result nil 0.02)
            result3 (pass-result nil 0.003)
            results [result1 result2 result3]
            output  (with-out-str (report-runs @reporter results))
            lines   (str/split-lines output)]
        (should= 4 (count lines))
        (should= "" (nth lines 0))
        (should= "" (nth lines 1))
        (should= (str "Finished in " (format-seconds 0.123) " seconds") (nth lines 2))
        (should= (green "3 examples, 0 failures") (nth lines 3)))))

  (it "reports failing run results"
    (binding [*color?* true]
      (let [description (new-description "Crazy" false "some.ns")
            char1       (new-characteristic "flips" description "flip" false)
            char2       (new-characteristic "spins" description "spin" false)
            char3       (new-characteristic "dives" description "dive" false)
            result1     (fail-result char1 0.3 (-new-failure "Expected flips"))
            result2     (fail-result char2 0.02 (-new-failure "Expected spins"))
            result3     (fail-result char3 0.001 (-new-failure "Expected dives"))
            results     [result1 result2 result3]
            lines       (str/split-lines (with-out-str (report-runs @reporter results)))]
        (should= 18 (count lines))
        (should= "" (nth lines 0))
        (should= "Failures:" (nth lines 2))
        (should= "" (nth lines 3))
        (should= "  1) Crazy flips" (nth lines 4))
        (should= (red "     Expected flips") (nth lines 5))
        ;        (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/progress_spec.clj:67" (nth lines 6))
        (should= "" (nth lines 7))
        (should= "  2) Crazy spins" (nth lines 8))
        (should= (red "     Expected spins") (nth lines 9))
        ;      (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/progress_spec.clj:55" (nth lines 10))
        (should= "" (nth lines 11))
        (should= "  3) Crazy dives" (nth lines 12))
        (should= (red "     Expected dives") (nth lines 13))
        ;      (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/progress_spec.clj:56" (nth lines 14))
        (should= "" (nth lines 15))
        (should= (str "Finished in " (format-seconds 0.321) " seconds") (nth lines 16))
        (should= (red "3 examples, 3 failures") (nth lines 17)))))

  (it "reports pending run results"
    (binding [*color?* true]
      (let [description (new-description "Crazy" false "some.ns")
            char1       (new-characteristic "flips" description "flip" false)
            result1     (pass-result char1 0.1)
            result2     (pass-result char1 0.02)
            result3     (pending-result char1 0.003 (-new-pending "Blah"))
            results     [result1 result2 result3]
            lines       (str/split-lines (with-out-str (print-summary results)))]
        (should= (yellow "3 examples, 0 failures, 1 pending") (last lines)))))

  (it "reports pending summary"
    (let [description (new-description "Crazy" false "some.ns")
          char1       (new-characteristic "flips" description "flip" false)
          result1     (pending-result char1 0.3 (-new-pending "Not Yet Implemented"))
          lines       (str/split-lines (with-out-str (print-pendings [result1])))]
      (should= 6 (count lines))
      (should= "" (nth lines 0))
      (should= "Pending:" (nth lines 1))
      (should= "" (nth lines 2))
      (should= (yellow "  Crazy flips") (nth lines 3))
      (should= (grey "    ; Not Yet Implemented") (nth lines 4))
      ;      (should= (grey "    ; /Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/progress_spec.clj:117") (nth lines 5))
      ))

  (it "reports error run results"
    (binding [*color?* true]
      (let [description (new-description "Crazy" false "some.ns")
            char1       (new-characteristic "flips" description "flip" false)
            result1     (pass-result char1 0.1)
            result2     (pass-result char1 0.02)
            result3     (error-result (-new-exception "blah"))
            results     [result1 result2 result3]
            lines       (str/split-lines (with-out-str (print-summary results)))]
        (should= (red "3 examples, 0 failures, 1 errors") (last lines)))))

  (it "reports error summary"
    (binding [*full-stack-trace?* false]
      (let [description (new-description "Crazy" false "some.ns")
            char1       (new-characteristic "flips" description "flip" false)
            result1     (error-result (-new-exception "blah"))
            lines       (str/split-lines (with-out-str (print-errors [result1])))]
        (should (> (count lines) 3))
        (should= "" (nth lines 0))
        (should= "Errors:" (nth lines 1))
        (should= "" (nth lines 2))
        (should= (str "  1) " (-new-exception "blah")) (nth lines 3)))))
epiccastle/spire
(ns spire.module.group
  (:require [spire.utils :as utils]
            [spire.facts :as facts]
            [spire.ssh :as ssh]
            [clojure.string :as string]))

(defmethod process-result :present
  [_ {:keys [user] :as opts} {:keys [out err exit] :as result}]
  (let [result (assoc result
                      :out-lines (string/split-lines out))]
    (cond
      (zero? exit)
      (assoc result
             :exit 0
             :result :ok)

(defmethod process-result :absent
  [_ {:keys [user] :as opts} {:keys [out err exit] :as result}]
  (let [result (assoc result
                      :out-lines (string/split-lines out))]
    (cond
      (zero? exit)
      (assoc result
             :exit 0
             :result :ok)