Index: . =================================================================== --- . (revision 2971) +++ . (working copy) @@ -46,6 +46,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; +import java.util.Calendar; +import java.util.GregorianCalendar; import java.util.Iterator; import java.util.List; import java.util.HashMap; @@ -145,7 +147,7 @@ module.defineFastModuleFunction("system", callbackFactory.getFastOptSingletonMethod("system")); // TODO: Implement Kernel#exec differently? module.defineFastModuleFunction("exec", callbackFactory.getFastOptSingletonMethod("system")); - // TODO: Implement Kernel#test (partial impl) + module.defineFastModuleFunction("test", callbackFactory.getFastOptSingletonMethod("test")); module.defineModuleFunction("throw", callbackFactory.getOptSingletonMethod("rbThrow")); // TODO: Implement Kernel#trace_var module.defineModuleFunction("trap", callbackFactory.getOptSingletonMethod("trap")); @@ -826,6 +828,70 @@ Thread.yield(); } } + public static IRubyObject test(IRubyObject recv, IRubyObject[] args) { + int cmd = (int) args[0].convertToInteger().getLongValue(); + IRuby runtime = recv.getRuntime(); + File pwd = new File(recv.getRuntime().getCurrentDirectory()); + File file1 = new File(pwd, args[1].toString()); + Calendar calendar; + switch (cmd) { + // ?A | Time | Last access time for file1 + // ?b | boolean | True if file1 is a block device + // ?c | boolean | True if file1 is a character device + // ?C | Time | Last change time for file1 + // ?d | boolean | True if file1 exists and is a directory + // ?e | boolean | True if file1 exists + // ?f | boolean | True if file1 exists and is a regular file + case 'f': + return new RubyBoolean(runtime, file1.isFile()); + // ?g | boolean | True if file1 has the \CF{setgid} bit + // | | set (false under NT) + // ?G | boolean | True if file1 exists and has a group + // | | ownership equal to the caller's group + // ?k | boolean | True if file1 exists and has the sticky bit set + // ?l | boolean | True if file1 exists and is a symbolic link + // ?M | Time | Last modification time for file1 + case 'M': + calendar = new GregorianCalendar(); + calendar.setTimeInMillis(file1.lastModified()); + return new RubyTime(runtime, runtime.getClass("Time"), calendar); + // ?o | boolean | True if file1 exists and is owned by + // | | the caller's effective uid + // ?O | boolean | True if file1 exists and is owned by + // | | the caller's real uid + // ?p | boolean | True if file1 exists and is a fifo + // ?r | boolean | True if file1 is readable by the effective + // | | uid/gid of the caller + // ?R | boolean | True if file is readable by the real + // | | uid/gid of the caller + // ?s | int/nil | If file1 has nonzero size, return the size, + // | | otherwise return nil + // ?S | boolean | True if file1 exists and is a socket + // ?u | boolean | True if file1 has the setuid bit set + // ?w | boolean | True if file1 exists and is writable by + // | | the effective uid/gid + // ?W | boolean | True if file1 exists and is writable by + // | | the real uid/gid + // ?x | boolean | True if file1 exists and is executable by + // | | the effective uid/gid + // ?X | boolean | True if file1 exists and is executable by + // | | the real uid/gid + // ?z | boolean | True if file1 exists and has a zero length + // + // Tests that take two files: + // + // ?- | boolean | True if file1 and file2 are identical + // ?= | boolean | True if the modification times of file1 + // | | and file2 are equal + // ?< | boolean | True if the modification time of file1 + // | | is prior to that of file2 + // ?> | boolean | True if the modification time of file1 + // | | is after that of file2 + // + } + // FIXME: We can probably fake some basic signals, but obviously can't do everything. For now, stub. + return recv.getRuntime().getNil(); + } public static IRubyObject backquote(IRubyObject recv, IRubyObject aString) { IRuby runtime = recv.getRuntime();