| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (def.clj)

This example Java source code file (def.clj) is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

all, eclipse, first, hickey, illegalargumentexception, invalid, license, math/pow, parameter, public, rich, the, you

The def.clj Java example source code

;   Copyright (c) Rich Hickey. All rights reserved.
;   The use and distribution terms for this software are covered by the
;   Eclipse Public License 1.0 (
;   which can be found in the file epl-v10.html at the root of this distribution.
;   By using this software in any fashion, you are agreeing to be bound by
;   the terms of this license.
;   You must not remove this notice, or any other, from this software.

(ns clojure.test-clojure.def
  (:use clojure.test clojure.test-helper

(deftest defn-error-messages
  (testing "multiarity syntax invalid parameter declaration"
    (is (fails-with-cause? 
          #"Parameter declaration \"arg1\" should be a vector"
          (eval-in-temp-ns (defn foo (arg1 arg2))))))

  (testing "multiarity syntax invalid signature"
    (is (fails-with-cause? 
          #"Invalid signature \"\[a b\]\" should be a list"
          (eval-in-temp-ns (defn foo 
                             ([a] 1)
                             [a b])))))

  (testing "assume single arity syntax"
    (is (fails-with-cause? 
          #"Parameter declaration \"a\" should be a vector"
          (eval-in-temp-ns (defn foo a)))))

  (testing "bad name"
    (is (fails-with-cause? 
          #"First argument to defn must be a symbol"
          (eval-in-temp-ns (defn "bad docstring" testname [arg1 arg2])))))
  (testing "missing parameter/signature"
    (is (fails-with-cause? 
          #"Parameter declaration missing"
          (eval-in-temp-ns (defn testname)))))

  (testing "allow trailing map"
    (is (eval-in-temp-ns (defn a "asdf" ([a] 1) {:a :b}))))

  (testing "don't allow interleaved map"
    (is (fails-with-cause? 
          #"Invalid signature \"\{:a :b\}\" should be a list"
          (eval-in-temp-ns (defn a "asdf" ([a] 1) {:a :b} ([] 1)))))))

(deftest non-dynamic-warnings
  (testing "no warning for **"
    (is (empty? (with-err-print-writer
                  (eval-in-temp-ns (defn ** ([a b] (Math/pow (double a) (double b)))))))))
  (testing "warning for *hello*"
    (is (not (empty? (with-err-print-writer
                       (eval-in-temp-ns (def *hello* "hi"))))))))

(deftest dynamic-redefinition
  ;; too many contextual things for this kind of caching to work...
  (testing "classes are never cached, even if their bodies are the same"
    (is (= :b
               (defmacro my-macro [] :a)
               (defn do-macro [] (my-macro))
               (defmacro my-macro [] :b)
               (defn do-macro [] (my-macro))

(deftest nested-dynamic-declaration
  (testing "vars :dynamic meta data is applied immediately to vars declared anywhere"
    (is (= 10
                 (declare ^:dynamic p)
                 (defn q [] @p))
               (binding [p (atom 10)]

Other Java examples (source code examples)

Here is a short list of links related to this Java def.clj source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.