Skip to content

Commit

Permalink
Merge pull request #1 from Zandelok/workflow
Browse files Browse the repository at this point in the history
(feat): spec coverage, rubocop and github ci
  • Loading branch information
Zandelok authored Nov 21, 2022
2 parents 1fe121c + 9a18a2e commit dfdfafc
Show file tree
Hide file tree
Showing 30 changed files with 529 additions and 27 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/ruby_github_workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Ruby

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
rspec:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@359bebbc29cbe6c87da6bc9ea3bc930432750108
with:
ruby-version: '3.1.0'
bundler-cache: true

- name: Run tests
run: bundle exec rspec

rubocop:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@359bebbc29cbe6c87da6bc9ea3bc930432750108
with:
ruby-version: '3.1.0'
bundler-cache: true

- name: Rubocop check
run: bundle exec rubocop
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Gemfile.lock
coverage/
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require: rubocop-rspec

AllCops:
NewCops: enable

Style/Documentation:
Enabled: false

RSpec/NamedSubject:
Enabled: false

RSpec/NestedGroups:
Enabled: false
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'faker'
gem 'rspec'
gem 'rubocop'
gem 'rubocop-rspec'
gem 'simplecov'
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Zandelok::Functions.multiply(1, 2, 3, 4) # will return 24
Zandelok::Functions.missing_numbers([1, 2, 3, 10]) # will return [4, 5, 6, 7, 8, 9]
```

### `missing_numbers` generate hashtag from array or string
### `missing_numbers` generate hashtag from array or string (hashtag max length 140 symbols)
```ruby
Zandelok::Functions.generate_hashtag([1, 2, 3, 10]) # will return '#12310'

Expand Down
4 changes: 3 additions & 1 deletion lib/array/count_array.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class Array
def count_elements
uniq.map { |el| [el, count(el)] }.to_h
uniq.to_h { |el| [el, count(el)] }
end
end
2 changes: 2 additions & 0 deletions lib/array/increase_array.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Array
def increase_with_index
map&.with_index { |el, i| el + i }
Expand Down
2 changes: 2 additions & 0 deletions lib/array/select_array.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Array
def select_solo
select { |el| one?(el) }
Expand Down
2 changes: 2 additions & 0 deletions lib/array/split_array.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Array
def split_by_parity
partition(&:even?)
Expand Down
2 changes: 2 additions & 0 deletions lib/errors/data_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Errors
class DataError < StandardError
def initialize(msg = 'Invalid Data')
Expand Down
2 changes: 2 additions & 0 deletions lib/errors/errors.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

require_relative 'data_error'
require_relative 'type_error'
2 changes: 2 additions & 0 deletions lib/errors/type_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Errors
class TypeError < StandardError
def initialize(msg = 'Invalid Type')
Expand Down
16 changes: 16 additions & 0 deletions lib/helpers/hashtag_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative '../errors/errors'

module Helpers
class HashtagHelper
def self.make_hashtag(array)
raise Errors::DataError, 'Hashtag should consist of one or more symbols' if array.empty?

hashtag = "##{array.map(&:to_s).map(&:capitalize).join}"
raise Errors::DataError, 'Hashtag max length 140 symbols' if hashtag.length > 140

hashtag
end
end
end
2 changes: 2 additions & 0 deletions lib/string/camelcase_string.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class String
def camelcase
str = split(/[^a-zA-Z]/)
Expand Down
2 changes: 2 additions & 0 deletions lib/string/count_string.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class String
def count_chars
chars.group_by(&:itself).transform_values(&:count)
Expand Down
2 changes: 2 additions & 0 deletions lib/string/palindrome_string.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class String
def palindrome?
downcase.eql?(downcase.reverse)
Expand Down
17 changes: 11 additions & 6 deletions lib/zandelok/functions.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
# frozen_string_literal: true

require_relative '../errors/errors'
require_relative 'helpers/hashtag_helper'
require_relative '../helpers/hashtag_helper'

module Zandelok
class Functions
def self.multiply(*args)
raise Errors::TypeError, 'Method accept only integers' unless args.all?(Integer)
raise Errors::DataError, 'Method should accept two or more values' unless args.size >= 2

args.reduce(:*)
end

def self.missing_numbers(numbers)
raise Errors::TypeError, 'Method accept only array' unless numbers.is_a?(Array)
raise Errors::DataError, "Array should consist of two or more variables" unless numbers.size >= 2
raise Errors::TypeError, 'Array should consist of integers only' unless numbers.all?(Integer)
raise Errors::DataError, 'Array should consist of two or more variables' unless numbers.size >= 2

