Created
July 29, 2015 03:13
-
-
Save patmaddox/8d41bff6e47397eebc33 to your computer and use it in GitHub Desktop.
Writing tiny tests
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 'minitest/autorun' | |
class ArrayTest < MiniTest::Test | |
def test_new_array_has_zero_size | |
assert(Array.new.size == 0) | |
end | |
def test_new_array_is_empty | |
assert(Array.new.empty?) | |
end | |
def test_empty_array_is_equivalent | |
assert(Array.new == []) | |
end | |
def test_initialized_arary_is_equivalent | |
assert(Array.new([1,2,3]) == [1,2,3]) | |
end | |
def test_pop_the_top | |
assert(Array.new([1,2,3]).pop == 3) | |
end | |
def test_pop_removes_the_top | |
assert(Array.new([1,2,3]).tap {|a| a.pop } == [1,2]) | |
end | |
# Lots more methods to document in http://www.ruby-doc.org/core-2.1.2/Array.html | |
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 'minitest/autorun' | |
class StringTest < Minitest::Test | |
def test_new_string_equals_empty_string | |
assert(String.new == "") | |
end | |
def test_capitalize_downcases_all_but_first | |
assert("hello Pat how are you?".capitalize == "Hello pat how are you?") | |
end | |
def test_downcase_makes_all_letters_small | |
assert("Hi I LOVE RubySteps".downcase == "hi i love rubysteps") | |
end | |
def test_gsub_substitutes_string | |
assert("I dislike RubySteps".gsub("dislike", "love") == "I love RubySteps") | |
end | |
def test_string_interning | |
assert("I know that 1 + 1 = #{1 + 1}" == "I know that 1 + 1 = 2") | |
end | |
# Check out http://www.ruby-doc.org/core-2.1.2/String.html for even more methods... | |
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 'minitest/autorun' | |
class HashTest < Minitest::Test | |
def test_new_hash_is_equivalent | |
assert(Hash.new == {}) | |
end | |
def test_default_value_is_nil | |
assert({}[:funky] == nil) | |
end | |
def test_set_default_value_on_initialize | |
assert(Hash.new("chicken")[:funky] == "chicken") | |
end | |
def test_set_value_overrides_default_value | |
assert(Hash.new("chicken").tap {|hash| hash[:funky] = "James Brown" }[:funky] == "James Brown") | |
end | |
def test_hash_with_initializer_sets_value | |
assert(Hash.new {|hash, k| k.to_s.upcase }[:funky] == "FUNKY") | |
end | |
# Plenty more in http://www.ruby-doc.org/core-2.1.2/Hash.html | |
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 'minitest/autorun' | |
class EnumerableTest < Minitest::Test | |
def test_all_is_true | |
assert([1,3,5,7].all?(&:odd?)) | |
end | |
def test_all_is_false | |
assert(![1,2,3].all?(&:odd?)) | |
end | |
def test_any_is_true | |
assert([1,2].any?(&:odd?)) | |
end | |
def test_any_is_false | |
assert(![2,4].any?(&:odd?)) | |
end | |
def test_select_chooses_items | |
assert([1,2,3,4,5].select(&:odd?) == [1,3,5]) | |
end | |
# Lots more in http://www.ruby-doc.org/core-2.1.2/Enumerable.html | |
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 'minitest/autorun' | |
class RubySteps | |
def fun! | |
true | |
end | |
def educational? | |
true | |
end | |
def about | |
['ruby', 'rails', 'tdd', 'oop', 'refactoring'] | |
end | |
end | |
class RubyStepsTest < Minitest::Test | |
def test_ruby_steps_is_fun | |
assert(RubySteps.new.fun! == true) | |
end | |
def test_ruby_steps_is_educational | |
assert(RubySteps.new.educational? == true) | |
end | |
def test_ruby_steps_is_about_good_stuff | |
assert(RubySteps.new.about == %w(ruby rails tdd oop refactoring)) | |
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
def verify(name, &block) | |
raise "Failed test: #{name}" unless block.call | |
end | |
verify "1 is #odd?" do | |
1.odd? | |
end | |
verify "2 is #even?" do | |
2.even? | |
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
def verify(name, &block) | |
raise "Failed test: #{name}" unless block.call | |
end | |
verify "1 is #odd?" do | |
2.odd? | |
end | |
verify "2 is #even?" do | |
1.even? | |
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
def verify(name, &block) | |
puts "Failed test: #{name}" unless block.call | |
end | |
verify "1 is #odd?" do | |
2.odd? | |
end | |
verify "2 is #even?" do | |
1.even? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment