function-name.test.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 use displayName by default", function() {
  7. jsc.assertForall("nestring", function(displayName) {
  8. var fn = { displayName: displayName };
  9. return functionName(fn) === fn.displayName;
  10. });
  11. });
  12. it("should use name if displayName is not available", function() {
  13. jsc.assertForall("nestring", function(name) {
  14. var fn = { name: name };
  15. return functionName(fn) === fn.name;
  16. });
  17. });
  18. it("should fallback to string parsing", function() {
  19. jsc.assertForall("nat", function(naturalNumber) {
  20. var name = "fn" + naturalNumber;
  21. var fn = {
  22. toString: function() {
  23. return "\nfunction " + name;
  24. }
  25. };
  26. return functionName(fn) === name;
  27. });
  28. });
  29. it("should not fail when a name cannot be found", function() {
  30. refute.exception(function() {
  31. var fn = {
  32. toString: function() {
  33. return "\nfunction (";
  34. }
  35. };
  36. functionName(fn);
  37. });
  38. });
  39. });