Giter VIP home page Giter VIP logo

oath's Introduction

Oath

Names

This project was originally called "Vow", but it looks like someone snagged that on hex. So, I had to change it to Oath.

Description

Oath provides a system for design by contract in elixir. It uses the excellent, :decorator library by @arjan.

Usage

You will first need to enable contracts in whichever envs you want to use them. Typically this will be development and test only.

# config/dev.exs
config :oath,
  enable_contracts: true

If you want to ensure contracts are compiled for any dependencies you may need to force a recompilation of your dependencies like so:

mix deps.compile --all --force
MIX_ENV=test mix deps.compile --all --force

After enabling contracts, you can add pre and post conditions to your functions. Preconditions are always run before your function body. Postconditions are run after your function body executes and will be provided the result from your function call. You can have any number of pre and post conditions for each function clause.

We'll use an example function with a broken implementation as a demonstration:

defmodule Mod do
  use Oath

  @decorate pre("a is an integer", fn(a, _) -> is_integer(a) end)
  @decorate pre("b is an integer", fn(_, b) -> is_integer(b) end)
  @decorate post("the result must be greater than a or b", fn(a, b, result) ->
    result >= a && result >= b
  end)
  def add(a, b) do
    if a == 7 do
      (a * -1) + b
    else
      a + b
    end
  end
end

Calling this function with contracts enabled will cause an exception to be raised.

Mod.add(7, 2)
 ** (Oath.ContractError) Mod.add/2 postcondition: 'the result must be greater than a or b' failed with input:
  Arguments:

  a
  => 7

  b
  => 2

  result
  => -5

Contract predicates don't have to be pure. Its often useful to use contracts as a way of validating the functions environment and any of the functions side-effects.

@doc """
Stores a name in the database.
"""
@decorate pre("name must be a string", & is_binary(&1))
@decorate pre("name must not be in db", fn name -> !in_database?(name) end)
@decorate post("Name must be normalized in the db", fn name, _result ->
  fetch_from_db(name) == String.capitalize(name)
end)
def store_in_db(name) do
  #...
end

When to enable contracts?

Classically, you should only enable contracts in dev and test modes. You want to avoid enabling contracts in production as this will lower your production performance due to additional checking. It also means that you won't be able to write side-effecting contracts, which you'll want to be able to do in testing modes.

By default, contracts are disabled and will be compiled away so you won't need to do anything for production builds.

Installation

def deps do
  [
    {:oath, "~> 0.1"},
  ]
end

oath's People

Contributors

gcauchon avatar keathley avatar novaugust avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

oath's Issues

Considered attempting compile time guarantees?

This is more of a discussion than an issue but are you familiar with projects like liquid haskell and the viper project (see eg nagini)?

It might not be insanely difficult with plug-and-play SMT solvers available now. They can't prove everything that you can do runtime of course but users could just be informed of which constraints are unprovable.

A proper implementation of compile-time design by contract / formal verification for elixir would ofc be a very large project . What you have here even at runtime alone is a big undertaking and likely a big improvement in reliability and security to the projects that use it properly

Sorry if you've addressed this somewhere - i couldn't find any mention

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.