Skip to content

Commit

Permalink
Merge pull request #640 from sparklemotion/flavorjones-frozen-string-…
Browse files Browse the repository at this point in the history
…literal-tests

test: update tests to use frozen string literals
  • Loading branch information
flavorjones committed Mar 24, 2024
2 parents d353494 + 608f729 commit 3509024
Show file tree
Hide file tree
Showing 49 changed files with 79 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -49,7 +49,7 @@ namespace "rubocop" do

desc "Run rubocop string literals check"
task :frozen_string_literals do
sh "rubocop lib --auto-correct-all --only Style/FrozenStringLiteralComment"
sh "rubocop lib test --auto-correct-all --only Style/FrozenStringLiteralComment"
end
end

Expand Down
3 changes: 2 additions & 1 deletion test/test_mechanize.rb
@@ -1,4 +1,5 @@
# coding: utf-8
# frozen_string_literal: true

require 'mechanize/test_case'

Expand Down Expand Up @@ -965,7 +966,7 @@ def test_post_file_upload_nonascii
})

assert_match(
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"".force_encoding(Encoding::ASCII_8BIT),
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"".dup.force_encoding(Encoding::ASCII_8BIT),
page.body
)
assert_match("Content-Type: application/zip", page.body)
Expand Down
13 changes: 7 additions & 6 deletions test/test_mechanize_cookie.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

module Enumerable
Expand Down Expand Up @@ -301,7 +302,7 @@ def test_parse_valid_cookie
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')

cookie_params.keys.combine.each do |c|
cookie_text = "#{cookie_value}; "
cookie_text = +"#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
Expand Down Expand Up @@ -336,7 +337,7 @@ def test_parse_valid_cookie_empty_value
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')

cookie_params.keys.combine.each do |c|
cookie_text = "#{cookie_value}; "
cookie_text = +"#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
Expand Down Expand Up @@ -373,7 +374,7 @@ def test_cookie_using_url_path

cookie_params.keys.combine.each do |c|
next if c.find { |k| k == 'path' }
cookie_text = "#{cookie_value}; "
cookie_text = +"#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
Expand Down Expand Up @@ -410,7 +411,7 @@ def test_cookie_with_secure

cookie_params.keys.combine.each do |c|
next unless c.find { |k| k == 'secure' }
cookie_text = "#{cookie_value}; "
cookie_text = +"#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
Expand Down Expand Up @@ -446,7 +447,7 @@ def test_parse_cookie_no_spaces
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')

cookie_params.keys.combine.each do |c|
cookie_text = "#{cookie_value};"
cookie_text = +"#{cookie_value};"
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
Expand Down Expand Up @@ -530,7 +531,7 @@ def test_cookie_httponly
expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')

cookie_params.keys.combine.each do |c|
cookie_text = "#{cookie_value}; "
cookie_text = +"#{cookie_value}; "
c.each_with_index do |key, idx|
if idx == (c.length - 1)
cookie_text << "#{cookie_params[key]}"
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_cookie_jar.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'
require 'fileutils'

Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_directory_saver.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeDirectorySaver < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_download.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeDownload < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_element_not_found_error.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeRedirectLimitReachedError < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_file.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFile < Mechanize::TestCase
Expand Down
3 changes: 2 additions & 1 deletion test/test_mechanize_file_connection.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFileConnection < Mechanize::TestCase
Expand All @@ -7,7 +8,7 @@ def test_request
uri = URI.parse "file://#{file_path}"
conn = Mechanize::FileConnection.new

body = ''
body = +''

conn.request uri, nil do |response|
assert_equal(file_path, response.file_path)
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_file_request.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFileRequest < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_file_response.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFileResponse < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_file_saver.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFileSaver < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeForm < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_check_box.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormCheckBox < Mechanize::TestCase
Expand Down
3 changes: 2 additions & 1 deletion test/test_mechanize_form_encoding.rb
@@ -1,4 +1,5 @@
# coding: utf-8
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormEncoding < Mechanize::TestCase
Expand All @@ -8,7 +9,7 @@ class TestMechanizeFormEncoding < Mechanize::TestCase

INPUTTED_VALUE = "テスト" # "test" in Japanese UTF-8 encoding
CONTENT_ENCODING = 'Shift_JIS' # one of Japanese encoding
encoded_value = "\x83\x65\x83\x58\x83\x67".force_encoding(::Encoding::SHIFT_JIS) # "test" in Japanese Shift_JIS encoding
encoded_value = "\x83\x65\x83\x58\x83\x67".dup.force_encoding(::Encoding::SHIFT_JIS) # "test" in Japanese Shift_JIS encoding
EXPECTED_QUERY = "first_name=#{CGI.escape(encoded_value)}&first_name=&gender=&green%5Beggs%5D="

