2
0

core_utils_spec.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _primitives = require("../../core/primitives");
  24. var _core_utils = require("../../core/core_utils");
  25. var _test_utils = require("./test_utils");
  26. describe('core_utils', function () {
  27. describe('getInheritableProperty', function () {
  28. it('handles non-dictionary arguments', function () {
  29. expect((0, _core_utils.getInheritableProperty)({
  30. dict: null,
  31. key: 'foo'
  32. })).toEqual(undefined);
  33. expect((0, _core_utils.getInheritableProperty)({
  34. dict: undefined,
  35. key: 'foo'
  36. })).toEqual(undefined);
  37. });
  38. it('handles dictionaries that do not contain the property', function () {
  39. var emptyDict = new _primitives.Dict();
  40. expect((0, _core_utils.getInheritableProperty)({
  41. dict: emptyDict,
  42. key: 'foo'
  43. })).toEqual(undefined);
  44. var filledDict = new _primitives.Dict();
  45. filledDict.set('bar', 'baz');
  46. expect((0, _core_utils.getInheritableProperty)({
  47. dict: filledDict,
  48. key: 'foo'
  49. })).toEqual(undefined);
  50. });
  51. it('fetches the property if it is not inherited', function () {
  52. var ref = _primitives.Ref.get(10, 0);
  53. var xref = new _test_utils.XRefMock([{
  54. ref: ref,
  55. data: 'quux'
  56. }]);
  57. var dict = new _primitives.Dict(xref);
  58. dict.set('foo', 'bar');
  59. expect((0, _core_utils.getInheritableProperty)({
  60. dict: dict,
  61. key: 'foo'
  62. })).toEqual('bar');
  63. dict.set('baz', ['qux', ref]);
  64. expect((0, _core_utils.getInheritableProperty)({
  65. dict: dict,
  66. key: 'baz',
  67. getArray: true
  68. })).toEqual(['qux', 'quux']);
  69. });
  70. it('fetches the property if it is inherited and present on one level', function () {
  71. var ref = _primitives.Ref.get(10, 0);
  72. var xref = new _test_utils.XRefMock([{
  73. ref: ref,
  74. data: 'quux'
  75. }]);
  76. var firstDict = new _primitives.Dict(xref);
  77. var secondDict = new _primitives.Dict(xref);
  78. firstDict.set('Parent', secondDict);
  79. secondDict.set('foo', 'bar');
  80. expect((0, _core_utils.getInheritableProperty)({
  81. dict: firstDict,
  82. key: 'foo'
  83. })).toEqual('bar');
  84. secondDict.set('baz', ['qux', ref]);
  85. expect((0, _core_utils.getInheritableProperty)({
  86. dict: firstDict,
  87. key: 'baz',
  88. getArray: true
  89. })).toEqual(['qux', 'quux']);
  90. });
  91. it('fetches the property if it is inherited and present on multiple levels', function () {
  92. var ref = _primitives.Ref.get(10, 0);
  93. var xref = new _test_utils.XRefMock([{
  94. ref: ref,
  95. data: 'quux'
  96. }]);
  97. var firstDict = new _primitives.Dict(xref);
  98. var secondDict = new _primitives.Dict(xref);
  99. firstDict.set('Parent', secondDict);
  100. firstDict.set('foo', 'bar1');
  101. secondDict.set('foo', 'bar2');
  102. expect((0, _core_utils.getInheritableProperty)({
  103. dict: firstDict,
  104. key: 'foo'
  105. })).toEqual('bar1');
  106. expect((0, _core_utils.getInheritableProperty)({
  107. dict: firstDict,
  108. key: 'foo',
  109. getArray: false,
  110. stopWhenFound: false
  111. })).toEqual(['bar1', 'bar2']);
  112. firstDict.set('baz', ['qux1', ref]);
  113. secondDict.set('baz', ['qux2', ref]);
  114. expect((0, _core_utils.getInheritableProperty)({
  115. dict: firstDict,
  116. key: 'baz',
  117. getArray: true,
  118. stopWhenFound: false
  119. })).toEqual([['qux1', 'quux'], ['qux2', 'quux']]);
  120. });
  121. it('stops searching when the loop limit is reached', function () {
  122. var dict = new _primitives.Dict();
  123. var currentDict = dict;
  124. var parentDict = null;
  125. for (var i = 0; i < 150; i++) {
  126. parentDict = new _primitives.Dict();
  127. currentDict.set('Parent', parentDict);
  128. currentDict = parentDict;
  129. }
  130. parentDict.set('foo', 'bar');
  131. expect((0, _core_utils.getInheritableProperty)({
  132. dict: dict,
  133. key: 'foo'
  134. })).toEqual(undefined);
  135. dict.set('foo', 'baz');
  136. expect((0, _core_utils.getInheritableProperty)({
  137. dict: dict,
  138. key: 'foo',
  139. getArray: false,
  140. stopWhenFound: false
  141. })).toEqual(['baz']);
  142. });
  143. });
  144. describe('toRomanNumerals', function () {
  145. it('handles invalid arguments', function () {
  146. var _loop = function _loop() {
  147. var input = _arr[_i];
  148. expect(function () {
  149. (0, _core_utils.toRomanNumerals)(input);
  150. }).toThrow(new Error('The number should be a positive integer.'));
  151. };
  152. for (var _i = 0, _arr = ['foo', -1, 0]; _i < _arr.length; _i++) {
  153. _loop();
  154. }
  155. });
  156. it('converts numbers to uppercase Roman numerals', function () {
  157. expect((0, _core_utils.toRomanNumerals)(1)).toEqual('I');
  158. expect((0, _core_utils.toRomanNumerals)(6)).toEqual('VI');
  159. expect((0, _core_utils.toRomanNumerals)(7)).toEqual('VII');
  160. expect((0, _core_utils.toRomanNumerals)(8)).toEqual('VIII');
  161. expect((0, _core_utils.toRomanNumerals)(10)).toEqual('X');
  162. expect((0, _core_utils.toRomanNumerals)(40)).toEqual('XL');
  163. expect((0, _core_utils.toRomanNumerals)(100)).toEqual('C');
  164. expect((0, _core_utils.toRomanNumerals)(500)).toEqual('D');
  165. expect((0, _core_utils.toRomanNumerals)(1000)).toEqual('M');
  166. expect((0, _core_utils.toRomanNumerals)(2019)).toEqual('MMXIX');
  167. });
  168. it('converts numbers to lowercase Roman numerals', function () {
  169. expect((0, _core_utils.toRomanNumerals)(1, true)).toEqual('i');
  170. expect((0, _core_utils.toRomanNumerals)(6, true)).toEqual('vi');
  171. expect((0, _core_utils.toRomanNumerals)(7, true)).toEqual('vii');
  172. expect((0, _core_utils.toRomanNumerals)(8, true)).toEqual('viii');
  173. expect((0, _core_utils.toRomanNumerals)(10, true)).toEqual('x');
  174. expect((0, _core_utils.toRomanNumerals)(40, true)).toEqual('xl');
  175. expect((0, _core_utils.toRomanNumerals)(100, true)).toEqual('c');
  176. expect((0, _core_utils.toRomanNumerals)(500, true)).toEqual('d');
  177. expect((0, _core_utils.toRomanNumerals)(1000, true)).toEqual('m');
  178. expect((0, _core_utils.toRomanNumerals)(2019, true)).toEqual('mmxix');
  179. });
  180. });
  181. });