Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Result class #675

Merged
merged 3 commits into from
May 28, 2018
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ profile.out
.DS_Store

.tmp_benchmarks
.tmp_benchmark_comparison
.tmp_benchmark_comparison
.vscode
29 changes: 29 additions & 0 deletions lib/result.gb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Result
def initialize(name=nil, value=nil)
@name = name
@value = value
@empty = name == nil
@used = false
end

def method_missing(name, *args)
if name == @name && @used == false
if block_given?
yield @value
end
@used = true
end

self
end

def or
if @used || @empty
return
end

if block_given?
yield @name, @value
end
end
end
83 changes: 83 additions & 0 deletions specs/result_spec.gb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require "spec"
require "result"

Spec.describe Result do
describe "empty Result" do
describe "#or" do
o = Result.new

it "never calls a given block" do
is_called = false
o.or do |name, val|
is_called = true
end
expect(is_called).to eq(false)
end
end
end

describe "filled Result" do
o = Result.new(:hello, "world")

describe "#wrong_key" do
it "block will not be called" do
called = false
o.wrong_key do |value|
called = true
end
expect(called).to eq(false)
end
end

describe "#or" do
n = ""
v = nil
o.or do |name, val|
n = name
v = val
end
it "always call block" do
expect(n).to eq(:hello)
expect(v).to eq("world")
end
end
end

describe "key other than or" do
o = Result.new(:hello, "world")

describe "#wrong_key" do
it "block will not be called" do
called = false
o.wrong_key do |value|
called = true
end
expect(called).to eq(false)
end
end

describe "#hello" do
it "will be called with value" do
called = false
value = nil
o.hello do |v|
called = true
value = v
end
expect(called).to eq(true)
expect(value).to eq("world")
end
end

describe "#or" do
it "won't be called" do
called = false
o.hello do |v|
called = true
end
expect(called).to eq(false)
end
end
end

end
6 changes: 4 additions & 2 deletions vm/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,8 +1204,10 @@ func builtinClassCommonInstanceMethods() []*BuiltinMethodObject {
loaders, ok := externalClasses[libName]
externalClassLock.Unlock()
if !ok {
return t.vm.InitErrorObject(errors.InternalError, sourceLine, "Can't require \"%s\"", libName)

err := t.execGobyLib(libName + ".gb")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👍

if err != nil {
return t.vm.InitErrorObject(errors.InternalError, sourceLine, "Can't require \"%s\"", libName)
}
}
initFunc = func(v *VM) {
for _, l := range loaders {
Expand Down