New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

solid_assert

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

solid_assert

  • 1.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

solid_assert

Build Status

solid_assert is a simple implementation of an assert utility in Ruby. It lets you write tests for your assumptions while coding.

Assertions are meant to test conditions about the integrity of your code. You should use them for testing assumptions like the following:

  • If the flow reaches here, then this variable has to have this value.
  • This line of code should never be executed.
  • At this point, this list should contain one entry for each key in this hash.

Installation

Add to your Gemfile:

gem "solid_assert"

Usage

You can enable/disable assertions with:

SolidAssert.enable_assertions
SolidAssert.disable_assertions

Assertions are disabled by default and are typically used in development mode only. You might want to disable them in production for performance reasons.

Use assert for testing conditions. You can optionally provide an error message.

assert some_string != "unexpected value"
assert user.authenticated?

assert apples_count > 5, "Not enough apples!"
assert !clients.empty?, "The list must NOT be empty!"

Use invariant for testing blocks of code. This comes handy when testing your assumptions requires several lines of code. You can provide an optional message too.

invariant do
  one_variable = calculate_some_value
  other_variable = calculate_some_other_value
  one_variable > other_variable
end
invariant "Lists must have equal sizes!" do
  len = calculate_list_length
  other_len = calculate_other_list_length
  len == other_len
end

Assertion Error

Failed assertion will raise SolidAssert::AssertionFailedError error. You shouldn't catch it in a rescue block! If it raised then something is wrong with either your code or with you assumption. Assertions shouldn't be used for handling error situations! Use Ruby built-in exception handling for that.

Rails

Create a file named solid_assert.rb in the config/initializers dir with the following content:

SolidAssert.enable_assertions unless Rails.env.production?

This way assertions will be disabled in production and enabled in the rest of environments.

References

FAQs

Package last updated on 22 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc