cardinal-highlight-file-async.js 2.2 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. cardinal.highlightFile(file, { theme: customTheme }, function (err, highlighted) {
  16. t.equals(null, err, 'no error')
  17. 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')
  18. t.end()
  19. })
  20. })
  21. test('not supplying custom theme', function (t) {
  22. cardinal.highlightFile(file, function (err, highlighted) {
  23. t.equals(null, err, 'no error')
  24. 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')
  25. t.end()
  26. })
  27. })
  28. test('errornous code', function (t) {
  29. cardinal.highlightFile(fileWithErrors, function (err, highlighted) {
  30. t.similar(err.message, /Unable to perform highlight. The code contained syntax errors.* Line 1: Unexpected token [(]/)
  31. t.end()
  32. })
  33. })
  34. test('non existing file', function (t) {
  35. cardinal.highlightFile('./not/existing', function (err, highlighted) {
  36. t.similar(err.message, /ENOENT, .*not.existing/)
  37. t.end()
  38. })
  39. })