Skip to content
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

enumerables test #1

Open
wants to merge 1 commit 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
19 changes: 19 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Linters

on: pull_request

jobs:
rubocop:
name: Rubocop
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-ruby@v1
with:
ruby-version: 2.6.x
- name: Setup Rubocop
run: |
gem install --no-document rubocop:'~>0.81.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
19 changes: 19 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Tests

on: pull_request

jobs:
rspec:
name: RSpec
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-ruby@v1
with:
ruby-version: 2.6.x
- name: Setup RSpec
run: |
[ -f Gemfile ] && bundle
gem install --no-document rspec:'~>3.0'
- name: RSpec Report
run: rspec --force-color --format documentation
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
# testing_enumerables
![](https://img.shields.io/badge/Microverse-blueviolet)

# Testing Enumerables with rspec

This is a project to test Enumerable methods by using rspec tests.
## RSPEC Tests
RSpec is a testing tool for Ruby. RSpec is a simple and powerful tool to test library for Ruby in production applications.
## Built With

![Ruby](https://img.shields.io/badge/ruby-%23CC342D.svg?&style=for-the-badge&logo=ruby&logoColor=white)
![RSpec](https://img.shields.io/badge/rspec-%23CC342D.svg?&style=for-the-badge&logo=ruby&logoColor=white)

## Prerequisites

- Ruby (latest version)
- RSpec gem

## Getting Started

To get a local copy up and running follow these simple example steps in your terminal.

- `git clone https://github.com/AymenBida/Testing_enumerables.git`
- `cd Testing_enumerables`
- `rspec spec/enumerables_spec.rb`
## Authors

👤 **Prerna Priyali**

- Github: [@prernapriyali](https://github.com/prernapriyali)
- Twitter: [@twitterhandle](https://twitter.com/prerna96440861)


## 🤝 Contributing

Contributions, issues, and feature requests are welcome!

## Show your support

Give a ⭐️ if you like this project and follow the creators' github accounts!

## 📝 License

Copyright 2021 Necmi Gunduz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
155 changes: 155 additions & 0 deletions bin/enumerables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/ModuleLength

module Enumerable
def my_each
return to_enum unless block_given?

arr = to_a
arr.length.times { |e| yield(arr[e]) }
self
end

def my_each_with_index
return to_enum unless block_given?

arr = to_a
arr.my_each { |index| yield(arr[index], index) }
self
end

def my_select
if block_given?
arr = []
my_each { |element| arr.push(element) if yield(element) }
arr
else
to_enum(:my_select)
end
end

def my_all?(argm = nil)
if block_given?
my_each { |element| return false if yield(element) == false }
true
elsif argm.nil?
my_each { |n| return false if n.nil? || n == false }
elsif !argm.nil? && (argm.is_a? Class)
my_each { |n| return false if n.is_a?(argm) == false }
elsif !argm.nil? && argm.class == Regexp
my_each { |n| return false unless argm.match(n) }
else
my_each { |n| return false if n != argm }
end
true
end

def my_any?(arg = nil)
if block_given?
my_each { |item| return true if yield(item) }
false
elsif arg.nil?
my_each { |n| return true if n }
elsif !arg.nil? && (arg.is_a? Class)
my_each { |n| return true if n.is_a?(arg) == true }
elsif !arg.nil? && arg.class == Regexp
my_each { |n| return true if arg.match(n) }
else
my_each { |n| return true if n == arg }
end
false
end

def my_none?(arg = nil)
if !block_given? && arg.nil?
my_each { |n| return false if n }
return true
end

if !block_given? && !arg.nil?

if arg.is_a?(Class)
my_each { |n| return false if n.class == arg }
return true
end

if arg.class == Regexp
my_each { |n| return false if arg.match(n) }
return true
end

my_each { |n| return false if n == arg }
return true
end

my_any? { |item| return false if yield(item) }
true
end

def my_count(arg = nil)
count = 0
if block_given?
my_each { |i| count += 1 if yield(i) }
elsif !arg.nil?
my_each { |i| count += 1 if i == arg }
else
my_each { |i| count += 1 if i }
end
count
end

def my_map(proc = nil)
return to_enum(:my_map) unless block_given?

result = []
my_each { |element| result << proc.call(element) } if block_given? && proc
my_each { |element| result << yield(element) } if proc.nil?
result
end

def my_inject(number = nil, symbol = nil)
return raise LocalJumpError, 'no block given' if !block_given? && number.nil? && symbol.nil?

if block_given?
accum = number
my_each do |item|
accum = accum.nil? ? item : yield(accum, item)
end
accum
elsif !number.nil? && (number.is_a?(Symbol) || number.is_a?(String))
accum = nil
my_each do |item|
accum = accum.nil? ? item : accum.send(number, item)
end
accum
elsif !symbol.nil? && (symbol.is_a?(Symbol) || symbol.is_a?(String))
accum = number
my_each do |item|
accum = accum.nil? ? item : accum.send(symbol, item)
end
accum
end
end

p [2, 2, 3, 2].my_inject(:+)
def my_map_proc
arr = []
if block_given?
my_each do |x|
arr.push(yield(x))
end
else
to_enum(:my_map_proc)
end
arr
end
end

def multiply_els(arr)
arr.my_inject { |multiply, element| multiply * element }
end

# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/ModuleLength
Loading