1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00
otto/global_test.go
Robert Krimen 3c93384f5c Cleanup of stash, property, and object
Use octal to designate write/enumerate/configure (experimental)
Move extensibility responsibility into the stash
Rename propertyStash => objectStash (be congruent with arrayStash, etc.)
Get rid of a bunch of useless methods
Privatize everything ([A-Z] => [a-z_])
gofmt
2012-10-26 15:47:19 -07:00

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")
}