Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created December 22, 2009 21:28
Show Gist options
  • Select an option

  • Save jcsalterego/262071 to your computer and use it in GitHub Desktop.

Select an option

Save jcsalterego/262071 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'test/unit'
class Context
RE_PAIRS = [[/#([a-z0-9_]+)/,
'<a href="http://search.twitter.com/search?q=#\1">' +
'#\1</a>'],
[/@([a-z0-9_]+)/,
'<a href="http://twitter.com/\1">@\1</a>']]
class << self
def transform(line)
RE_PAIRS.each do |find_re, repl_txt|
line.gsub!(find_re, repl_txt)
end
line
end
end
end
class ContextTester < Test::Unit::TestCase
def setup
@c = Context
end
def test_screen_name
assert_equal(@c.transform("@damon"),
'<a href="http://twitter.com/damon">@damon</a>')
end
def test_screen_name_only_at
assert_equal(@c.transform("@"), '@')
end
def test_screen_name_cutoff_at_invalid
assert_equal(@c.transform("@damon's face"),
'<a href="http://twitter.com/damon">@damon</a>\'s face')
end
def test_hashtag
assert_equal(@c.transform("#snaptweet"),
'<a href="http://search.twitter.com/search?q=#snaptweet">' +
'#snaptweet</a>')
end
def test_hashtag_only_at
assert_equal(@c.transform("#"), '#')
end
def test_hashtag_cutoff_at_invalid
assert_equal(@c.transform("#snaptweet's face"),
'<a href="http://search.twitter.com/search?q=#snaptweet">' +
'#snaptweet</a>\'s face')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment