Esempio n. 1
0
func BenchmarkClone_(b *testing.B) {
	otto := New()
	otto.Run(underscore.Source())
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		otto.clone()
	}
}
Esempio n. 2
0
func underscoreTest() func(string, ...interface{}) Value {
	cache := &_underscoreTest
	if cache.Otto == nil {
		Otto, test := runTestWithOtto()
		cache.Otto, cache.test = Otto, test
		Otto.Run(underscore.Source())
		Otto.Set("assert", func(call FunctionCall) Value {
			if !toBoolean(call.Argument(0)) {
				message := "Assertion failed"
				if len(call.ArgumentList) > 1 {
					message = toString(call.ArgumentList[1])
				}
				Fail(message)
				return FalseValue()
			}
			return TrueValue()
		})

		Otto.Run(`

            var templateSettings;

            function _setup() {
                templateSettings = _.clone(_.templateSettings);
            }

            function _teardown() {
                _.templateSettings = templateSettings;
            }

            function module() {
                /* Nothing happens. */
            }
        
            function equals(a, b, emit) {
                assert(a == b, emit + ", <" + a + "> != <" + b + ">");
            }
            var equal = equals;

            function notStrictEqual(a, b, emit) {
                assert(a !== b, emit);
            }

            function strictEqual(a, b, emit) {
                assert(a === b, emit);
            }

            function ok(a, emit) {
                assert(a, emit);
            }

            function raises(fn, want, emit) {
                var have, _ok = false;
                if (typeof want === "string") {
                    emit = want;
                    want = null;
                }
                
                try {
                    fn();
                } catch(tmp) {
                    have = tmp;
                }
                
                if (have) {
                    if (!want) {
                        _ok = true;
                    }
                    else if (want instanceof RegExp) {
                        _ok = want.test(have);
                    }
                    else if (have instanceof want) {
                        _ok = true
                    }
                    else if (want.call({}, have) === true) {
                        _ok = true;
                    }
                }
                
                ok(_ok, emit);
            }

            function test(name){
                _setup()
                try {
                    templateSettings = _.clone(_.templateSettings);
                    if (arguments.length == 3) {
                        count = 0
                        for (count = 0; count < arguments[1]; count++) {
                            arguments[2]()
                        }
                    } else {
                        // For now.
                        arguments[1]()
                    }
                }
                catch (tmp) {
                }
                _teardown()
            }

            function deepEqual(a, b, emit) {
                // Also, for now.
                assert(_.isEqual(a, b), emit)
            }
        `)
	}
	return cache.test
}
Esempio n. 3
0
func BenchmarkNew_(b *testing.B) {
	for i := 0; i < b.N; i++ {
		otto := New()
		otto.Run(underscore.Source())
	}
}
Esempio n. 4
0
func (self *_tester) underscore() {
	vm := self.vm
	_, err := vm.Run(underscore.Source())
	if err != nil {
		panic(err)
	}

	vm.Set("assert", func(call FunctionCall) Value {
		if !call.Argument(0).bool() {
			message := "Assertion failed"
			if len(call.ArgumentList) > 1 {
				message = call.ArgumentList[1].string()
			}
			t := terst.Caller().T()
			is(message, nil)
			t.Fail()
			return falseValue
		}
		return trueValue
	})

	vm.Run(`
        var templateSettings;

        function _setup() {
            templateSettings = _.clone(_.templateSettings);
        }

        function _teardown() {
            _.templateSettings = templateSettings;
        }

        function module() {
            /* Nothing happens. */
        }
    
        function equals(a, b, emit) {
            assert(a == b, emit + ", <" + a + "> != <" + b + ">");
        }
        var equal = equals;

        function notStrictEqual(a, b, emit) {
            assert(a !== b, emit);
        }

        function strictEqual(a, b, emit) {
            assert(a === b, emit);
        }

        function ok(a, emit) {
            assert(a, emit);
        }

        function raises(fn, want, emit) {
            var have, _ok = false;
            if (typeof want === "string") {
                emit = want;
                want = null;
            }
            
            try {
                fn();
            } catch(tmp) {
                have = tmp;
            }
            
            if (have) {
                if (!want) {
                    _ok = true;
                }
                else if (want instanceof RegExp) {
                    _ok = want.test(have);
                }
                else if (have instanceof want) {
                    _ok = true
                }
                else if (want.call({}, have) === true) {
                    _ok = true;
                }
            }
            
            ok(_ok, emit);
        }

        function test(name){
            _setup()
            try {
                templateSettings = _.clone(_.templateSettings);
                if (arguments.length == 3) {
                    count = 0
                    for (count = 0; count < arguments[1]; count++) {
                        arguments[2]()
                    }
                } else {
                    // For now.
                    arguments[1]()
                }
            }
            finally {
                _teardown()
            }
        }

        function deepEqual(a, b, emit) {
            // Also, for now.
            assert(_.isEqual(a, b), emit)
        }
    `)
}