0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Data type testing tool
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.12
~> 0.10.3
~> 10.0
~> 3.3
~> 0.35.1

Runtime

~> 0.8.7
 Project Readme

DuckTesting

Build Status Code Climate Coverage Status

Simple data type testing tool

Usage

YARD

require "duck_testing"

DuckTesting::YARD.apply

This code automatically generates duck_testing module for all classes in {lib,app}/**/*.rb and prepends them into corresponding classes. In most cases, you might put the code in spec/spec_helper.rb or test/test_helper.rb.

You can include and exclude custom paths by using paths and excluded arguments. For instance, excluding Rails' controllers and views is written as follows:

DuckTesting::YARD.apply(excluded: [%r{^app/(controllers|views)}])

Manually

Suppose there are a class and corresponding duck_testing module:

require "duck_testing"

class Foo
  # @param [Fixnum, Float]
  # @return [Fixnum, Float]
  def double(x)
    x * 2
  end
end

module FooDuckTesting
  def double(x)
    tester = DuckTesting::Tester.new(self, "double")
    tester.test_param(x, [
      DuckTesting::Type::ClassInstance.new(Fixnum),
      DuckTesting::Type::ClassInstance.new(Float)
    ])
    tester.test_return(super, [
      DuckTesting::Type::ClassInstance.new(Fixnum),
      DuckTesting::Type::ClassInstance.new(Float)
    ])
  end
end

Now you can activate type testing by prepending duck_testing module into the class:

before = Foo.new

before.double("2")
# => "22"

Foo.send(:prepend, FooDuckTesting)

after = Foo.new

after.double(2)
# => 4

after.double(2.0)
# => 4.0

after.double("2")
# ContractViolationError: Contract violation for argument Foo#double