/documentation/example/literate.js

https://bitbucket.org/darrint/qc.js/ · JavaScript · 50 lines · 41 code · 5 blank · 4 comment · 5 complexity · 207df171f36e2f501f5517b995d82685 MD5 · raw file

  1. // A specialized listener which pokes results into a div with jquery.
  2. function LiterateListener(jqDest) {
  3. this.jqDest = jqDest;
  4. }
  5. LiterateListener.prototype.noteResult = function(result) {
  6. var status_string = result.status + ": " + result.name;
  7. this.log(status_string);
  8. this.logResult(result);
  9. if (result.status == "pass") {
  10. // do nothing.
  11. } else {
  12. this.jqDest.removeClass("green");
  13. this.jqDest.addClass("red");
  14. }
  15. if (result.status == "fail") {
  16. this.log("Failed case:");
  17. this.log(result.failedCase.toString());
  18. }
  19. };
  20. LiterateListener.prototype.done = function(result) {
  21. this.log('done.');
  22. };
  23. LiterateListener.prototype.log = function(text) {
  24. this.jqDest.append($('<span>' + text + '</span>'));
  25. this.jqDest.append($('<br/>'));
  26. };
  27. LiterateListener.prototype.logResult = function(result) {
  28. this.log("passes: " + result.stats.pass);
  29. this.log("invalid: " + result.stats.invalid);
  30. }
  31. // Given a jquery identifier, run the code in the element and display
  32. // results in an helpful manner near the code.
  33. function literateRun(id) {
  34. resetProps();
  35. eval($(id).text());
  36. var dest = $(id).after("<div>").next();
  37. dest.addClass("test-result");
  38. dest.addClass("green");
  39. runAllProps(new Config(100, 1000), new LiterateListener(dest));
  40. }
  41. function runAllLiterate() {
  42. $(".runme").each(function () {
  43. literateRun(this);
  44. });
  45. }