Module: Oktest::SpecHelper
- Defined in:
- lib/oktest.rb
Instance Attribute Summary collapse
-
#__at_end_blocks ⇒ Object
Returns the value of attribute __at_end_blocks.
-
#__TODO ⇒ Object
Returns the value of attribute __TODO.
Instance Method Summary collapse
- #AND(*args) ⇒ Object
- #Any ⇒ Object
- #at_end(&block) ⇒ Object
- #Bool ⇒ Object
- #capture_command(command, input = "", &error_handler) ⇒ Object
- #capture_command!(command, input = "", &error_handler) ⇒ Object
- #capture_stderr(input = "", tty: false, &b) ⇒ Object
- #capture_stdio(input = "", tty: false, &b) ⇒ Object (also: #capture_sio)
- #capture_stdout(input = "", tty: false, &b) ⇒ Object
- #dummy_attrs(object, **keyvals, &b) ⇒ Object
- #dummy_dir(dirname = nil, &b) ⇒ Object
- #dummy_file(filename = nil, content = nil, encoding: 'utf-8', &b) ⇒ Object
- #dummy_ivars(object, **keyvals, &b) ⇒ Object
-
#dummy_values(hashobj, keyvals = {}, &b) ⇒ Object
never use keyword args!.
- #Enum(*values) ⇒ Object
- #fixture(name, *args, **kwargs) ⇒ Object
- #JSON(actual) ⇒ Object
- #Length(n) ⇒ Object
- #not_ok ⇒ Object
- #ok ⇒ Object
- #OR(*args) ⇒ Object
- #recorder ⇒ Object
- #skip_when(condition, reason) ⇒ Object
- #TODO ⇒ Object
Instance Attribute Details
#__at_end_blocks ⇒ Object
Returns the value of attribute __at_end_blocks.
1254 1255 1256 |
# File 'lib/oktest.rb', line 1254 def __at_end_blocks @__at_end_blocks end |
#__TODO ⇒ Object
Returns the value of attribute __TODO.
1254 1255 1256 |
# File 'lib/oktest.rb', line 1254 def __TODO @__TODO end |
Instance Method Details
#AND(*args) ⇒ Object
1519 1520 1521 1522 |
# File 'lib/oktest.rb', line 1519 def AND(*args) #; [!38jln] creates `AND` object. return JsonMatcher::AND.new(*args) end |
#Any ⇒ Object
1529 1530 1531 1532 |
# File 'lib/oktest.rb', line 1529 def Any() #; [!dlo1o] creates an 'Any' object. return JsonMatcher::Any.new end |
#at_end(&block) ⇒ Object
1315 1316 1317 1318 |
# File 'lib/oktest.rb', line 1315 def at_end(&block) #; [!x58eo] records clean-up block. (@__at_end_blocks ||= []) << block end |
#Bool ⇒ Object
1509 1510 1511 1512 |
# File 'lib/oktest.rb', line 1509 def Bool() #; [!vub5j] creates a set of true and false. return Enum(true, false) end |
#capture_command(command, input = "", &error_handler) ⇒ Object
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 |
# File 'lib/oktest.rb', line 1365 def capture_command(command, input="", &error_handler) require 'open3' unless defined?(::Open3) #; [!wyp17] executes command with stdin data. sout, serr, pstat = ::Open3.capture3(command, :stdin_data=>input) #; [!jd63p] raises error if command failed. #; [!lsmgq] calls error handler block if command failed. #; [!vivq3] doesn't call error handler block if command finished successfully. #; [!nxw59] not raise error if command failed and error handler specified. if pstat.exitstatus != 0 if block_given?() yield pstat else raise "Command failed with status (#{pstat.exitstatus}): `#{command}`" end end #; [!h5994] returns output of stdin and stderr. return sout, serr end |
#capture_command!(command, input = "", &error_handler) ⇒ Object
1384 1385 1386 1387 1388 1389 1390 1391 1392 |
# File 'lib/oktest.rb', line 1384 def capture_command!(command, input="", &error_handler) #; [!vlbpo] executes command with stdin data. #; [!yfohb] not raise error even if command failed. #; [!andyj] calls error handler block if command failed. #; [!xnkqc] doesn't call error handler block if command finished successfully. #; [!3xdgo] returns output of stdin and stderr. error_handler ||= proc do end capture_command(command, input, &error_handler) end |
#capture_stderr(input = "", tty: false, &b) ⇒ Object
1355 1356 1357 1358 1359 1360 1361 1362 1363 |
# File 'lib/oktest.rb', line 1355 def capture_stderr(input="", tty: false, &b) #; [!46tj4] same as `sout, serr = capture_stdio(); ok {sout} == ''`. #; [!3zh32] fails when stdout is not empty. sout, serr = capture_stdio(input, tty: tty, &b) sout == "" or raise FAIL_EXCEPTION, "Output of $stdout expected to be empty, but got: #{sout.inspect}" #; [!5vs64] returns output of stderr. return serr end |
#capture_stdio(input = "", tty: false, &b) ⇒ Object Also known as: capture_sio
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 |
# File 'lib/oktest.rb', line 1320 def capture_stdio(input="", tty: false, &b) require 'stringio' unless defined?(StringIO) bkup = [$stdin, $stdout, $stderr] #; [!53mai] takes $stdin data. $stdin = sin = StringIO.new(input) #; [!1kbnj] captures $stdio and $stderr. $stdout = sout = StringIO.new $stderr = serr = StringIO.new #; [!6ik8b] can simulate tty. if tty def sin.tty?; true; end def sout.tty?; true; end def serr.tty?; true; end end #; [!4j494] returns outpouts of stdout and stderr. yield sout, serr return sout.string, serr.string ensure #; [!wq8a9] recovers stdio even when exception raised. $stdin, $stdout, $stderr = bkup end |
#capture_stdout(input = "", tty: false, &b) ⇒ Object
1345 1346 1347 1348 1349 1350 1351 1352 1353 |
# File 'lib/oktest.rb', line 1345 def capture_stdout(input="", tty: false, &b) #; [!4agii] same as `sout, serr = capture_stdio(); ok {serr} == ''`. #; [!may84] fails when stderr is not empty. sout, serr = capture_stdio(input, tty: tty, &b) serr == "" or raise FAIL_EXCEPTION, "Output of $stderr expected to be empty, but got: #{serr.inspect}" #; [!5n04e] returns output of stdout. return sout end |
#dummy_attrs(object, **keyvals, &b) ⇒ Object
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 |
# File 'lib/oktest.rb', line 1460 def dummy_attrs(object, **keyvals, &b) #; [!4vd73] changes object attributes temporarily. prev_values = {} keyvals.each do |k, v| prev_values[k] = object.__send__(k) object.__send__("#{k}=", v) end #; [!fi0t3] recovers attribute values. recover = proc do prev_values.each {|k, v| object.__send__("#{k}=", v) } end #; [!27yeh] returns keyvals. #; [!j7tvp] can take block argument. return __do_dummy(keyvals, recover, &b) end |
#dummy_dir(dirname = nil, &b) ⇒ Object
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 |
# File 'lib/oktest.rb', line 1422 def dummy_dir(dirname=nil, &b) #; [!r14uy] generates temporary directory name if 1st arg is nil. dirname ||= "_tmpdir_#{rand().to_s[2...8]}" #; [!zypj6] raises error when dummy dir already exists. ! File.exist?(dirname) or raise ArgumentError, "dummy_dir('#{dirname}'): temporary directory already exists." #; [!l34d5] creates dummy directory. require 'fileutils' unless defined?(FileUtils) FileUtils.mkdir_p(dirname) #; [!01gt7] removes dummy directory even if it contains other files. recover = proc { FileUtils.rm_rf(dirname) if File.exist?(dirname) } #; [!jxh30] returns directory name. #; [!tfsqo] can take block argument. return __do_dummy(dirname, recover, &b) end |
#dummy_file(filename = nil, content = nil, encoding: 'utf-8', &b) ⇒ Object
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 |
# File 'lib/oktest.rb', line 1408 def dummy_file(filename=nil, content=nil, encoding: 'utf-8', &b) #; [!3mg26] generates temporary filename if 1st arg is nil. filename ||= "_tmpfile_#{rand().to_s[2...8]}" #; [!yvfxq] raises error when dummy file already exists. ! File.exist?(filename) or raise ArgumentError, "dummy_file('#{filename}'): temporary file already exists." #; [!7e0bo] creates dummy file. File.write(filename, content, encoding: encoding) recover = proc { File.unlink(filename) if File.exist?(filename) } #; [!nvlkq] returns filename. #; [!ky7nh] can take block argument. return __do_dummy(filename, recover, &b) end |
#dummy_ivars(object, **keyvals, &b) ⇒ Object
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 |
# File 'lib/oktest.rb', line 1476 def dummy_ivars(object, **keyvals, &b) #; [!rnqiv] changes instance variables temporarily. prev_values = {} keyvals.each do |k, v| prev_values[k] = object.instance_variable_get("@#{k}") object.instance_variable_set("@#{k}", v) end #; [!8oirn] recovers instance variables. recover = proc do prev_values.each {|k, v| object.instance_variable_set("@#{k}", v) } end #; [!01dc8] returns keyvals. #; [!myzk4] can take block argument. return __do_dummy(keyvals, recover, &b) end |
#dummy_values(hashobj, keyvals = {}, &b) ⇒ Object
never use keyword args!
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 |
# File 'lib/oktest.rb', line 1438 def dummy_values(hashobj, keyvals={}, &b) # never use keyword args! #; [!hgwg2] changes hash value temporarily. prev_values = {} key_not_exists = {} keyvals.each do |k, v| if hashobj.key?(k) prev_values[k] = hashobj[k] else key_not_exists[k] = true end hashobj[k] = v end #; [!jw2kx] recovers hash values. recover = proc do key_not_exists.each {|k, _| hashobj.delete(k) } prev_values.each {|k, v| hashobj[k] = v } end #; [!w3r0p] returns keyvals. #; [!pwq6v] can take block argument. return __do_dummy(keyvals, recover, &b) end |
#Enum(*values) ⇒ Object
1504 1505 1506 1507 |
# File 'lib/oktest.rb', line 1504 def Enum(*values) #; [!fbfr0] creates Enum object which is a subclass of Set. return JsonMatcher::Enum.new(values) end |
#fixture(name, *args, **kwargs) ⇒ Object
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 |
# File 'lib/oktest.rb', line 1292 def fixture(name, *args, **kwargs) #; [!zgfg9] finds fixture block in current or parent node. node = self.class.__node while node && (tuple = node.get_fixture_block(name)) == nil node = node.parent end #; [!wxcsp] raises error when fixture not found. unless tuple exc = FixtureNotFoundError.new("`#{name.inspect}`: fixture not found.") exc.set_backtrace([caller_locations(1, 1).first.to_s]) raise exc end #; [!m4ava] calls fixture block and returns result of it. #; [!l2mcx] accepts block arguments. block, _, _ = tuple return block.call(*args, **kwargs) end |
#JSON(actual) ⇒ Object
1499 1500 1501 1502 |
# File 'lib/oktest.rb', line 1499 def JSON(actual) #; [!n0k03] creates JsonMatcher object. return JsonMatcher.new(actual) end |
#Length(n) ⇒ Object
1524 1525 1526 1527 |
# File 'lib/oktest.rb', line 1524 def Length(n) #; [!qqas3] creates Length object. return JsonMatcher::Length.new(n) end |
#not_ok ⇒ Object
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 |
# File 'lib/oktest.rb', line 1271 def not_ok() #; [!agmx8] records invoked location. #; [!a9508] not record invoked location when `Config.ok_location == false`. if Config.ok_location location = caller_locations(1, 1).first else location = nil end #; [!d332o] creates new assertion object for negative condition. actual = yield ass = Oktest::AssertionObject.new(actual, false, location) Oktest::AssertionObject::NOT_YET[ass.__id__] = ass return ass end |
#ok ⇒ Object
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 |
# File 'lib/oktest.rb', line 1256 def ok() #; [!bc3l2] records invoked location. #; [!mqtdy] not record invoked location when `Config.ok_location == false`. if Config.ok_location location = caller_locations(1, 1).first else location = nil end #; [!3jhg6] creates new assertion object. actual = yield ass = Oktest::AssertionObject.new(actual, true, location) Oktest::AssertionObject::NOT_YET[ass.__id__] = ass return ass end |
#OR(*args) ⇒ Object
1514 1515 1516 1517 |
# File 'lib/oktest.rb', line 1514 def OR(*args) #; [!9e8im] creates `OR` object. return JsonMatcher::OR.new(*args) end |
#recorder ⇒ Object
1492 1493 1494 1495 1496 1497 |
# File 'lib/oktest.rb', line 1492 def recorder() #; [!qwrr8] loads 'benry/recorder' automatically. require 'benry/recorder' unless defined?(Benry::Recorder) #; [!glfvx] creates Benry::Recorder object. return Benry::Recorder.new end |
#skip_when(condition, reason) ⇒ Object
1286 1287 1288 1289 1290 |
# File 'lib/oktest.rb', line 1286 def skip_when(condition, reason) #; [!3xqf4] raises SkipException if condition is truthy. #; [!r7cxx] not raise nothing if condition is falsy. raise SkipException, reason if condition end |
#TODO ⇒ Object
1310 1311 1312 1313 |
# File 'lib/oktest.rb', line 1310 def TODO() location = caller_locations(1, 1).first # ex: "foo_test.rb:123:in ...." @__TODO = location end |