Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled frozen string literal compatibility #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: ["3.2", "3.3"]
ruby: ["3.2", "3.3", "3.4.0-preview2"]
gemfile:
- activemodel-7.1
- activemodel-7.2
Expand All @@ -26,6 +26,7 @@ jobs:

env:
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
RUBYOPT: --enable=frozen-string-literal --debug=frozen-string-literal
steps:
# https://github.com/marketplace/actions/checkout
- uses: actions/checkout@v4
Expand Down
10 changes: 6 additions & 4 deletions lib/strip_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module StripAttributes
MULTIBYTE_SPACE = /[[:space:]#{MULTIBYTE_WHITE}]/.freeze
MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]/.freeze
MULTIBYTE_SUPPORTED = "\u0020" == " "
EMPTY_STRING = "".freeze
SINGLE_SPACE = " ".freeze

def self.strip(record_or_string, options = {})
if record_or_string.respond_to?(:attributes)
Expand Down Expand Up @@ -59,21 +61,21 @@ def self.strip_string(value, options = {})
replace_newlines = options[:replace_newlines]
regex = options[:regex]

value.gsub!(regex, "") if regex
value.gsub!(regex, EMPTY_STRING) if regex

if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_SPACE)
value.gsub!(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "")
else
value.strip!
end

value.gsub!(/[\r\n]+/, " ") if replace_newlines
value.gsub!(/[\r\n]+/, SINGLE_SPACE) if replace_newlines

