cardinal-highlight-json.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. /*jshint asi: true*/
  3. var test = require('tap').test
  4. , util = require('util')
  5. , customTheme = require('./fixtures/custom')
  6. , cardinal = require('..')
  7. function inspect (obj) {
  8. return console.log(util.inspect(obj, false, 5, false))
  9. }
  10. var json = JSON.stringify({
  11. foo: 'bar',
  12. baz: 'quux',
  13. bam: null
  14. });
  15. test('supplying custom theme', function (t) {
  16. var highlighted = cardinal.highlight(json, { json: true, theme: customTheme });
  17. t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[92m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[92m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[92m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m')
  18. t.end();
  19. });
  20. test('not supplying custom theme', function (t) {
  21. var highlighted = cardinal.highlight(json, { json: true });
  22. t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[32m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[32m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m')
  23. t.end();
  24. });
  25. test('without json option', function (t) {
  26. try {
  27. cardinal.highlight(json);
  28. } catch (e) {
  29. t.similar(e.message, /Unable to perform highlight. The code contained syntax errors.* Line 1: Unexpected token /)
  30. t.end();
  31. }
  32. });