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.



926
927
928
# File 'lib/oktest.rb', line 926

def __at_end_blocks
  @__at_end_blocks
end

#__TODOObject

Returns the value of attribute __TODO.



926
927
928
# File 'lib/oktest.rb', line 926

def __TODO
  @__TODO
end

Instance Method Details

#at_end(&block) ⇒ Object



987
988
989
990
# File 'lib/oktest.rb', line 987

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

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



992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/oktest.rb', line 992

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



1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
# File 'lib/oktest.rb', line 1080

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



1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/oktest.rb', line 1042

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



1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
# File 'lib/oktest.rb', line 1028

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



1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'lib/oktest.rb', line 1096

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!



1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# File 'lib/oktest.rb', line 1058

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

#fixture(name, *args) ⇒ Object



964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
# File 'lib/oktest.rb', line 964

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

#not_okObject



943
944
945
946
947
948
949
950
951
952
953
954
955
956
# File 'lib/oktest.rb', line 943

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



928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/oktest.rb', line 928

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

#recorderObject



1112
1113
1114
1115
1116
1117
# File 'lib/oktest.rb', line 1112

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:



958
959
960
961
962
# File 'lib/oktest.rb', line 958

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



982
983
984
985
# File 'lib/oktest.rb', line 982

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