Git Product home page Git Product logo

elm-doctest's Introduction

Build Status npm version

elm-doctest

doctest runner against Elm-lang source files

installation

npm install elm-doctest

It depends on elm and assumes that elm-make and elm-repl are available either via systemwide installation or npm module installation. Make sure elm-make succeeds with your elm source files.

how does it work?

It utilizes elm-repl for expression evaluation and compare the values against the expected value. (It does not comapre stringified values like haskell doctest does via GHCi outputs.)

It only evaluates the expressions that follows -- >>> (i.e. Elm comment symbol followed by space and three LT chars until end of the line) and the expression on the next line after -- .

For example, if the comment states:

-- >>> x =
-- >>>   1 + 2
--
-- >>> x * 2
-- 6

Then, elm-doctest asks elm-repl to evaluate the actual code section in the source file and effectively following expression:

((1 + 2) * 2) == (6)

If value reported by elm-repl is True then test passes, fail otherwise.

usage

Usage: elm-doctest [--watch] [--help] [--elm-path PATH]
                   [--pretest CMD] FILES...
  run doctest against given Elm files

Available options:
  -h,--help             Show this help text
  --pretest CMD         Command to run before doc-test
  --elm-path PATH       Path to elm executable
  -w,--watch            Watch and run tests when target files get updated

example

ModuleTobeTested.elm:

module ModuleTobeTested exposing(..)

-- |
-- >>> add 3 5
-- 8
--
-- >>> removeZeros [0, 1, 2, 3, 0]
-- [1, 2, 3]
--
-- >>> greetingTo "World"
-- "Konnichiwa World"
--
add : Int -> Int -> Int
add x y = x + y

greetingTo : String -> String
greetingTo x = "Hello " ++ x

removeZeros : List Int -> List Int
removeZeros = List.filter (\x -> x /= 0)

evaluation elm-doctest ModuleTobeTested.elm outputs

Starting elm-doctest ...

 processing: test/TestData/TestFail.elm
### Failure in test/TestData/TestFail.elm:10: expression
  greetingTo "World"
expected: "Konnichiwa World"
 but got: "Hello World"
Examples: 3  Failures: 1

limitation

As it utilizes elm-repl, the script must be runnable inside elm-repl. For example, code which imports elm-lang/[email protected] module cannot be tested. Since the stdout is used to evaluate the test result, avoid using Debug.log.

Also, make sure elm-make runs without error. You can auto run elm-make by using --pretest command-line option.

license

MIT

elm-doctest's People

Contributors

arowm avatar emilgoldsmith avatar stoeffel avatar tshm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

elm-doctest's Issues

Unexpected token > in JSON at position 0

Hi ๐Ÿ‘‹

First of all, thanks for this tool, good stuff!
Secondly, I wanted to ask you about this intermittent error that I'm having.
I can't seem to figure out where it is coming from.

Here's what I'm doing:

โšก make test
> Run tests
Starting elm-doctest ...

 processing: src/App/Sources/Crypto/Hex.elm
Examples: 14  Failures: 0

 processing: src/App/Sources/Crypto/Hmac.elm
evaluation failed: Unexpected token > in JSON at position 0
Unexpected token > in JSON at position 0
make: *** [test] Error 1

~/Projects/ongaku-ryoho 12.67s master            
โšก make test
> Run tests
Starting elm-doctest ...

 processing: src/App/Sources/Crypto/Hex.elm
Examples: 14  Failures: 0

 processing: src/App/Sources/Crypto/Hmac.elm
Examples: 2  Failures: 0

~/Projects/ongaku-ryoho 13.47s master  

So yeah, sometimes it fails and sometimes it doesn't.
Any ideas?

Thanks!

PS. You can find the source code here. I'm calling elm-doctest in the Makefile.

alternative implementation

You might be interested in https://github.com/stoeffel/elm-doc-test.
I used a different approach than you. It creates elm-tests from your doc-tests and you can run them using node-test-runner. It supports {-, multiline tests, you get nice compiler messages and because of it's dependency to elm-test you get nice assertion messages.

Please close this issue when you see it, it's not an bug. I just didn't know how to contact you differently. We might profit from each others work. Would be awesome to hear what you think of my approach. Hope it's okay that I opened this issue. Thanks for the inspiration.

Screenshots

passing doc-test screen shot 2016-11-29 at 10 50 56
failing doc-test screen shot 2016-11-29 at 10 51 25
broken doc-test (doesn't compile) screen shot 2016-11-29 at 10 51 11

Running on all file saves

It'd be great if elm-doctest was run on all file saves, and that a user got feedback just like a regular elm compiler error. Don't know if it'd be better to tie in with elm-reactor or latch onto/plug into elm, but I think getting errors inline in one's editor would be the slickest.

Valid file with no tests raises a SyntaxError exception immediately after "Starting elm-doctest ..."

Using:

http://elm-lang.org/examples/random

$ elm-doctest Random.elm
Starting elm-doctest ...

processing: /tmp/Random.elm
undefined:1
> > > > | | > 
^

SyntaxError: Unexpected token > in JSON at position 0
    at Object.parse (native)
    at Array.app.ports.evaluate.subscribe.resource (/home/user/projects/trustedexpert/projects/brokedown-web/node_modules/elm-doctest/src/bridge.js:103:40)
    at Function.onEffects [as func] (/home/user/projects/trustedexpert/projects/brokedown-web/node_modules/elm-doctest/distribution/index.js:3456:12)
    at A3 (/home/user/projects/trustedexpert/projects/brokedown-web/node_modules/elm-doctest/distribution/index.js:98:11)
    at onMessage (/home/user/projects/trustedexpert/projects/brokedown-web/node_modules/elm-doctest/distribution/index.js:3241:12)
    at Object.callback (/home/user/projects/trustedexpert/projects/brokedown-web/node_modules/elm-doctest/distribution/index.js:3283:11)
    at step (/home/user/projects/trustedexpert/projects/brokedown-web/node_modules/elm-doctest/distribution/index.js:2921:32)
    at Timeout.work [as _onTimeout] (/home/user/projects/trustedexpert/projects/brokedown-web/node_modules/elm-doctest/distribution/index.js:2963:15)
    at tryOnTimeout (timers.js:232:11)
    at Timer.listOnTimeout (timers.js:202:5)

Looks like its having an issue parsing the JSON at bridge.js line 103.

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.