Skip to content

Instantly share code, notes, and snippets.

@kaspth
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaspth/6ba8ac0356549e219b57 to your computer and use it in GitHub Desktop.
Save kaspth/6ba8ac0356549e219b57 to your computer and use it in GitHub Desktop.
cache_name_regex.rb
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