class Object

print

nonempty

set_attr

flib

must

unproc

__funcall

Public Instance Methods

__funcall(meth, *args, &block) click to toggle source

Call a method even if it is private.

# File lib/el4r/el4r-sub.rb, line 947
def __funcall(meth, *args, &block)
  m = method(meth)
  instance_eval { m.call(*args, &block) }
end
_getopts_sub(argv, single_opts, *long_opts) click to toggle source

getopts

# File lib/el4r/el4r-sub.rb, line 190
def _getopts_sub(argv, single_opts, *long_opts)
  require 'optparse'
  opt = OptionParser.new

  (single_opts || "").split(//).each do |single_opt|
    opt.on("-#{single_opt}"){ eval "$OPT_#{single_opt}=true" }
  end

  long_opts.each do |long_opt|
    have_arg_p = (long_opt[-1,1] == ':')
    long_opt.chomp!(':')
    block = lambda{|x| eval "$OPT_#{long_opt}=x"}
    if have_arg_p
      if long_opt.length == 1   # -x arg
        opt.on("-#{long_opt} [ARG]",&block)
      else                    # --long arg
        opt.on("--#{long_opt}=[ARG]",&block)
      end
    else                        # --long
      opt.on("--#{long_opt}"){ eval "$OPT_#{long_opt}=true"}
    end
  end

  opt.parse! argv
end
bug!( message = 'must not happen' ) click to toggle source

Raises ScriptBug exception.

# File lib/el4r/el4r-sub.rb, line 772
def bug!( message = 'must not happen' )
  raise ScriptBug, "\n[SCRIPT BUG] " + message
end
ev(str) click to toggle source

eval and print

# File lib/el4r/el4r-sub.rb, line 33
def ev(str)
  puts "#{str} = #{eval str}"
end
exec_el4r(test_mode=true) click to toggle source
# File lib/el4r/exec-el4r.rb, line 23
def exec_el4r(test_mode=true)
  el4r_root = nil
  shellargs = []
  emacs = nil
  ruby = nil
  unittest_args = []
  strip_instance_error = "t"
  load_path = []
  show_only = false
  init = false

  ARGV.options do |o|
    o.on("-Q", "--init", "load site-start.el and .emacs") { init = true }
    o.on("-b", "--batch", "batch mode"){ shellargs << "-batch" }
    o.on("-i", "interactive mode") { shellargs.delete "-batch"}
    o.on("-e EMACS", "--emacs=EMACS", "set emacs binary [default: #{emacs}]") {|v| emacs = v }
    o.on("--ruby=RUBY", "set ruby binary [default: #{ruby}]") {|v| ruby = v }
    o.on("-I load-path", "set load-path") {|v| load_path << v}
    o.on("-r DIR", "--el4r-root=DIR", "el4r package root directory [for debug]") {|v| ENV['EL4R_ROOT'] = el4r_root = File.expand_path(v) }
    if test_mode
      o.on("-n NAME", "--name=NAME", "Runs tests matching NAME.",
           "(patterns may be used).") {|v| unittest_args << "-n" << v}
      o.on("-t TESTCASE", "--testcase=TESTCASE", "Runs tests in TestCases matching TESTCASE.",
           "(patterns may be used).") {|v| unittest_args << "-t" << v}
      o.on("-v", "verbose output"){|v| unittest_args << "-v"}
    end
    o.on("--nw", "don't communicate with X, ignoring $DISPLAY",
         "(emacs -nw)"){|v| shellargs << "--nw"}
    o.on("-d", "--debug", "debug output"){ strip_instance_error = "nil" }
    o.on("-l LOGFILE", "--log=LOGFILE", "Specify a log file.") {|v|
      ENV['EL4R_LOG']=File.expand_path(v);
      ENV['EL4R_PRESERVE_LOG']='1'
    }
    o.on("--show", "Show the test information only, for diagnosis.") { show_only = true}
    o.banner += " file"
    o.parse!
  end

  load "~/.el4rrc.rb"; conf=__conf__
  emacs ||= conf.emacs_program
  ruby ||= conf.ruby_program

  basearg = if init
              []
            else
              if emacs =~ /xemacs/
                ["-no-site-file", "-no-init-file"]
              else
                ["--no-site-file", "--no-init-file"]
              end
            end
  

  if el4r_root
    el4r_el = File.join(el4r_root, conf.el_program_relative)
    el4r_instance = File.join(el4r_root, conf.instance_program_relative)
  else
    el4r_el = conf.el_program
    el4r_instance = conf.instance_program
  end
  unittest_source = ARGV.first    # TODO: multiple sources

  raise "no such file or directory: #{unittest_source}" unless File.exist? unittest_source

  unittest_args_lisp = "'(" + unittest_args.compact.map{|x| x.dump}.join(" ") + ")"
  load_path_lisp = "(setq load-path (append '(" + load_path.map{|x| x.dump}.join(" ") + ") load-path))"

  lisp = format(<<'EOF', el4r_el, ruby, el4r_instance, unittest_source, unittest_args_lisp, strip_instance_error, load_path_lisp)
(when noninteractive
  (defun message (&rest args)))
(defun instantly-kill-emacs ()
  (interactive)
  (kill-emacs 0))

(setq help-msg "q, M-k:quit / C-c: run-test / l, C-l: show-log / SPC,b: scroll")
(defun unittest ()
  (interactive)
  (el4r-run-unittest)
  (or noninteractive (message help-msg)))
  
(define-key global-map "\C-x\C-c" 'instantly-kill-emacs)

(define-derived-mode unittest-mode fundamental-mode "UnitTest"
  ""
  (define-key unittest-mode-map "\M-k" 'instantly-kill-emacs)
  (define-key unittest-mode-map "q" 'instantly-kill-emacs)

  (define-key unittest-mode-map "\C-c" 'unittest)
  (define-key unittest-mode-map "\C-l" 'el4r-show-log)
  (define-key unittest-mode-map "l" 'el4r-show-log)

  (define-key unittest-mode-map "\M-d" 'edebug-defun)
  (define-key unittest-mode-map " " 'scroll-up)
  (define-key unittest-mode-map "b" 'scroll-down)
  )

(fset 'yes-or-no-p 'y-or-n-p)
;(setq debug-on-error t)
;(setq debug-on-signal t)
(setq debug-on-quit t)
(setq pop-up-windows nil)

(load "%s")
(defun el4r-override-variables ()
  (setq el4r-ruby-program "%s")
  (setq el4r-instance-program "%s")
  (setq el4r-unittest-file-name "%s")
  (setq el4r-load-script el4r-unittest-file-name)
  (setq el4r-unittest-args %s)
  (setq el4r-coding-system 'euc-jp-unix)
  )
(defun el4r-load-script ()
  (interactive)
  (el4r-load el4r-load-script))
(el4r-boot t)
(setq el4r-unittest-strip-instance-error %s)
(el4r-ruby-eval "self.el4r_is_debug = true")
(set-buffer "*scratch*")
%s
EOF
#'

  if show_only
    %w[
  ruby emacs el4r_el el4r_instance shellargs basearg
  unittest_source unittest_args load_path ENV['EL4R_ROOT']
  ].each do |varname|
      value = eval(varname).inspect
      puts "#{varname} = #{value}"
    end
    exit
  end


  begin
    el = File.join(Dir.tmpdir, "testrun.el")

    open(el,"w"){|w| w.write lisp}

    shellargs << "-l" << el
    if test_mode
      shellargs << "-f" << "unittest"
    else
      shellargs << "-f" << "el4r-load-script"
    end

    args = [emacs]+basearg+shellargs
    system *args

  ensure
#    File.unlink el
  end

end
getopts(single_opts, *long_opts) click to toggle source

getopts compatibility layer using optparse.rb

# File lib/el4r/el4r-sub.rb, line 217
def getopts(single_opts, *long_opts)
  _getopts_sub ARGV, single_opts, *long_opts
end
must( *args ) click to toggle source

Assert: type === obj ex. obj.must Fixnum, Float

# File lib/el4r/el4r-sub.rb, line 781
def must( *args )
  args.each {|c| return self if c === self }
  raise TypeError, "wrong arg type '#{self.class}' for required #{args.join('/')}"
end
must_be( obj ) click to toggle source

Assert: self == obj

# File lib/el4r/el4r-sub.rb, line 800
def must_be( obj )
  self == obj or
          raise ArgumentError, "expected #{obj.inspect} but is #{inspect}"
  self
end
must_exist() click to toggle source

Assert: self != nil

# File lib/el4r/el4r-sub.rb, line 807
def must_exist
  nil? and raise ArgumentError, 'receiver is wrongly nil'
end
must_have( *args ) click to toggle source

Assert: obj.respond_to? meth ex. obj.must_have :read, :readlines ex. obj.needed :read, :readlines

# File lib/el4r/el4r-sub.rb, line 789
def must_have( *args )
  args.each do |m|
    self.respond_to? m or
        raise ArgumentError, "receiver #{inspect} does not have '#{m}'"
  end
  self
end
Also aliased as: needed
needed( *args )
Alias for: must_have
nonempty?() click to toggle source

Is self non-nil and not-empty.

# File lib/el4r/el4r-sub.rb, line 132
def nonempty?
  if self and !self.empty?
    self
  end
end
notify_exit() click to toggle source

notify_exit

# File lib/el4r/el4r-sub.rb, line 590
def notify_exit
  # Notify when the program is exited.
  at_exit do
    bell_message "#$0 exited."
  end
end
pr(msg=nil) click to toggle source

puts MSG = self.inspect for debug.

# File lib/el4r/el4r-sub.rb, line 13
def pr(msg=nil)
  if msg then
    print "#{msg} = "
  end

  display
  print "\n"
  self
end
readf(filename) click to toggle source

Same as File.read. But FILENAME is expanded.

# File lib/el4r/el4r-sub.rb, line 579
def readf(filename)
  File.read( File.expand_path(filename.to_s) )
end
set_attr(ivar_name, init_value) click to toggle source

Defines a singleton attribute. for testing purpose.

# File lib/el4r/el4r-sub.rb, line 271
def set_attr(ivar_name, init_value)
  eval("class << self; attr_accessor :#{ivar_name} end")
  self.instance_variable_set("@#{ivar_name}", init_value)
end
show_usage(msg=nil) click to toggle source
show_usage

Prints the script's first comment block.

# File lib/el4r/el4r-sub.rb, line 843
def show_usage(msg=nil)
  name = caller[-1].sub(/:\d+$/, '')
  $stderr.puts "\nError: #{msg}" if msg
  $stderr.puts
  File.open(name) do |f|
    while line = f.readline and line.sub!(/^# ?/, '')
      $stderr.puts line
    end
  end
  exit 1
end
system_safe(*x) click to toggle source
system_safe

mswin32 ruby's `system' workaround.

# File lib/el4r/el4r-sub.rb, line 876
def system_safe(*x)
  begin
    system *x
  rescue
  end
end
system_to_string(*args) click to toggle source
# File lib/el4r/el4r-sub.rb, line 538
def system_to_string(*args)
  begin
    tmpf = File.join(Dir.tmpdir, "#{$$}-#{@@__system_to_string_count__}")
    @@__system_to_string_count__ += 1
    ret = nil
    open(tmpf,"w") do |f|
      IO.redirect(f) {
        system *args
      }
    end
    File.read(tmpf)
  ensure
    FileUtils.rm_f tmpf
  end
end
unproc(*x) click to toggle source
# File lib/el4r/el4r-sub.rb, line 886
def unproc(*x)
  self
end
vpr(msg=nil) click to toggle source

pr when $VERBOSE

# File lib/el4r/el4r-sub.rb, line 24
def vpr(msg=nil)
  if $VERBOSE then
    self.pr msg
  else
    self
  end
end
vrequire(f) click to toggle source
vrequire

verbose require

# File lib/el4r/el4r-sub.rb, line 55
def vrequire(f)
  puts "loading " + f + "..."
  require f
  puts "loading " + f + "...done" 
end
vsystem(cmd) click to toggle source
vsystem

verbose system

# File lib/el4r/el4r-sub.rb, line 63
def vsystem(cmd)
  puts cmd
  system cmd
end
with_temp_dir(dir=Dir.tmpdir+"/tmp_ { |dir| ... } click to toggle source

Makes a temporary directory and executes a block and cleans the directory.

# File lib/el4r/el4r-sub.rb, line 468
def with_temp_dir(dir=Dir.tmpdir+"/tmp_#{$$}")
  require 'fileutils'
  begin
    dir = Pathname.new(dir).expand_path
    dir.mkdir
    Dir.chdir(dir) { yield(dir) }
  ensure
    FileUtils.rm_rf(dir)
  end
end
writef(filename) click to toggle source

Write an object's string form. FILENAME is expanded.

# File lib/el4r/el4r-sub.rb, line 584
def writef(filename)
  open(File.expand_path(filename.to_s), "w"){|f| f.write(self.to_s)}
end