I can't run a basic clojurescript.test sample - clojure

I'm trying to run a basic test with
https://github.com/cemerick/clojurescript.test
I've created a simple project such as:
(defproject sampleproj "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/clojurescript "0.0-2014"]
]
:plugins [[lein-cljsbuild "1.0.0"]
[com.cemerick/clojurescript.test "0.2.2"]]
:cljsbuild {:builds [{:source-paths ["src-js" "test-js"]
:compiler {:output-to "target/cljs/testable.js"
:optimizations :whitespace
:pretty-print true}}]
:test-commands {"unit-tests" ["phantomjs" :runner
"this.literal_js_was_evaluated=true"
"target/cljs/testable.js"]}}
)
Both folders src-js and test-js exist since I intend to put all my clojurescript files there.
In my test-js/sampleproj folder I copied the sample test in the documentation:
(ns sampleproj.sampletest
(:require-macros [cemerick.cljs.test
:refer (is deftest with-test run-tests testing test-var)])
(:require [cemerick.cljs.test :as t]))
(deftest somewhat-less-wat
(is (= "{}[]" (+ {} []))))
(deftest javascript-allows-div0
(is (= js/Infinity (/ 1 0) (/ (int 1) (int 0)))))
(with-test
(defn pennies->dollar-string
[pennies]
{:pre [(integer? pennies)]}
(str "$" (int (/ pennies 100)) "." (mod pennies 100)))
(testing "assertions are nice"
(is (thrown-with-msg? js/Error #"integer?" (pennies->dollar-string 564.2)))))
Now if I try to run the tests
-> % lein cljsbuild test
Compiling ClojureScript.
Running ClojureScript test: unit-tests
ReferenceError: Can't find variable: cemerick
phantomjs://webpage.evaluate():2
phantomjs://webpage.evaluate():5
phantomjs://webpage.evaluate():5
ReferenceError: Can't find variable: cemerick
phantomjs://webpage.evaluate():2
phantomjs://webpage.evaluate():5
phantomjs://webpage.evaluate():5
Subprocess failed
I'm fairly new to clojurescript so probably I'm missing something.
Update:
My environment
-> % lein -v
Leiningen 2.3.4 on Java 1.7.0_06 Java HotSpot(TM) 64-Bit Server VM
-> % phantomjs -v
1.9.2
-> % java -version
java version "1.7.0_06"
Java(TM) SE Runtime Environment (build 1.7.0_06-b24)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b09, mixed mode)

Are macros located right into clj sources ?
After that th compiler shoud compiler you cemeric macros to javascript.
You propably have dependencies right in project.clj

This hasn't been answered, so I will. You need to specify your dependency for clojurescript.test : [com.cemerick/clojurescript.test "0.3.1"]

Related

Auto-refresh/ Auto-reload assets

I'm new to clojure and i'm having a hard time figuring out how to reload/ refresh the browser when changes have been made to either html/ js/ css etc.
this is my current setup project.clj
(defproject app2 "0.1.0-SNAPSHOT"
:description "FIXME: write this!"
:url "http://exampl.com/FIXME"
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.9.89"]
[ring/ring-core "1.5.0"]
[ring/ring-jetty-adapter "1.5.0"]
[enfocus "2.0.0-SNAPSHOT"]]
:plugins [[lein-cljsbuild "1.1.3"]
[lein-ring "0.9.7"]]
:cljsbuild {:builds [{:source-paths ["src/cljs"],
:compiler {
:main "scripts.client"
:output-to "resources/public/js/main.js"
:output-dir "resources/public/js/out"
:asset-path "js/out"
;;:pretty-print true
;;:optimizations :none
}}]}
:main app2.server/app
:ring {:handler app2.server/app :auto-reload? true :auto-refresh? true :reload-paths ["src" "resources"]}
:profiles {
:dev {
:ring {
:nrepl {
:start? true
:port 9000
}
}
}
}
)
This is my server.clj
(ns app2.server
(:use [ring.middleware.resource :only [wrap-resource]]
[ring.middleware.file-info :only [wrap-file-info]]
[ring.middleware.reload :refer [wrap-reload]])
;;(:require app2.repl)
)
(defn handler
[request]
{:status 200}
)
;handling routing "/" -> "/index.html"
(defn wrap-index [handler]
(fn [req]
(println (pr-str req))
(if (= (:uri req) "/")
(handler (assoc req :uri "/index.html"))
(handler req))))
;setting up a simple resource handler for ring
(def app (-> handler
(wrap-resource "public")
(wrap-file-info)
(wrap-index)
(wrap-reload app {:dirs ["src" "resources"]})
))
How can this be accomplished?
I'm used to developing in node and you have tools like browser sync, weinre and supervisor. What are the equivalents in clojure?
I suggest you have a look at figwheel, which lets you do hot reloading of your ClojureScript and CSS in the browser.
There is of course not one good way of setting up your build, but my way to go for languages like SASS etc. is to watch and compile them as a separate process, and have Figwheel watch the generated CSS.
For example, on one of my ClojureScript projects, I had a script file for LESS compilation which used the LESS compiler and the wr utility directly:
#!/usr/bin/env bash
lessc src/styles/main.main.less resources/public/css/main.css --source-map && cp src/styles/*.less resources/public/css
wr "lessc src/styles/main.main.less resources/public/css/main.css --source-map && cp src/styles/*.less resources/public/css" src/**/*.less
Of course you can also use things like Gulp, Webpack - or any tool you're used to.
The alternative is to use Leiningen plugins, see the list here.

how to write test cases in clojure

i am beginner in clojure
I wrote
(expect true (valid? "henry" "as#gmail.com" "9999999999" "tesxt message"))
in my (ns clojuregeek.test.contact)
when i run the test cases by lein test in clojure i found:--
lein test clojuregeek.test.contact
Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
failure in (contact.clj:32) : clojuregeek.test.contact
(expect
true
(valid? "henry" "as#gmail.com" "9999999999" "tesxt message"))
act-msg: exception in actual: (valid? "henry" "as#gmail.com" "9999999999" "tesxt message")
threw: class java.lang.ClassCastException - clojure.lang.Var$Unbound cannot be cast to java.util.concurrent.Future
noir.validation$get_errors$doInvoke (validation.clj:94)
noir.validation$errors_QMARK_$doInvoke (validation.clj:140)
on (contact.clj:34)
on (contact.clj:32)
Ran 1 tests containing 1 assertions in 178 msecs
0 failures, 1 errors.
While developing a petri net simulator in Clojure, the testing environment Midje was the best choice to write useful and simple testcases. Maybe you just take a look at this...
Testcases are really simple. You just create your facts in test/core_tests.clj like:
(ns your-namespace.core-tests
(:use midje.sweet)
:require [your-namespace.core :as core])
(fact "Testing valid?"
(core/valid? "henry" "as#gmail.com" "9999999999" "tesxt message") => true)
;; some more facts...
For preparation you need to modify your project.clj:
(defproject your-project "0.1.0-SNAPSHOT"
:description ""
:url "http://example.com/FIXME";TODO
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[midje "1.6.2"]]
:main ^:skip-aot your-project.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}
:dev {:dependencies [[midje "1.6.2"]]
:plugins [[lein-midje "3.1.3"]]}})
After this you can start a new terminal window and type in your project folder first lein deps and after this:
lein midje :autotest
and Midje is going to run through your test every time you save a file in your project folder.
In my opinion one of the best solutions to write simple and useful testcases.

