Skip to content

Commit

Permalink
Pyfiurl tests converted to py.test
Browse files Browse the repository at this point in the history
Also fixed a bug related to capitalized ÅÄÖ
  • Loading branch information
lepinkainen committed Dec 29, 2015
1 parent 7435056 commit cabc64b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 107 deletions.
108 changes: 1 addition & 107 deletions pyfibot/util/pyfiurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import re
import string
import sys

_countrycodes = ['ac', 'ad', 'ae', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao',
'aq', 'ar', 'as', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb',
Expand Down Expand Up @@ -124,7 +123,7 @@
string.digits,
'/;?:@&=+$,%#'
'-_.!~*()',
'åäö[]<>{}^\|\'`–', # not so valid but used
'ÅÄÖåäö[]<>{}^\|\'`–', # not so valid but used
]

validUserinfoChars = [string.ascii_letters,
Expand Down Expand Up @@ -221,108 +220,3 @@ def grab(txt, needScheme=True):
seekpos = e

return possibleUrls


def unitTest():
import unittest

class TestPRUL(unittest.TestCase):
def setUp(self):
self.needScheme = True

def testLeadingSpaces(self):
"""Leading spaces before URL"""
self.assertEqual(["http://tomtom.foobar.org/"], grab(' http://tomtom.foobar.org/', self.needScheme))
self.assertEqual(["http://www.foobi.org/saatoimia"], grab(' http://www.foobi.org/saatoimia', self.needScheme))

def testTrailingSpaces(self):
"""Trailing spaces after URL"""
self.assertEqual(["http://tomtom.foobar.org/"], grab('http://tomtom.foobar.org/ ', self.needScheme))
self.assertEqual(["http://www.foobi.org/saatoimia"], grab('http://www.foobi.org/saatoimia ', self.needScheme))

def testLongURL(self):
"""A long URL-like string"""
self.assertEqual([], grab('www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www', self.needScheme))

def testQuestionMarkURI(self):
"""An URI with a question mark"""
self.assertEqual(["http://www.bdog.fi/cgi-bin/netstore/tuotehaku.pl?tuoteryhma=16"], grab('http://www.bdog.fi/cgi-bin/netstore/tuotehaku.pl?tuoteryhma=16', self.needScheme))

def testLeadingText(self):
"""Leading text"""
self.assertEqual(["http://www.technikfoo.de/mai"], grab('here it is: http://www.technikfoo.de/mai', self.needScheme))

def testLeadingAndTrailingText(self):
"""Leading and trailing text"""
self.assertEqual(["http://123.123.123.123"], grab('fooasdf asdf a http://123.123.123.123 asdfasdf', self.needScheme))

def testIP(self):
"""http URI with an ip number"""
self.assertEqual(["http://234.234.234.234"], grab('http://234.234.234.234', self.needScheme))

def testFoobarIP(self):
"""ip number-like text"""
self.assertEqual([], grab('http://11123.123.123.123/eisaa http://123.123.123.12345/eisaa', self.needScheme))

def test2URIs(self):
"""2 URIs on same text"""
self.assertEqual(["http://foobar.fi/1234{}[]{}", "http://127.0.0.1/"], grab('http://foobar.fi/1234{}[]{} sadfljs dlfkjsd lf;asdf http://127.0.0.1/', self.needScheme))

def testIPv6(self):
"""IPv6 URL with scheme"""
self.assertEqual(["http://[2001:a68:104:1337:250:daff:fe72:871c]/toimia"], grab('foo http://[2001:a68:104:1337:250:daff:fe72:871c]/toimia', self.needScheme))

def testIPv6noscheme(self):
"""IPv6 URL without a scheme"""
if self.needScheme:
return

self.assertEqual(["[2001:a68:104:1337:250:daff:fe72:871c]/toimia"], grab('foo [2001:a68:104:1337:250:daff:fe72:871c]/toimia', self.needScheme))

def testNoScheme(self):
"""URI without a scheme"""
if self.needScheme:
return

self.assertEqual(["123.123.123.123"], grab('123.123.123.123', self.needScheme))

def testRedirect(self):
"""Redirect URL"""
self.assertEqual(['http://rediretinmyurl.com/http://dest.url.org/1/2/3/4?434', 'http://secondurl.com', 'ftp://1.2.3.4/adsfasdf'], grab('http://rediretinmyurl.com/http://dest.url.org/1/2/3/4?434 http://secondurl.com ftp://1.2.3.4/adsfasdf', self.needScheme))

def testAnchor(self):
"""Link with an anchor tag"""
self.assertEqual(['http://foo.com/page.html#anchor'], grab('http://foo.com/page.html#anchor', self.needScheme))

def testScandinavian(self):
"""Test åäö"""
self.assertEqual(['http://www.hs.fi/kotimaa/artikkeli/Äidin+avovaimosta+lapsen+toinen+huoltaja+KKOn+päätöksellä/1135253379084'], grab('http://www.hs.fi/kotimaa/artikkeli/Äidin+avovaimosta+lapsen+toinen+huoltaja+KKOn+päätöksellä/1135253379084', self.needScheme))

def testBlocks(self):
"""Test blocks"""
self.assertEqual(['http://link1.com',
'http://link2.com',
'http://link3.com',
'http://link4.com',
'http://link5.com',
], grab('(http://link1.com) <http://link2.com> "http://link3.com" \'http://link4.com\' [http://link5.com]', self.needScheme))

# run unittest
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestPRUL))
unittest.TextTestRunner(verbosity=2).run(suite)


