Git Product home page Git Product logo

stream_data's Introduction

StreamData

Build Status Hex.pm

StreamData is an Elixir library for data generation and property testing.

Note: StreamData is in beta. It's a candidate to be included in Elixir itself at some point (but it's not guaranteed to).

Installation

Add stream_data to your list of dependencies:

defp deps() do
  [{:stream_data, "~> 0.1", only: :test}]
end

and run mix deps.get. StreamData is usually added only to the :test environment since it's used in tests and test data generation.

Usage

The documentation is available online.

StreamData is made of two main components: data generation and property testing. The StreamData module provides tools to work with data generation. The PropertyTest module takes care of the property testing functionality.

Data generation

All data generation functionality is provided in the StreamData module. StreamData provides "generators" and functions to combine those generators and create new ones. Since generators implement the Enumerable protocol, it's easy to use them as infinite streams of data:

StreamData.integer() |> Stream.map(&abs/1) |> Enum.take(3)
#=> [1, 0, 2]

StreamData provides all the necessary tools to create arbitrarily complex custom generators:

require StreamData

domains = [
  "gmail.com",
  "hotmail.com",
  "yahoo.com",
]

email_generator =
  StreamData.gen all name <- StreamData.filter(StreamData.alphanumeric_string(), &(&1 != "")),
                     domain <- StreamData.member_of(domains) do
    name <> "@" <> domain
  end

Enum.take(StreamData.resize(email_generator, 20), 2)
#=> ["[email protected]", "[email protected]"]

Property testing

Property testing aims at randomizing test data in order to make tests more robust. Instead of writing a bunch of inputs and expected outputs by hand, with property testing we write a property of our code that should hold for a set of data, and then we generated data in this set to verify that property. To generate this data, we can use the above-mentioned StreamData module.

import PropertyTest

property "bin1 <> bin2 always starts with bin1" do
  check all bin1 <- StreamData.binary(),
            bin2 <- StreamData.binary() do
    assert String.starts_with?(bin1 <> bin2, bin1)
  end
end

To know more about property testing, read the PropertyTest documentation. Another great resource about property testing in Erlang (but with most ideas that apply to Elixir as well) is Fred Hebert's website propertesting.com.

The property testing side of this library is heavily inspired by the original QuickCheck paper (which targeted Haskell) as well as Clojure's take on property testing, test.check.

License

Copyright 2017 Andrea Leopardi and José Valim

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

stream_data's People

Contributors

whatyouhide avatar lexmag avatar keathley avatar gazler avatar josevalim avatar manukall avatar

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.