Clojure REPL environment lost after running refresh-all

I'm hacking on a Clojure app I created with lein new app inclojure. There are a few things I'd like to pre-load every time I fire up the REPL, so I created a dev/user.clj file, and source it in my project.clj:
(defproject inclojure "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]]
:main ^:skip-aot inclojure.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}
:dev {:source-paths ["dev"]}})
The file mostly just requires a bunch of crap so I can use shorthand:
(ns inclojure.core
(:require [clojure.java.io :as io]
[clojure.tools.namespace.repl :refer (refresh-all)])
(:use [clojure.reflect :only [reflect]]
[clojure.pprint :only [pprint print-table pp]]))
(defn r
"inspect all of the properties in a java object, optionally by specifying a
pattern"
([o] (r o "."))
([o prefix]
(->> (reflect o :ancestors true)
:members
(filter #(re-find (re-pattern (str "(?i)" prefix)) (str (:name %))))
(pprint))))
This all works, but when I run (refresh-all), it loses everything from dev/user.clj. Is there a way for me to be able to (refresh-all) from the and either keep or re-load everything from dev/user.clj?
It seems like I may want to create a REPL-specific namespace like inclojure.repl in dev/user.clj, and then make that the REPL's init-ns.

Clojure's :require and Instaparse

I am trying to use instaparse lib for my clojure project. I use leiningen 2.0 and clojure 1.5.1 in my project dependencies. I add instaparse to my project dependencies as follow:
(defproject bachelor "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/clojure-contrib "1.2.0"]
[instaparse "1.1.0"]])
And that is my source where i'm trying to require that lib:
(ns bachelor.data
(:require [clojure.string :as str])
(:require [instaparse.core :as insta])
(:use [clojure.contrib.generic.math-functions])
)
When I try to compile that I get following error message:
cd c:/bachelor/src/bachelor.data/ 1 compiler notes:
Unknown location: error: java.io.FileNotFoundException: Could not
locate instaparse/core__init.class or instaparse/core.clj on
classpath:
company.clj:1:1: error: java.io.FileNotFoundException: Could not
locate instaparse/core__init.class or instaparse/core.clj on
classpath: (company.clj:1)
Compilation failed.
I checked classpath for my project and I think that instaparse should be found there.
lein classpath
C:\bachelor\test;C:\bachelor\src;C:\bachelor\dev-resources;C:\bachelor\resources;C:\bachelor\target\classes;C:\Users\Maciej.m2\repository\instaparse\instaparse\1.1.0\instaparse-1.1.0.jar;C:\Users\Mac
iej.m2\repository\org\clojure\clojure-contrib\1.2.0\clojure-contrib-1.2.0.jar;C:\Users\Maciej.m2\repository\org\clojure\clojure\1.5.1\clojure-1.5.1.jar
Have any idea what I am doing wrong?
UPDATE
I updated result for lein classpath. Earlier, I've pasted old result.
here is a working sample project:
project.clj:
(defproject parse "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[instaparse "1.1.0"]])
you don't need the lines for contrib, and string is built into clojure now.
src/parse/core.clj:
(ns parse.core
(:require [instaparse.core :as insta]
[clojure.string :as str]))
(def as-and-bs
(insta/parser
"S = AB*
AB = A B
A = 'a'+
B = 'b'+"))
repl:
#<Namespace parse.core>
parse.core> (as-and-bs "aaaaabbbaaaabb")
[:S [:AB [:A "a" "a" "a" "a" "a"] [:B "b" "b" "b"]] [:AB [:A "a" "a" "a" "a"] [:B "b" "b"]]]
parse.core> (str/join "," ["a" "b" "c"])
"a,b,c"
My general Liningen strangeness resolution checklist:
run lein deps and restart nrepl/emacs
lein clean and restart nrepl/emacs
remove the local libs dir (lein v1.x)
remove my local maven repository and run lein deps
I've found out what was wrong. I was creating project with leiningen but develop source with Clojure-box or Clooj. I was also trying to compile my source with that tools and it was mistake. When you run such IDE it loads that's own classpath and that is why it could not find library I'd like to use. Now I compile my src with
lein compile
and run it in
lein repl
and everything is just working fine.

ClassNotFound Exception in Lein (can't compile)

I am trying to :gen-class a Servlet
This is my code:
(ns test.test
(:import (java.io PrintWriter) (javax.servlet.http HttpServlet))
(:gen-class :name test.TestServlet :extends javax.servlet.http.HttpServlet))
(defn -doGet[request response]
(let [wrtr (.getWriter response)]
(.println wrtr "hello from clojure")))
it can't be compiled by Lein
it said Exception in thread "main" java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet (Test.clj:1)
I already modified the :library-path in Lein as ":library-path "/home/long/workspaces/spring/LongHDi/war/WEB-INF/lib" but it didn't work.
Do you have any idea why?
I am trying to work with Google App Engine. The servlet class I want to extend is already in the lib folder I specified.
Which version of lein are you using ?
I downloaded jetty from here, and lein version1 worked for me with project.clj
(defproject st2 "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:library-path "/Users/Niko/Downloads/jetty-hightide-8.1.7.v20120910/lib"
:aot [st2.core]
:dependencies [[org.clojure/clojure "1.3.0"]])
with st2.core the same as your code:
(ns st2.core
(:import (java.io PrintWriter) (javax.servlet.http HttpServlet))
(:gen-class :name test.TestServlet :extends javax.servlet.http.HttpServlet))
(defn -doGet[request response]
(let [wrtr (.getWriter response)]
(.println wrtr "hello from clojure")))
If you are using lein2, :library-path is not supported so I suspect you would have to add the dependencies "a-la-maven" and add them to your project dependencies.