diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-03-23 04:45:56 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-03-23 04:45:56 +0800 |
commit | 3133372a6a81c91528afbde58e22b3f9df257d03 (patch) | |
tree | 16778611a22c9fd249a94c88660cb0c6d297ce9b /Godeps/_workspace/src/github.com/robertkrimen/otto/testing_test.go | |
parent | 59eab49cb849ca93b9608763f4842654e8044d0f (diff) | |
parent | 85acdadcfa99ea5c74907029cb63919cc0302c1a (diff) | |
download | go-tangerine-3133372a6a81c91528afbde58e22b3f9df257d03.tar go-tangerine-3133372a6a81c91528afbde58e22b3f9df257d03.tar.gz go-tangerine-3133372a6a81c91528afbde58e22b3f9df257d03.tar.bz2 go-tangerine-3133372a6a81c91528afbde58e22b3f9df257d03.tar.lz go-tangerine-3133372a6a81c91528afbde58e22b3f9df257d03.tar.xz go-tangerine-3133372a6a81c91528afbde58e22b3f9df257d03.tar.zst go-tangerine-3133372a6a81c91528afbde58e22b3f9df257d03.zip |
Merge pull request #536 from zsfelfoldi/develop
using robertkrimen/otto, godeps updated
Diffstat (limited to 'Godeps/_workspace/src/github.com/robertkrimen/otto/testing_test.go')
-rw-r--r-- | Godeps/_workspace/src/github.com/robertkrimen/otto/testing_test.go | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/robertkrimen/otto/testing_test.go b/Godeps/_workspace/src/github.com/robertkrimen/otto/testing_test.go new file mode 100644 index 000000000..bf820bc0c --- /dev/null +++ b/Godeps/_workspace/src/github.com/robertkrimen/otto/testing_test.go @@ -0,0 +1,135 @@ +package otto + +import ( + "./terst" + "errors" + "strings" + "testing" + "time" +) + +func tt(t *testing.T, arguments ...func()) { + halt := errors.New("A test was taking too long") + timer := time.AfterFunc(2*time.Second, func() { + panic(halt) + }) + defer func() { + timer.Stop() + }() + terst.Terst(t, arguments...) +} + +func is(arguments ...interface{}) bool { + var got, expect interface{} + + switch len(arguments) { + case 0, 1: + return terst.Is(arguments...) + case 2: + got, expect = arguments[0], arguments[1] + default: + got, expect = arguments[0], arguments[2] + } + + switch value := got.(type) { + case Value: + if value.value != nil { + got = value.value + } + case *Error: + if value != nil { + got = value.Error() + } + if expect == nil { + // FIXME This is weird + expect = "" + } + } + + if len(arguments) == 2 { + arguments[0] = got + arguments[1] = expect + } else { + arguments[0] = got + arguments[2] = expect + } + + return terst.Is(arguments...) +} + +func test(arguments ...interface{}) (func(string, ...interface{}) Value, *_tester) { + tester := newTester() + if len(arguments) > 0 { + tester.test(arguments[0].(string)) + } + return tester.test, tester +} + +type _tester struct { + vm *Otto +} + +func newTester() *_tester { + return &_tester{ + vm: New(), + } +} + +func (self *_tester) Get(name string) (Value, error) { + return self.vm.Get(name) +} + +func (self *_tester) Set(name string, value interface{}) Value { + err := self.vm.Set(name, value) + is(err, nil) + if err != nil { + terst.Caller().T().FailNow() + } + return self.vm.getValue(name) +} + +func (self *_tester) Run(src interface{}) (Value, error) { + return self.vm.Run(src) +} + +func (self *_tester) test(name string, expect ...interface{}) Value { + vm := self.vm + raise := false + defer func() { + if caught := recover(); caught != nil { + if exception, ok := caught.(*_exception); ok { + caught = exception.eject() + } + if raise { + if len(expect) > 0 { + is(caught, expect[0]) + } + } else { + dbg("Panic, caught:", caught) + panic(caught) + } + } + }() + var value Value + var err error + if isIdentifier(name) { + value = vm.getValue(name) + } else { + source := name + index := strings.Index(source, "raise:") + if index == 0 { + raise = true + source = source[6:] + source = strings.TrimLeft(source, " ") + } + value, err = vm.runtime.cmpl_run(source) + if err != nil { + panic(err) + } + } + value = value.resolve() + if len(expect) > 0 { + is(value, expect[0]) + } + return value +} |