svg2png_test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*!
  2. * grunt-svg2png
  3. * https://github.com/dbushell/grunt-svg2png
  4. *
  5. * Copyright (c) 2013 David Bushell
  6. * Licensed under The MIT License (MIT)
  7. */
  8. 'use strict';
  9. var fs = require('fs'),
  10. grunt = require('grunt'),
  11. pngjs = require('pngjs');
  12. exports.svg2png = {
  13. test1: function(test)
  14. {
  15. var all = fs.readdirSync('test/png'),
  16. png = [];
  17. if (all instanceof Array) {
  18. all.forEach(function(file) {
  19. if (/\.png$/.test(file)) {
  20. png.push(file);
  21. }
  22. });
  23. }
  24. test.ok(png instanceof Array && png.length >= 0, 'PNG files should exist in test directory');
  25. test.done();
  26. },
  27. test2: function(test)
  28. {
  29. fs.stat('test/png/grunt-logo.png', function(err, stats)
  30. {
  31. test.ok(err === null && stats.isFile(), 'PNG "grunt-logo.png" should exist');
  32. test.done();
  33. });
  34. },
  35. test3: function(test)
  36. {
  37. fs.stat('test/png/orange-square.png', function(err, stats)
  38. {
  39. test.ok(err === null && stats.isFile(), 'PNG "orange-square.png" should exist');
  40. test.done();
  41. });
  42. },
  43. test4: function(test)
  44. {
  45. fs.stat('test/png/grunt-logo.png', function(err, stats)
  46. {
  47. test.ok(err === null && stats.size > 44000 && stats.size < 46000, 'PNG "grunt-logo.png" should be ~45kb in size');
  48. test.done();
  49. });
  50. },
  51. test5: function(test)
  52. {
  53. var png = new pngjs.PNG({ filterType: 4 }),
  54. stream = fs.createReadStream('test/png/orange-square.png').pipe(png);
  55. stream.on('parsed', function()
  56. {
  57. test.ok(this.width === 1000 && this.height === 1000, 'PNG "orange-square.png" should be 1000x1000px in resolution');
  58. test.ok(this.data[0] === 251 && this.data[1] === 169 && this.data[2] === 25, 'North-west pixel in PNG "orange-square.png" should be #fba919');
  59. test.ok(this.data[2000000] === 251 && this.data[2000001] === 169 && this.data[2000002] === 25, 'Centre pixel in PNG "orange-square.png" should be #fba919');
  60. test.done();
  61. });
  62. stream.on('error', function(err)
  63. {
  64. test.ok(false, 'File should be readable');
  65. test.done();
  66. });
  67. }
  68. };