cardinal-highlight-file-sync.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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('errornous code', function (t) {
  25. try {
  26. cardinal.highlightFileSync(fileWithErrors);
  27. } catch (e) {
  28. t.similar(e.message, /Unable to perform highlight. The code contained syntax errors.* Line 1: Unexpected token [(]/)
  29. t.end()
  30. }
  31. })
  32. test('non existing file', function (t) {
  33. try {
  34. cardinal.highlightFileSync('./not/existing');
  35. } catch (e) {
  36. t.similar(e.message, /ENOENT, .*not.existing/)
  37. t.end()
  38. }
  39. })