From 0f1a7f2b11ec9a200a5e7b2d59afb75b5896456f Mon Sep 17 00:00:00 2001
From: Domizio Demichelis
Date: Mon, 14 Jun 2021 14:19:17 +0700
Subject: [PATCH] improved deprecation_tests (see #311)
---
lib/pagy/deprecation.rb | 10 +++----
tasks/test.rake | 1 +
test/pagy/deprecation_test.rb | 51 ++++++++++++++++++++++++++++++++---
3 files changed, 53 insertions(+), 9 deletions(-)
diff --git a/lib/pagy/deprecation.rb b/lib/pagy/deprecation.rb
index 17f821026..304663d74 100644
--- a/lib/pagy/deprecation.rb
+++ b/lib/pagy/deprecation.rb
@@ -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
diff --git a/tasks/test.rake b/tasks/test.rake
index c90cd4899..cabd8280d 100644
--- a/tasks/test.rake
+++ b/tasks/test.rake
@@ -19,6 +19,7 @@ test_tasks = {}
console
support
trim
+ deprecation
].each do |name|
task_name = :"test_#{name}"
file_path = "test/**/#{name}_test.rb"
diff --git a/test/pagy/deprecation_test.rb b/test/pagy/deprecation_test.rb
index 137fbb91a..704f078f4 100644
--- a/test/pagy/deprecation_test.rb
+++ b/test/pagy/deprecation_test.rb
@@ -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
@@ -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