forked from amueller/word_cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_wordcloud_cli.py
109 lines (80 loc) · 3.87 KB
/
test_wordcloud_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import argparse
import os
from collections import namedtuple
from tempfile import NamedTemporaryFile
import wordcloud as wc
from wordcloud import wordcloud_cli as cli
from mock import patch
from nose.tools import assert_equal, assert_greater, assert_true, assert_in, assert_not_in
temp = NamedTemporaryFile()
ArgOption = namedtuple('ArgOption', ['cli_name', 'init_name', 'pass_value', 'fail_value'])
ARGUMENT_SPEC_TYPED = [
ArgOption(cli_name='width', init_name='width', pass_value=13, fail_value=1.),
ArgOption(cli_name='height', init_name='height', pass_value=15, fail_value=1.),
ArgOption(cli_name='margin', init_name='margin', pass_value=17, fail_value=1.),
ArgOption(cli_name='relative_scaling', init_name='relative_scaling', pass_value=1, fail_value='c')
]
ARGUMENT_SPEC_REMAINING = [
ArgOption(cli_name='stopwords', init_name='stopwords', pass_value=temp.name, fail_value=None),
ArgOption(cli_name='mask', init_name='mask', pass_value=temp.name, fail_value=None),
ArgOption(cli_name='fontfile', init_name='font_path', pass_value=temp.name, fail_value=None),
ArgOption(cli_name='color', init_name='color_func', pass_value='red', fail_value=None),
ArgOption(cli_name='background', init_name='background_color', pass_value='grey', fail_value=None)
]
def all_arguments():
arguments = []
arguments.extend(ARGUMENT_SPEC_TYPED)
arguments.extend(ARGUMENT_SPEC_REMAINING)
return arguments
def test_main_passes_arguments_through():
temp_imagefile = NamedTemporaryFile()
args = argparse.Namespace(text='some long text', imagefile=open(temp_imagefile.name, 'w'))
for option in all_arguments():
setattr(args, option.init_name, option.pass_value)
with patch('wordcloud.wordcloud_cli.wc.WordCloud', autospec=True) as mock_word_cloud:
cli.main(args)
posargs, kwargs = mock_word_cloud.call_args
for option in all_arguments():
assert_in(option.init_name, kwargs)
def check_argument(name, result_name, value):
text = NamedTemporaryFile()
args = cli.parse_args(['--text', text.name, '--' + name, str(value)])
assert_in(result_name, vars(args))
def check_argument_type(name, value):
text = NamedTemporaryFile()
try:
with patch('sys.stderr') as mock_stderr:
args = cli.parse_args(['--text', text.name, '--' + name, str(value)])
raise AssertionError('argument "{}" was accepted even though the type did not match'.format(name))
except SystemExit:
pass
except ValueError:
pass
def test_parse_args_are_passed_along():
for option in all_arguments():
if option.cli_name != 'mask':
yield check_argument, option.cli_name, option.init_name, option.pass_value
def test_parse_arg_types():
for option in ARGUMENT_SPEC_TYPED:
yield check_argument_type, option.cli_name, option.fail_value
def test_check_duplicate_color_error():
color_mask_file = NamedTemporaryFile()
text_file = NamedTemporaryFile()
try:
cli.parse_args(['--color', 'red', '--colormask', color_mask_file.name, '--text', text_file.name])
raise AssertionError('parse_args(...) didn\'t raise')
except ValueError as e:
assert_true('specify either' in str(e), msg='expecting the correct error message, instead got: ' + str(e))
def test_parse_args_defaults_to_random_color():
text = NamedTemporaryFile()
args = cli.parse_args(['--text', text.name])
assert_equal(args.color_func, wc.random_color_func)
def test_cli_writes_image():
# ensure writting works with all python versions
temp_imagefile = NamedTemporaryFile()
temp_textfile = NamedTemporaryFile()
temp_textfile.write(b'some text')
temp_textfile.flush()
args = cli.parse_args(['--text', temp_textfile.name, '--imagefile', temp_imagefile.name])
cli.main(args)
assert_greater(os.path.getsize(temp_imagefile.name), 0, msg='expecting image to be written')