| 1 |
# ############################################################################## |
| 2 |
#(G)odot (U)nit (T)est class |
| 3 |
# |
| 4 |
# ############################################################################## |
| 5 |
# The MIT License (MIT) |
| 6 |
# ===================== |
| 7 |
# |
| 8 |
# Copyright (c) 2020 Tom "Butch" Wesley |
| 9 |
# |
| 10 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 |
# of this software and associated documentation files (the "Software"), to deal |
| 12 |
# in the Software without restriction, including without limitation the rights |
| 13 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 |
# copies of the Software, and to permit persons to whom the Software is |
| 15 |
# furnished to do so, subject to the following conditions: |
| 16 |
# |
| 17 |
# The above copyright notice and this permission notice shall be included in |
| 18 |
# all copies or substantial portions of the Software. |
| 19 |
# |
| 20 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 |
# THE SOFTWARE. |
| 27 |
# |
| 28 |
# ############################################################################## |
| 29 |
# Description |
| 30 |
# ----------- |
| 31 |
# Command line interface for the GUT unit testing tool. Allows you to run tests |
| 32 |
# from the command line instead of running a scene. Place this script along with |
| 33 |
# gut.gd into your scripts directory at the root of your project. Once there you |
| 34 |
# can run this script (from the root of your project) using the following command: |
| 35 |
# godot -s -d test/gut/gut_cmdln.gd |
| 36 |
# |
| 37 |
# See the readme for a list of options and examples. You can also use the -gh |
| 38 |
# option to get more information about how to use the command line interface. |
| 39 |
# ############################################################################## |
| 40 |
|
| 41 |
#------------------------------------------------------------------------------- |
| 42 |
# Parses the command line arguments supplied into an array that can then be |
| 43 |
# examined and parsed based on how the gut options work. |
| 44 |
#------------------------------------------------------------------------------- |
| 45 |
class CmdLineParser: |
| 46 |
var _used_options = [] |
| 47 |
# an array of arrays. Each element in this array will contain an option |
| 48 |
# name and if that option contains a value then it will have a sedond |
| 49 |
# element. For example: |
| 50 |
# [[-gselect, test.gd], [-gexit]] |
| 51 |
var _opts = [] |
| 52 |
|
| 53 |
func _init(): |
| 54 |
for i in range(OS.get_cmdline_args().size()): |
| 55 |
var opt_val = OS.get_cmdline_args()[i].split('=') |
| 56 |
_opts.append(opt_val) |
| 57 |
|
| 58 |
# Parse out multiple comma delimited values from a command line |
| 59 |
# option. Values are separated from option name with "=" and |
| 60 |
# additional values are comma separated. |
| 61 |
func _parse_array_value(full_option): |
| 62 |
var value = _parse_option_value(full_option) |
| 63 |
var split = value.split(',') |
| 64 |
return split |
| 65 |
|
| 66 |
# Parse out the value of an option. Values are separated from |
| 67 |
# the option name with "=" |
| 68 |
func _parse_option_value(full_option): |
| 69 |
if(full_option.size() > 1): |
| 70 |
return full_option[1] |
| 71 |
else: |
| 72 |
return null |
| 73 |
|
| 74 |
# Search _opts for an element that starts with the option name |
| 75 |
# specified. |
| 76 |
func find_option(name): |
| 77 |
var found = false |
| 78 |
var idx = 0 |
| 79 |
|
| 80 |
while(idx < _opts.size() and !found): |
| 81 |
if(_opts[idx][0] == name): |
| 82 |
found = true |
| 83 |
else: |
| 84 |
idx += 1 |
| 85 |
|
| 86 |
if(found): |
| 87 |
return idx |
| 88 |
else: |
| 89 |
return -1 |
| 90 |
|
| 91 |
func get_array_value(option): |
| 92 |
_used_options.append(option) |
| 93 |
var to_return = [] |
| 94 |
var opt_loc = find_option(option) |
| 95 |
if(opt_loc != -1): |
| 96 |
to_return = _parse_array_value(_opts[opt_loc]) |
| 97 |
_opts.remove(opt_loc) |
| 98 |
|
| 99 |
return to_return |
| 100 |
|
| 101 |
# returns the value of an option if it was specified, null otherwise. This |
| 102 |
# used to return the default but that became problemnatic when trying to |
| 103 |
# punch through the different places where values could be specified. |
| 104 |
func get_value(option): |
| 105 |
_used_options.append(option) |
| 106 |
var to_return = null |
| 107 |
var opt_loc = find_option(option) |
| 108 |
if(opt_loc != -1): |
| 109 |
to_return = _parse_option_value(_opts[opt_loc]) |
| 110 |
_opts.remove(opt_loc) |
| 111 |
|
| 112 |
return to_return |
| 113 |
|
| 114 |
# returns true if it finds the option, false if not. |
| 115 |
func was_specified(option): |
| 116 |
_used_options.append(option) |
| 117 |
return find_option(option) != -1 |
| 118 |
|
| 119 |
# Returns any unused command line options. I found that only the -s and |
| 120 |
# script name come through from godot, all other options that godot uses |
| 121 |
# are not sent through OS.get_cmdline_args(). |
| 122 |
# |
| 123 |
# This is a onetime thing b/c i kill all items in _used_options |
| 124 |
func get_unused_options(): |
| 125 |
var to_return = [] |
| 126 |
for i in range(_opts.size()): |
| 127 |
to_return.append(_opts[i][0]) |
| 128 |
|
| 129 |
var script_option = to_return.find('-s') |
| 130 |
if script_option != -1: |
| 131 |
to_return.remove(script_option + 1) |
| 132 |
to_return.remove(script_option) |
| 133 |
|
| 134 |
while(_used_options.size() > 0): |
| 135 |
var index = to_return.find(_used_options[0].split("=")[0]) |
| 136 |
if(index != -1): |
| 137 |
to_return.remove(index) |
| 138 |
_used_options.remove(0) |
| 139 |
|
| 140 |
return to_return |
| 141 |
|
| 142 |
#------------------------------------------------------------------------------- |
| 143 |
# Simple class to hold a command line option |
| 144 |
#------------------------------------------------------------------------------- |
| 145 |
class Option: |
| 146 |
var value = null |
| 147 |
var option_name = '' |
| 148 |
var default = null |
| 149 |
var description = '' |
| 150 |
|
| 151 |
func _init(name, default_value, desc=''): |
| 152 |
option_name = name |
| 153 |
default = default_value |
| 154 |
description = desc |
| 155 |
value = null#default_value |
| 156 |
|
| 157 |
func pad(to_pad, size, pad_with=' '): |
| 158 |
var to_return = to_pad |
| 159 |
for _i in range(to_pad.length(), size): |
| 160 |
to_return += pad_with |
| 161 |
|
| 162 |
return to_return |
| 163 |
|
| 164 |
func to_s(min_space=0): |
| 165 |
var subbed_desc = description |
| 166 |
if(subbed_desc.find('[default]') != -1): |
| 167 |
subbed_desc = subbed_desc.replace('[default]', str(default)) |
| 168 |
return pad(option_name, min_space) + subbed_desc |
| 169 |
|
| 170 |
#------------------------------------------------------------------------------- |
| 171 |
# The high level interface between this script and the command line options |
| 172 |
# supplied. Uses Option class and CmdLineParser to extract information from |
| 173 |
# the command line and make it easily accessible. |
| 174 |
#------------------------------------------------------------------------------- |
| 175 |
var options = [] |
| 176 |
var _opts = [] |
| 177 |
var _banner = '' |
| 178 |
|
| 179 |
func add(name, default, desc): |
| 180 |
options.append(Option.new(name, default, desc)) |
| 181 |
|
| 182 |
func get_value(name): |
| 183 |
var found = false |
| 184 |
var idx = 0 |
| 185 |
|
| 186 |
while(idx < options.size() and !found): |
| 187 |
if(options[idx].option_name == name): |
| 188 |
found = true |
| 189 |
else: |
| 190 |
idx += 1 |
| 191 |
|
| 192 |
if(found): |
| 193 |
return options[idx].value |
| 194 |
else: |
| 195 |
print("COULD NOT FIND OPTION " + name) |
| 196 |
return null |
| 197 |
|
| 198 |
func set_banner(banner): |
| 199 |
_banner = banner |
| 200 |
|
| 201 |
func print_help(): |
| 202 |
var longest = 0 |
| 203 |
for i in range(options.size()): |
| 204 |
if(options[i].option_name.length() > longest): |
| 205 |
longest = options[i].option_name.length() |
| 206 |
|
| 207 |
print('---------------------------------------------------------') |
| 208 |
print(_banner) |
| 209 |
|
| 210 |
print("\nOptions\n-------") |
| 211 |
for i in range(options.size()): |
| 212 |
print(' ' + options[i].to_s(longest + 2)) |
| 213 |
print('---------------------------------------------------------') |
| 214 |
|
| 215 |
func print_options(): |
| 216 |
for i in range(options.size()): |
| 217 |
print(options[i].option_name + '=' + str(options[i].value)) |
| 218 |
|
| 219 |
func parse(): |
| 220 |
var parser = CmdLineParser.new() |
| 221 |
|
| 222 |
for i in range(options.size()): |
| 223 |
var t = typeof(options[i].default) |
| 224 |
# only set values that were specified at the command line so that |
| 225 |
# we can punch through default and config values correctly later. |
| 226 |
# Without this check, you can't tell the difference between the |
| 227 |
# defaults and what was specified, so you can't punch through |
| 228 |
# higher level options. |
| 229 |
if(parser.was_specified(options[i].option_name)): |
| 230 |
if(t == TYPE_INT): |
| 231 |
options[i].value = int(parser.get_value(options[i].option_name)) |
| 232 |
elif(t == TYPE_STRING): |
| 233 |
options[i].value = parser.get_value(options[i].option_name) |
| 234 |
elif(t == TYPE_ARRAY): |
| 235 |
options[i].value = parser.get_array_value(options[i].option_name) |
| 236 |
elif(t == TYPE_BOOL): |
| 237 |
options[i].value = parser.was_specified(options[i].option_name) |
| 238 |
elif(t == TYPE_NIL): |
| 239 |
print(options[i].option_name + ' cannot be processed, it has a nil datatype') |
| 240 |
else: |
| 241 |
print(options[i].option_name + ' cannot be processed, it has unknown datatype:' + str(t)) |
| 242 |
|
| 243 |
var unused = parser.get_unused_options() |
| 244 |
if(unused.size() > 0): |
| 245 |
print("Unrecognized options: ", unused) |
| 246 |
return false |
| 247 |
|
| 248 |
return true |