helper.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @param {string} actual
  3. * @param {string} expected
  4. * @param {string} [message]
  5. * TODO: http://github.com/jquery/qunit/issues#issue/39
  6. */
  7. function equalOwnProperties(actual, expected, message) {
  8. var actualDummy = cloneOwnProperties(actual);
  9. var expectedDummy = cloneOwnProperties(expected);
  10. deepEqual(actualDummy, expectedDummy, message);
  11. }
  12. /**
  13. * Make a deep copy of an object
  14. * @param {Object|Array} object
  15. * @return {Object|Array}
  16. */
  17. function cloneOwnProperties(object) {
  18. var result = {};
  19. for (var key in object) {
  20. if (key.charAt(0) == "_" || !object.hasOwnProperty(key)) {
  21. continue;
  22. }
  23. if (typeof object[key] == "object") {
  24. result[key] = cloneOwnProperties(object[key]);
  25. } else {
  26. result[key] = object[key];
  27. }
  28. }
  29. return result;
  30. }
  31. /**
  32. * @param {Object|Array} actual
  33. * @param {Object|Array} expected
  34. * @param {string} message
  35. */
  36. function hasOwnProperties(actual, expected, message){
  37. var diff = subsetOfOwnProperties(actual, expected);
  38. if (diff) {
  39. QUnit.push(false, diff, {}, message);
  40. } else {
  41. // QUnit.jsDump is so dumb. It can't even parse circular references.
  42. QUnit.push(true, "okay", "okay", message);
  43. }
  44. }
  45. function subsetOfOwnProperties(base, another) {
  46. if (base === another) {
  47. return false;
  48. }
  49. if (typeof base != "object" || typeof another != "object") {
  50. return another;
  51. }
  52. var diff = {};
  53. var isDiff = false;
  54. for (var key in another) {
  55. if (key.charAt(0) == "_" || !another.hasOwnProperty(key)) {
  56. continue;
  57. }
  58. if (key in base) {
  59. if (base[key] === another[key]) {
  60. // skip equal pairs
  61. } else {
  62. var sub = subsetOfOwnProperties(base[key], another[key]);
  63. if (sub) {
  64. isDiff = true;
  65. diff[key] = sub;
  66. }
  67. }
  68. } else {
  69. isDiff = true;
  70. diff[key] = another[key];
  71. }
  72. }
  73. return isDiff ? diff : false;
  74. }
  75. /**
  76. * Compare two stylesheets
  77. * @param {string} css
  78. * @param {Object} expected
  79. * @param {string} [message]
  80. */
  81. function compare(css, expected, message) {
  82. var actual = CSSOM.parse(css);
  83. test(css, function(){
  84. equalOwnProperties(actual, expected, message || "");
  85. });
  86. }