示例#1
0
func valueSliceToString(input []otto.Value) string {
	values := make([]string, len(input))
	for i, v := range input {
		values[i] = fmt.Sprintf("%v", gohan_otto.ConvertOttoToGo(v))
	}
	return "[" + strings.Join(values, ", ") + "]"
}
示例#2
0
func (env *Environment) loadSchemas() error {
	schemaValue, err := env.VM.Get(schemasVar)
	if err != nil {
		return fmt.Errorf("%s string array not specified", schemasVar)
	}
	schemaFilenamesRaw := gohan_otto.ConvertOttoToGo(schemaValue)
	schemaFilenames, ok := schemaFilenamesRaw.([]interface{})
	if !ok {
		return fmt.Errorf("Bad type of %s - expected an array of strings", schemasVar)
	}

	manager := schema.GetManager()
	for _, schemaRaw := range schemaFilenames {
		schema, ok := schemaRaw.(string)
		if !ok {
			return fmt.Errorf("Bad type of schema - expected a string")
		}
		err = manager.LoadSchemaFromFile(schema)
		if err != nil {
			return err
		}
	}
	environmentManager := extension.GetManager()
	for schemaID := range manager.Schemas() {
		environmentManager.RegisterEnvironment(schemaID, env)
	}

	pathValue, err := env.VM.Get(pathVar)
	if err != nil || !pathValue.IsString() {
		return fmt.Errorf("%s string not specified", pathVar)
	}
	pathString, _ := pathValue.ToString()

	return env.LoadExtensionsForPath(manager.Extensions, pathString)
}
示例#3
0
func valueSliceToString(input []otto.Value) string {
	output := "["
	for _, v := range input {
		output += fmt.Sprintf("%v, ", gohan_otto.ConvertOttoToGo(v))
	}
	output = output[:len(output)-2] + "]"
	return output
}
示例#4
0
func (env *Environment) addTestingAPI() {
	builtins := map[string]interface{}{
		"Fail": func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 0 {
				panic(fmt.Errorf("Fail!"))
			}

			if !call.ArgumentList[0].IsString() {
				panic(fmt.Errorf("Invalid call to 'Fail': format string expected first"))
			}

			format, _ := call.ArgumentList[0].ToString()
			args := []interface{}{}
			for _, value := range call.ArgumentList[1:] {
				args = append(args, gohan_otto.ConvertOttoToGo(value))
			}

			panic(fmt.Errorf(format, args...))
		},
		"MockTransaction": func(call otto.FunctionCall) otto.Value {
			newTransaction := false
			if len(call.ArgumentList) > 1 {
				panic("Wrong number of arguments in MockTransaction call.")
			} else if len(call.ArgumentList) == 1 {
				rawNewTransaction, _ := call.Argument(0).Export()
				newTransaction = rawNewTransaction.(bool)
			}
			transactionValue, _ := call.Otto.ToValue(env.getTransaction(newTransaction))
			return transactionValue
		},
		"CommitMockTransaction": func(call otto.FunctionCall) otto.Value {
			tx := env.getTransaction(false)
			tx.Commit()
			tx.Close()
			return otto.Value{}
		},
		"MockPolicy": func(call otto.FunctionCall) otto.Value {
			policyValue, _ := call.Otto.ToValue(schema.NewEmptyPolicy())
			return policyValue
		},
		"MockAuthorization": func(call otto.FunctionCall) otto.Value {
			authorizationValue, _ := call.Otto.ToValue(schema.NewAuthorization("", "", "", []string{}, []*schema.Catalog{}))
			return authorizationValue
		},
	}
	for name, object := range builtins {
		env.VM.Set(name, object)
	}
	// NOTE: There is no way to return error back to Otto after calling a Go
	// function, so the following function has to be written in pure JavaScript.
	env.VM.Otto.Run(`function GohanTrigger(event, context) { gohan_handle_event(event, context); }`)
	env.mockFunction("gohan_http")
	env.mockFunction("gohan_raw_http")
	env.mockFunction("gohan_db_transaction")
	env.mockFunction("gohan_config")
}
示例#5
0
文件: otto_test.go 项目: nati/gohan
			env *otto.Environment
		)
		channel := make(chan string)
		Context("Given environment", func() {
			BeforeEach(func() {
				env = otto.NewEnvironment(testDB, &middleware.FakeIdentity{})
				env.SetUp()
				vm := env.VM
				builtins := map[string]interface{}{
					"test_consume": func(call ottopkg.FunctionCall) ottopkg.Value {
						result := <-channel
						ottoResult, _ := vm.ToValue(result)
						return ottoResult
					},
					"test_produce": func(call ottopkg.FunctionCall) ottopkg.Value {
						ottoProduct := otto.ConvertOttoToGo(call.Argument(0))
						product := otto.ConvertOttoToGo(ottoProduct).(string)
						channel <- product
						return ottopkg.NullValue()
					},
				}
				for name, object := range builtins {
					vm.Set(name, object)
				}

				Expect(env.Load("<race_test>", `
				var produce = function() {
					for (var i = 0; i < 10; i++) {
						console.log("producing:", i);
						test_produce(i.toString());
					}