Module: Oktest::SpecHelper

Defined in:
lib/oktest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__at_end_blocksObject

Returns the value of attribute __at_end_blocks.



1221
1222
1223
# File 'lib/oktest.rb', line 1221

def __at_end_blocks
  @__at_end_blocks
end

#__TODOObject

Returns the value of attribute __TODO.



1221
1222
1223
# File 'lib/oktest.rb', line 1221

def __TODO
  @__TODO
end

Instance Method Details

#AND(*args) ⇒ Object



1434
1435
1436
1437
# File 'lib/oktest.rb', line 1434

def AND(*args)
  #; [!38jln] creates `AND` object.
  return JsonMatcher::AND.new(*args)
end

#AnyObject



1444
1445
1446
1447
# File 'lib/oktest.rb', line 1444

def Any()
  #; [!dlo1o] creates an 'Any' object.
  return JsonMatcher::Any.new
end

#at_end(&block) ⇒ Object



1282
1283
1284
1285
# File 'lib/oktest.rb', line 1282

def at_end(&block)
  #; [!x58eo] records clean-up block.
  (@__at_end_blocks ||= []) << block
end

#BoolObject



1424
1425
1426
1427
# File 'lib/oktest.rb', line 1424

def Bool()
  #; [!vub5j] creates a set of true and false.
  return Enum(true, false)
end

#capture_sio(input = "", tty: false, &b) ⇒ Object



1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
# File 'lib/oktest.rb', line 1287

def capture_sio(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

#dummy_attrs(object, **keyvals, &b) ⇒ Object



1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
# File 'lib/oktest.rb', line 1375

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



1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
# File 'lib/oktest.rb', line 1337

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



1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
# File 'lib/oktest.rb', line 1323

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



1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
# File 'lib/oktest.rb', line 1391

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!



1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
# File 'lib/oktest.rb', line 1353

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



1419
1420
1421
1422
# File 'lib/oktest.rb', line 1419

def Enum(*values)
  #; [!fbfr0] creates Enum object which is a subclass of Set.
  return JsonMatcher::Enum.new(values)
end

#fixture(name, *args, **kwargs) ⇒ Object



1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'lib/oktest.rb', line 1259

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



1414
1415
1416
1417
# File 'lib/oktest.rb', line 1414

def JSON(actual)
  #; [!n0k03] creates JsonMatcher object.
  return JsonMatcher.new(actual)
end

#Length(n) ⇒ Object



1439
1440
1441
1442
# File 'lib/oktest.rb', line 1439

def Length(n)
  #; [!qqas3] creates Length object.
  return JsonMatcher::Length.new(n)
end

#not_okObject



1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
# File 'lib/oktest.rb', line 1238

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

#okObject



1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
# File 'lib/oktest.rb', line 1223

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



1429
1430
1431
1432
# File 'lib/oktest.rb', line 1429

def OR(*args)
  #; [!9e8im] creates `OR` object.
  return JsonMatcher::OR.new(*args)
end

#recorderObject



1407
1408
1409
1410
1411
1412
# File 'lib/oktest.rb', line 1407

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

Raises:



1253
1254
1255
1256
1257
# File 'lib/oktest.rb', line 1253

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

#TODOObject



1277
1278
1279
1280
# File 'lib/oktest.rb', line 1277

def TODO()
  location = caller_locations(1, 1).first   # ex: "foo_test.rb:123:in ...."
  @__TODO = location
end