js/src/jsapi-tests/README

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 === JSAPI Test Suite
michael@0 2
michael@0 3 The tests in this directory exercise the JSAPI.
michael@0 4
michael@0 5
michael@0 6 --- Building and running the tests
michael@0 7
michael@0 8 If you built JS, you already built the tests.
michael@0 9
michael@0 10 If you did `make check` in your JS objdir, you already ran them.
michael@0 11
michael@0 12 The tests are built by default when you build JS. All the tests are compiled
michael@0 13 into a single binary named jsapi-tests. They all run in a single process.
michael@0 14
michael@0 15 To run the tests in a debugger:
michael@0 16
michael@0 17 cd $OBJDIR/jsapi-tests
michael@0 18 gdb ./jsapi-tests
michael@0 19
michael@0 20
michael@0 21 --- Creating new tests
michael@0 22
michael@0 23 1. You can either add to an existing test*.cpp file or make a new one.
michael@0 24 Copy an existing test and replace the body with your test code.
michael@0 25 The test harness provides `cx`, `rt`, and `global` for your use.
michael@0 26
michael@0 27 2. If you made a new .cpp file, add it to the CPPSRCS list in Makefile.in.
michael@0 28
michael@0 29
michael@0 30 --- Writing test code
michael@0 31
michael@0 32 Here is a sample test:
michael@0 33
michael@0 34 #include "tests.h"
michael@0 35
michael@0 36 BEGIN_TEST(testIntString_bug515273)
michael@0 37 {
michael@0 38 jsval v;
michael@0 39 EVAL("'42';", &v);
michael@0 40
michael@0 41 JSString *str = JSVAL_TO_STRING(v);
michael@0 42 const char *bytes = JS_GetStringBytes(str);
michael@0 43 CHECK(strcmp(bytes, "42") == 0);
michael@0 44 return true;
michael@0 45 }
michael@0 46 END_TEST(testIntString_bug515273)
michael@0 47
michael@0 48 The BEGIN_TEST and END_TEST macros bracket each test. By convention, the test
michael@0 49 name is <testFilename>_<detail>. (The above test is in testIntString.cpp.)
michael@0 50
michael@0 51 The curly braces are required. This block is the body of a C++ member function
michael@0 52 that returns bool. The test harness calls this member function
michael@0 53 automatically. If the function returns true, the test passes. False, it fails.
michael@0 54
michael@0 55 JSAPI tests often need extra global C/C++ code: a JSClass, a getter or setter
michael@0 56 function, a resolve hook. Put these before the BEGIN_TEST macro.
michael@0 57
michael@0 58 The body of the test can use these member variables and macros, defined in
michael@0 59 tests.h:
michael@0 60
michael@0 61 JSRuntime *rt;
michael@0 62 JSContext *cx;
michael@0 63 JSObject *global;
michael@0 64
michael@0 65 The test framework creates these fresh for each test. The default
michael@0 66 environment has reasonable default settings, including
michael@0 67 JSOPTION_VAROBJFIX, JSOPTION_JIT, a global object of a class with
michael@0 68 JSCLASS_GLOBAL_FLAGS, and an error reporter that prints to stderr.
michael@0 69 See also "Custom test setup" below.
michael@0 70
michael@0 71 EXEC(const char *code);
michael@0 72
michael@0 73 Execute some JS code in global scope, using JS_EvaluateScript. Return
michael@0 74 false if that fails. (This means that if the code throws an uncaught JS
michael@0 75 exception, the test fails.)
michael@0 76
michael@0 77 EVAL(const char *code, jsval *vp);
michael@0 78
michael@0 79 Same as EXEC, but store the result value in *vp.
michael@0 80
michael@0 81 CHECK(bool cond);
michael@0 82
michael@0 83 If the condition is not true, print an error message and return false,
michael@0 84 failing the test.
michael@0 85
michael@0 86 CHECK_SAME(jsval a, jsval b);
michael@0 87
michael@0 88 If a and b are different values, print an error message and return
michael@0 89 false, failing the test.
michael@0 90
michael@0 91 This is like CHECK(sameValue(a, b)) but with a more detailed error
michael@0 92 message. See sameValue below.
michael@0 93
michael@0 94 bool knownFail;
michael@0 95
michael@0 96 Set this to true if your test is known to fail. The test runner will
michael@0 97 print a TEST-KNOWN-FAIL line rather than a TEST-UNEXPECTED-FAIL
michael@0 98 line. This way you can check in a test illustrating a bug ahead of the
michael@0 99 fix.
michael@0 100
michael@0 101 If your test actually crashes the process or triggers an assertion,
michael@0 102 this of course will not help, so you should add something like
michael@0 103
michael@0 104 knownFail = true; // see bug 123456
michael@0 105 return false; // the code below crashes!
michael@0 106
michael@0 107 as the first two lines of the test.
michael@0 108
michael@0 109 bool isNegativeZero(jsval v);
michael@0 110 bool isNaN(jsval v);
michael@0 111
michael@0 112 Self-explanatory.
michael@0 113
michael@0 114 bool sameValue(jsval v1, jsval v2);
michael@0 115
michael@0 116 True if v1 and v2 are the same value according to the ES5 SameValue()
michael@0 117 function, to wit:
michael@0 118
michael@0 119 SameValue(NaN, NaN) is true.
michael@0 120 SameValue(-0, 0) is false.
michael@0 121 Otherwise SameValue(a, b) iff a === b.
michael@0 122
michael@0 123
michael@0 124 --- Custom test setup
michael@0 125
michael@0 126 Before executing each test, the test framework calls the tests' init() member
michael@0 127 function, which populates the rt, cx, and global member variables.
michael@0 128
michael@0 129 A test can customize the test setup process by overloading virtual member
michael@0 130 functions, like this:
michael@0 131
michael@0 132 const JSClass globalClassWithResolve = { ... };
michael@0 133
michael@0 134 BEGIN_TEST(testGlobalResolveHook)
michael@0 135 {
michael@0 136 RootedValue v;
michael@0 137 EVAL("v", v.address());
michael@0 138 CHECK_SAME(v, JSVAL_VOID);
michael@0 139 return true;
michael@0 140 }
michael@0 141
michael@0 142 // Other class members can go here.
michael@0 143
michael@0 144 // This one overloads a base-class method.
michael@0 145 virtual JSClass *getGlobalJSClass() {
michael@0 146 return &globalClassWithResolve;
michael@0 147 }
michael@0 148 END_TEST(testGlobalResolveHook)
michael@0 149
michael@0 150 The overloadable member functions are:
michael@0 151
michael@0 152 virtual bool init();
michael@0 153 virtual void uninit();
michael@0 154 virtual JSRuntime * createRuntime();
michael@0 155 virtual JSContext * createContext();
michael@0 156 virtual JSClass * getGlobalClass();
michael@0 157 virtual JSObject * createGlobal();
michael@0 158

mercurial