| 48 | |
| 49 | |
| 50 | def BuildTests(function, full_name, options): |
| 51 | assert function["type"] == "function" |
| 52 | global g_var_index |
| 53 | g_var_index = 0 |
| 54 | result = ["// AUTO-GENERATED BY tools/generate-builtins-tests.py.\n"] |
| 55 | result.append("// Function call test:") |
| 56 | length = function["length"] |
| 57 | TryCatch(result, "%s(%s);" % (full_name, GetVars(result, length))) |
| 58 | |
| 59 | if "prototype" in function: |
| 60 | proto = function["prototype"] |
| 61 | result.append("\n// Constructor test:") |
| 62 | TryCatch(result, |
| 63 | "var recv = new %s(%s);" % (full_name, GetVars(result, length)), |
| 64 | "var recv = new Object();") |
| 65 | |
| 66 | getters = [] |
| 67 | methods = [] |
| 68 | for prop in proto: |
| 69 | proto_property = proto[prop] |
| 70 | proto_property_type = proto_property["type"] |
| 71 | if proto_property_type == "getter": |
| 72 | getters.append(proto_property) |
| 73 | result.append("recv.__defineGetter__(\"%s\", " |
| 74 | "function() { return %s; });" % |
| 75 | (proto_property["name"], GetVars(result, 1))) |
| 76 | if proto_property_type == "number": |
| 77 | result.append("recv.__defineGetter__(\"%s\", " |
| 78 | "function() { return %s; });" % |
| 79 | (proto_property["name"], GetVars(result, 1))) |
| 80 | if proto_property_type == "function": |
| 81 | methods.append(proto_property) |
| 82 | if getters: |
| 83 | result.append("\n// Getter tests:") |
| 84 | for getter in getters: |
| 85 | result.append("print(recv.%s);" % getter["name"]) |
| 86 | if methods: |
| 87 | result.append("\n// Method tests:") |
| 88 | for method in methods: |
| 89 | args = GetVars(result, method["length"], ["recv"]) |
| 90 | call = "%s.prototype.%s.call(%s)" % (full_name, method["name"], args) |
| 91 | TryCatch(result, call) |
| 92 | |
| 93 | filename = os.path.join(options.outdir, "%s.js" % (full_name)) |
| 94 | with open(filename, "w") as f: |
| 95 | f.write("\n".join(result)) |
| 96 | f.write("\n") |
| 97 | |
| 98 | |
| 99 | def VisitObject(obj, path, options): |