diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index f8fab3c..ea10b9c 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -1,6 +1,8 @@ --- -name: coverage +name: Coverage + on: [push] + jobs: build: runs-on: ubuntu-latest @@ -10,12 +12,8 @@ jobs: uses: ruby/setup-ruby@v1 with: ruby-version: 3.2 - - uses: amancevice/setup-code-climate@v0 - with: - cc_test_reporter_id: ${{ secrets.CC_TEST_REPORTER_ID }} - - run: cc-test-reporter before-build + bundler-cache: true - name: Build and test with RSpec - run: | - bundle install --jobs 4 --retry 3 - bundle exec rspec - - run: cc-test-reporter after-build + run: bundle exec rspec + env: + COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} diff --git a/.github/workflows/danger-comment.yml b/.github/workflows/danger-comment.yml new file mode 100644 index 0000000..e6aa88c --- /dev/null +++ b/.github/workflows/danger-comment.yml @@ -0,0 +1,11 @@ +name: Danger Comment + +on: + workflow_run: + workflows: [Danger] + types: [completed] + +jobs: + comment: + uses: numbata/danger-pr-comment/.github/workflows/danger-comment.yml@main + secrets: inherit diff --git a/.github/workflows/danger.yml b/.github/workflows/danger.yml index c643c4b..76c745c 100644 --- a/.github/workflows/danger.yml +++ b/.github/workflows/danger.yml @@ -1,22 +1,13 @@ ---- -name: danger +name: Danger + on: pull_request: types: [opened, reopened, edited, synchronize] + jobs: danger: - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 2.7 - bundler-cache: true - - name: Run Danger - run: | - # the personal token is public, this is ok, base64 encode to avoid tripping Github - TOKEN=$(echo -n Z2hwX0xNQ3VmanBFeTBvYkZVTWh6NVNqVFFBOEUxU25abzBqRUVuaAo= | base64 --decode) - DANGER_GITHUB_API_TOKEN=$TOKEN bundle exec danger --verbose + uses: numbata/danger-pr-comment/.github/workflows/danger-run.yml@main + secrets: inherit + with: + ruby-version: '3.1' + bundler-cache: true diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9ffe62f..bc70a44 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,5 +1,7 @@ name: Lint + on: [push, pull_request] + jobs: rubocop: name: RuboCop diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cdbde68..a9aa482 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,8 @@ --- -name: test +name: Test + on: [push, pull_request] + jobs: test: runs-on: ubuntu-latest @@ -12,6 +14,7 @@ jobs: - { ruby: '3.0', allowed-failure: false } - { ruby: '3.1', allowed-failure: false } - { ruby: '3.2', allowed-failure: false } + - { ruby: '4.0', allowed-failure: false } - { ruby: 'ruby-head', allowed-failure: true } - { ruby: 'truffleruby-head', allowed-failure: true } - { ruby: 'jruby-head', allowed-failure: true } @@ -21,7 +24,7 @@ jobs: - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.entry.ruby }} - - run: bundle install --jobs=3 --retry=3 --path=vendor/bundle + - run: bundle install --jobs=3 --retry=3 - run: bundle exec rake spec continue-on-error: ${{ matrix.entry.allowed-failure }} - name: Specs for when the i18n gem is not available diff --git a/.rubocop.yml b/.rubocop.yml index 1f7eac4..a71c181 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,3 @@ -require: - - rubocop-rake - - rubocop-rspec - AllCops: TargetRubyVersion: 2.7 NewCops: enable @@ -15,7 +11,7 @@ Metrics/BlockLength: RSpec/SpecFilePathFormat: Enabled: false -RSpec/FilePath: +Style/LineLength: Enabled: false Style/HashEachMethods: @@ -27,4 +23,32 @@ Style/HashTransformKeys: Style/HashTransformValues: Enabled: true +Naming/FileName: + Enabled: false + +Style/Documentation: + Enabled: false + +Naming/MethodParameterName: + Enabled: false + +RSpec/NestedGroups: + Enabled: false + +RSpec/ExampleLength: + Enabled: false + +RSpec/MultipleExpectations: + Enabled: false + +Lint/ConstantDefinitionInBlock: + Enabled: false + +RSpec/LeakyConstantDeclaration: + Enabled: false + inherit_from: .rubocop_todo.yml + +plugins: + - rubocop-rake + - rubocop-rspec diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9039cdc..00ce58d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,72 +1,7 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-02-14 01:34:25 UTC using RuboCop version 1.45.1. +# on 2025-12-30 16:44:41 UTC using RuboCop version 1.82.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. - -# Offense count: 4 -# Configuration parameters: AllowedMethods. -# AllowedMethods: enums -Lint/ConstantDefinitionInBlock: - Exclude: - - 'spec/ruby-enum/enum_spec.rb' - -# Offense count: 1 -# Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, CheckDefinitionPathHierarchyRoots, Regex, IgnoreExecutableScripts, AllowedAcronyms. -# CheckDefinitionPathHierarchyRoots: lib, spec, test, src -# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS -Naming/FileName: - Exclude: - - 'lib/ruby-enum.rb' - -# Offense count: 6 -# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. -# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to -Naming/MethodParameterName: - Exclude: - - 'lib/ruby-enum/enum.rb' - -# Offense count: 3 -# Configuration parameters: CountAsOne. -RSpec/ExampleLength: - Max: 11 - -# Offense count: 4 -RSpec/LeakyConstantDeclaration: - Exclude: - - 'spec/ruby-enum/enum_spec.rb' - -# Offense count: 6 -RSpec/MultipleExpectations: - Max: 11 - -# Offense count: 18 -# Configuration parameters: EnforcedStyle, IgnoreSharedExamples. -# SupportedStyles: always, named_only -RSpec/NamedSubject: - Exclude: - - 'spec/ruby-enum/enum_spec.rb' - -# Offense count: 1 -# Configuration parameters: AllowedGroups. -RSpec/NestedGroups: - Max: 4 - -# Offense count: 4 -# Configuration parameters: AllowedConstants. -Style/Documentation: - Exclude: - - 'spec/**/*' - - 'test/**/*' - - 'lib/ruby-enum/enum.rb' - - 'lib/ruby-enum/errors/base.rb' - - 'lib/ruby-enum/errors/uninitialized_constant_error.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. -# URISchemes: http, https -Layout/LineLength: - Max: 148 diff --git a/CHANGELOG.md b/CHANGELOG.md index f845b29..1a3190e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ -### 1.0.1 (Next) +### 1.1.0 (Next) +* [#54](https://github.com/dblock/ruby-enum/pull/54): Add support for Ruby 4.0 - [@dblock](https://github.com/dblock). +* [#53](https://github.com/dblock/ruby-enum/pull/53): Replace code climate with coveralls - [@dblock](https://github.com/dblock). * Your contribution here. ### 1.0.0 (2023/01/10) diff --git a/Dangerfile b/Dangerfile index a0a4a57..b4bc366 100644 --- a/Dangerfile +++ b/Dangerfile @@ -1,4 +1,6 @@ # frozen_string_literal: true +danger.import_dangerfile(gem: 'danger-pr-comment') + changelog.check! toc.check! diff --git a/Gemfile b/Gemfile index dc4319a..3ce44a3 100644 --- a/Gemfile +++ b/Gemfile @@ -8,14 +8,15 @@ gem 'rake' group :development, :test do gem 'danger' - gem 'danger-changelog', '0.6.1' - gem 'danger-toc', '0.2.0' - gem 'rspec', '~> 3.0' - gem 'rubocop', '~> 1.0' + gem 'danger-changelog' + gem 'danger-pr-comment' + gem 'danger-toc' + gem 'rspec' + gem 'rubocop', '1.82.1' gem 'rubocop-rake' gem 'rubocop-rspec' end group :test do - gem 'simplecov', require: false + gem 'coveralls_reborn', require: false end diff --git a/README.md b/README.md index 49ab445..38aed41 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ Ruby::Enum ========== [![Gem Version](http://img.shields.io/gem/v/ruby-enum.svg)](http://badge.fury.io/rb/ruby-enum) -[![Build Status](https://github.com/dblock/ruby-enum/workflows/test/badge.svg?branch=master)](https://github.com/dblock/ruby-enum/actions) -[![Code Climate](https://codeclimate.com/github/dblock/ruby-enum.svg)](https://codeclimate.com/github/dblock/ruby-enum) +[![Test](https://github.com/dblock/ruby-enum/actions/workflows/test.yml/badge.svg)](https://github.com/dblock/ruby-enum/actions/workflows/test.yml) +[![Coverage Status](https://coveralls.io/repos/github/dblock/ruby-enum/badge.svg?branch=master)](https://coveralls.io/github/dblock/ruby-enum?branch=master) Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.com/enumerations-and-ruby), and improved upon [another blog post](http://code.dblock.org/how-to-define-enums-in-ruby). diff --git a/lib/ruby-enum/errors/base.rb b/lib/ruby-enum/errors/base.rb index 5ab2ee1..5e2dfdb 100644 --- a/lib/ruby-enum/errors/base.rb +++ b/lib/ruby-enum/errors/base.rb @@ -26,10 +26,10 @@ def compose_message(key, attributes = {}) "\nSummary:\n #{@summary}" + "\nResolution:\n #{@resolution}" end - private - BASE_KEY = 'ruby.enum.errors.messages' # :nodoc: + private + # Given the key of the specific error and the options hash, translate the # message. # diff --git a/lib/ruby-enum/version.rb b/lib/ruby-enum/version.rb index fc7588c..ce01fd3 100644 --- a/lib/ruby-enum/version.rb +++ b/lib/ruby-enum/version.rb @@ -2,6 +2,6 @@ module Ruby module Enum - VERSION = '1.0.1' + VERSION = '1.1.0' end end diff --git a/spec/ruby-enum/enum/case_spec.rb b/spec/ruby-enum/enum/case_spec.rb index 8e7c588..f3b6395 100644 --- a/spec/ruby-enum/enum/case_spec.rb +++ b/spec/ruby-enum/enum/case_spec.rb @@ -2,55 +2,56 @@ require 'spec_helper' -RSpec.describe Ruby::Enum::Case do - test_enum = - Class.new do - include Ruby::Enum - include Ruby::Enum::Case - - define :RED, :red - define :GREEN, :green - define :BLUE, :blue - end +module Case + class Colors + include Ruby::Enum + include Ruby::Enum::Case + + define :RED, :red + define :GREEN, :green + define :BLUE, :blue + end +end +RSpec.describe Ruby::Enum::Case do describe '.case' do context 'when all cases are defined' do - subject { test_enum.case(test_enum::RED, cases) } + subject { Case::Colors.case(Case::Colors::RED, cases) } let(:cases) do { - [test_enum::RED, test_enum::GREEN] => -> { 'red or green' }, - test_enum::BLUE => -> { 'blue' } + [Case::Colors::RED, Case::Colors::GREEN] => -> { 'red or green' }, + Case::Colors::BLUE => -> { 'blue' } } end it { is_expected.to eq('red or green') } context 'when the value is nil' do - subject { test_enum.case(nil, cases) } + subject { Case::Colors.case(nil, cases) } it { is_expected.to be_nil } end context 'when the value is empty' do - subject { test_enum.case('', cases) } + subject { Case::Colors.case('', cases) } it { is_expected.to be_nil } end context 'when the value is the value of the enum' do - subject { test_enum.case(:red, cases) } + subject { Case::Colors.case(:red, cases) } it { is_expected.to eq('red or green') } end context 'when the value is used inside the lambda' do - subject { test_enum.case(test_enum::RED, cases) } + subject { Case::Colors.case(Case::Colors::RED, cases) } let(:cases) do { - [test_enum::RED, test_enum::GREEN] => ->(color) { "is #{color}" }, - test_enum::BLUE => -> { 'blue' } + [Case::Colors::RED, Case::Colors::GREEN] => ->(color) { "is #{color}" }, + Case::Colors::BLUE => -> { 'blue' } } end @@ -60,12 +61,12 @@ context 'when there are mutliple matches' do subject do - test_enum.case( - test_enum::RED, + Case::Colors.case( + Case::Colors::RED, { - [test_enum::RED, test_enum::GREEN] => -> { 'red or green' }, - test_enum::RED => -> { 'red' }, - test_enum::BLUE => -> { 'blue' } + [Case::Colors::RED, Case::Colors::GREEN] => -> { 'red or green' }, + Case::Colors::RED => -> { 'red' }, + Case::Colors::BLUE => -> { 'blue' } } ) end @@ -76,9 +77,9 @@ context 'when not all cases are defined' do it 'raises an error' do expect do - test_enum.case( - test_enum::RED, - { [test_enum::RED, test_enum::GREEN] => -> { 'red or green' } } + Case::Colors.case( + Case::Colors::RED, + { [Case::Colors::RED, Case::Colors::GREEN] => -> { 'red or green' } } ) end.to raise_error(Ruby::Enum::Case::ClassMethods::NotAllCasesHandledError) end @@ -87,10 +88,10 @@ context 'when not all cases are defined but :else is specified (default case)' do it 'does not raise an error' do expect do - result = test_enum.case( - test_enum::BLUE, + result = Case::Colors.case( + Case::Colors::BLUE, { - [test_enum::RED, test_enum::GREEN] => -> { 'red or green' }, + [Case::Colors::RED, Case::Colors::GREEN] => -> { 'red or green' }, else: -> { 'blue' } } ) @@ -103,11 +104,11 @@ context 'when a superfluous case is defined' do it 'raises an error' do expect do - test_enum.case( - test_enum::RED, + Case::Colors.case( + Case::Colors::RED, { - [test_enum::RED, test_enum::GREEN] => -> { 'red or green' }, - test_enum::BLUE => -> { 'blue' }, + [Case::Colors::RED, Case::Colors::GREEN] => -> { 'red or green' }, + Case::Colors::BLUE => -> { 'blue' }, :something => -> { 'green' } } ) diff --git a/spec/ruby-enum/enum_spec.rb b/spec/ruby-enum/enum_spec.rb index bd0f1c9..a3d811e 100644 --- a/spec/ruby-enum/enum_spec.rb +++ b/spec/ruby-enum/enum_spec.rb @@ -2,22 +2,21 @@ require 'spec_helper' -class Colors - include Ruby::Enum - - define :RED, 'red' - define :GREEN, 'green' -end +describe Ruby::Enum do + class Colors + include Ruby::Enum -class FirstSubclass < Colors - define :ORANGE, 'orange' -end + define :RED, 'red' + define :GREEN, 'green' + end -class SecondSubclass < FirstSubclass - define :PINK, 'pink' -end + class FirstSubclass < Colors + define :ORANGE, 'orange' + end -describe Ruby::Enum do + class SecondSubclass < FirstSubclass + define :PINK, 'pink' + end it 'returns an enum value' do expect(Colors::RED).to eq 'red' expect(Colors::GREEN).to eq 'green' @@ -158,6 +157,18 @@ class SecondSubclass < FirstSubclass it 'returns values' do expect(Colors.values).to eq(%w[red green]) end + + context 'when a subclass is defined' do + it 'returns all values' do + expect(FirstSubclass.values).to eq(%w[red green orange]) + end + end + + context 'when a subclass of a subclass is defined' do + it 'returns all values' do + expect(SecondSubclass.values).to eq(%w[red green orange pink]) + end + end end describe '#to_h' do @@ -250,29 +261,23 @@ class EmptyEnums end context 'when defining a 2 level depth subclass' do - subject { SecondSubclass } - it 'contains its own enums and all the enums defined in the parent classes' do - expect(subject::RED).to eq 'red' - expect(subject::GREEN).to eq 'green' - expect(subject::ORANGE).to eq 'orange' - expect(subject::PINK).to eq 'pink' + expect(SecondSubclass::RED).to eq 'red' + expect(SecondSubclass::GREEN).to eq 'green' + expect(SecondSubclass::ORANGE).to eq 'orange' + expect(SecondSubclass::PINK).to eq 'pink' end describe '#values' do - subject { SecondSubclass.values } - it 'contains the values from all of the parent classes' do - expect(subject).to eq(%w[red green orange pink]) + expect(SecondSubclass.values).to eq(%w[red green orange pink]) end end end describe '#values' do - subject { FirstSubclass.values } - it 'contains the values from the parent class' do - expect(subject).to eq(%w[red green orange]) + expect(FirstSubclass.values).to eq(%w[red green orange]) end end end @@ -280,41 +285,40 @@ class EmptyEnums describe 'default value' do class Default include Ruby::Enum + define :KEY end - subject { Default::KEY } - it 'equals the key' do - expect(subject).to eq(:KEY) + expect(Default::KEY).to eq(:KEY) end end describe 'non constant definitions' do class States include Ruby::Enum + define :created, 'Created' define :published, 'Published' define :undefined end - subject { States } it 'behaves like an enum' do - expect(subject.created).to eq 'Created' - expect(subject.published).to eq 'Published' - expect(subject.undefined).to eq :undefined + expect(States.created).to eq 'Created' + expect(States.published).to eq 'Published' + expect(States.undefined).to eq :undefined - expect(subject.key?(:created)).to be true - expect(subject.key('Created')).to eq :created + expect(States.key?(:created)).to be true + expect(States.key('Created')).to eq :created - expect(subject.value?('Created')).to be true - expect(subject.value(:created)).to eq 'Created' + expect(States.value?('Created')).to be true + expect(States.value(:created)).to eq 'Created' - expect(subject.key?(:undefined)).to be true - expect(subject.key(:undefined)).to eq :undefined + expect(States.key?(:undefined)).to be true + expect(States.key(:undefined)).to eq :undefined - expect(subject.value?(:undefined)).to be true - expect(subject.value(:undefined)).to eq :undefined + expect(States.value?(:undefined)).to be true + expect(States.value(:undefined)).to eq :undefined end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 38a174f..bdc51a2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,8 +4,8 @@ require 'rubygems' -require 'simplecov' -SimpleCov.start +require 'coveralls' +Coveralls.wear! require 'rspec' require 'ruby-enum' diff --git a/spec_i18n/spec/i18n_spec.rb b/spec_i18n/spec/i18n_spec.rb index 653b9ed..4de14ec 100644 --- a/spec_i18n/spec/i18n_spec.rb +++ b/spec_i18n/spec/i18n_spec.rb @@ -2,18 +2,20 @@ require 'spec_helper' -test_class = Class.new do - include Ruby::Enum +module I18n + class Colors + include Ruby::Enum - define :RED, 'red' - define :GREEN, 'green' + define :RED, 'red' + define :GREEN, 'green' + end end describe Ruby::Enum do context 'when the i18n gem is not loaded' do it 'raises UninitializedConstantError on an invalid constant' do expect do - test_class::ANYTHING + I18n::Colors::ANYTHING end.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /ruby.enum.errors.messages.uninitialized_constant.summary/ end @@ -24,7 +26,7 @@ it 'raises DuplicateKeyError' do expect do - test_class.class_eval do + I18n::Colors.class_eval do define :RED, 'some' end end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /ruby.enum.errors.messages.duplicate_key.message/ @@ -38,7 +40,7 @@ it 'raises a DuplicateValueError' do expect do - test_class.class_eval do + I18n::Colors.class_eval do define :Other, 'red' end end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /ruby.enum.errors.messages.duplicate_value.summary/