[*numbers.min..numbers.max] - numbers
end

def self.generate_hashtag(input)
if input.is_a?(Array)
Zandelok::Helpers::HashtagHelper.make_hashtag(input)
elsif input.is_a?(String)
Zandelok::Helpers::HashtagHelper.make_hashtag(input.split)
case input
when Array
Helpers::HashtagHelper.make_hashtag(input)
when String
Helpers::HashtagHelper.make_hashtag(input.split)
else
raise Errors::TypeError, 'Method accept only array or string'
end
Expand Down
16 changes: 0 additions & 16 deletions lib/zandelok/helpers/hashtag_helper.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/zandelok_custom_functions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative 'array/count_array'
require_relative 'array/increase_array'
require_relative 'array/select_array'
Expand Down
26 changes: 26 additions & 0 deletions spec/array/count_array_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative '../../lib/array/count_array'

describe Array do
describe '#count_elements' do
let(:method) { input.count_elements }

context 'when valid data' do
let(:input) { [1, 1, 2, 3] }
let(:result) { { 1 => 2, 2 => 1, 3 => 1 } }

it 'expect to return hash' do
expect(method).to eq result
end
end

context 'when invalid data' do
let(:input) { 'Test' }

it 'expect to raise NoMethodError' do
expect { method }.to raise_error NoMethodError
end
end
end
end
67 changes: 67 additions & 0 deletions spec/array/increase_array_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

require_relative '../../lib/array/increase_array'

describe Array do
describe '#increase_with_index' do
let(:method) { input.increase_with_index }

context 'when valid data' do
let(:input) { [1, 1, 2, 3] }
let(:result) { [1, 2, 4, 6] }

it 'expect to return increased array' do
expect(method).to eq result
end
end

context 'when invalid data' do
context 'when string passed' do
let(:input) { 'Test' }

it 'expect to raise NoMethodError' do
expect { method }.to raise_error NoMethodError
end
end

context 'when array of strings passed' do
let(:input) { %w[Test Test] }

it 'expect to raise TypeError' do
expect { method }.to raise_error TypeError
end
end
end
end

describe '#increase_with_position' do
let(:method) { input.increase_with_position }

context 'when valid data' do
let(:input) { [1, 1, 2, 3] }
let(:result) { [2, 3, 5, 7] }

it 'expect to return increased array' do
expect(method).to eq result
end
end

context 'when invalid data' do
context 'when string passed' do
let(:input) { 'Test' }

it 'expect to raise NoMethodError' do
expect { method }.to raise_error NoMethodError
end
end

context 'when array of strings passed' do
let(:input) { %w[Test Test] }

it 'expect to raise TypeError' do
expect { method }.to raise_error TypeError
end
end
end
end
end
26 changes: 26 additions & 0 deletions spec/array/select_array_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative '../../lib/array/select_array'

describe Array do
describe '#select_solo' do
let(:method) { input.select_solo }

context 'when valid data' do
let(:input) { [1, 1, 2, 3] }
let(:result) { [2, 3] }

it 'expect to return array with solo values' do
expect(method).to eq result
end
end

context 'when invalid data' do
let(:input) { 'Test' }

it 'expect to raise NoMethodError' do
expect { method }.to raise_error NoMethodError
end
end
end
end
44 changes: 44 additions & 0 deletions spec/array/split_array_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require_relative '../../lib/array/split_array'

describe Array do
describe '#split_by_parity' do
let(:method) { input.split_by_parity }

context 'when valid data' do
let(:input) { [1, 1, 2, 3] }
let(:result) { [[2], [1, 1, 3]] }

it 'expect to return splited array' do
expect(method).to eq result
end
end

context 'when invalid data' do
context 'when string passed' do
let(:input) { 'Test' }

it 'expect to raise NoMethodError' do
expect { method }.to raise_error NoMethodError
end
end

context 'when array with subarrays' do
let(:input) { [[1, 2], [2, 3]] }

it 'expect to raise NoMethodError' do
expect { method }.to raise_error NoMethodError
end
end

context 'when array of strings passed' do
let(:input) { %w[Test Test] }

it 'expect to raise NoMethodError' do
expect { method }.to raise_error NoMethodError
end
end
end
end
end
Loading

0 comments on commit dfdfafc

Please sign in to comment.