spec/option_spec.rb in option-0.3.1 vs spec/option_spec.rb in option-0.4.0
- old
+ new
@@ -1,10 +1,25 @@
require "minitest/autorun"
+require "minitest/pride"
require "minitest/spec"
require "option"
+module MiniTest::Assertions
+
+ def assert_some(value, option, msg = nil)
+ assert (option.is_a?(Some) && option.or_nil == value), "Expected Some(#{value})"
+ end
+
+ def assert_none(value, option, msg = nil)
+ assert option == None, "Expected None"
+ end
+end
+
+OptionClass.infect_an_assertion :assert_some, :must_be_some
+OptionClass.infect_an_assertion :assert_none, :must_be_none
+
def value
12
end
describe NoneClass do
@@ -39,15 +54,15 @@
it "#empty? should be true" do
None.empty?.must_equal(true)
end
it "#map should return itself" do
- None.map {}.must_equal(None)
+ None.map {}.must_be_none
end
it "#flat_map should return itself" do
- None.flat_map {}.must_equal(None)
+ None.flat_map {}.must_be_none
end
it "#exists? should return false" do
None.exists? {}.must_equal(false)
end
@@ -55,15 +70,15 @@
it "#fold should invoke the default proc" do
None.fold(proc { value }) { |v| v.to_f }.must_equal(value)
end
it "#filter with a true predicate returns itself" do
- Option(value).filter { |i| i == 12 }.must_equal(Option(value))
+ Option(value).filter { |i| i == 12 }.must_be_some(value)
end
it "#filter with a false predicate returns None" do
- Option(value).filter { |i| i == 1 }.must_equal(None)
+ Option(value).filter { |i| i == 1 }.must_be_none
end
it "should be aliased to None" do
None.must_be_instance_of(NoneClass)
end
@@ -71,10 +86,18 @@
it "#inside should return itself without invoking the block" do
expected = nil
None.inside { |v| expected = value }
expected.must_be_nil
end
+
+ it "#or_else should invoke the block and return an Option" do
+ None.or_else { Some(value) }.must_be_some(value)
+ end
+
+ it "#or_else should raise a TypeError if an Option is not returned" do
+ lambda { None.or_else { value } }.must_raise TypeError
+ end
end
describe SomeClass do
it "#to_a returns the value wrapped in an array" do
@@ -114,23 +137,23 @@
it "#empty? should be false" do
Some(value).empty?.must_equal(false)
end
it "#map should return the result of the proc over the value in an Option" do
- Some(value).map { |v| v * 2 }.must_equal(Some(24))
+ Some(value).map { |v| v * 2 }.must_be_some(24)
end
it "#flat_map should raise TypeError if the returned value is not an Option" do
lambda { Some(value).flat_map { |v| v * 2 } }.must_raise TypeError
end
it "#flat_map should return an Option value from the block" do
- Some(value).flat_map { |v| Option(v * 2) }.must_equal(Some(24))
+ Some(value).flat_map { |v| Option(v * 2) }.must_be_some(24)
end
it "#flat_map can return None from the block" do
- Some(value).flat_map { |_| None }.must_equal(None)
+ Some(value).flat_map { |_| None }.must_be_none
end
it "#exists? should return true when the block evaluates true" do
Some(value).exists? { |v| v % 2 == 0 }.must_equal(true)
end
@@ -142,35 +165,39 @@
it "#fold should map the proc over the value and return it" do
Some(value).fold(proc { value * 2 }) { |v| v * 3 }.must_equal(36)
end
it "#filter should return itself" do
- None.filter { |i| i == 0 }.must_equal(None)
+ None.filter { |i| i == 0 }.must_be_none
end
it "#inside should invoke the proc and return itself" do
expected = nil
- Some(value).inside { |v| expected = v }.must_equal(Some(value))
+ Some(value).inside { |v| expected = v }.must_be_some(value)
expected.must_equal(value)
end
+ it "#or_else should return itself" do
+ Some(value).or_else { None }.must_be_some(value)
+ end
+
it "should wrap the creation of a Some" do
Some(value).must_be_instance_of(SomeClass)
end
it "should be aliased to Some" do
- Some.new(value).must_equal(Some(value))
+ Some.new(value).must_be_some(value)
end
end
describe OptionClass do
it "must return a some if the passed value is not nil" do
- Option(value).must_equal(Some(value))
+ Option(value).must_be_some(value)
end
it "must return a None if the passed value is nil" do
- Option(nil).must_equal(None)
+ Option(nil).must_be_none
end
it "should do equality checks against the boxed value" do
Option(value).must_equal(value)
end