Giter VIP home page Giter VIP logo

impasta's Introduction

Impasta

im-past-a (noun)

  1. A piece of pasta found with other pasta noodles that do not share its type.
  2. A test spy that can impersonate a given class and/or track methods passed to it.

Origin: portmanteau of imposter and pasta

protip: definition #2 is the one we're talking about

Gem Version Build Status

Installation

Add this line to your application's Gemfile:

gem 'impasta'

And then execute:

$ bundle

Or install it yourself as:

$ gem install impasta

Usage

It's pretty easy, to create a new Impasta just do:

imp = Impasta.decoy

You can give it a name too:

aka_imp = Impasta.decoy aka: 'totally not an imposter'

Both of those Impastas will accept any method and args you throw at them.

imp.whatever                       #=> self
aka_imp.lulz 2, 3, :slimpickins    #=> self

But you can also constrain the methods Impasta objects accept by passing in a target:

array_imp = Impasta.infiltrate target: Array.new

array_imp.first         #=> self
array_imp.johnnycarson  #=> raises Impasta::MissingMethod

You can also extract information from an instance of Impasta:

array_imp.impasta.ledger            # will display all methods sent to it in this format: [[method_name, arguments_sent_to_method, block_passed_to_method], ...]
array_imp.impasta.target            #=> [] # (this is the same array passed in earlier)
array_imp.impasta.origin            #=> "readme.rb line #54" # this will display where the Impasta was instantiated

Meet the Spies

Decoy

  • Impasta.decoy aka: "A type of dummy object"
  • It responds to any message with self! (ignores any target and does its own thing)
  • Excellent placeholder when you don't care about enforcing which methods can be called.

Wiretap

  • Impasta.wiretap aka: "A type of proxy object"
  • Passes method calls on to the target and returns exactly that.
  • Good to keep an eye on what your objects are talking about when you're not looking.

Infiltrate

  • Impasta.infiltrate aka: "A type of test double"
  • Responds with self (the Infiltate object) for any message the target recognizes, raises an exception for anything else.
  • Will raise for everything if no target provided!
  • Good for enforcing API integrity, if you can use this one instead of a Decoy, I highly recommend it.

Ghoul

  • Impasta.ghoul aka: "A type of null object"
  • Responds with nil for any message the target recognizes, raises an exception for anything else.
  • Like Infilitrate, but returns nil instead for situations where you need to test nil-resiliency or stop returning endless selfs.

Forging Messages (AKA: Mocking & Stubbing Methods)

  • Decoy and Infiltrate Impastas will always return self for any method they accept (any for Decoy, only what the target does for Infiltrate).
  • Ghoul always returns nil.
  • Wiretap always returns the same thing as the target.

But what if they didn't?

Enter: FORGERY

imp.decoy
tmp.identity                                     #=> self
imp.impasta.forge :identity, returns: "fake id"
imp.identity                                     #=> "fake id"

You can use a single returns keyword as above or you can use a block instead to set the value it will respond with when it receives the message. Arguments are ignored by the forgery, but you can check the ledger to make sure they are correct.

Secrets

All spies have secrets, you've already interacted with them above, obtained by calling the Spy#impasta method.

But they can do other things, here are a few more:

  • secrets.inspect_target to remind yourself about who this spy is targetting
  • secrets.inspect_forgeries to see the list of forgeries this spy is carrying
  • secrets.codename to get the spy to spill the beans on everything it knows
  • secrets.within accepts a block and executes in the context of the Impasta::Secrets instance, good for configuring a bunch of things all at once

Contributing

  1. Fork it ( http://github.com/acook/impasta/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 new Pull Request

impasta's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

gdrtiop

impasta's Issues

Consider making ledger and entries custom objects

The ledger itself is an array which returns structured arrays of each method call it received:

[[method_name, arguments_sent_to_method, block_passed_to_method], ...]

But you have to know the structure or look it up every time to make sure you know what is what. If instead the ledger returned objects (even just OpenStruct) which provided those values directly or optionally converted to an array, it might require less time consulting the documentation.

We could also add interesting methods to the ledger which could extract different dimensions of that data directly (like the list of methods only as demonstrated below).

imp.impasta.ledger.first.caller #=> caller stacktrace
imp.impasta.ledger.messages #=> [:foo, :bar, :baz] 

Cleanup and refactor

  • Impasta < BasicObject
  • Clean up ivars
  • Split out different strategies into different methods

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.