ENCODING_ERRORS = [EncodingError, Encoding::ConverterNotFoundError] # and so on
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_field.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormField < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_file_upload.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormFileUpload < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_image_button.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormImageButton < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_keygen.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormKeygen < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_multi_select_list.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormMultiSelectList < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_option.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormOption < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_radio_button.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormRadioButton < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_select_list.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormSelectList < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_form_textarea.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFormTextarea < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_headers.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeHeaders < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_history.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeHistory < Mechanize::TestCase
Expand Down
7 changes: 4 additions & 3 deletions test/test_mechanize_http_agent.rb
@@ -1,4 +1,5 @@
# coding: utf-8
# frozen_string_literal: true

require 'mechanize/test_case'

Expand Down Expand Up @@ -1027,7 +1028,7 @@ def test_response_content_encoding_gzip_encoding_bad

body = @agent.response_content_encoding @res, body_io

expected = "test\xB2"
expected = +"test\xB2"
expected.force_encoding Encoding::BINARY if have_encoding?

content = body.read
Expand Down Expand Up @@ -1432,11 +1433,11 @@ def test_response_read_file

io = @agent.response_read res, req, uri

expected = \n".force_encoding(Encoding::BINARY)
expected = \n".dup.force_encoding(Encoding::BINARY)

# Ruby 1.8.7 doesn't let us set the write mode of the tempfile to binary,
# so we should expect an inserted carriage return on some platforms
expected_with_carriage_return = \r\n".force_encoding(Encoding::BINARY)
expected_with_carriage_return = \r\n".dup.force_encoding(Encoding::BINARY)

body = io.read
assert_match(/^(#{expected}|#{expected_with_carriage_return})$/m, body)
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_http_auth_challenge.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeHttpAuthChallenge < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_http_auth_realm.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeHttpAuthRealm < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_http_auth_store.rb
@@ -1,4 +1,5 @@
# coding: utf-8
# frozen_string_literal: true

require 'mechanize/test_case'

Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_http_content_disposition_parser.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeHttpContentDispositionParser < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_http_www_authenticate_parser.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeHttpWwwAuthenticateParser < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_image.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeImage < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_link.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeLink < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_page.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizePage < Mechanize::TestCase
Expand Down
11 changes: 6 additions & 5 deletions test/test_mechanize_page_encoding.rb
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# frozen_string_literal: true
require 'mechanize/test_case'

# tests for Page encoding and charset and parsing
Expand All @@ -12,7 +13,7 @@ def setup

@uri = URI('http://localhost/')
@response_headers = { 'content-type' => 'text/html' }
@body = '<title>hi</title>'
@body = +'<title>hi</title>'
end

def util_page body = @body, headers = @response_headers
Expand Down Expand Up @@ -118,7 +119,7 @@ def test_page_meta_charset_handles_whitespace
end

def test_meta_charset
body = '<meta http-equiv="content-type" content="text/html;charset=META">'
body = +'<meta http-equiv="content-type" content="text/html;charset=META">'
page = util_page body

assert_equal ['META'], page.meta_charset
Expand All @@ -132,7 +133,7 @@ def test_detected_encoding

def test_encodings
response = {'content-type' => 'text/html;charset=HEADER'}
body = '<meta http-equiv="content-type" content="text/html;charset=META">'
body = +'<meta http-equiv="content-type" content="text/html;charset=META">'
@mech.default_encoding = 'DEFAULT'
page = util_page body, response

Expand Down Expand Up @@ -175,7 +176,7 @@ def test_parser_encoding_equals_overwrites_force_default_encoding
def test_parser_encoding_when_searching_elements
skip "Encoding not implemented" unless have_encoding?

body = '<span id="latin1">hi</span>'
body = +'<span id="latin1">hi</span>'
page = util_page body, 'content-type' => 'text/html,charset=ISO-8859-1'

result = page.search('#latin1')
Expand All @@ -187,7 +188,7 @@ def test_parser_error_message_containing_encoding_errors
skip if RUBY_ENGINE == 'jruby' # this is a libxml2-specific condition

# https://github.com/sparklemotion/mechanize/issues/553
body = <<~EOF
body = +<<~EOF
<html>
<body>
<!--
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_page_frame.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizePageFrame < Mechanize::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/test_mechanize_page_image.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizePageImage < Mechanize::TestCase
Expand Down

0 comments on commit 3509024

Please sign in to comment.