Giter Club home page Giter Club logo

docspec's Introduction

docspec

Build Status Read the Docs (version)

A crystal library for automatically testing documentation examples.

Docspec is crystal's equivalent of a doctest library.

Use Cases

  • Docspec encourages documentation by creating tests from it.
  • Docspec encourages testing by reducing boilerplate code for test cases.
  • Docspec encourages fast development by reducing boilerplate code for test cases.

Installation

Add this to your application's shard.yml:

dependencies:
  docspec:
    github: skippi/docspec

Usage

Docspec parses source files for any commented codeblocks with code in them. For each codeblock line with a prefix of >>, it executes the line and stores the result. If the line also had an expression appended with # =>, then docspec will test that the result equals the appended expression.

Alternatively, you can use the [](@doctest) annotation instead of >> to mark entire codeblocks for doctesting.

In this example, we will fully doctest Foo.bar and Foo.baz, while ignoring doctesting for Foo.add. Note the usage of >> and [](@doctest):

# src/foo.cr

module Foo
  # Returns "hello world".
  #
  # ```
  # >> Foo.bar # => "hello world"
  #
  # >> name = "say #{Foo.bar}"
  # >> name # => "say hello world"
  # ```
  def self.bar
    "hello world"
  end

  # Subtracts two numbers.
  #
  # [](@doctest)
  # ```
  # Foo.baz(4, 2) # => 2
  # Foo.baz(-8, -4) # => -4
  # ```
  def self.baz(a, b)
    a - b
  end

  # Adds two numbers.
  #
  # ```
  # Foo.add(1, 3) # => 4
  # Foo.add(-2, -4) # => -6
  # ```
  def self.add(a, b)
    a + b
  end
end

Require docspec and doctest the source file using a relative path:

# spec/foo_spec.cr

require "docspec"

Docspec.doctest("../src/foo.cr")

Lastly, run your tests in your project's root directory.

crystal spec

Documentation

Contributing

  1. Fork it ( https://github.com/skippi/docspec/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • skippi - creator, maintainer

docspec's People

Contributors

skippi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

docspec's Issues

Needs globbing rather than individual files

This makes it more trouble that it's worth in the real world. It needs something like:

  # Parses all files matching *glob* for marked examples to create specs.
  macro doctest(glob)
    {% calling_dir = glob.filename.gsub(/[^\/]*$/, "") %}\
    {% for filename, _ in `find #{calling_dir}/$(echo #{glob} | sed 's/\*.*//') -type f -regex $(echo #{calling_dir}/#{glob} | sed 's!/!\\\/!g;s!\.!\\.!g;s/\*\*/.,,,,/g;s/\*/[^\\]*/g;s/,,,,/*/g;s/"$/$"/')`.lines %}\
      Docspec.doctest_file({{filename}})
    {% end %}\
  end

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.