redeyed-keywords.js 1.7 KB

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