cardinal-highlight-file-sync.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. /*jshint asi: true*/
  3. var test = require('tap').test
  4. , util = require('util')
  5. , fs = require('fs')
  6. , path = require('path')
  7. , customTheme = require('./fixtures/custom')
  8. , cardinal = require('..')
  9. function inspect (obj) {
  10. return console.log(util.inspect(obj, false, 5, false))
  11. }
  12. var file = path.join(__dirname, 'fixtures/foo.js')
  13. , fileWithErrors = path.join(__dirname, 'fixtures/foo-with-errors.js')
  14. test('supplying custom theme', function (t) {
  15. var highlighted = cardinal.highlightFileSync(file, { theme: customTheme });
  16. t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[96mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[96ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[96ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[31mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\n')
  17. t.end()
  18. })
  19. test('not supplying custom theme', function (t) {
  20. var highlighted = cardinal.highlightFileSync(file);
  21. t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[37mfoo\u001b[39m\u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\n')
  22. t.end()
  23. })
  24. test('syntactically invalid code', function (t) {
  25. var highlighted = cardinal.highlightFileSync(fileWithErrors);
  26. t.equals(highlighted, '\u001b[94mfunction\u001b[39m \u001b[90m(\u001b[39m\u001b[90m)\u001b[39m \u001b[33m{\u001b[39m \n \u001b[32mvar\u001b[39m \u001b[37ma\u001b[39m \u001b[93m=\u001b[39m \u001b[34m3\u001b[39m\u001b[90m;\u001b[39m \u001b[31mreturn\u001b[39m \u001b[37ma\u001b[39m \u001b[93m>\u001b[39m \u001b[34m2\u001b[39m \u001b[93m?\u001b[39m \u001b[91mtrue\u001b[39m \u001b[93m:\u001b[39m \u001b[91mfalse\u001b[39m\u001b[90m;\u001b[39m \n\u001b[33m}\u001b[39m\u001b[90m;\u001b[39m\n')
  27. t.end()
  28. })
  29. test('non existing file', function (t) {
  30. try {
  31. cardinal.highlightFileSync('./not/existing');
  32. } catch (e) {
  33. t.similar(e.message, /ENOENT. .*not.existing/)
  34. t.end()
  35. }
  36. })