Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,26 @@ func (vm *VM) bootstrap() {
}
return arr
}}
// Range.new(begin, end, exclude_end = false) builds a Range value (the third
// argument is exclusive when truthy, as MRI treats any non-false value). The
// endpoints must be comparable with #<=> unless one is nil (a beginless or
// endless range); a nil comparison is a "bad value for range" and an exception
// from #<=> propagates. A subclass instance wraps the range.
vm.cRange.smethods["new"] = &Method{name: "new", owner: vm.cRange,
native: func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
if len(args) < 2 || len(args) > 3 {
raise("ArgumentError", "wrong number of arguments (given %d, expected 2..3)", len(args))
}
lo, hi := args[0], args[1]
if !object.IsNil(lo) && !object.IsNil(hi) && object.IsNil(vm.send(lo, "<=>", []object.Value{hi}, nil)) {
raise("ArgumentError", "bad value for range")
}
r := object.NewRange(lo, hi, len(args) == 3 && args[2].Truthy())
if recv := self.(*RClass); recv != vm.cRange {
return &RObject{class: recv, ivars: map[string]object.Value{}, builtin: r}
}
return r
}}
vm.cArray.define("values_at", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
a := self.(*object.Array).Elems
out := make([]object.Value, len(args))
Expand Down
48 changes: 48 additions & 0 deletions internal/vm/range_new_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestRangeNew covers Range.new: an inclusive or exclusive range (the third
// argument is exclusive when truthy, not only when literally true), string
// endpoints, beginless and endless ranges, the ArgumentError for endpoints that
// #<=> reports as incomparable, an exception from #<=> propagating, the arity
// errors, and a subclass instance wrapping the range. Verified against ruby
// 4.0.6.
func TestRangeNew(t *testing.T) {
cases := []struct{ src, want string }{
{`p Range.new(1, 10).to_a`, `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`},
{`p Range.new(1, 10, true).to_a`, `[1, 2, 3, 4, 5, 6, 7, 8, 9]`},
{`p Range.new(1, 10, false).to_a`, `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`},
{`p Range.new("a", "c").to_a`, `["a", "b", "c"]`},
{`p Range.new(1, 5).sum`, `15`},
{`p Range.new(1, 5).exclude_end?`, `false`},
{`p Range.new(1, 5, true).exclude_end?`, `true`},
// Any truthy third argument makes the range exclusive.
{`p Range.new(1, 3, 1).to_a`, `[1, 2]`},
{`p Range.new(1, 3, :x).to_a`, `[1, 2]`},
{`p Range.new(1, 3, nil).to_a`, `[1, 2, 3]`},
// Beginless / endless ranges skip the comparison.
{`p Range.new(nil, 5).begin`, `nil`},
{`p Range.new(1, nil).end`, `nil`},
// Incomparable endpoints (#<=> gives nil) raise ArgumentError.
{`begin; Range.new(1, Object.new); rescue ArgumentError => e; p e.message; end`, `"bad value for range"`},
// An exception raised inside #<=> propagates (is not rescued).
{`class Boom; def <=>(o); raise "boom"; end; end
begin; Range.new(Boom.new, Boom.new); rescue RuntimeError => e; p e.message; end`, `"boom"`},
// Arity errors.
{`begin; Range.new(1); rescue ArgumentError => e; p e.class; end`, `ArgumentError`},
{`begin; Range.new(1, 2, 3, 4); rescue ArgumentError => e; p e.class; end`, `ArgumentError`},
// A subclass instance wraps the range and still works.
{`class MyRange < Range; end; p MyRange.new(1, 3).to_a`, `[1, 2, 3]`},
{`class MyRange2 < Range; end; p MyRange2.new(1, 3).class.superclass`, `Range`},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want+"\n" {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n")
}
}
}