Skip to content

Instantly share code, notes, and snippets.

@vinbarnes
Created July 16, 2009 14:44
Show Gist options
  • Save vinbarnes/148445 to your computer and use it in GitHub Desktop.
Save vinbarnes/148445 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -w
class QuoteCleaner
module ClassMethods
LEFT_SINGLE_SMARTQUOTE = /\342\200\230/
RIGHT_SINGLE_SMARTQUOTE = /\342\200\231/
SINGLE_SMARTQUOTES = Regexp.union(LEFT_SINGLE_SMARTQUOTE, RIGHT_SINGLE_SMARTQUOTE)
LEFT_DOUBLE_SMARTQUOTE = /\342\200\234/
RIGHT_DOUBLE_SMARTQUOTE = /\342\200\235/
DOUBLE_SMARTQUOTES = Regexp.union(LEFT_DOUBLE_SMARTQUOTE, RIGHT_DOUBLE_SMARTQUOTE)
def inner_quotes(phrase)
(phrase =~ /\s/) ? "'#{phrase}'": phrase
end
def remove_smartquotes(phrase)
phrase.gsub(SINGLE_SMARTQUOTES, "'").gsub(DOUBLE_SMARTQUOTES, '"')
end
def process(phrase)
# p ARGV.map {|w| inner_quotes(w)}.join(' ')
end
end
end
QuoteCleaner.extend(QuoteCleaner::ClassMethods)
if __FILE__ == $0 && ARGV[0] =~ /(-z|--ztest)/
ARGV.shift
require 'test/unit'
module TestCaseExtensions
def should(testname, &block)
unless block_given?
pending = 'PENDING '
block = lambda {}
end
testname = "test #{pending}#{@focus}should #{testname}".gsub(/\s(no|not)\s/i, ' *\1* ')
define_method(testname, block)
end
def focus(object)
return unless block_given?
@focus = object.to_s + ' '
yield
@focus = nil
end
end
class QuoteCleanerTest < Test::Unit::TestCase
extend ::TestCaseExtensions
focus QuoteCleaner do
should 'have inner_quotes method' do
assert_nothing_raised { QuoteCleaner.inner_quotes('test quote') }
end
should 'have remove_smartquotes method' do
assert_nothing_raised { QuoteCleaner.remove_smartquotes('test quote') }
end
end
focus '.inner_quotes' do
should 'not quote a phrase with no spaces' do
assert_equal("asdf", QuoteCleaner.inner_quotes('asdf'))
end
should 'quote a phrase with spaces' do
assert_equal("'as df'", QuoteCleaner.inner_quotes('as df'))
end
end
focus '.remove_smartquotes' do
should 'return string untouched if there are no smartquotes' do
assert_equal("'asdf'", QuoteCleaner.remove_smartquotes("'asdf'"))
end
should 'return string with left single smartquotes replaced' do
assert_equal("'asdf'", QuoteCleaner.remove_smartquotes("\342\200\230asdf'"))
end
should 'return string with left single smartquotes replaced' do
assert_equal("'asdf'", QuoteCleaner.remove_smartquotes("'asdf\342\200\231"))
end
should 'return string with left double smartquotes replaced' do
assert_equal('"asdf"', QuoteCleaner.remove_smartquotes("\342\200\234asdf\""))
end
should 'return string with right double smartquotes replaced' do
assert_equal('"asdf"', QuoteCleaner.remove_smartquotes("\"asdf\342\200\235"))
end
should 'return string with all smartquotes replaced' do
assert_equal("'\"asdf\"'", QuoteCleaner.remove_smartquotes("\342\200\230\342\200\234asdf\342\200\235\342\200\231"))
end
end
focus '.process'
focus 'integration tests' do
should 'do something to test shit'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment