~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/translator/js/test/test_jseval.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import division
 
2
import py
 
3
 
 
4
from pypy.translator.js.test.runtest import compile_function
 
5
from pypy.rpython.lltypesystem import lltype 
 
6
from pypy.rlib.rjs import jseval
 
7
from pypy.translator.js import conftest
 
8
 
 
9
def jsnative(cmd):
 
10
    def do():
 
11
        return jseval(cmd)
 
12
    return do
 
13
 
 
14
getDate = jsnative("new Date().getDate()")
 
15
getTime = jsnative("Math.floor(new Date().getTime())")
 
16
 
 
17
#
 
18
def test_jseval1():
 
19
    def jseval1(s):
 
20
        return jseval(s)
 
21
 
 
22
    jseval1_fn = compile_function(jseval1, [str])
 
23
    e = "4+7"
 
24
    assert jseval1_fn(e) == eval(e)
 
25
 
 
26
def test_jsnative1():
 
27
    from time import localtime
 
28
 
 
29
    def jsnative1():
 
30
        getTime()
 
31
        return getDate()
 
32
 
 
33
    jsnative1_fn = compile_function(jsnative1, [])
 
34
    assert jsnative1_fn() == localtime()[2]
 
35
 
 
36
callbacks = []
 
37
#n_times_called = lltype.malloc(lltype.GcArray(lltype.Signed), 1) 
 
38
n_times_called = [0]
 
39
 
 
40
def callback_function():
 
41
    n_times_called[0] += 1
 
42
    jseval("document.title=" + str(n_times_called[0]))
 
43
    jseval("setTimeout('callback_function()', 100)")
 
44
 
 
45
def test_register_callback():
 
46
    py.test.skip("Hangs")
 
47
    if not conftest.option.browser:
 
48
        py.test.skip("works only in a browser (use py.test --browser)")
 
49
 
 
50
    def register_callback():
 
51
        callback_function() #..start timer
 
52
        start_time = current_time = int(getTime())
 
53
        while current_time - start_time < 1000:
 
54
            current_time = int(getTime())
 
55
        return n_times_called[0]
 
56
 
 
57
    register_callback_fn = compile_function(register_callback, [])
 
58
    result = register_callback_fn()
 
59
    assert result == 1