if collapse_spaces
if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_BLANK)
value.gsub!(/#{MULTIBYTE_BLANK}+/, " ")
value.gsub!(/#{MULTIBYTE_BLANK}+/, SINGLE_SPACE)
else
value.squeeze!(" ")
value.squeeze!(SINGLE_SPACE)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/strip_attributes/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def initialize(attributes)
def matches?(subject)
@attributes.all? do |attribute|
@attribute = attribute
subject.send("#{@attribute}=", " string ")
subject.send("#{@attribute}=", +" string ")
subject.valid?
subject.send(@attribute) == "string" and collapse_spaces?(subject) and replace_newlines?(subject)
end
Expand Down Expand Up @@ -67,7 +67,7 @@ def description
def collapse_spaces?(subject)
return true if !@options[:collapse_spaces]

subject.send("#{@attribute}=", " string string ")
subject.send("#{@attribute}=", +" string string ")
subject.valid?
subject.send(@attribute) == "string string"
end
Expand All @@ -82,7 +82,7 @@ def expectation(past = true)
def replace_newlines?(subject)
return true if !@options[:replace_newlines]

subject.send("#{@attribute}=", "string\nstring")
subject.send("#{@attribute}=", +"string\nstring")
subject.valid?
subject.send(@attribute) == "string string"
end
Expand Down
54 changes: 27 additions & 27 deletions test/strip_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CoexistWithOtherObjects < Tableless
attr_accessor :arr, :hsh, :str
strip_attributes
def initialize
@arr, @hsh, @str = [], {}, "foo "
@arr, @hsh, @str = [], {}, +"foo "
end
def attributes
{arr: arr, hsh: hsh, str: str}
Expand Down Expand Up @@ -114,15 +114,15 @@ class IfProcMockRecord < Tableless
class StripAttributesTest < Minitest::Test
def setup
@init_params = {
foo: "\tfoo",
bar: "bar \t ",
biz: "\tbiz ",
baz: "",
bang: " ",
foz: " foz foz",
fiz: "fiz \n fiz",
qax: "\n\t ",
qux: "\u200B"
foo: +"\tfoo",
bar: +"bar \t ",
biz: +"\tbiz ",
baz: +"",
bang: +" ",
foz: +" foz foz",
fiz: +"fiz \n fiz",
qax: +"\n\t ",
qux: +"\u200B"
}
end

Expand Down Expand Up @@ -285,7 +285,7 @@ def test_should_coexist_with_other_validations

def test_should_strip_regex
record = StripRegexMockRecord.new
record.assign_attributes(@init_params.merge(foo: "^%&*abc "))
record.assign_attributes(@init_params.merge(foo: +"^%&*abc "))
record.valid?
assert_equal "abc", record.foo
assert_equal "bar", record.bar
Expand All @@ -294,7 +294,7 @@ def test_should_strip_regex
def test_should_strip_unicode
skip "multi-byte characters not supported by this version of Ruby" unless StripAttributes::MULTIBYTE_SUPPORTED

record = StripOnlyOneMockRecord.new({foo: "\u200A\u200B foo\u200A\u200B\u00A0 "})
record = StripOnlyOneMockRecord.new({foo: +"\u200A\u200B foo\u200A\u200B\u00A0 "})
record.valid?
assert_equal "foo", record.foo
end
Expand Down Expand Up @@ -373,42 +373,42 @@ def test_should_strip_no_fields_if_false_proc

class ClassMethodsTest < Minitest::Test
def test_should_strip_whitespace
assert_nil StripAttributes.strip("")
assert_nil StripAttributes.strip(" \t ")
assert_equal "thirty six", StripAttributes.strip(" thirty six \t \n")
assert_nil StripAttributes.strip(+"")
assert_nil StripAttributes.strip(+" \t ")
assert_equal "thirty six", StripAttributes.strip(+" thirty six \t \n")
end

def test_should_allow_empty
assert_equal "", StripAttributes.strip("", allow_empty: true)
assert_equal "", StripAttributes.strip(" \t ", allow_empty: true)
assert_equal "", StripAttributes.strip(+"", allow_empty: true)
assert_equal "", StripAttributes.strip(+" \t ", allow_empty: true)
end

def test_should_collapse_spaces
assert_equal "1 2 3", StripAttributes.strip(" 1 2 3\t ", collapse_spaces: true)
assert_equal "1 2 3", StripAttributes.strip(+" 1 2 3\t ", collapse_spaces: true)
end

def test_should_collapse_multibyte_spaces
assert_equal "1 2 3", StripAttributes.strip(" 1 \u00A0 2\u00A03\t ", collapse_spaces: true)
assert_equal "1 2 3", StripAttributes.strip(+" 1 \u00A0 2\u00A03\t ", collapse_spaces: true)
end

def test_should_replace_newlines
assert_equal "1 2", StripAttributes.strip("1\n2", replace_newlines: true)
assert_equal "1 2", StripAttributes.strip("1\r\n2", replace_newlines: true)
assert_equal "1 2", StripAttributes.strip("1\r2", replace_newlines: true)
assert_equal "1 2", StripAttributes.strip(+"1\n2", replace_newlines: true)
assert_equal "1 2", StripAttributes.strip(+"1\r\n2", replace_newlines: true)
assert_equal "1 2", StripAttributes.strip(+"1\r2", replace_newlines: true)
end

def test_should_strip_regex
assert_equal "abc", StripAttributes.strip("^%&*abc ^ ", regex: /[\^\%&\*]/)
assert_equal "abc", StripAttributes.strip(+"^%&*abc ^ ", regex: /[\^\%&\*]/)
end

def test_should_keep_only_alphanumerics
nickname = " funky BAT-2009"
nickname = +" funky BAT-2009"
assert_equal "funkyBAT-2009", StripAttributes.strip(nickname, regex: /[^[:alnum:]_-]/)
end

def test_should_strip_trailing_whitespace
messy_code =
"const hello = (name) => { \n" +
+"const hello = (name) => { \n" +
" if (name === 'voldemort') return; \n" +
" \n" +
" console.log(`Hello ${name}!`); \t \t \n" +
Expand All @@ -427,8 +427,8 @@ def test_should_strip_trailing_whitespace
def test_should_strip_unicode
skip "multi-byte characters not supported by this version of Ruby" unless StripAttributes::MULTIBYTE_SUPPORTED

assert_equal "foo", StripAttributes.strip("\u200A\u200B foo\u200A\u200B ")
assert_equal "foo\u20AC".force_encoding("ASCII-8BIT"), StripAttributes.strip("foo\u20AC ".force_encoding("ASCII-8BIT"))
assert_equal "foo", StripAttributes.strip(+"\u200A\u200B foo\u200A\u200B ")
assert_equal (+"foo\u20AC").force_encoding("ASCII-8BIT"), StripAttributes.strip((+"foo\u20AC ").force_encoding("ASCII-8BIT"))
end
end
end