Git Product home page Git Product logo

dwarf's Introduction

Dwarf

A helpful, little dwarf providing helpers/utilities for your Clojure(Script)
projects.

Test utils

with-redef-calls

with-redef-calls works like with-redefs, but it also makes sure any redefined function was called in its body. If it wasn't, it would report a test failure:

(ns yourapp.core-test
  (:require #?(:clj [clojure.test :refer :all]
               :cljs [cljs.test :refer-macros [deftest is are testing]])
            #?(:clj [dwarf.mock :refer [with-redef-calls]])
               :cljs [dwarf.mock :refer-macros [with-redef-calls]]))

(deftest foo-test
  (with-redef-calls [yourapp.core/foo (fn [a] (is (= a 1)))]
    (is (= 1 1))))

Above will result in the following failure, since yourapp.core/foo never fired in the test:

FAIL in (foo-test) (core_test.cljc:7)
yourapp.core/foo@b91d8c4 is called somewhere within `with-redef-calls` body.
expected: "yourapp.core/foo@b91d8c4 to be called."
  actual: "yourapp.core/foo@b91d8c4 was not called."

Date utils

parse-date

Parses a date from a couple of potential date formats:

user=> (:require ''[dwarf.dates :refer [parse-date]]))
nil
user=> (parse-date "10/31/2016")
#object[org.joda.time.DateTime 0x10d10130 "2016-10-31T00:00:00.000Z"]

user=> (parse-date "2016-9-2")
#object[org.joda.time.DateTime 0x77979057 "2016-09-02T00:00:00.000Z"]

user=> (parse-date "2016-01-02 03:04:05")
#object[org.joda.time.DateTime 0x6080252f "2016-01-02T03:04:05.000Z"]

user=> (parse-date "2016-01-02T03:04:05")
#object[org.joda.time.DateTime 0x2a65659f "2016-01-02T03:04:05.000Z"]

user=> (parse-date "2014-10-05T14:15:16.988Z")
#object[org.joda.time.DateTime 0x3d3e6a04 "2014-10-05T14:15:16.988Z"]

user=> (parse-date "2014-10-05 14:15:16.988Z")
#object[org.joda.time.DateTime 0x36efde7d "2014-10-05T14:15:16.988Z"]

To see a full list of built-in formatters refer to the source code. You can also pass your own clj(s)-time formatters (which will override the default-ones):

(:require [clj-time.format :as tf])

(def formatters [(tf/formatter "yyyy M d")
                 (tf/formatter "yyyy MMMM")])

(parse-date "2016 1 1" formatters) ;=> #object[org.joda.time.DateTime 0x32a595d4 "2016-01-01T00:00:00.000Z"]
(parse-date "2016 February") ;=> #object[org.joda.time.DateTime 0x5fa5e51a "2016-02-01T00:00:00.000Z"]

format-date

Reformats a given date string:

(ns yourapp.core
  (:require [dwarf.dates :refer [format-date]]

(format-date "2016-01-02 03:04:05" "MM/dd/yyyy") ;=> "01/02/2016"
(format-date "2016-09-07T11:17:13.090Z" "dd MMM") ;=> "07 Sep"
(format-date "2016-09-07T11:17:13.090Z") ;=> "2016-09-07"

Development

To run the Clojure tests: lein test To run the ClojureScript tests: lein doo firefox test

Cljs tests are ran using (Doo)(https://github.com/bensu/doo), and you'll need a bunch of dependencies. Refer to Doo's docs for more details, but here's a tldr;

npm install karma karma-cljs-test karma-firefox-launcher

You also have to have karma-cli installed globally:

npm install karma-cli -g

License

Copyright © 2018 Rackspace Managed Security.

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

dwarf's People

Contributors

irinarenteria avatar sirsean avatar smogg avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

smogg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.