Module: Roby::App::CucumberHelpers
- Defined in:
- lib/roby/app/cucumber/helpers.rb
Overview
Module with helpers to be used in cucumber specifications
Defined Under Namespace
Classes: InvalidSyntax, InvalidUnit, MissingArgument, MixingOrderAndNames, UnexpectedArgument
Constant Summary collapse
- KNOWN_UNITS =
{ length: %w[m], angle: %w[deg], time: %w[h min s] }.freeze
Class Method Summary collapse
-
.apply_unit(value, unit) ⇒ Object
private
Helper that converts a value with unit into the corresponding normalized value (e.g. degrees to radians).
- .parse_argument_text_to_hash(raw_arguments) ⇒ Object
-
.parse_arguments(raw_arguments, expected = {}, strict: true) ⇒ Object
Parsing of a set of quantities, in the form.
-
.parse_arguments_respectively(reference, raw_arguments, expected = {}, strict: true) ⇒ Object
Parsing of a set of quantities that follow another already given set.
- .parse_hash_numerical_entry(key, value, expected, strict: true) ⇒ Object
- .parse_hash_numerical_values(hash, expected = {}, strict: true) ⇒ Object
-
.parse_numerical_value(text, expected_quantity = nil) ⇒ (Float,String)
Parses a numerical value, possibly with a unit, in which case it is converted to the corresponding “natural” unit (e.g. meters, seconds, …).
-
.try_numerical_value_with_unit(string) ⇒ (Numeric,String)?
private
Helper that identifies a value which looks like a numerical value with unit and returns it as (numeric, unit).
- .validate_unit(name, value, unit, quantity) ⇒ Object
Class Method Details
.apply_unit(value, unit) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Helper that converts a value with unit into the corresponding normalized value (e.g. degrees to radians)
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/roby/app/cucumber/helpers.rb', line 296 def self.apply_unit(value, unit) case unit when "deg" value * Math::PI / 180 when "m" value when "h" value * 3600 when "min" value * 60 when "s" value else raise InvalidUnit, "unknown unit #{unit}, known units are deg, m (meters), "\ "h (hour), min (minute) and s (seconds)" end end |
.parse_argument_text_to_hash(raw_arguments) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/roby/app/cucumber/helpers.rb', line 100 def self.parse_argument_text_to_hash(raw_arguments) current = {} stack = [] scanner = StringScanner.new(raw_arguments) until scanner.eos? arg_name = scanner.scan_until(/=/) unless arg_name raise InvalidSyntax, "expected to find '=' in #{raw_arguments}\n"\ "#{' ' * (24 + scanner.pos)}^" end arg_name = arg_name[0, arg_name.size - 1].to_sym if scanner.peek(1) == "{" scanner.getch stack << current current[arg_name] = child = {} current = child else match = scanner.scan_until(/(\s*,\s*|\s+and\s+|\s*}\s*)/) if match && (scanner[1].strip == "}") current[arg_name] = match[0, match.size - scanner[1].size] loop do current = stack.pop unless current raise InvalidSyntax, "unbalanced closed hash" end break unless scanner.scan(/\s*}\s*/) end if !scanner.eos? && !scanner.scan(/\s*,\s*|\s+and\s+/) raise InvalidSyntax, "expected comma or 'and' after }" end elsif match current[arg_name] = match[0, match.size - scanner[1].size] else current[arg_name] = scanner.rest scanner.terminate end end end unless stack.empty? raise InvalidSyntax, "expected closing } at the end of string" end current end |
.parse_arguments(raw_arguments, expected = {}, strict: true) ⇒ Object
Parsing of a set of quantities, in the form
x=VAL, y=VAL and z=VAL
The value can be specified as a numerical value with unit, in which case the value is converted into the corresponding SI unit, as e.g.
yaw=10deg
will be converted to
Hash[yaw: 0.1745] # 10 degrees in radians
36 37 38 39 |
# File 'lib/roby/app/cucumber/helpers.rb', line 36 def self.parse_arguments(raw_arguments, expected = {}, strict: true) hash = parse_argument_text_to_hash(raw_arguments) parse_hash_numerical_values(hash, expected, strict: strict) end |
.parse_arguments_respectively(reference, raw_arguments, expected = {}, strict: true) ⇒ Object
Parsing of a set of quantities that follow another already given set
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/roby/app/cucumber/helpers.rb', line 156 def self.parse_arguments_respectively( reference, raw_arguments, expected = {}, strict: true ) arguments = {} has_implicit = false has_explicit = false raw_arguments = raw_arguments.split(/(?:\s*,\s*|\s+and\s+)/) # Same value for all keys if raw_arguments.size == 1 && (raw_arguments.first !~ /=/) arg_value = raw_arguments.first if strict reference.each do |key| unless expected[key] raise UnexpectedArgument, "unexpected argument found #{key}" end end end if (value_with_unit = try_numerical_value_with_unit(arg_value)) reference.each do |key| if (expectation = expected[key]) validate_unit(key, *value_with_unit, expectation) end end arg_value = apply_unit(*value_with_unit) else reference.each do |key| is_numeric = Float(arg_value) rescue nil next unless (expectation = expected[key]) msg = if is_numeric "but it got no unit" else "but it is not even a number" end raise InvalidUnit, "expected #{key}=#{arg_value} to be "\ "a #{expectation}, #{msg}" end end reference.each do |key| arguments[key] = arg_value end return arguments end raw_arguments.each_with_index do |arg, arg_i| arg_name, arg_value = arg.split("=") if arg_value if has_implicit raise MixingOrderAndNames, "cannot mix order-based syntax and explicit names" end has_explicit = true else if has_explicit raise MixingOrderAndNames, "cannot mix order-based syntax and explicit names" end arg_value = arg_name arg_name = reference[arg_i] unless arg_name raise UnexpectedArgument, "too many implicit values given, expected "\ "#{reference.size} (#{reference.join(', ')})" end has_implicit = true end expected_quantity = expected[arg_name.to_sym] if strict && !expected_quantity raise UnexpectedArgument, "unexpected argument found #{arg_name}" elsif (value_with_unit = try_numerical_value_with_unit(arg_value)) if expected_quantity validate_unit(arg_name, *value_with_unit, expected_quantity) end arg_value = apply_unit(*value_with_unit) elsif expected_quantity raise InvalidUnit, "expected #{arg_name}=#{arg_value} to be "\ "a #{expected_quantity}" end unless reference.include?(arg_name.to_sym) raise UnexpectedArgument, "got '#{arg_name}' but was expecting one of "\ "#{reference.map(&:to_s).sort.join(', ')}" end arguments[arg_name.to_sym] = arg_value end if arguments.keys.to_set != reference.to_set missing = reference.to_set - arguments.keys raise MissingArgument, "missing #{missing.size} argument(s) "\ "(for #{missing.map(&:to_s).sort.join(', ')})" end arguments end |
.parse_hash_numerical_entry(key, value, expected, strict: true) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/roby/app/cucumber/helpers.rb', line 49 def self.parse_hash_numerical_entry(key, value, expected, strict: true) if strict && !expected raise UnexpectedArgument, "unexpected argument found #{key}" end if value.kind_of?(Hash) parse_hash_numerical_values(value, expected || {}, strict: strict) elsif (value_with_unit = try_numerical_value_with_unit(value)) validate_unit(key, *value_with_unit, expected) if expected apply_unit(*value_with_unit) elsif expected raise InvalidUnit, "expected #{key}=#{value} to be a #{expected}, "\ "but it got no unit" else begin Integer(value) rescue ArgumentError begin Float(value) rescue ArgumentError value end end end end |
.parse_hash_numerical_values(hash, expected = {}, strict: true) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/roby/app/cucumber/helpers.rb', line 41 def self.parse_hash_numerical_values(hash, expected = {}, strict: true) hash.each_with_object({}) do |(key, value), h| h[key] = parse_hash_numerical_entry( key, value, expected[key], strict: strict ) end end |
.parse_numerical_value(text, expected_quantity = nil) ⇒ (Float,String)
Parses a numerical value, possibly with a unit, in which case it is converted to the corresponding “natural” unit (e.g. meters, seconds, …)
265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/roby/app/cucumber/helpers.rb', line 265 def self.parse_numerical_value(text, expected_quantity = nil) value, unit = try_numerical_value_with_unit(text) if unit if expected_quantity validate_unit(nil, value, unit, expected_quantity) end [apply_unit(value, unit), unit] elsif expected_quantity raise InvalidUnit, "expected a #{expected_quantity}, but got #{text}" else Float(text) end end |
.try_numerical_value_with_unit(string) ⇒ (Numeric,String)?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Helper that identifies a value which looks like a numerical value with unit and returns it as (numeric, unit)
285 286 287 288 |
# File 'lib/roby/app/cucumber/helpers.rb', line 285 def self.try_numerical_value_with_unit(string) m = /^(-?\.\d+|-?\d+(?:\.\d+)?)([^\d.]\w*)$/.match(string) [Float(m[1]), m[2]] if m end |
.validate_unit(name, value, unit, quantity) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/roby/app/cucumber/helpers.rb', line 85 def self.validate_unit(name, value, unit, quantity) unless (valid_units = KNOWN_UNITS[quantity]) raise ArgumentError, "unknown quantity definition '#{quantity}', "\ "expected one of #{KNOWN_UNITS.keys.map(&:inspect).join(', ')}" end unless valid_units.include?(unit) raise InvalidUnit, "expected a #{quantity} in place of #{name}=#{value}#{unit}" end nil end |