redeyed-keywords.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. /*jshint asi: true*/
  3. var test = require('tap').test
  4. , util = require('util')
  5. , redeyed = require('..')
  6. function inspect (obj) {
  7. return util.inspect(obj, false, 5, true)
  8. }
  9. test('adding custom asserts ... ', function (t) {
  10. t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
  11. var optsi = inspect(opts);
  12. var result = redeyed(code, opts).code
  13. this.equals( result
  14. , expected
  15. , util.format('%s: %s => %s', optsi, inspect(code), inspect(expected))
  16. )
  17. return this;
  18. }
  19. t.end()
  20. })
  21. test('types', function (t) {
  22. t.test('\n# Keyword', function (t) {
  23. var keyconfig = { 'Keyword': { _default: '$:%' } };
  24. t.assertSurrounds('import foo from \'foo\';', keyconfig, '$import% foo from \'foo\';')
  25. t.assertSurrounds('export default foo;', keyconfig, '$export% $default% foo;')
  26. t.assertSurrounds('if(foo) { let bar = 1;}', keyconfig, '$if%(foo) { $let% bar = 1;}')
  27. t.assertSurrounds('const x = "foo";', keyconfig, '$const% x = "foo";')
  28. t.assertSurrounds('"use strict";(function* () { yield *v })', keyconfig, '"use strict";($function%* () { $yield% *v })')
  29. t.assertSurrounds('"use strict"; (class A { static constructor() { super() }})', keyconfig
  30. ,'"use strict"; ($class% A { $static% constructor() { $super%() }})')
  31. t.assertSurrounds('class Foo { constructor(name){this.name = name;}}', keyconfig
  32. , '$class% Foo { constructor(name){$this%.name = name;}}')
  33. t.assertSurrounds('class Foo extends Bar { constructor(name,value){super(value);this.name = name;}}', keyconfig
  34. , '$class% Foo $extends% Bar { constructor(name,value){$super%(value);$this%.name = name;}}')
  35. t.end()
  36. })
  37. t.end()
  38. })