Skip to content

Commit

Permalink
improved deprecation_tests (see #311)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jun 14, 2021
1 parent 7e9c9cd commit 0f1a7f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/pagy/deprecation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ class << self

# deprecated variables
def deprecated_var(var, val, new_var, new_val)
value = new_val || val
Warning.warn %([PAGY WARNING] deprecated use of `#{var}` var will not be supported in 5.0! Use `#{new_var}: #{value.inspect}` instead.)
value = val || new_val
Warning.warn %([PAGY WARNING] deprecated use of '#{var}' var will not be supported in 5.0! Use '#{new_var}: #{value.inspect}' instead.)
value
end

# deprecated pagy_url_for argument order
def deprecated_order(pagy, page)
Warning.warn '[PAGY WARNING] inverted use of pagy/page in pagy_url_for will not be supported in 5.0! Use pagy_url_for(pagy, page) instead.'
Warning.warn %([PAGY WARNING] inverted use of pagy/page in pagy_url_for will not be supported in 5.0! Use pagy_url_for(pagy, page) instead.)
[page, pagy]
end


# deprecated posiitioal arguments
def deprecated_arg(arg, val, new_key, new_val)
value = new_val || val # we use the new_val if present
Warning.warn %([PAGY WARNING] deprecated use of positional `#{arg}` arg will not be supported in 5.0! Use only the keyword arg `#{new_key}: #{value.inspect}` instead.)
value = val || new_val # we use the new_val if present
Warning.warn %([PAGY WARNING] deprecated use of positional '#{arg}' arg will not be supported in 5.0! Use only the keyword arg '#{new_key}: #{value.inspect}' instead.)
value
end

Expand Down
1 change: 1 addition & 0 deletions tasks/test.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test_tasks = {}
console
support
trim
deprecation
].each do |name|
task_name = :"test_#{name}"
file_path = "test/**/#{name}_test.rb"
Expand Down
51 changes: 47 additions & 4 deletions test/pagy/deprecation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,56 @@

describe 'pagy/deprecation' do

STYLES=%w[bootstrap bulma foundation materialize navs semantic uikit].freeze # rubocop:disable Lint/ConstantDefinitionInBlock # we are actually in a test class
STYLES.each { |name| require "pagy/extras/#{name}" }
require 'pagy/extras/items'
require 'pagy/extras/support'

require_relative '../mock_helpers/view'
let(:view) { MockView.new }
let(:pagy) { Pagy.new(count: 100)}

describe 'Pagy.deprecated_var' do
it 'deprecates arg and returns right value' do
assert_output('', "[PAGY WARNING] deprecated use of `var` var will not be supported in 5.0! Use `new_var: \"new-value\"` instead.") do
_(Pagy.deprecated_var(:var, 'deprecated-val', :new_var, 'new-value')).must_equal 'new-value'
assert_output('', "[PAGY WARNING] deprecated use of 'var' var will not be supported in 5.0! Use 'new_var: \"deprecated-val\"' instead.") do
_(Pagy.deprecated_var(:var, 'deprecated-val', :new_var, 'new-value')).must_equal 'deprecated-val'
end
end
it 'works with anchor/fragment variable' do
assert_output('', "[PAGY WARNING] deprecated use of 'anchor' var will not be supported in 5.0! Use 'fragment: \"#fragment\"' instead.") do
Pagy.new(count: 100, anchor: '#fragment')
end
end
end

describe 'Pagy.deprecated_arg' do
it 'deprecates arg and returns right value' do
assert_output('', "[PAGY WARNING] deprecated use of positional `arg` arg will not be supported in 5.0! Use only the keyword arg `new_key: \"new-value\"` instead.") do
_(Pagy.deprecated_arg(:arg, 'deprecated-val', :new_key, 'new-value')).must_equal 'new-value'
assert_output('', "[PAGY WARNING] deprecated use of positional 'arg' arg will not be supported in 5.0! Use only the keyword arg 'new_key: \"deprecated-val\"' instead.") do
_(Pagy.deprecated_arg(:arg, 'deprecated-val', :new_key, 'new-value')).must_equal 'deprecated-val'
end
end
it 'works with id/pagy_id arguments' do
STYLES.each do |name|
name_fragment = name == 'navs' ? '' : "#{name}_"
assert_output('', "[PAGY WARNING] deprecated use of positional 'id' arg will not be supported in 5.0! Use only the keyword arg 'pagy_id: \"pagy-id\"' instead.") do
view.send(:"pagy_#{name_fragment}nav_js", pagy, 'pagy-id')
end
assert_output('', "[PAGY WARNING] deprecated use of positional 'id' arg will not be supported in 5.0! Use only the keyword arg 'pagy_id: \"pagy-id\"' instead.") do
view.send(:"pagy_#{name_fragment}combo_nav_js", pagy, 'pagy-id' )
end
end
assert_output('', "[PAGY WARNING] deprecated use of positional 'id' arg will not be supported in 5.0! Use only the keyword arg 'pagy_id: \"pagy-id\"' instead.") do
view.pagy_items_selector_js(pagy, 'pagy-id')
end
end
it 'works with text and extra_link arguments' do
%i[pagy_prev_link pagy_next_link].each do |meth|
assert_output('', "[PAGY WARNING] deprecated use of positional 'text' arg will not be supported in 5.0! Use only the keyword arg 'text: \"my-text\"' instead.") do
view.send(meth, pagy, 'my-text')
end
assert_output('', "[PAGY WARNING] deprecated use of positional 'link_extra' arg will not be supported in 5.0! Use only the keyword arg 'link_extra: \"link-extra\"' instead.") do
view.send(meth, pagy, nil, 'link-extra')
end
end
end
end
Expand All @@ -26,5 +64,10 @@
_(Pagy.deprecated_order('page', 'pagy')).must_equal %w[pagy page]
end
end
it 'works with pagy_url_for' do
assert_output('', "[PAGY WARNING] inverted use of pagy/page in pagy_url_for will not be supported in 5.0! Use pagy_url_for(pagy, page) instead.") do
view.pagy_url_for(2, pagy)
end
end
end
end

0 comments on commit 0f1a7f2

Please sign in to comment.