diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index a888f64e..92e2aa45 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -398,6 +398,39 @@ func (vm *VM) bootstrap() { return object.NewString(vm.formatString(vm.coerceFormatString(args[0]), args[1:])) } vm.cObject.define("format", formatFn) + // Kernel#test(cmd, file1, file2 = nil): a single-character file test, delegating + // to the matching File query. cmd is the ?x character literal (an Integer) or a + // one-character String. + vm.cObject.define("test", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) < 2 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 2..3)", len(args)) + } + var c byte + switch cmd := args[0].(type) { + case object.Integer: + c = byte(cmd) + case *object.String: + if len(cmd.Str()) == 0 { + raise("ArgumentError", "argument for test is not a valid command") + } + c = cmd.Str()[0] + default: + raise("TypeError", "no implicit conversion of %s into Integer", vm.classOf(args[0]).name) + } + cFile := vm.consts["File"].(*RClass) + meth := map[byte]string{ + 'e': "exist?", 'f': "file?", 'd': "directory?", + 'r': "readable?", 'R': "readable?", 'w': "writable?", 'W': "writable?", + 'x': "executable?", 'X': "executable?", 'l': "symlink?", + 's': "size?", 'z': "zero?", 'M': "mtime", 'A': "atime", 'C': "ctime", + }[c] + if meth == "" { + raise("ArgumentError", "unknown command '?%c'", c) + } + return vm.send(cFile, meth, []object.Value{args[1]}, nil) + }) + // Kernel#test is a private method, so it does not show up in respond_to?. + vm.setInstanceVisibility(vm.cObject, "test", visPrivate) vm.cObject.define("sprintf", formatFn) vm.cObject.define("printf", nativePrintf) vm.cObject.define("proc", func(_ *VM, _ object.Value, _ []object.Value, blk *Proc) object.Value { diff --git a/internal/vm/kernel_test_internal_test.go b/internal/vm/kernel_test_internal_test.go new file mode 100644 index 00000000..d27fcf80 --- /dev/null +++ b/internal/vm/kernel_test_internal_test.go @@ -0,0 +1,48 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import ( + "strings" + "testing" +) + +// TestKernelTest covers Kernel#test: the boolean/size/time file tests delegated +// to File, the ?x-character (Integer) and one-character-String command forms, the +// private visibility (not in respond_to?), and the error paths. Files are created +// under Dir.mktmpdir. Asserted against MRI Ruby 4.0.6. +func TestKernelTest(t *testing.T) { + pre := `require "tmpdir" +Dir.mktmpdir do |d| + f = File.join(d, "a"); File.write(f, "hi") + e = File.join(d, "e"); File.write(e, "") + ` + c := func(body string) string { return pre + body + "\nend" } + cases := []struct{ src, want string }{ + {c(`p [test(?e, f), test(?f, f), test(?d, d), test(?d, f)]`), "[true, true, true, false]\n"}, + {c(`p [test(?r, f), test(?w, f), test(?R, f), test(?W, f)]`), "[true, true, true, true]\n"}, + {c(`p [test(?s, f), test(?s, e), test(?z, f), test(?z, e)]`), "[2, nil, false, true]\n"}, + {c(`p [test(?M, f).class, test(?A, f).class, test(?C, f).class]`), "[Time, Time, Time]\n"}, + {c(`p test("e", f)`), "true\n"}, // a one-character String command + {c(`p test(?e, "/no/such/path")`), "false\n"}, + {c(`p respond_to?(:test)`), "false\n"}, // Kernel#test is private + } + for _, tc := range cases { + if got := eval(t, tc.src); got != tc.want { + t.Errorf("src=%q got=%q want=%q", tc.src, got, tc.want) + } + } + errs := []struct{ src, want string }{ + {`test(?e)`, "wrong number of arguments"}, + {`test(?Q, "x")`, "unknown command"}, + {`test("", "x")`, "not a valid command"}, + {`test(Object.new, "x")`, "no implicit conversion of Object into Integer"}, + } + for _, tc := range errs { + if err := runErr(t, tc.src); err == nil || !strings.Contains(err.Error(), tc.want) { + t.Errorf("src=%q err=%v, want substring %q", tc.src, err, tc.want) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index b02867d9..29938696 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16250 +16260