function-name.test.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. var jsc = require("jsverify");
  3. var refute = require("@sinonjs/referee-sinon").refute;
  4. var functionName = require("./function-name");
  5. describe("function-name", function() {
  6. it("should return empty string if func is falsy", function() {
  7. jsc.assertForall("falsy", function(fn) {
  8. return functionName(fn) === "";
  9. });
  10. });
  11. it("should use displayName by default", function() {
  12. jsc.assertForall("nestring", function(displayName) {
  13. var fn = { displayName: displayName };
  14. return functionName(fn) === fn.displayName;
  15. });
  16. });
  17. it("should use name if displayName is not available", function() {
  18. jsc.assertForall("nestring", function(name) {
  19. var fn = { name: name };
  20. return functionName(fn) === fn.name;
  21. });
  22. });
  23. it("should fallback to string parsing", function() {
  24. jsc.assertForall("nat", function(naturalNumber) {
  25. var name = "fn" + naturalNumber;
  26. var fn = {
  27. toString: function() {
  28. return "\nfunction " + name;
  29. }
  30. };
  31. return functionName(fn) === name;
  32. });
  33. });
  34. it("should not fail when a name cannot be found", function() {
  35. refute.exception(function() {
  36. var fn = {
  37. toString: function() {
  38. return "\nfunction (";
  39. }
  40. };
  41. functionName(fn);
  42. });
  43. });
  44. it("should not fail when toString is undefined", function() {
  45. refute.exception(function() {
  46. functionName(Object.create(null));
  47. });
  48. });
  49. it("should not fail when toString throws", function() {
  50. refute.exception(function() {
  51. var fn;
  52. try {
  53. // eslint-disable-next-line no-eval
  54. fn = eval("(function*() {})")().constructor;
  55. } catch (e) {
  56. // env doesn't support generators
  57. return;
  58. }
  59. functionName(fn);
  60. });
  61. });
  62. });