blob: 08597f968c69a2390758f58fa5758ca91ba2085d [file] [log] [blame]
Michael Walsh18176322016-11-15 15:11:21 -06001#!/usr/bin/env python
2
3r"""
4This file contains functions useful for validating variables in robot.
5"""
6
7import gen_robot_print as grp
8import gen_valid as gv
9
10from robot.libraries.BuiltIn import BuiltIn
11from robot.api import logger
12
13
14###############################################################################
15def rvalid_value(var_name,
16 invalid_values=[],
17 valid_values=[]):
18
19 r"""
Michael Walsh3e26e102017-01-10 11:29:28 -060020 Validate a robot value.
21
22 This function is the robot wrapper for gen_robot_print.svalid_value.
Michael Walsh18176322016-11-15 15:11:21 -060023
24 Description of arguments:
25 var_name The name of the variable whose value is to
26 be validated.
27 invalid_values A list of invalid values. If var_value is
28 equal to any of these, it is invalid.
29 Note that if you specify anything for
30 invalid_values (below), the valid_values
31 list is not even processed.
32 valid_values A list of invalid values. var_value must
33 be equal to one of these values to be
34 considered valid.
35
36 Examples of robot calls and corresponding output:
37
38 Robot code...
39 rvalid_value MY_PARM
40
41 Output...
42 #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e.
43 #it's undefined).
44
45 or if it is defined but blank:
46
47 Output...
48 #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an
49 #invalid value:
50 MY_PARM:
51
52 It must NOT be one of the following values:
53 invalid_values:
54 invalid_values[0]: <blank>
55
56 Robot code...
57 ${invalid_values}= Create List one two three
58 ${MY_PARM}= Set Variable one
59 rvalid_value MY_PARM invalid_values=${invalid_values}
60
61 Output...
62 #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an
63 #invalid value:
64 MY_PARM: one
65
66 It must NOT be one of the following values:
67 invalid_values:
68 invalid_values[0]: one
69 invalid_values[1]: two
70 invalid_values[2]: three
71
72 """
73
74 # Note: get_variable_value() seems to have no trouble with local variables.
75 var_value = BuiltIn().get_variable_value("${" + var_name + "}")
76
77 if var_value is None:
78 var_value = ""
79 error_message = "Variable \"" + var_name +\
80 "\" not found (i.e. it's undefined).\n"
81 else:
82 error_message = gv.svalid_value(var_value, invalid_values,
83 valid_values, var_name)
84 if not error_message == "":
Michael Walsh8fb1f532016-12-09 14:06:15 -060085 error_message = grp.sprint_error_report(error_message)
Michael Walsh18176322016-11-15 15:11:21 -060086 BuiltIn().fail(error_message)
87
88###############################################################################
89
90
91###############################################################################
92def rvalid_integer(var_name):
93
94 r"""
Michael Walsh3e26e102017-01-10 11:29:28 -060095 Validate a robot integer.
96
97 This function is the robot wrapper for gen_robot_print.svalid_integer.
Michael Walsh18176322016-11-15 15:11:21 -060098
99 Description of arguments:
100 var_name The name of the variable whose value is to
101 be validated.
102
103 Examples of robot calls and corresponding output:
104
105 Robot code...
106 Rvalid Integer MY_PARM
107
108 Output...
109 #(CDT) 2016/11/02 10:44:43 - **ERROR** Variable "MY_PARM" not found (i.e.
110 #it's undefined).
111
112 or if it is defined but blank:
113
114 Output...
115 #(CDT) 2016/11/02 10:45:37 - **ERROR** Invalid integer value:
116 MY_PARM: <blank>
117
118 Robot code...
119 ${MY_PARM}= Set Variable HELLO
120 Rvalid Integer MY_PARM
121
122 Output...
123 #(CDT) 2016/11/02 10:46:18 - **ERROR** Invalid integer value:
124 MY_PARM: HELLO
125
126 """
127
128 # Note: get_variable_value() seems to have no trouble with local variables.
129 var_value = BuiltIn().get_variable_value("${" + var_name + "}")
130
131 if var_value is None:
132 var_value = ""
133 error_message = "Variable \"" + var_name +\
134 "\" not found (i.e. it's undefined).\n"
135 else:
136 error_message = gv.svalid_integer(var_value, var_name)
137 if not error_message == "":
Michael Walsh8fb1f532016-12-09 14:06:15 -0600138 error_message = grp.sprint_error_report(error_message)
Michael Walsh18176322016-11-15 15:11:21 -0600139 BuiltIn().fail(error_message)
140
141###############################################################################