2017-01-10 14:53:15 +00:00
|
|
|
# coding: utf-8
|
2017-01-03 17:17:57 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2017-01-10 14:53:15 +00:00
|
|
|
|
2017-01-03 17:17:57 +00:00
|
|
|
URLS = [
|
2017-01-10 14:54:25 +00:00
|
|
|
"http://www.nytimes.com/2016/04/20/us/politics/new-york-primary-preview.html?hp&action=click&pgtype=Homepage&clickSource=story-heading&module=a-lede-package-region®ion=top-news&WT.nav=top-news&_r=0",
|
|
|
|
"www.google.com?q=google",
|
|
|
|
"www.red-stars.com",
|
|
|
|
"http://foo.com/blah_(wikipedia)#cite-1",
|
|
|
|
"mailto:foo.bar@baz.com",
|
|
|
|
"mailto:foo-bar@baz-co.com"
|
2017-01-03 17:17:57 +00:00
|
|
|
]
|
|
|
|
|
2017-01-10 14:56:35 +00:00
|
|
|
|
2017-01-03 17:17:57 +00:00
|
|
|
# Punctuation we want to check is split away before the URL
|
|
|
|
PREFIXES = [
|
2017-01-10 14:54:25 +00:00
|
|
|
"(", '"', ">"
|
2017-01-03 17:17:57 +00:00
|
|
|
]
|
|
|
|
|
2017-01-10 14:56:35 +00:00
|
|
|
|
2017-01-03 17:17:57 +00:00
|
|
|
# Punctuation we want to check is split away after the URL
|
|
|
|
SUFFIXES = [
|
|
|
|
'"', ":", ">"]
|
|
|
|
|
2017-01-10 14:56:35 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("url", URLS)
|
|
|
|
def test_tokenizer_handles_simple_url(tokenizer, url):
|
|
|
|
tokens = tokenizer(url)
|
2017-01-03 17:17:57 +00:00
|
|
|
assert len(tokens) == 1
|
2017-01-10 14:56:35 +00:00
|
|
|
assert tokens[0].text == url
|
2017-01-03 17:17:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("prefix", PREFIXES)
|
|
|
|
@pytest.mark.parametrize("url", URLS)
|
2017-01-10 14:56:35 +00:00
|
|
|
def test_tokenizer_handles_prefixed_url(tokenizer, prefix, url):
|
2017-01-09 13:10:40 +00:00
|
|
|
tokens = tokenizer(prefix + url)
|
2017-01-10 14:57:00 +00:00
|
|
|
assert len(tokens) == 2
|
2017-01-03 17:17:57 +00:00
|
|
|
assert tokens[0].text == prefix
|
|
|
|
assert tokens[1].text == url
|
2017-01-10 14:56:35 +00:00
|
|
|
|
|
|
|
|
2017-01-03 17:17:57 +00:00
|
|
|
@pytest.mark.parametrize("suffix", SUFFIXES)
|
|
|
|
@pytest.mark.parametrize("url", URLS)
|
2017-01-10 14:56:35 +00:00
|
|
|
def test_tokenizer_handles_suffixed_url(tokenizer, url, suffix):
|
2017-01-09 13:10:40 +00:00
|
|
|
tokens = tokenizer(url + suffix)
|
2017-01-10 14:57:00 +00:00
|
|
|
assert len(tokens) == 2
|
2017-01-03 17:17:57 +00:00
|
|
|
assert tokens[0].text == url
|
|
|
|
assert tokens[1].text == suffix
|
2017-01-10 14:56:35 +00:00
|
|
|
|
|
|
|
|
2017-01-10 14:57:26 +00:00
|
|
|
@pytest.mark.parametrize("url", URLS)
|
|
|
|
def test_tokenizer_handles_simple_surround_url(tokenizer, url):
|
|
|
|
tokens = tokenizer("(" + url + ")")
|
|
|
|
assert len(tokens) == 3
|
|
|
|
assert tokens[0].text == "("
|
|
|
|
assert tokens[1].text == url
|
|
|
|
assert tokens[2].text == ")"
|
|
|
|
|
|
|
|
|
2017-01-03 17:17:57 +00:00
|
|
|
@pytest.mark.parametrize("prefix", PREFIXES)
|
|
|
|
@pytest.mark.parametrize("suffix", SUFFIXES)
|
|
|
|
@pytest.mark.parametrize("url", URLS)
|
2017-01-10 14:56:35 +00:00
|
|
|
def test_tokenizer_handles_surround_url(tokenizer, prefix, suffix, url):
|
2017-01-09 13:10:40 +00:00
|
|
|
tokens = tokenizer(prefix + url + suffix)
|
2017-01-10 14:57:00 +00:00
|
|
|
assert len(tokens) == 3
|
2017-01-03 17:17:57 +00:00
|
|
|
assert tokens[0].text == prefix
|
|
|
|
assert tokens[1].text == url
|
|
|
|
assert tokens[2].text == suffix
|
2017-01-10 14:56:35 +00:00
|
|
|
|
|
|
|
|
2017-01-03 17:17:57 +00:00
|
|
|
@pytest.mark.parametrize("prefix1", PREFIXES)
|
|
|
|
@pytest.mark.parametrize("prefix2", PREFIXES)
|
|
|
|
@pytest.mark.parametrize("url", URLS)
|
2017-01-10 14:56:35 +00:00
|
|
|
def test_tokenizer_handles_two_prefix_url(tokenizer, prefix1, prefix2, url):
|
2017-01-09 13:10:40 +00:00
|
|
|
tokens = tokenizer(prefix1 + prefix2 + url)
|
2017-01-10 14:57:00 +00:00
|
|
|
assert len(tokens) == 3
|
2017-01-03 17:17:57 +00:00
|
|
|
assert tokens[0].text == prefix1
|
|
|
|
assert tokens[1].text == prefix2
|
|
|
|
assert tokens[2].text == url
|
2017-01-10 14:56:35 +00:00
|
|
|
|
|
|
|
|
2017-01-03 17:17:57 +00:00
|
|
|
@pytest.mark.parametrize("suffix1", SUFFIXES)
|
|
|
|
@pytest.mark.parametrize("suffix2", SUFFIXES)
|
|
|
|
@pytest.mark.parametrize("url", URLS)
|
2017-01-10 14:56:35 +00:00
|
|
|
def test_tokenizer_handles_two_prefix_url(tokenizer, suffix1, suffix2, url):
|
2017-01-09 13:10:40 +00:00
|
|
|
tokens = tokenizer(url + suffix1 + suffix2)
|
2017-01-10 14:57:00 +00:00
|
|
|
assert len(tokens) == 3
|
2017-01-03 17:17:57 +00:00
|
|
|
assert tokens[0].text == url
|
|
|
|
assert tokens[1].text == suffix1
|
|
|
|
assert tokens[2].text == suffix2
|