diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2018-08-30 14:05:00 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2018-09-11 13:45:49 +0300 |
commit | 71fefe147475bb85c67de95fba8211385cfb0a79 (patch) | |
tree | 12a8d1409f699ccd0832f6a26dad055c05b19898 /spec/lib/gitlab/patch | |
parent | c56f2b96159afaf6f1e0831d0e7a756a40568cab (diff) |
Properly implement prepending for Concern
Diffstat (limited to 'spec/lib/gitlab/patch')
-rw-r--r-- | spec/lib/gitlab/patch/prependable_spec.rb | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/spec/lib/gitlab/patch/prependable_spec.rb b/spec/lib/gitlab/patch/prependable_spec.rb new file mode 100644 index 00000000000..1c90a24c0e8 --- /dev/null +++ b/spec/lib/gitlab/patch/prependable_spec.rb @@ -0,0 +1,121 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +# Patching ActiveSupport::Concern +require_relative '../../../../config/initializers/0_as_concern' + +describe Gitlab::Patch::Prependable do + let(:prepended_modules) { [] } + + let(:ee) do + # So that block in Module.new could see them + prepended_modules_ = prepended_modules + + Module.new do + extend ActiveSupport::Concern + + class_methods do + def class_name + super.tr('C', 'E') + end + end + + this = self + prepended do + prepended_modules_ << this + end + + def name + super.tr('c', 'e') + end + end + end + + let(:ce) do + prepended_modules_ = prepended_modules + ee_ = ee + + Module.new do + extend ActiveSupport::Concern + prepend ee_ + + class_methods do + def class_name + 'CE' + end + end + + this = self + prepended do + prepended_modules_ << this + end + + def name + 'ce' + end + end + end + + describe 'a class including a concern prepending a concern' do + subject { Class.new.include(ce) } + + it 'returns values from prepended module ee' do + expect(subject.new.name).to eq('ee') + expect(subject.class_name).to eq('EE') + end + + it 'prepends only once' do + ce.prepend(ee) + ce.prepend(ee) + + subject + + expect(prepended_modules).to eq([ee, ce]) + end + end + + describe 'a class prepending a concern prepending a concern' do + subject { Class.new.prepend(ce) } + + it 'returns values from prepended module ce' do + expect(subject.new.name).to eq('ce') + expect(subject.class_name).to eq('CE') + end + + it 'prepends only once' do + subject.prepend(ce) + + expect(prepended_modules).to eq([ee, ce]) + end + end + + describe 'a class prepending a concern' do + subject do + ee_ = ee + + Class.new do + prepend ee_ + + def self.class_name + 'CE' + end + + def name + 'ce' + end + end + end + + it 'returns values from prepended module ee' do + expect(subject.new.name).to eq('ee') + expect(subject.class_name).to eq('EE') + end + + it 'prepends only once' do + subject.prepend(ee) + + expect(prepended_modules).to eq([ee]) + end + end +end |