-
-
Save kaspth/6ba8ac0356549e219b57 to your computer and use it in GitHub Desktop.
cache_name_regex.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support' | |
require 'active_support/test_case' | |
require 'minitest/autorun' | |
ActiveSupport::TestCase.test_order = :random | |
class RegexTest < ActiveSupport::TestCase | |
setup do | |
@regex = / | |
\A(?:<%#.*%>\n?)? # optional ERB comment | |
<%\s+cache\(?\s* # cache call with optional starting paren and whitespace | |
(\w+\.?) # variable name and optional period so topic.name will not bust the topic cache | |
/x | |
end | |
test "cache call regex" do | |
[ | |
"<% cache horse_shoe %>", | |
"<% cache horse_shoe %>\n", | |
"<% cache(horse_shoe) %>", | |
"<% cache( horse_shoe) %>", | |
"<% cache( horse_shoe ) %>", | |
"<% cache horse_shoe, skip_digest: true %>", | |
"<% cache(horse_shoe, skip_digest: true) %>" | |
].each do |call| | |
assert_match @regex, call | |
assert_equal "horse_shoe", @regex.match(call).captures.first | |
end | |
end | |
test "regex shouldn't match" do | |
[ | |
"hello <% cache topic %>", | |
"<% cache %>" | |
].each do |call| | |
refute_match @regex, call | |
end | |
end | |
test "attribute don't match" do | |
refute_equal 'notification', @regex.match("<% cache notification.name %>").captures.first | |
end | |
test "beginning of the line comment ignored" do | |
assert_equal 'notification', @regex.match("<%# Comment %><% cache notification do %>").captures.first | |
end | |
test "beginning of the line comment ignored with newline" do | |
assert_equal 'notification', @regex.match("<%# Comment %>\n<% cache notification do %>").captures.first | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment