Last active
December 1, 2020 13:27
-
-
Save serradura/1bc0dd7cad6ed7dabc243ea7e40e1fde to your computer and use it in GitHub Desktop.
Exemplos de testes com minitest
This file contains 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 | |
extend self | |
def add(a, b); number(a) + number(b); end | |
def sub(a, b); number(a) - number(b); end | |
private | |
def number(value) | |
return value if value.is_a?(Numeric) | |
return value.include?('.') ? value.to_f : value.to_i if numeric_string?(value) | |
raise TypeError, 'the value must be Numeric or a String with numbers' | |
end | |
def numeric_string?(value) | |
value.is_a?(String) && value =~ /((\d+)?\.\d|\d+)/ | |
end | |
end | |
require 'minitest/autorun' | |
class CalcAdditionTest < Minitest::Test | |
def test_the_operation_with_numeric_values | |
assert Calc.add(1, 1) == 2 | |
assert Calc.add(1.5, 1) == 2.5 | |
end | |
def test_the_operation_with_string_values | |
assert Calc.add(1, '1') == 2 | |
assert Calc.add('1.5', 1) == 2.5 | |
assert_raises(TypeError) { Calc.add('a', 1) } | |
end | |
def test_the_operation_with_invalid_args | |
assert_raises(TypeError) { Calc.add([], '1') } | |
assert_raises(TypeError) { Calc.add('1.5', {}) } | |
end | |
end |
This file contains 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 | |
extend self | |
def add(a, b); number(a) + number(b); end | |
def sub(a, b); number(a) - number(b); end | |
private | |
def numeric_string?(value) | |
value.is_a?(String) && value =~ /((\d+)?\.\d|\d+)/ | |
end | |
def number(value) | |
return value if value.is_a?(Numeric) | |
return value.include?('.') ? value.to_f : value.to_i if numeric_string?(value) | |
raise TypeError, 'the value must be Numeric or a String with numbers' | |
end | |
end | |
require 'minitest/autorun' | |
class CalcAdditionTest < Minitest::Test | |
def test_the_operation_with_numeric_values | |
assert_equal(2, Calc.add(1, 1)) | |
assert_in_delta(2.1232, Calc.add(1, 1.1232), 0.01) | |
end | |
def test_the_operation_with_string_values | |
assert Calc.add(1, '1') == 2 | |
assert Calc.add('1.5', 1) == 2.5 | |
assert_raises(TypeError) { Calc.add('a', 1) } | |
end | |
def test_the_operation_with_invalid_args | |
assert_raises(TypeError) { Calc.add([], '1') } | |
assert_raises(TypeError) { Calc.add('1.5', {}) } | |
end | |
end | |
class CalcSubtractionTest < Minitest::Test | |
def test_the_operation_with_numeric_values | |
assert_equal(0, Calc.sub(1, 1)) | |
assert_in_delta(0.1232, Calc.sub(1.1232, 1), 0.01) | |
end | |
def test_the_operation_with_string_values | |
assert Calc.sub(1, '1') == 0 | |
assert Calc.sub('1.5', 1) == 0.5 | |
assert_raises(TypeError) { Calc.sub('a', 1) } | |
end | |
def test_the_operation_with_invalid_args | |
assert_raises(TypeError) { Calc.sub([], '1') } | |
assert_raises(TypeError) { Calc.sub('1.5', {}) } | |
end | |
end |
This file contains 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 | |
extend self | |
def add(a, b); number(a) + number(b); end | |
def sub(a, b); number(a) - number(b); end | |
private | |
def numeric_string?(value) | |
value.is_a?(String) && value =~ /((\d+)?\.\d|\d+)/ | |
end | |
def number(value) | |
return value if value.is_a?(Numeric) | |
return value.include?('.') ? value.to_f : value.to_i if numeric_string?(value) | |
raise TypeError, 'the value must be Numeric or a String with numbers' | |
end | |
end | |
require 'minitest/autorun' | |
module Calc | |
class AdditionTest < Minitest::Test | |
def test_the_operation_with_numeric_values | |
assert_equal(2, Calc.add(1, 1)) | |
assert_in_delta(2.1232, Calc.add(1, 1.1232), 0.01) | |
end | |
def test_the_operation_with_string_values | |
assert Calc.add(1, '1') == 2 | |
assert Calc.add('1.5', 1) == 2.5 | |
assert_raises(TypeError) { Calc.add('a', 1) } | |
end | |
def test_the_operation_with_invalid_args | |
assert_raises(TypeError) { Calc.add([], '1') } | |
assert_raises(TypeError) { Calc.add('1.5', {}) } | |
end | |
end | |
class SubtractionTest < Minitest::Test | |
def test_the_operation_with_numeric_values | |
assert_equal(0, Calc.sub(1, 1)) | |
assert_in_delta(0.1232, Calc.sub(1.1232, 1), 0.01) | |
end | |
def test_the_operation_with_string_values | |
assert Calc.sub(1, '1') == 0 | |
assert Calc.sub('1.5', 1) == 0.5 | |
assert_raises(TypeError) { Calc.sub('a', 1) } | |
end | |
def test_the_operation_with_invalid_args | |
assert_raises(TypeError) { Calc.sub([], '1') } | |
assert_raises(TypeError) { Calc.sub('1.5', {}) } | |
end | |
end | |
end |
This file contains 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 | |
extend self | |
def add(a, b); number(a) + number(b); end | |
def sub(a, b); number(a) - number(b); end | |
private | |
def numeric_string?(value) | |
value.is_a?(String) && value =~ /((\d+)?\.\d|\d+)/ | |
end | |
def number(value) | |
return value if value.is_a?(Numeric) | |
return value.include?('.') ? value.to_f : value.to_i if numeric_string?(value) | |
raise TypeError, 'the value must be Numeric or a String with numbers' | |
end | |
end | |
require 'minitest/autorun' | |
class CalcAdditionTest < Minitest::Test | |
def setup | |
@numbers = {a: 1, b: 2} | |
end | |
def teardown | |
@numbers.delete(:a) | |
@numbers.delete(:b) | |
end | |
def test_the_operation_with_numeric_values | |
assert_equal(3, Calc.add(@numbers[:a], @numbers[:b])) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment