blob: 61c621aa78b8695cd54a4e9d0f4ee0376f0876ea [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
Michael Walsh18176322016-11-15 15:11:21 -060014def rvalid_value(var_name,
15 invalid_values=[],
16 valid_values=[]):
Michael Walsh18176322016-11-15 15:11:21 -060017 r"""
Michael Walsh3e26e102017-01-10 11:29:28 -060018 Validate a robot value.
19
20 This function is the robot wrapper for gen_robot_print.svalid_value.
Michael Walsh18176322016-11-15 15:11:21 -060021
22 Description of arguments:
23 var_name The name of the variable whose value is to
24 be validated.
25 invalid_values A list of invalid values. If var_value is
26 equal to any of these, it is invalid.
27 Note that if you specify anything for
28 invalid_values (below), the valid_values
29 list is not even processed.
30 valid_values A list of invalid values. var_value must
31 be equal to one of these values to be
32 considered valid.
33
34 Examples of robot calls and corresponding output:
35
36 Robot code...
37 rvalid_value MY_PARM
38
39 Output...
40 #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e.
41 #it's undefined).
42
43 or if it is defined but blank:
44
45 Output...
46 #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an
47 #invalid value:
48 MY_PARM:
49
50 It must NOT be one of the following values:
51 invalid_values:
52 invalid_values[0]: <blank>
53
54 Robot code...
55 ${invalid_values}= Create List one two three
56 ${MY_PARM}= Set Variable one
57 rvalid_value MY_PARM invalid_values=${invalid_values}
58
59 Output...
60 #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an
61 #invalid value:
62 MY_PARM: one
63
64 It must NOT be one of the following values:
65 invalid_values:
66 invalid_values[0]: one
67 invalid_values[1]: two
68 invalid_values[2]: three
69
70 """
71
72 # Note: get_variable_value() seems to have no trouble with local variables.
73 var_value = BuiltIn().get_variable_value("${" + var_name + "}")
74
75 if var_value is None:
76 var_value = ""
77 error_message = "Variable \"" + var_name +\
78 "\" not found (i.e. it's undefined).\n"
79 else:
80 error_message = gv.svalid_value(var_value, invalid_values,
81 valid_values, var_name)
82 if not error_message == "":
Michael Walsh8fb1f532016-12-09 14:06:15 -060083 error_message = grp.sprint_error_report(error_message)
Michael Walsh18176322016-11-15 15:11:21 -060084 BuiltIn().fail(error_message)
85
Michael Walsh18176322016-11-15 15:11:21 -060086
Michael Walsh18176322016-11-15 15:11:21 -060087def rvalid_integer(var_name):
Michael Walsh18176322016-11-15 15:11:21 -060088 r"""
Michael Walsh3e26e102017-01-10 11:29:28 -060089 Validate a robot integer.
90
91 This function is the robot wrapper for gen_robot_print.svalid_integer.
Michael Walsh18176322016-11-15 15:11:21 -060092
93 Description of arguments:
94 var_name The name of the variable whose value is to
95 be validated.
96
97 Examples of robot calls and corresponding output:
98
99 Robot code...
100 Rvalid Integer MY_PARM
101
102 Output...
103 #(CDT) 2016/11/02 10:44:43 - **ERROR** Variable "MY_PARM" not found (i.e.
104 #it's undefined).
105
106 or if it is defined but blank:
107
108 Output...
109 #(CDT) 2016/11/02 10:45:37 - **ERROR** Invalid integer value:
110 MY_PARM: <blank>
111
112 Robot code...
113 ${MY_PARM}= Set Variable HELLO
114 Rvalid Integer MY_PARM
115
116 Output...
117 #(CDT) 2016/11/02 10:46:18 - **ERROR** Invalid integer value:
118 MY_PARM: HELLO
119
120 """
121
122 # Note: get_variable_value() seems to have no trouble with local variables.
123 var_value = BuiltIn().get_variable_value("${" + var_name + "}")
124
125 if var_value is None:
126 var_value = ""
127 error_message = "Variable \"" + var_name +\
128 "\" not found (i.e. it's undefined).\n"
129 else:
130 error_message = gv.svalid_integer(var_value, var_name)
131 if not error_message == "":
Michael Walsh8fb1f532016-12-09 14:06:15 -0600132 error_message = grp.sprint_error_report(error_message)
Michael Walsh18176322016-11-15 15:11:21 -0600133 BuiltIn().fail(error_message)