Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This file contains functions useful for validating variables in robot. |
| 5 | """ |
| 6 | |
| 7 | import gen_robot_print as grp |
| 8 | import gen_valid as gv |
| 9 | |
| 10 | from robot.libraries.BuiltIn import BuiltIn |
| 11 | from robot.api import logger |
| 12 | |
| 13 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 14 | def rvalid_value(var_name, |
| 15 | invalid_values=[], |
| 16 | valid_values=[]): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 17 | r""" |
Michael Walsh | 3e26e10 | 2017-01-10 11:29:28 -0600 | [diff] [blame] | 18 | Validate a robot value. |
| 19 | |
| 20 | This function is the robot wrapper for gen_robot_print.svalid_value. |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 21 | |
| 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 Walsh | 8fb1f53 | 2016-12-09 14:06:15 -0600 | [diff] [blame] | 83 | error_message = grp.sprint_error_report(error_message) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 84 | BuiltIn().fail(error_message) |
| 85 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 86 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 87 | def rvalid_integer(var_name): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 88 | r""" |
Michael Walsh | 3e26e10 | 2017-01-10 11:29:28 -0600 | [diff] [blame] | 89 | Validate a robot integer. |
| 90 | |
| 91 | This function is the robot wrapper for gen_robot_print.svalid_integer. |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 92 | |
| 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 Walsh | 8fb1f53 | 2016-12-09 14:06:15 -0600 | [diff] [blame] | 132 | error_message = grp.sprint_error_report(error_message) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 133 | BuiltIn().fail(error_message) |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 134 | |
| 135 | |
| 136 | def rvalid_range(var_name, |
| 137 | range): |
| 138 | r""" |
| 139 | Validate that a robot integer is within the given range. |
| 140 | |
| 141 | This function is the robot wrapper for gen_robot_print.svalid_range. |
| 142 | |
| 143 | Description of arguments: |
| 144 | var_name The name of the variable whose value is to |
| 145 | be validated. |
| 146 | range A list comprised of one or two elements |
| 147 | which are the lower and upper ends of a |
| 148 | range. These values must be integers |
| 149 | except where noted. Valid specifications |
| 150 | may be of the following forms: [lower, |
| 151 | upper], [lower] or [None, upper]. The |
| 152 | caller may also specify this value as a |
| 153 | string which will then be converted to a |
| 154 | list in the aforementioned format: |
| 155 | lower..upper, lower.. or ..upper. |
| 156 | |
| 157 | Examples of robot calls and corresponding output: |
| 158 | |
| 159 | Robot code... |
| 160 | Rvalid Range MY_PARM 5..9 |
| 161 | |
| 162 | Output... |
| 163 | #(CDT) 2018/05/09 11:45:00.166344 - 0.004252 - **ERROR** The following |
| 164 | # variable is not within the expected range: |
| 165 | MY_PARM: 4 |
| 166 | valid_range: 5..9 |
| 167 | """ |
| 168 | |
| 169 | var_value = BuiltIn().get_variable_value("${" + var_name + "}") |
| 170 | |
| 171 | if var_value is None: |
| 172 | var_value = "" |
| 173 | error_message = "Variable \"" + var_name +\ |
| 174 | "\" not found (i.e. it's undefined).\n" |
| 175 | else: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame^] | 176 | if isinstance(range, unicode): |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 177 | range = range.split("..") |
| 178 | if range[0] == "": |
| 179 | range[0] = None |
| 180 | range = [x for x in range if x] |
| 181 | error_message = gv.svalid_range(var_value, range, var_name) |
| 182 | if not error_message == "": |
| 183 | error_message = grp.sprint_error_report(error_message) |
| 184 | BuiltIn().fail(error_message) |