1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/test/models/filename_test.rb
yuuji.yaginuma 1b86d90136 Enable Performance/UnfreezeString cop
In Ruby 2.3 or later, `String#+@` is available and `+@` is faster than `dup`.

```ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "benchmark-ips"
end

Benchmark.ips do |x|
  x.report('+@') { +"" }
  x.report('dup') { "".dup }
  x.compare!
end
```

```
$ ruby -v benchmark.rb
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Warming up --------------------------------------
                  +@   282.289k i/100ms
                 dup   187.638k i/100ms
Calculating -------------------------------------
                  +@      6.775M (± 3.6%) i/s -     33.875M in   5.006253s
                 dup      3.320M (± 2.2%) i/s -     16.700M in   5.032125s

Comparison:
                  +@:  6775299.3 i/s
                 dup:  3320400.7 i/s - 2.04x  slower

```
2018-09-23 08:56:55 +09:00

56 lines
2.2 KiB
Ruby
Raw Blame History

# frozen_string_literal: true
require "test_helper"
class ActiveStorage::FilenameTest < ActiveSupport::TestCase
test "base" do
assert_equal "racecar", ActiveStorage::Filename.new("racecar.jpg").base
assert_equal "race.car", ActiveStorage::Filename.new("race.car.jpg").base
assert_equal "racecar", ActiveStorage::Filename.new("racecar").base
end
test "extension with delimiter" do
assert_equal ".jpg", ActiveStorage::Filename.new("racecar.jpg").extension_with_delimiter
assert_equal ".jpg", ActiveStorage::Filename.new("race.car.jpg").extension_with_delimiter
assert_equal "", ActiveStorage::Filename.new("racecar").extension_with_delimiter
end
test "extension without delimiter" do
assert_equal "jpg", ActiveStorage::Filename.new("racecar.jpg").extension_without_delimiter
assert_equal "jpg", ActiveStorage::Filename.new("race.car.jpg").extension_without_delimiter
assert_equal "", ActiveStorage::Filename.new("racecar").extension_without_delimiter
end
test "sanitize" do
"%$|:;/\t\r\n\\".each_char do |character|
filename = ActiveStorage::Filename.new("foo#{character}bar.pdf")
assert_equal "foo-bar.pdf", filename.sanitized
assert_equal "foo-bar.pdf", filename.to_s
end
end
test "sanitize transcodes to valid UTF-8" do
{ (+"\xF6").force_encoding(Encoding::ISO8859_1) => "ö",
(+"\xC3").force_encoding(Encoding::ISO8859_1) => "Ã",
"\xAD" => "<EFBFBD>",
"\xCF" => "<EFBFBD>",
"\x00" => "",
}.each do |actual, expected|
assert_equal expected, ActiveStorage::Filename.new(actual).sanitized
end
end
test "strips RTL override chars used to spoof unsafe executables as docs" do
# Would be displayed in Windows as "evilexe.pdf" due to the right-to-left
# (RTL) override char!
assert_equal "evil-fdp.exe", ActiveStorage::Filename.new("evil\u{202E}fdp.exe").sanitized
end
test "compare case-insensitively" do
assert_operator ActiveStorage::Filename.new("foobar.pdf"), :==, ActiveStorage::Filename.new("FooBar.PDF")
end
test "compare sanitized" do
assert_operator ActiveStorage::Filename.new("foo-bar.pdf"), :==, ActiveStorage::Filename.new("foo\tbar.pdf")
end
end