core_utils_spec.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.js");
  24. var _core_utils = require("../../core/core_utils.js");
  25. var _test_utils = require("./test_utils.js");
  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. const emptyDict = new _primitives.Dict();
  40. expect((0, _core_utils.getInheritableProperty)({
  41. dict: emptyDict,
  42. key: "foo"
  43. })).toEqual(undefined);
  44. const 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. const ref = _primitives.Ref.get(10, 0);
  53. const xref = new _test_utils.XRefMock([{
  54. ref,
  55. data: "quux"
  56. }]);
  57. const dict = new _primitives.Dict(xref);
  58. dict.set("foo", "bar");
  59. expect((0, _core_utils.getInheritableProperty)({
  60. dict,
  61. key: "foo"
  62. })).toEqual("bar");
  63. dict.set("baz", ["qux", ref]);
  64. expect((0, _core_utils.getInheritableProperty)({
  65. 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. const ref = _primitives.Ref.get(10, 0);
  72. const xref = new _test_utils.XRefMock([{
  73. ref,
  74. data: "quux"
  75. }]);
  76. const firstDict = new _primitives.Dict(xref);
  77. const 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. const ref = _primitives.Ref.get(10, 0);
  93. const xref = new _test_utils.XRefMock([{
  94. ref,
  95. data: "quux"
  96. }]);
  97. const firstDict = new _primitives.Dict(xref);
  98. const 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. const dict = new _primitives.Dict();
  123. let currentDict = dict;
  124. let parentDict = null;
  125. for (let 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,
  133. key: "foo"
  134. })).toEqual(undefined);
  135. dict.set("foo", "baz");
  136. expect((0, _core_utils.getInheritableProperty)({
  137. 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. for (const input of ["foo", -1, 0]) {
  147. expect(function () {
  148. (0, _core_utils.toRomanNumerals)(input);
  149. }).toThrow(new Error("The number should be a positive integer."));
  150. }
  151. });
  152. it("converts numbers to uppercase Roman numerals", function () {
  153. expect((0, _core_utils.toRomanNumerals)(1)).toEqual("I");
  154. expect((0, _core_utils.toRomanNumerals)(6)).toEqual("VI");
  155. expect((0, _core_utils.toRomanNumerals)(7)).toEqual("VII");
  156. expect((0, _core_utils.toRomanNumerals)(8)).toEqual("VIII");
  157. expect((0, _core_utils.toRomanNumerals)(10)).toEqual("X");
  158. expect((0, _core_utils.toRomanNumerals)(40)).toEqual("XL");
  159. expect((0, _core_utils.toRomanNumerals)(100)).toEqual("C");
  160. expect((0, _core_utils.toRomanNumerals)(500)).toEqual("D");
  161. expect((0, _core_utils.toRomanNumerals)(1000)).toEqual("M");
  162. expect((0, _core_utils.toRomanNumerals)(2019)).toEqual("MMXIX");
  163. });
  164. it("converts numbers to lowercase Roman numerals", function () {
  165. expect((0, _core_utils.toRomanNumerals)(1, true)).toEqual("i");
  166. expect((0, _core_utils.toRomanNumerals)(6, true)).toEqual("vi");
  167. expect((0, _core_utils.toRomanNumerals)(7, true)).toEqual("vii");
  168. expect((0, _core_utils.toRomanNumerals)(8, true)).toEqual("viii");
  169. expect((0, _core_utils.toRomanNumerals)(10, true)).toEqual("x");
  170. expect((0, _core_utils.toRomanNumerals)(40, true)).toEqual("xl");
  171. expect((0, _core_utils.toRomanNumerals)(100, true)).toEqual("c");
  172. expect((0, _core_utils.toRomanNumerals)(500, true)).toEqual("d");
  173. expect((0, _core_utils.toRomanNumerals)(1000, true)).toEqual("m");
  174. expect((0, _core_utils.toRomanNumerals)(2019, true)).toEqual("mmxix");
  175. });
  176. });
  177. describe("log2", function () {
  178. it("handles values smaller than/equal to zero", function () {
  179. expect((0, _core_utils.log2)(0)).toEqual(0);
  180. expect((0, _core_utils.log2)(-1)).toEqual(0);
  181. });
  182. it("handles values larger than zero", function () {
  183. expect((0, _core_utils.log2)(1)).toEqual(0);
  184. expect((0, _core_utils.log2)(2)).toEqual(1);
  185. expect((0, _core_utils.log2)(3)).toEqual(2);
  186. expect((0, _core_utils.log2)(3.14)).toEqual(2);
  187. });
  188. });
  189. describe("isWhiteSpace", function () {
  190. it("handles space characters", function () {
  191. expect((0, _core_utils.isWhiteSpace)(0x20)).toEqual(true);
  192. expect((0, _core_utils.isWhiteSpace)(0x09)).toEqual(true);
  193. expect((0, _core_utils.isWhiteSpace)(0x0d)).toEqual(true);
  194. expect((0, _core_utils.isWhiteSpace)(0x0a)).toEqual(true);
  195. });
  196. it("handles non-space characters", function () {
  197. expect((0, _core_utils.isWhiteSpace)(0x0b)).toEqual(false);
  198. expect((0, _core_utils.isWhiteSpace)(null)).toEqual(false);
  199. expect((0, _core_utils.isWhiteSpace)(undefined)).toEqual(false);
  200. });
  201. });
  202. });