Skip to content

Commit

Permalink
Add new array support
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Jun 6, 2024
1 parent 948430d commit 44dde53
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions features/array.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Array

Scenario: Create a new array
When I execute ruby code:
"""
[1, "str", 3]
"""
Then there should return an array
"""
[1 str 3]
"""
19 changes: 19 additions & 0 deletions godogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ func (feat *RubyFeature) thereShouldReturnModule(expected string) error {
return nil
}

func (feat *RubyFeature) thereShouldReturnAnArray(doc *godog.DocString) error {
actual, ok := feat.ret.([]mruby.Value)

if !ok {
return fmt.Errorf("expected array, got %T", feat.ret)
}

actualStr := fmt.Sprintf("%+v", actual)
expectedStr := doc.Content

if !cmp.Equal(actualStr, expectedStr) {
return fmt.Errorf("array not matched %s", cmp.Diff(actual, expectedStr))

}

return nil
}

func (feat *RubyFeature) theExceptionMessageShouldBe(expected string) error {
exc, ok := feat.ret.(mruby.RException)

Expand Down Expand Up @@ -176,6 +194,7 @@ func InitializeScenario(s *godog.ScenarioContext) {
s.Step(`^there should return object$`, feat.thereShouldReturnObject)
s.Step(`^there should return class "([^"]*)"$`, feat.thereShouldReturnClass)
s.Step(`^there should return module "([^"]*)"$`, feat.thereShouldReturnModule)
s.Step(`^there should return an array$`, feat.thereShouldReturnAnArray)
s.Step(`^the exception message should be "([^"]*)"$`, feat.theExceptionMessageShouldBe)
}

Expand Down
5 changes: 4 additions & 1 deletion stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ func (s *Stack[T]) Slice(start, end int) []T {
end = s.end
}

return s.elements[start:end]
subset := make([]T, end-start)
copy(subset, s.elements[start:end])

return subset
}

func (s *Stack[T]) Get(idx int) T {
Expand Down
8 changes: 8 additions & 0 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ func (mrb *State) VmExec(proc RProc, code *insn.Sequence) (ret Value, err error)
}

ctx.Set(int(a), res)
case op.Array:
a := code.ReadB()
b := code.ReadB()

begin := int(a)
end := int(a) + int(b) - 1

ctx.Set(int(a), ctx.Slice(begin, end))
case op.String:
a := code.ReadB()
b := code.ReadB()
Expand Down

0 comments on commit 44dde53

Please sign in to comment.