def test_sample_words_custom_delimiter(words): k = 5 delim = "_" res = internal.sample_words(words, k, delimiter=delim) assert res.count(delim) == k - 1 regex = "^[^{delim}]+({delim}[^{delim}]+){{{n}}}" regex = regex.format(delim=delim, n=k - 1) assert re.match(regex, res)
def test_sample_words_unique(words): k = 4 # normalize for testing purposes below words = list(set([w.lower() for w in words])) results = [internal.sample_words(words, k) for i in range(20)] for result in results: ws = result.split(internal.DELIMITER) assert sorted(set(ws)) == sorted(ws)
def test_sample_words_random_case(words): def has_title_case(results, delim=internal.DELIMITER): for ws in results: for w in ws.split(delim): if w.title() == w: return True return False k = 5 assert len(words) > k # verify default (True) results = [internal.sample_words(words, k) for i in range(10)] assert has_title_case(results) # verify False words = [w.lower() for w in words] words = [w for w in words if re.match("^[a-z]+$", w)] results = [ internal.sample_words(words, k, random_case=False) for i in range(10) ] assert not has_title_case(results)
def test_sample_words_len(words): k = 5 assert len(words) > k ws = internal.sample_words(words, k) assert len(ws.split(internal.DELIMITER)) == k
def test_sample_words_k_max(): result = internal.sample_words(["a", "b"], 2, delimiter=" ", random_case=False) assert result in ["a b", "b a"]
def test_sample_words_k_too_large(): with pytest.raises(ValueError) as err: internal.sample_words(["a", "b"], 3, delimiter=" ", random_case=False) assert "can't sample 3 of 2 words" == str(err.value)