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