mirror of
https://github.com/robertkrimen/otto
synced 2025-09-28 18:45:22 +08:00
80 lines
2.6 KiB
Go
80 lines
2.6 KiB
Go
package otto
|
|
|
|
import (
|
|
"testing"
|
|
. "github.com/robertkrimen/terst"
|
|
)
|
|
|
|
func TestGlobal(t *testing.T) {
|
|
Terst(t)
|
|
|
|
Otto, test := runTestWithOtto()
|
|
runtime := Otto.runtime
|
|
|
|
{
|
|
//trueValue, falseValue := TrueValue(), FalseValue()
|
|
|
|
result := runtime.localGet("Object")._object().Call(UndefinedValue(), []Value{toValue(runtime.newObject())})
|
|
Is(result.IsObject(), true)
|
|
Is(result, "[object Object]")
|
|
Is(result._object().Prototype == runtime.Global.ObjectPrototype, true)
|
|
Is(result._object().Prototype == runtime.Global.Object.Get("prototype")._object(), true)
|
|
Is(runtime.newObject().Prototype == runtime.Global.Object.Get("prototype")._object(), true)
|
|
Is(result._object().Get("toString"), "[function]")
|
|
//Is(result.Object().CallMethod("hasOwnProperty", "hasOwnProperty"), falseValue)
|
|
//Is(result.Object().Get("toString").Object().Prototype.CallMethod("toString"), "[function]")
|
|
//Is(result.Object().Get("toString").Object().Get("toString").Object(), "[function]")
|
|
//Is(result.Object().Get("toString").Object().Get("toString"), "[function]")
|
|
//Is(runtime.localGet("Object").Object().CallMethod("isPrototypeOf", result), falseValue)
|
|
//Is(runtime.localGet("Object").Object().Get("prototype").Object().CallMethod("isPrototypeOf", result), trueValue)
|
|
//Is(runtime.localGet("Function").Object().CallMethod("isPrototypeOf", result), falseValue)
|
|
//Is(result.Object().CallMethod("propertyIsEnumerable", "isPrototypeOf"), falseValue)
|
|
//result.Object().WriteValue("xyzzy", toValue("Nothing happens."), false)
|
|
//Is(result.Object().CallMethod("propertyIsEnumerable", "xyzzy"), trueValue)
|
|
//Is(result.Object().Get("xyzzy"), "Nothing happens.")
|
|
|
|
abc := runtime.newBoolean(TrueValue())
|
|
Is(abc, "true")
|
|
|
|
def := runtime.localGet("Boolean")._object().Construct(UndefinedValue(), []Value{})
|
|
Is(def, "false")
|
|
}
|
|
|
|
test(`new Number().constructor == Number`, "true")
|
|
}
|
|
|
|
func TestGlobalLength(t *testing.T) {
|
|
Terst(t)
|
|
|
|
test := runTest()
|
|
|
|
test(`Object.length`, "1")
|
|
test(`Function.length`, "1")
|
|
test(`RegExp.length`, "2")
|
|
test(`Math.length`, "undefined")
|
|
}
|
|
|
|
func TestGlobalError(t *testing.T) {
|
|
Terst(t)
|
|
|
|
test := runTest()
|
|
|
|
test(`TypeError.length`, "1")
|
|
test(`TypeError()`, "TypeError")
|
|
test(`TypeError("Nothing happens.")`, "TypeError: Nothing happens.")
|
|
|
|
test(`URIError.length`, "1")
|
|
test(`URIError()`, "URIError")
|
|
test(`URIError("Nothing happens.")`, "URIError: Nothing happens.")
|
|
}
|
|
|
|
func TestGlobalReadOnly(t *testing.T) {
|
|
Terst(t)
|
|
|
|
test := runTest()
|
|
|
|
test(`Number.POSITIVE_INFINITY`, "Infinity")
|
|
test(`Number.POSITIVE_INFINITY = 1`, "1")
|
|
test(`Number.POSITIVE_INFINITY`, "Infinity")
|
|
}
|