Name: js-handler/node_modules/nodeunit/node_modules/tap/example/test/test-example.js
| 1: | var tap = require("tap") |
| 2: | , test = tap.test |
| 3: | , plan = tap.plan |
| 4: | , math |
| 5: | |
| 6: | test("load sut", function (t) { |
| 7: | math = require("../lib/math") |
| 8: | t.ok(math, "object loaded") |
| 9: | t.end() |
| 10: | }) |
| 11: | |
| 12: | test("validate constants", function (t) { |
| 13: | t.equal(math.LN10, 2.302585092994046, "ln 10") |
| 14: | t.equal(math.PI, 3.141592653589793, "pi") |
| 15: | t.equal(math.E, 2.718281828459045, "e") |
| 16: | t.equal(math.LOG10E, 0.4342944819032518, "log 10 e") |
| 17: | t.equal(math.SQRT2, 1.4142135623730951, "sqrt 2") |
| 18: | t.equal(math.SQRT1_2, 0.7071067811865476, "sqrt 1/2") |
| 19: | t.equal(math.LN2, 0.6931471805599453, "ln2") |
| 20: | t.end() |
| 21: | }) |
| 22: | |
| 23: | test("using this", function (t) { |
| 24: | // this also works. |
| 25: | this.equal(t, this, "call in scope of test obj") |
| 26: | this.end() |
| 27: | }) |
| 28: | |
| 29: | // test setTimeout, just a trivial example. |
| 30: | test("setTimeout", function (t) { |
| 31: | var start = Date.now() |
| 32: | setTimeout(function () { |
| 33: | t.ok(Date.now() >= start + 50, "timeout fired after delay") |
| 34: | t.end() |
| 35: | }, 50) |
| 36: | }) |
| 37: | |
| 38: | // another way to do the same, using a plan. |
| 39: | // this is more robust, but annoying when you have a long list |
| 40: | // of tests for something. For async stuff, it's generally better, |
| 41: | // since there's a higher risk of the control flowing off to lala land. |
| 42: | test("setTimeout planned", function (t) { |
| 43: | t.plan(1) |
| 44: | var start = Date.now() |
| 45: | setTimeout(function () { |
| 46: | t.ok(Date.now() >= start + 50, "timeout fired after delay") |
| 47: | }, 50) |
| 48: | }) |
| 49: | |
| 50: | // plans also are good for cases where things may fire in a non-deterministic |
| 51: | // order, since it won't be as obvious when everything is done. |
| 52: | test("setTimeout parallel", function (t) { |
| 53: | t.plan(2) |
| 54: | var start = Date.now() |
| 55: | setTimeout(function A () { |
| 56: | t.ok(Date.now() >= start + 50, "timeout A fired after delay") |
| 57: | }, 50) |
| 58: | setTimeout(function B () { |
| 59: | t.ok(Date.now() >= start + 50, "timeout B fired after delay") |
| 60: | }, 50) |
| 61: | }) |
| 62: | |
| 63: | // something slightly less hello worldy |
| 64: | test("async test", function (t) { |
| 65: | t.plan(4) |
| 66: | var fs = require("fs") |
| 67: | t.ok(fs, "fs library should load") |
| 68: | var rs = fs.createReadStream(__filename) |
| 69: | t.ok(rs, "read stream should start fine.") |
| 70: | rs.on("open", function (fd) { |
| 71: | t.type(fd, "number", "file descriptor should be a number") |
| 72: | t.equal(fd, rs.fd, "fd should match stream fd") |
| 73: | }) |
| 74: | }) |
| 75: | |
| 76: | // you can bail out of the entire everything if something is just |
| 77: | // Not Right (db not installed, etc.) |
| 78: | test("tarp", function (parent) { |
| 79: | if (7 === 5) { |
| 80: | parent.bailout("math is broken") |
| 81: | } |
| 82: | // bailout bubbles up a bit like "error" events |
| 83: | // if unhandled, then the parent will bail, as well. |
| 84: | parent.test("child bailouts", function (child) { |
| 85: | child.on("bailout", function (s) { |
| 86: | parent.fail("children shouldn't bail.") |
| 87: | }) |
| 88: | child.bailout("try to bail out, but instead just fail a test") |
| 89: | }) |
| 90: | |
| 91: | parent.test("child bailout 2", function (child) { |
| 92: | child.bailout("this one will bail out") |
| 93: | }) |
| 94: | }) |
| 95: | |
| 96: | // tests marked "todo" can fail without counting against the overall score |
| 97: | // never ever ever write tests to "verify" incorrect behavior! |
| 98: | test("unfinished test", function (t) { |
| 99: | t.equal(math.cos(math.PI), -1, "cos(PI)") |
| 100: | t.equal(math.sin(math.PI), 0, "sin(PI)") |
| 101: | t.equal(math.face, "your face", "math.face should be your face # TODO") |
| 102: | t.end() |
| 103: | }) |
| 104: | |
| 105: | // tests can have children. |
| 106: | test("http server", function (t) { |
| 107: | // one test plus 4 children. |
| 108: | t.plan(5) |
| 109: | |
| 110: | var http = require("http") |
| 111: | , PORT = 12346 |
| 112: | |
| 113: | t.ok(http, "http module should load") |
| 114: | var server |
| 115: | |
| 116: | t.test("set up server", function (t) { |
| 117: | t.plan(2) |
| 118: | server = http.createServer(function (req, res) { |
| 119: | t.comment("Request: "+req.url) |
| 120: | res.writeHead(200, {}) |
| 121: | res.end(req.method + " " + req.url) |
| 122: | }) |
| 123: | t.ok(server, "createServer should create a server") |
| 124: | server.listen(PORT, t.cb("listen should fire callback")) |
| 125: | }) |
| 126: | |
| 127: | // set the "parallel" flag on this one. |
| 128: | // That signals the harness to proceed immediately to the next test, |
| 129: | // and run them in parallel. |
| 130: | // Default behavior is to wait for each test to complete before proceeding |
| 131: | // to the next one. |
| 132: | // The first not-parallel test encountered will cause it to wait for that |
| 133: | // test, as well as all the parallel tests before it. |
| 134: | // A, B', C', D', E (where ' means "parallel") |
| 135: | // Runs A, and then B, C, and D in parallel, and then E. |
| 136: | t.test("testing POST", {parallel: true}, function (t) { |
| 137: | t.plan(1) |
| 138: | http.request("POST", { method: "POST" |
| 139: | , host: "localhost" |
| 140: | , path: "/foo" |
| 141: | , port: PORT }).on("response", function (res) { |
| 142: | t.bufferStream(res, function (s) { t.equal(s, "POST /foo") }) |
| 143: | }).end() |
| 144: | }) |
| 145: | |
| 146: | t.test("testing GET", {parallel: true}, function (t) { |
| 147: | t.plan(1) |
| 148: | http.request("POST", { method: "GET" |
| 149: | , host: "localhost" |
| 150: | , path: "/foo" |
| 151: | , port: PORT }).on("response", function (res) { |
| 152: | t.bufferStream(res, function (s) { t.equal(s, "GET /foo") }) |
| 153: | }).end() |
| 154: | }) |
| 155: | |
| 156: | // wrap in a test so that if this throws, it'll log as a failed test. |
| 157: | t.test("teardown", function (t) { |
| 158: | server.close() |
| 159: | t.end() |
| 160: | }) |
| 161: | }) |
| 162: | |
| 163: | // yo dawg! |
| 164: | test("meta-tests", function (t) { |
| 165: | t.plan(5) |
| 166: | |
| 167: | // t.fails() wraps a child test and succeeds if it fails. |
| 168: | t.fails(t.test("this should fail", function (t) { |
| 169: | t.ok(false, "assert false") |
| 170: | t.end() |
| 171: | })) |
| 172: | |
| 173: | // t.timesOut() wraps a child test and succeeds if it times out. |
| 174: | // if t.end() is called, or if a plan is completed, then it fails. |
| 175: | // set the timeout really low so that it will not take forever. |
| 176: | t.timesOut(t.test("this should timeout", { timeout: 1 }, function (t) { |
| 177: | t.ok(true, "assert true") |
| 178: | // t.end() never called. |
| 179: | })) |
| 180: | |
| 181: | // t.incomplete() wraps a child test and succeeds if it ends before |
| 182: | // the plan is finished. |
| 183: | t.incomplete(t.test("this should be incomplete", function (t) { |
| 184: | t.plan(100) |
| 185: | t.ok(true, "assert true") |
| 186: | // calling end prematurely. |
| 187: | t.end() |
| 188: | })) |
| 189: | |
| 190: | // t.bailsOut() wraps a child test and succeeds if it calls bailout() |
| 191: | t.bailsOut(t.test("this should bailout", function (t) { |
| 192: | t.bailout("oh noes, bailing out!") |
| 193: | })) |
| 194: | |
| 195: | // low-level analysis of subtests |
| 196: | t.test("verifying test success/failure expectations", function (t) { |
| 197: | t.once("end", function () { |
| 198: | var res = t.results |
| 199: | , is = t.equal |
| 200: | // hijack! |
| 201: | t.clear() |
| 202: | is(res.ok, false, "ok") |
| 203: | |
| 204: | is(res.bailedOut, false, "bailed out") |
| 205: | |
| 206: | is(res.skip, 2, "skips") |
| 207: | is(res.skipPass, 1, "skip that passed") |
| 208: | is(res.skipFail, 1, "skip that failed") |
| 209: | |
| 210: | is(res.todo, 2, "todos") |
| 211: | is(res.todoPass, 1, "todo that passed") |
| 212: | is(res.todoFail, 1, "todo that failed") |
| 213: | |
| 214: | is(res.failTotal, 3, "failures total") |
| 215: | is(res.fail, 1, "relevant failure") |
| 216: | |
| 217: | is(res.passTotal, 3, "passes total") |
| 218: | is(res.pass, 1, "relevant pass") |
| 219: | |
| 220: | is(res.testsTotal, 6, "total tests") |
| 221: | is(res.tests, 2, "should be 2 relevant tests") |
| 222: | |
| 223: | t.end() |
| 224: | }) |
| 225: | |
| 226: | // run the metatest. |
| 227: | // *this* is the actual SUT in this case. |
| 228: | t.ok(false, "failing todo #todo") |
| 229: | // can also set #todo or #skip explicitly |
| 230: | t.ok(true, "succeeding todo", {todo: true}) |
| 231: | t.ok(false, "failing skip #skip", {skip: true}) |
| 232: | t.ok(true, "suceeding skip #skip") |
| 233: | t.ok(false, "failing test") |
| 234: | t.ok(true, "succeeding test") |
| 235: | t.end() |
| 236: | }) |
| 237: | }) |
