misc_test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var misc = require('utils/misc');
  19. describe('misc', function () {
  20. describe('#formatBandwidth', function () {
  21. var tests = Em.A([
  22. {m:'undefined to undefined',i:undefined,e:undefined},
  23. {m:'0 to <1KB',i:'0',e:'<1KB'},
  24. {m:'1000 to <1KB',i:'1000',e:'<1KB'},
  25. {m:'1024 to 1.0KB',i:'1024',e:'1.0KB'},
  26. {m:'2048 to 2.0KB',i:'2048',e:'2.0KB'},
  27. {m:'1048576 to 1.0MB',i:'1048576',e:'1.0MB'},
  28. {m:'1782579 to 1.7MB',i:'1782579',e:'1.7MB'},
  29. {m:'1546188226 to 1.44GB',i:'1546188226',e:'1.44GB'}
  30. ]);
  31. tests.forEach(function(test) {
  32. it(test.m + ' ', function () {
  33. expect(misc.formatBandwidth(test.i)).to.equal(test.e);
  34. });
  35. });
  36. it('NaN to NaN' + ' ', function () {
  37. expect(isNaN(misc.formatBandwidth(NaN))).to.equal(true);
  38. });
  39. });
  40. describe('#ipToInt', function () {
  41. var tests = Em.A([
  42. {m:'0.0.0.0 to 0',i:'0.0.0.0',e:0},
  43. {m:'255.255.255.255 to 4294967295',i:'255.255.255.255',e:4294967295},
  44. {m:'"" to false',i:'',e:false},
  45. {m:'255.255.255.256 to false',i:'255.255.255.256',e:false},
  46. {m:'255.255.255 to false',i:'255.255.255',e:false}
  47. ]);
  48. tests.forEach(function(test) {
  49. it(test.m + ' ', function () {
  50. expect(misc.ipToInt(test.i)).to.equal(test.e);
  51. });
  52. });
  53. });
  54. describe('#sortByOrder', function() {
  55. var tests = Em.A([
  56. {
  57. sortOrder: ['b', 'c', 'a'],
  58. array: [{id:'a'}, {id:'b'}, Em.Object.create({id:'c'})],
  59. e: [{id:'b'}, Em.Object.create({id:'c'}), {id:'a'}],
  60. m: 'Array with Ember and native objects'
  61. },
  62. {
  63. sortOrder: ['b', 'c', 'a'],
  64. array: [{id:'a'}, {id:'b'}, {id:'c'}],
  65. e: [{id:'b'}, {id:'c'}, {id:'a'}],
  66. m: 'Array with native objects'
  67. },
  68. {
  69. sortOrder: ['b', 'c', 'a'],
  70. array: [Em.Object.create({id:'a'}), Em.Object.create({id:'b'}), Em.Object.create({id:'c'})],
  71. e: [Em.Object.create({id:'b'}), Em.Object.create({id:'c'}), Em.Object.create({id:'a'})],
  72. m: 'Array with Ember objects'
  73. }
  74. ]);
  75. tests.forEach(function(test) {
  76. it(test.m, function() {
  77. expect(misc.sortByOrder(test.sortOrder, test.array)).to.eql(test.e);
  78. });
  79. });
  80. });
  81. describe('#xmlToObject()', function(){
  82. var xml = '<!-- Edited by XMLSpy -->'+
  83. '<stacks name="HDP-2.1">'+
  84. '<service>'+
  85. '<name>NAGIOS</name>'+
  86. '<name>OOZIE</name>'+
  87. '<name>HDFS</name>'+
  88. '<component>NAGIOS_SERVER</component>'+
  89. '</service>'+
  90. '</stacks>';
  91. xml = new DOMParser().parseFromString(xml,"text/xml");
  92. var converted = misc.xmlToObject(xml);
  93. it('should be an object', function(){
  94. expect(converted).to.a('object');
  95. });
  96. it('`attribute` name should be present', function(){
  97. expect(converted.stacks).to.ok;
  98. });
  99. it('`stacks.service.name` should be an array', function() {
  100. expect(converted.stacks.service.name).to.a('array');
  101. });
  102. });
  103. });