Last active
November 12, 2020 17:02
-
-
Save serradura/aece27bd3e0101e22f8a8c5e84f1fdb8 to your computer and use it in GitHub Desktop.
minitest (just ruby) VS rspec (a DSL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'minitest' , '~> 5.14', '>= 5.14.2' | |
end | |
module Calc | |
IsANumericStr = -> val { val.is_a?(String) && val =~ /((\d+)?\.\d|\d+)/ } | |
Number = -> val do | |
return val if val.is_a?(Numeric) | |
return val.include?('.') ? val.to_f : val.to_i if IsANumericStr[val] | |
raise TypeError, 'the value must be Numeric or a String with numbers' | |
end | |
def self.sum(a, b); Number[a] + Number[b]; end | |
def self.sub(a, b); Number[a] - Number[b]; end | |
def self.div(a, b); Number[a] / Number[b]; end | |
def self.mult(a, b); Number[a] * Number[b]; end | |
end | |
require 'minitest/autorun' | |
class Calc::AdditionTest < Minitest::Test | |
def test_the_operation_with_numeric_values | |
assert Calc.sum(1, 1) == 2 | |
assert Calc.sum(1.5, 1) == 2.5 | |
end | |
def test_the_operation_with_string_values | |
assert Calc.sum(1, '1') == 2 | |
assert Calc.sum('1.5', 1) == 2.5 | |
assert_raises(TypeError) { Calc.sum('a', 1) } | |
end | |
def test_the_operation_with_invalid_args | |
assert_raises(TypeError) { Calc.sum([], '1') } | |
assert_raises(TypeError) { Calc.sum('1.5', {}) } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'rspec', '~> 3.10' | |
end | |
module Calc | |
IsANumericStr = -> val { val.is_a?(String) && val =~ /((\d+)?\.\d|\d+)/ } | |
Number = -> val do | |
return val if val.is_a?(Numeric) | |
return val.include?('.') ? val.to_f : val.to_i if IsANumericStr[val] | |
raise TypeError, 'the value must be Numeric or a String with numbers' | |
end | |
def self.sum(a, b); Number[a] + Number[b]; end | |
def self.sub(a, b); Number[a] - Number[b]; end | |
def self.div(a, b); Number[a] / Number[b]; end | |
def self.mult(a, b); Number[a] * Number[b]; end | |
end | |
require 'rspec/autorun' | |
RSpec.describe Calc do | |
context 'addition' do | |
context 'when receive numeric values' do | |
it { expect(Calc.sum(1, 1)).to be == 2 } | |
it { expect(Calc.sum(1.5, 1)).to be == 2.5 } | |
end | |
context 'when receive string values' do | |
it { expect(Calc.sum(1, '1')).to be == 2 } | |
it { expect(Calc.sum('1.5', 1)).to be == 2.5 } | |
it { expect { Calc.sum('a', 1) }.to raise_error(TypeError) } | |
end | |
context 'when receive invalid values' do | |
it { expect { Calc.sum([], '1') }.to raise_error(TypeError) } | |
it { expect { Calc.sum('1.5', {}) }.to raise_error(TypeError) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment