utils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. var utils = require('../../lib/util/utils');
  2. var should = require('should');
  3. describe('utils test', function() {
  4. describe('#invokeCallback', function() {
  5. it('should invoke the function with the parameters', function() {
  6. var p1 = 1, p2 = 'str';
  7. var func = function(arg1, arg2) {
  8. p1.should.equal(arg1);
  9. p2.should.equal(arg2);
  10. };
  11. utils.invokeCallback(func, p1, p2);
  12. });
  13. it('should ok if cb is null', function() {
  14. var p1 = 1, p2 = 'str';
  15. (function() {
  16. utils.invokeCallback(null, p1, p2);
  17. }).should.not.throw();
  18. });
  19. });
  20. describe('#size', function() {
  21. it('should return the own property count of the object', function() {
  22. var obj = {
  23. p1: 'str',
  24. p2: 1,
  25. m1: function() {}
  26. };
  27. utils.size(obj).should.equal(2);
  28. });
  29. });
  30. describe('#startsWith', function() {
  31. it('should return true if the string do start with the prefix', function() {
  32. var src = 'prefix with a string';
  33. var prefix = 'prefix';
  34. utils.startsWith(src, prefix).should.be.true;
  35. });
  36. it('should return false if the string not start with the prefix', function() {
  37. var src = 'prefix with a string';
  38. var prefix = 'prefix222';
  39. utils.startsWith(src, prefix).should.be.false;
  40. prefix = 'with';
  41. utils.startsWith(src, prefix).should.be.false;
  42. });
  43. it('should return false if the src not a string', function() {
  44. utils.startsWith(1, 'str').should.be.false;
  45. });
  46. });
  47. describe('#endsWith', function() {
  48. it('should return true if the string do end with the prefix', function() {
  49. var src = 'string with a suffix';
  50. var suffix = 'suffix';
  51. utils.endsWith(src, suffix).should.be.true;
  52. });
  53. it('should return false if the string not end with the prefix', function() {
  54. var src = 'string with a suffix';
  55. var suffix = 'suffix222';
  56. utils.endsWith(src, suffix).should.be.false;
  57. suffix = 'with';
  58. utils.endsWith(src, suffix).should.be.false;
  59. });
  60. it('should return false if the src not a string', function() {
  61. utils.endsWith(1, 'str').should.be.false;
  62. });
  63. });
  64. describe('#hasChineseChar', function() {
  65. it('should return false if the string does not have any Chinese characters', function() {
  66. var src = 'string without Chinese characters';
  67. utils.hasChineseChar(src).should.be.false;
  68. });
  69. it('should return true if the string has Chinese characters', function() {
  70. var src = 'string with Chinese characters 你好';
  71. utils.hasChineseChar(src).should.be.true;
  72. });
  73. });
  74. describe('#unicodeToUtf8', function() {
  75. it('should return the origin string if the string does not have any Chinese characters', function() {
  76. var src = 'string without Chinese characters';
  77. utils.unicodeToUtf8(src).should.equal(src);
  78. });
  79. it('should not return the origin string if the string has Chinese characters', function() {
  80. var src = 'string with Chinese characters 你好';
  81. utils.unicodeToUtf8(src).should.not.equal(src);
  82. });
  83. });
  84. describe('#isLocal', function() {
  85. it('should return true if the ip is local', function() {
  86. var ip = '127.0.0.1';
  87. var host = 'localhost';
  88. var other = '192.168.1.1';
  89. utils.isLocal(ip).should.be.true;
  90. utils.isLocal(host).should.be.true;
  91. utils.isLocal(other).should.be.false;
  92. });
  93. });
  94. describe('#loadCluster', function() {
  95. it('should produce cluster servers', function() {
  96. var clusterServer = {host: '127.0.0.1', port: '3010++', serverType: 'chat', cluster: true, clusterCount: 2};
  97. var serverMap = {};
  98. var app = {clusterSeq:{}};
  99. utils.loadCluster(app, clusterServer, serverMap);
  100. utils.size(serverMap).should.equal(2);
  101. });
  102. });
  103. describe('#arrayDiff', function() {
  104. it('should return the difference of two arrays', function() {
  105. var array1 = [1, 2, 3, 4, 5];
  106. var array2 = [1, 2, 3];
  107. var array = utils.arrayDiff(array1, array2);
  108. array.should.eql([4, 5]);
  109. });
  110. });
  111. describe('#extends', function() {
  112. it('should extends opts', function() {
  113. var opts = {
  114. test: 123
  115. };
  116. var add = {
  117. aaa: 555
  118. };
  119. var result = utils.extends(opts, add);
  120. result.should.eql({
  121. test: 123,
  122. aaa: 555
  123. });
  124. });
  125. });
  126. describe('#ping', function() {
  127. it('should ping server', function() {
  128. utils.ping('127.0.0.1', function(flag) {
  129. flag.should.be.true;
  130. });
  131. utils.ping('111.111.111.111', function(flag) {
  132. flag.should.be.false;
  133. });
  134. });
  135. });
  136. });