object_utils_test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 objectUtils = require('utils/object_utils');
  19. describe('utils/object_utils', function() {
  20. describe('#recursiveTree()', function() {
  21. var testObj = {
  22. a1: {
  23. a2: 'v1',
  24. a3: {
  25. a4: {
  26. a5: {
  27. a6: 'v2',
  28. a7: 'v3'
  29. }
  30. }
  31. }
  32. }
  33. };
  34. it('should return correct tree of childs', function(){
  35. var result = objectUtils.recursiveTree(testObj);
  36. expect(result).to.eql('a2 (/a1)<br/>a5 (/a1/a3/a4)<br/>');
  37. });
  38. it('should return `null` if type missed', function() {
  39. var result = objectUtils.recursiveTree('{ a1: "v1"}');
  40. expect(result).to.be.null;
  41. });
  42. });
  43. describe('#recursiveKeysCount()', function() {
  44. var tests = [
  45. {
  46. m: 'should return 1 child',
  47. e: 3,
  48. obj: {
  49. a1: {
  50. a2: 'v1',
  51. a3: 'v2',
  52. a4: {
  53. a5: 'v3'
  54. }
  55. }
  56. }
  57. },
  58. {
  59. m: 'should return 1 childs',
  60. e: 1,
  61. obj: {
  62. a1: 'c1'
  63. }
  64. },
  65. {
  66. m: 'should return `null`',
  67. e: null,
  68. obj: 'a1'
  69. }
  70. ];
  71. tests.forEach(function(test){
  72. it(test.m, function() {
  73. expect(objectUtils.recursiveKeysCount(test.obj)).to.be.eql(test.e);
  74. });
  75. });
  76. });
  77. });