Giter Site home page Giter Site logo

zapata's Introduction

Zapata

Who has time to write tests? This is a revolutional tool to make them write themselves.

Gem Version Build Status Dependency Status Coverage Status Code Climate

Emiliano Zapata

What is your problem?

There comes a day where you have this bullshit class RobotToTest. We need tests. :shipit:

class RobotToTest
  def initialize(human_name, cv)
    @name = robot_name(human_name)
  end

  def robot_name(human_name)
    "#{self.class.prefix}_#{human_name}"
  end

  def cv
    { planets: planets }
  end

  def nested_fun_objects(fun_objects)
    'It was fun'
  end

  def self.prefix
    'Robot'
  end

  private

  def planets
    ['Mars', Human.home]
  end

  def fun_objects
    [%i(array in array), { hash: nested_hash }]
  end

  def nested_hash
    { in_hash: { in: array } }
  end

  def array
    %w(array)
  end
end

# just another class to analyze
class Human
  def initialize
    human_name = 'Emiliano'
  end

  def self.home
    'Earth'
  end
end

Solving it

You run zapata generate app/models/robot_to_test.rb and pop the champagne. Zapata generated spec/models/robot_to_test_spec.rb for you.

describe RobotToTest do
  let(:robot_to_test) do
    RobotToTest.new('Emiliano', { planets: ['Mars', Human.home] })
  end

  it '#robot_name' do
    expect(robot_to_test.robot_name('Emiliano')).to eq('Robot_Emiliano')
  end

  it '#cv' do
    expect(robot_to_test.cv).to eq({ planets: ['Mars', 'Earth'] })
  end

  it '#nested_fun_objects' do
    expect(robot_to_test.nested_fun_objects([
      [:array, :in, :array],
      {
        hash: {
          in_hash: {
            in: ['array']
          }
        }
      }
    ])).to eq('It was fun')
  end

  it '#prefix' do
    expect(RobotToTest.prefix).to eq('Robot')
  end
end

What does it do?

It tries to write a passing RSpec spec off the bat. It does fancy analysis to predict the values it could feed to the API of a class.

To be more specific:

  • Analyzes all vars and methods definitions in app/models
  • Checks what arguments does a testable method in app/models/robot_to_test.rb need
  • Searches for such variable or method name in methods in analyzed
  • Selects the most probable value by how many times it was repeated in code
  • Runs the RSpec and fills in the expected values of the test with those returned by RSpec

For more things it can currently do check https://github.com/Nedomas/zapata/tree/master/spec

Workflow with Zapata

Say you are writing some new feature on your existing project. Before writing that, you probably want to test out the current functionality. But who has time for that?

You let Zapata create that quick spec for you. Think of it as a current functionality lock. Write more code and when you're happy with the result - lock it up again.

Requirements

  • Ruby 2.0+
  • Rails 3.0+

Installation

It should be as easy as

gem install zapata

or

gem 'zapata', groups: %w(development test)

Usage

To use run

zapata generate app/models/model_name.rb

To ignore other files and analyze a single model you want to generate a spec for:

zapata generate app/models/model_name.rb --single

Collaboration ❤️

It is encouraged by somehow managing to bring a cake to your house. I promise, I will really try.

This is a great project to understand language architecture in general. A project to let your free and expressionistic side shine through by leaving meta hacks and rainbows everywhere.

Thank you to everyone who do. I strongly believe that this can make the developer job less robotic and more creative.

To install, run:

git clone https://github.com/Nedomas/zapata
cd zapata
bundle exec rake install

For specs:

  1. cd spec/support/rails_test_app && bundle update
  2. cd ../../..
  3. Run:
bundle exec rspec --pattern "spec/*_spec.rb"

Awareness

I am well aware that this is featured in Ruby Weekly 223. On that note I'd like to thank everybody who helped it shine through.

Special thanks to my comrade @jpalumickas, with whom we share a vision of a better world.

Copyright

Copyright (c) 2014 Domas. See LICENSE for details.

zapata's People

Contributors

nedomas avatar jpalumickas avatar

Watchers

kathyonu avatar James Cloos 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.