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.



1172
1173
1174
# File 'lib/oktest.rb', line 1172

def __at_end_blocks
  @__at_end_blocks
end

#__TODOObject

Returns the value of attribute __TODO.



1172
1173
1174
# File 'lib/oktest.rb', line 1172

def __TODO
  @__TODO
end

Instance Method Details

#AND(*args) ⇒ Object



1385
1386
1387
1388
# File 'lib/oktest.rb', line 1385

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

#AnyObject



1395
1396
1397
1398
# File 'lib/oktest.rb', line 1395

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

#at_end(&block) ⇒ Object



1233
1234
1235
1236
# File 'lib/oktest.rb', line 1233

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

#BoolObject



1375
1376
1377
1378
# File 'lib/oktest.rb', line 1375

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

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



1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'lib/oktest.rb', line 1238

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



1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
# File 'lib/oktest.rb', line 1326

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



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

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



1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# File 'lib/oktest.rb', line 1274

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



1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
# File 'lib/oktest.rb', line 1342

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!



1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'lib/oktest.rb', line 1304

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



1370
1371
1372
1373
# File 'lib/oktest.rb', line 1370

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

#fixture(name, *args) ⇒ Object



1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
# File 'lib/oktest.rb', line 1210

def fixture(name, *args)
  #; [!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(1).first])
    raise exc
  end
  #; [!m4ava] calls fixture block and returns result of it.
  #; [!l2mcx] accepts block arguments.
  block, _, _ = tuple
  return block.call(*args)
end

#JSON(actual) ⇒ Object



1365
1366
1367
1368
# File 'lib/oktest.rb', line 1365

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

#Length(n) ⇒ Object



1390
1391
1392
1393
# File 'lib/oktest.rb', line 1390

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

#not_okObject



1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
# File 'lib/oktest.rb', line 1189

def not_ok()
  #; [!agmx8] records invoked location.
  #; [!a9508] not record invoked location when `Config.ok_location == false`.
  if Config.ok_location
    location = caller(1).first  # caller() makes performance slower, but necessary.
  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



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'lib/oktest.rb', line 1174

def ok()
  #; [!bc3l2] records invoked location.
  #; [!mqtdy] not record invoked location when `Config.ok_location == false`.
  if Config.ok_location
    location = caller(1).first  # caller() makes performance slower, but necessary.
  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



1380
1381
1382
1383
# File 'lib/oktest.rb', line 1380

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

#recorderObject



1358
1359
1360
1361
1362
1363
# File 'lib/oktest.rb', line 1358

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:



1204
1205
1206
1207
1208
# File 'lib/oktest.rb', line 1204

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



1228
1229
1230
1231
# File 'lib/oktest.rb', line 1228

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