- Iteration stop when all the element in each has already yielded the block
- The
each
method in classTriple
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
- All the implementations is in the lib
folder
, therefore, my implementation of the Enumerate module and the Triple class is in this folder. - All the unit testing files are in the
spec
folder.- The
spec_helper
in spec folder is for Rspec to manage the test.
- The
- Tree Structure of the file structure:
.
├── Gemfile
├── Gemfile.lock
├── README.md
├── lib
│ └── chichun_enum.rb
└── spec
├── chichun_enum_spec.rb
└── spec_helper.rb
- RSpec is a tool for behavior driven test process, it allows developers to describe a sequences of actions, and build our test case in whatever situation
- Simple case of writing tests using RSpec:
- Firstly, use the keyword
describe
to describe the method you'd like to test - And then use keyword
context
to indicate the current situation, such as:- Whether the method has argument
- Whether the method get block as argument
- Finally, write the unit tests in the
it
block
- Firstly, use the keyword
- The output hierarchy of
RSpec
- If unit tests is written in a
context
block, then the output of tests indent - Method
all?
andany?
can both accept calling without block, therefore, I put the tests in thecontext
block, and all of the unit tests inside thecontext
block indent
- If unit tests is written in a
#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?
- All of the method I wrote in
describe
added#
as prefix, which means it's not an instance method, an instance method should be write as.foo
- From the ruby doc, if no argument is given or nil is given as an argument, then method
cycle
go into an infinite loop - My way of testing infinite loop is to create a thread and then call
cycle()
orcycle(nil)
inside that thread to make sure that the test is not blocked - And then call sleep(x) in main thread to wait for the loop
- x is an arbitrary time which is not too short for setting the loop flag, and not too long to block our test suite
- The body of the loop set a flag to indicate that while calling
cycle
, the program really go into the loop, after a arbitrary time, main thread kill the created thread, and test the flag
- How to return
Enumerator
class if block not givento_enum(:name_of_this_method, arg1, arg2, ..., argn) unless block_given?
- What if the
Enumerator
that we want to return is not the one that we can get from :each- Use
Enumerator.new
to create one- in the block of the creation, we call yield just like we do in each
- Use