def _main():
if '--test' in sys.argv:
unitTest()
return

needScheme = '--noscheme' not in sys.argv

for line in sys.stdin:
for url in grab(line, needScheme):
print url

if __name__ == '__main__':
_main()
80 changes: 80 additions & 0 deletions tests/test_pyfiurl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
from pyfibot.util.pyfiurl import grab
from nose.tools import eq_

needScheme = True

def testLeadingSpaces():
"""Leading spaces before URL"""
assert ["http://tomtom.foobar.org/"] == grab(' http://tomtom.foobar.org/', needScheme)
assert ["http://www.foobi.org/saatoimia"] == grab(' http://www.foobi.org/saatoimia', needScheme)

def testTrailingSpaces():
"""Trailing spaces after URL"""
assert ["http://tomtom.foobar.org/"] == grab('http://tomtom.foobar.org/ ', needScheme)
assert ["http://www.foobi.org/saatoimia"] == grab('http://www.foobi.org/saatoimia ', needScheme)

def testLongURL():
"""A long URL-like string"""
assert [] == grab('www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www.www', needScheme)

def testQuestionMarkURI():
"""An URI with a question mark"""
assert ["http://www.bdog.fi/cgi-bin/netstore/tuotehaku.pl?tuoteryhma=16"] == grab('http://www.bdog.fi/cgi-bin/netstore/tuotehaku.pl?tuoteryhma=16', needScheme)

def testLeadingText():
"""Leading text"""
assert ["http://www.technikfoo.de/mai"] == grab('here it is: http://www.technikfoo.de/mai', needScheme)

def testLeadingAndTrailingText():
"""Leading and trailing text"""
assert ["http://123.123.123.123"] == grab('fooasdf asdf a http://123.123.123.123 asdfasdf', needScheme)

def testIP():
"""http URI with an ip number"""
assert ["http://234.234.234.234"] == grab('http://234.234.234.234', needScheme)

def testFoobarIP():
"""ip number-like text"""
assert [] == grab('http://11123.123.123.123/eisaa http://123.123.123.12345/eisaa', needScheme)

def test2URIs():
"""2 URIs on same text"""
assert ["http://foobar.fi/1234{}[]{}", "http://127.0.0.1/"] == grab('http://foobar.fi/1234{}[]{} sadfljs dlfkjsd lf;asdf http://127.0.0.1/', needScheme)

def testIPv6():
"""IPv6 URL with scheme"""
assert ["http://[2001:a68:104:1337:250:daff:fe72:871c]/toimia"] == grab('foo http://[2001:a68:104:1337:250:daff:fe72:871c]/toimia', needScheme)

def testIPv6noscheme():
"""IPv6 URL without a scheme"""
if needScheme:
return
assert ["[2001:a68:104:1337:250:daff:fe72:871c]/toimia"] == grab('foo [2001:a68:104:1337:250:daff:fe72:871c]/toimia', needScheme)

def testNoScheme():
"""URI without a scheme"""
if needScheme:
return
assert ["123.123.123.123"] == grab('123.123.123.123', needScheme)

def testRedirect():
"""Redirect URL"""
assert ['http://rediretinmyurl.com/http://dest.url.org/1/2/3/4?434', 'http://secondurl.com', 'ftp://1.2.3.4/adsfasdf'] == grab('http://rediretinmyurl.com/http://dest.url.org/1/2/3/4?434 http://secondurl.com ftp://1.2.3.4/adsfasdf', needScheme)

def testAnchor():
"""Link with an anchor tag"""
assert ['http://foo.com/page.html#anchor'] == grab('http://foo.com/page.html#anchor', needScheme)

def testScandinavian():
"""Test åäö"""
assert ['http://www.hs.fi/kotimaa/artikkeli/Äidin+avovaimosta+lapsen+toinen+huoltaja+KKOn+päätöksellä/1135253379084'] == grab('http://www.hs.fi/kotimaa/artikkeli/Äidin+avovaimosta+lapsen+toinen+huoltaja+KKOn+päätöksellä/1135253379084', needScheme)

def testBlocks():
"""Test blocks"""
assert ['http://link1.com',
'http://link2.com',
'http://link3.com',
'http://link4.com',
'http://link5.com',
] == grab('(http://link1.com) <http://link2.com> "http://link3.com" \'http://link4.com\' [http://link5.com]', needScheme)

0 comments on commit cabc64b

Please sign in to comment.