test.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. var assert = require('assert');
  3. var testit = require('testit');
  4. var isExpression = require('./');
  5. function passes(src, options) {
  6. testit(JSON.stringify(src, options), function () {
  7. options = options || {};
  8. assert(isExpression(src, options));
  9. });
  10. }
  11. testit('passes', function () {
  12. passes('myVar');
  13. passes('["an", "array", "\'s"].indexOf("index")');
  14. passes('\npublic');
  15. passes('abc // my comment', {lineComment: true});
  16. passes('() => a');
  17. passes('function (a = "default") {"use strict";}', {ecmaVersion: 6});
  18. });
  19. function error(src, line, col, options) {
  20. testit(JSON.stringify(src), function () {
  21. options = options || {};
  22. assert(!isExpression(src, options));
  23. options.throw = true;
  24. assert.throws(function () {
  25. isExpression(src, options);
  26. }, function (err) {
  27. assert.equal(err.loc.line, line);
  28. assert.equal(err.loc.column, col);
  29. assert(err.message);
  30. return true;
  31. });
  32. });
  33. }
  34. testit('fails', function () {
  35. error('', 1, 0);
  36. error('var', 1, 0);
  37. error('weird error', 1, 6);
  38. error('asdf}', 1, 4);
  39. error('\npublic', 2, 0, {strict: true});
  40. error('abc // my comment', 1, 4);
  41. error('() => a', 1, 1, {ecmaVersion: 5});
  42. error('function (a = "default") {"use strict";}', 1, 26);
  43. });