each
method in class Triple
yield all of it’s arguments, and then return itself before exit
gem install bundler
bundle install
After the installation, just type rspec to run the spec
folder
, therefore, my implementation of the Enumerate module and the Triple class is in this folder.spec
folder.spec_helper
in spec folder is for Rspec to manage the test.
.
├── Gemfile
├── Gemfile.lock
├── README.md
├── lib
│ └── chichun_enum.rb
└── spec
├── chichun_enum_spec.rb
└── spec_helper.rb
describe
to describe the method you’d like to testcontext
to indicate the current situation, such as:it
blockRSpec
context
block, then the output of tests indentall?
and any?
can both accept calling without block, therefore, I put the tests in the context
block, and all of the unit tests inside the context
block indent
#all?
return true if all three elements in t > 0
return false if one of the elements in t < 0
when block not given
return false if there are any elements that is nil
return false if there are any elements that is false
return true if there no elements that is false or nil
#any?
returns true if there is one element greater than 0
returns false since there is no elements in t greater than 0
when block not given
return true if there are any elements?
return false if no any elements?
describe
added #
as prefix, which means it’s not an instance method, an instance method should be write as .foo
cycle
go into an infinite loopcycle()
or cycle(nil)
inside that thread to make sure that the test is not blockedcycle
, the program really go into the loop, after a arbitrary time, main thread kill the created thread, and test the flagEnumerator
class if block not givento_enum(:name_of_this_method, arg1, arg2, ..., argn) unless block_given?
Enumerator
that we want to return is not the one that we can get from :eachEnumerator.new
to create one