Skip to content

Rspec #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
rubocop:
name: Rubocop
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v2
- uses: actions/setup-ruby@v1
Expand All @@ -17,4 +17,4 @@ jobs:
gem install --no-document rubocop:'~>1.9.0' # https://docs.rubocop.org/en/stable/installation/
[ -f .rubocop.yml ] || wget https://raw.githubusercontent.com/microverseinc/linters-config/master/ruby/.rubocop.yml
- name: Rubocop Report
run: rubocop --color
run: rubocop --color
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ For the: [Github-link](https://github.com/MarkoNS1990/enumerables/tree/enumerabl

- Ruby

## Rspec checking

### Rspec install

`$ gem install rspec`
`$ rspec --init`
### Rspec check

- To Check enumerables:- `rspec spec/spec_enumerables.rb`

# Authors 1
- Name :Marko Zecevic
- Githhub :https://github.com/MarkoNS1990
Expand Down
225 changes: 225 additions & 0 deletions spec/enumerables_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
require_relative '../enumerables'
require 'rspec'
# To Run "rspec spec/enumerables_spec.rb"

describe 'Enumerables' do
let(:arr) { [1, 2, 3, 4, 5] }
let(:false_array) { [false] }
let(:empty_array) { [] }

describe 'my_each method' do
it 'when no block it should return Enumerator' do
expect(arr.my_each).to be_a(Enumerator)
end

it 'when block given then should execute the block for each element' do
arr2 = []
arr.my_each { |ele| arr2 << ele }
expect(arr2).to eq(arr)
end

it 'when block given then should return self with same object' do
arr = [1, 1, 1]
expect(arr.my_each { |ele| ele }).to be(arr)
end
end

describe 'my_each_with _index method' do
it 'when no block it should return Enumerator' do
expect(arr.my_each_with_index).to be_a(Enumerator)
end
end

describe 'my_select method' do
it 'when no block it should return Enumerator' do
expect(arr.my_select).to be_a(Enumerator)
end

it 'when block given then should return array which satisfy condition' do
arr2 = [3, 4, 5]
arr3 = arr.my_select { |ele| ele > 2 }
expect(arr3).to eq(arr2)
end

it 'when false given in block then return empty array' do
arr3 = arr.my_select { |_ele| false }
expect(arr3).to eq(empty_array)
end
end

describe 'my_all? method' do
it 'when no block it should return true' do
expect(arr.my_all?).to be true
end

it 'should return true if empty array' do
expect(empty_array.my_all?).to be true
end

it 'when block given then should return true if all element satisfy the condition' do
expect(arr.my_all? { |ele| ele > 0 }).to be true
end

it 'when parameter given then should return true if all element satisfy the condition' do
expect([1, 2, 3, 4].my_all? { |ele| ele > 0 }).to be true
end

it 'when block given then should return false if any one of element not satisfy the condition' do
expect(arr.my_all? { |ele| ele > 4 }).not_to be true
end

it 'when parameter given then should return false if any one of element not satisfy the condition' do
expect([1, 2, 3, 4].my_all? { |ele| ele == 4 }).not_to be true
end

it 'when parameter given then should return false if any one of element doesntsatisfy the condition' do
expect([1, 2.91, 3, 4].my_all?(Float)).not_to be true
end

it 'when null is an element it should return false' do
expect([1, 2, 3, nil].my_all?).not_to be true
end
it 'when false is an element it should return false' do
expect(false_array.my_all?).not_to be true
end
end

describe 'my_any? method' do
it 'when no block it should return true if element in the array' do
expect(arr.my_any?).to be true
end

it 'when all element is true should return true' do
expect([1, 2, 3, nil].my_any?).to be true
end

it 'when block given then should return true if any element satisfy condition' do
expect(arr.my_any? { |ele| ele > 2 }).to be true
end

it 'when parameter given then should return true if any one of element satisfy the condition' do
expect([1, 2, 3, 4].my_any? { |ele| ele == 4 }).to be true
end

it 'when parameter given then should return true if any one of element satisfy the condition' do
expect([1, 2.91, 3, 4].my_any?(Float)).to be true
end

it 'should return false if empty array' do
expect(empty_array.my_any?).not_to be true
end

it 'when block given then should return false if all element not satisfy the condition' do
expect(arr.my_any? { |ele| ele > 10 }).not_to be true
end

it 'when parameter given then should return false if all of element not satisfy the condition' do
expect([1, 2, 3, 4].my_any? { |ele| ele > 5 }).not_to be true
end
end

describe 'my_none? method' do
it 'when null and false is an element it should return true' do
expect(false_array.my_none?).to be true
end

it 'should return true if empty array' do
expect(empty_array.my_none?).to be true
end

it 'when block given then should return true if no element satisfy the condition' do
expect(arr.my_none? { |ele| ele < 0 }).to be true
end

it 'when parameter given then should return true if no element satisfy the condition' do
expect([1, 2, 3, 4].my_none? { |ele| ele < 0 }).to be true
end

it 'when block given then should return false if any one of element satisfy the condition' do
expect(arr.my_none? { |ele| ele == 4 }).not_to be true
end

it 'when parameter given then should return false if any one of element satisfy the condition' do
expect([1, 2.91, 3, 4].my_none?(Float)).not_to be true
end
end

describe 'my_count method' do
it 'return length of array' do
expect(arr.my_count).to eq(arr.length)
end

it 'return the count of the repeated element' do
arr1 = [1, 2, 2, 3, 2]
expect(arr1.my_count(2)).to eq(3)
end

it 'return the count of the odd element' do
arr1 = [1, 2, 2, 3, 2]
expect(arr1.my_count(&:odd?)).to eq(2)
end

it 'return the count of the repeated element' do
expect(empty_array.my_count).to eq(0)
end
end

describe 'my_map method' do
it 'when no block it should return Enumerator' do
expect(arr.my_map).to be_a(Enumerator)
end

it 'should return the array after calculation with each element' do
arr1 = [3, 4, 5]
arr2 = [6, 8, 10]
arr3 = arr1.my_map { |ele| ele * 2 }
expect(arr3).to eq(arr2)
end

it 'should returns thearray with all elements in upper case' do
expect(%w[Deepak].my_map(&:upcase)).to eql(%w[DEEPAK])
end

it 'should returns ehe array with all elements converted to integrs' do
expect(%w[1 2 3 4 5].my_map(&:to_i)).to eql([1, 2, 3, 4, 5])
end

it 'should returns the class of each element in the given array' do
expect([10, 'sweet', :a].my_map(&:class)).to eql([Integer, String, Symbol])
end
end

describe 'my_inject method' do
it 'Returns local jump error if no block given' do
expect { arr.my_inject }.to raise_error(LocalJumpError)
end

it 'should return the sum' do
arr2 = [5, 6, 7, 8, 9, 10]
arr3 = arr2.my_inject { |sum, n| sum + n }
expect(arr3).to eq(45)
end

it 'should return the product' do
arr2 = [5, 6, 7, 8, 9, 10]
arr3 = arr2.my_inject(1) { |product, n| product * n }
expect(arr3).to eq(151_200)
end

it 'should return the longest word' do
longest = %w[cat sheep bear]
long_word = longest.my_inject { |memo, word| memo.length > word.length ? memo : word }
expect(long_word).to eq('sheep')
end

it 'should return the sum of all element' do
expect((1..4).my_inject(:+)).to eql(10)
end
end

describe 'multiply_els method' do
it 'returns result from multiply_els method' do
expect(multiply_els(1..5)).to eq(120)
end
end
end
98 changes: 98 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
# # This allows you to limit a spec run to individual examples or groups
# # you care about by tagging them with `:focus` metadata. When nothing
# # is tagged with `:focus`, all examples get run. RSpec also provides
# # aliases for `it`, `describe`, and `context` that include `:focus`
# # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
# config.filter_run_when_matching :focus
#
# # Allows RSpec to persist some state between runs in order to support
# # the `--only-failures` and `--next-failure` CLI options. We recommend
# # you configure your source control system to ignore this file.
# config.example_status_persistence_file_path = "spec/examples.txt"
#
# # Limits the available syntax to the non-monkey patched syntax that is
# # recommended. For more details, see:
# # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
# config.disable_monkey_patching!
#
# # This setting enables warnings. It's recommended, but in some cases may
# # be too noisy due to issues in dependencies.
# config.warnings = true
#
# # Many RSpec users commonly either run the entire suite or an individual
# # file, and it's useful to allow more verbose output when running an
# # individual spec file.
# if config.files_to_run.one?
# # Use the documentation formatter for detailed output,
# # unless a formatter has already been configured
# # (e.g. via a command-line flag).
# config.default_formatter = "doc"
# end
#
# # Print the 10 slowest examples and example groups at the
# # end of the spec run, to help surface which specs are running
# # particularly slow.
# config.profile_examples = 10
#
# # Run specs in random order to surface order dependencies. If you find an
# # order dependency and want to debug it, you can fix the order by providing
# # the seed, which is printed after each run.
# # --seed 1234
# config.order = :random
#
# # Seed global randomization in this process using the `--seed` CLI option.
# # Setting this allows you to use `--seed` to deterministically reproduce
# # test failures related to randomization by passing the same `--seed` value
# # as the one that triggered the failure.
# Kernel.srand config.seed
end