Skip to content

Commit

Permalink
Add anyToString method
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Oct 24, 2024
1 parent 4822044 commit 42474d9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (mrb *State) DefineClassId(name Symbol, super RClass) (RClass, error) {
return mrb.defineClass(name, super, mrb.ObjectClass)
}

func (mrb *State) ObjectClassName(obj Value) string {
return mrb.ClassName(mrb.Class(obj))
}

func (mrb *State) ClassName(class RClass) string {
if class == nil {
return ""
Expand Down
2 changes: 1 addition & 1 deletion features/object.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ Feature: Object
end
Hello.new.inspect
"""
Then there should return string "Hello"
Then there should return string like "#<Hello:0x[0-9a-f]+>"
20 changes: 20 additions & 0 deletions godogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"regexp"
"testing"

"github.com/cucumber/godog"
Expand Down Expand Up @@ -99,6 +100,24 @@ func (feat *RubyFeature) thereShouldReturnString(expected string) error {
return nil
}

func (feat *RubyFeature) thereShouldReturnStringLike(expected string) error {
actual, ok := feat.ret.(string)
if !ok {
return fmt.Errorf("expected string, got %T (%+v)", feat.ret, feat.ret)
}

expr, err := regexp.Compile(expected)
if err != nil {
return err
}

if !expr.MatchString(actual) {
return fmt.Errorf("string not matched %s", cmp.Diff(actual, expected))
}

return nil
}

func (feat *RubyFeature) thereShouldReturnSymbol(expected string) error {
ret, ok := feat.ret.(mruby.Symbol)
if !ok {
Expand Down Expand Up @@ -213,6 +232,7 @@ func InitializeScenario(s *godog.ScenarioContext) {
s.Step(`^there should return false$`, feat.thereShouldReturnFalse)
s.Step(`^there should return nil$`, feat.thereShouldReturnNil)
s.Step(`^there should return string "([^"]*)"$`, feat.thereShouldReturnString)
s.Step(`^there should return string like "([^"]*)"$`, feat.thereShouldReturnStringLike)
s.Step(`^there should return symbol "([^"]*)"$`, feat.thereShouldReturnSymbol)
s.Step(`^there should return object$`, feat.thereShouldReturnObject)
s.Step(`^there should return class "([^"]*)"$`, feat.thereShouldReturnClass)
Expand Down
5 changes: 1 addition & 4 deletions kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ func (mrb *State) Inspect(obj Value) string {

func objectInspect(mrb *State, self Value) Value {
switch v := self.(type) {
case *Object:
name := mrb.ObjectInstanceVariableGet(v.Class(), _classname(mrb))
return name
case RClass:
name := mrb.ObjectInstanceVariableGet(v, _classname(mrb))
return name
Expand All @@ -24,7 +21,7 @@ func objectInspect(mrb *State, self Value) Value {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", v)
default:
return nil
return anyToString(mrb, self)
}
}

Expand Down
7 changes: 7 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mruby

import "fmt"

type RBasic interface {
Class() RClass
Flags() uint32
Expand Down Expand Up @@ -44,6 +46,11 @@ func (obj *Object) ivGet(sym Symbol) Value {
return obj.iv.Get(sym)
}

func anyToString(mrb *State, obj Value) Value {
className := mrb.ObjectClassName(obj)
return fmt.Sprintf("#<%s:%p>", className, obj)
}

func initObject(mrb *State) (err error) {
mrb.FalseClass, err = mrb.DefineClassId(_FalseClass(mrb), mrb.ObjectClass)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ func Test(v Value) bool {
return Bool(v)
}

func ObjectP(v Value) bool {
switch v.(type) {
case *Object:
return true
default:
return false
}
}

func ClassP(v Value) bool {
switch v.(type) {
case *Class:
Expand Down

0 comments on commit 42474d9

Please sign in to comment.