www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

utilities_internalTest.js (5719B)


      1 "use strict";
      2 
      3 describe("Zotero.Utilities.Internal", function () {
      4 	var ZUI;
      5 		
      6 	before(function () {
      7 		ZUI = Zotero.Utilities.Internal;
      8 	});
      9 	
     10 	
     11 	
     12 	describe("#md5()", function () {
     13 		it("should generate hex string given file path", function* () {
     14 			var file = OS.Path.join(getTestDataDirectory().path, 'test.png');
     15 			assert.equal(
     16 				Zotero.Utilities.Internal.md5(Zotero.File.pathToFile(file)),
     17 				'93da8f1e5774c599f0942dcecf64b11c'
     18 			);
     19 		})
     20 	})
     21 	
     22 	
     23 	describe("#md5Async()", function () {
     24 		it("should generate hex string given file path", function* () {
     25 			var file = OS.Path.join(getTestDataDirectory().path, 'test.png');
     26 			yield assert.eventually.equal(
     27 				Zotero.Utilities.Internal.md5Async(file),
     28 				'93da8f1e5774c599f0942dcecf64b11c'
     29 			);
     30 		});
     31 		
     32 		it("should generate hex string given file path for file bigger than chunk size", function* () {
     33 			var tmpDir = Zotero.getTempDirectory().path;
     34 			var file = OS.Path.join(tmpDir, 'md5Async');
     35 			
     36 			let encoder = new TextEncoder();
     37 			let arr = encoder.encode("".padStart(100000, "a"));
     38 			yield OS.File.writeAtomic(file, arr);
     39 			
     40 			yield assert.eventually.equal(
     41 				Zotero.Utilities.Internal.md5Async(file),
     42 				'1af6d6f2f682f76f80e606aeaaee1680'
     43 			);
     44 			
     45 			yield OS.File.remove(file);
     46 		});
     47 	})
     48 	
     49 	
     50 	describe("#gzip()/gunzip()", function () {
     51 		it("should compress and decompress a Unicode text string", function* () {
     52 			var text = "VoilĂ ! \u1F429";
     53 			var compstr = yield Zotero.Utilities.Internal.gzip(text);
     54 			assert.isAbove(compstr.length, 0);
     55 			assert.notEqual(compstr.length, text.length);
     56 			var str = yield Zotero.Utilities.Internal.gunzip(compstr);
     57 			assert.equal(str, text);
     58 		});
     59 	});
     60 	
     61 	
     62 	describe("#delayGenerator", function () {
     63 		var spy;
     64 		
     65 		before(function () {
     66 			spy = sinon.spy(Zotero.Promise, "delay");
     67 		});
     68 		
     69 		afterEach(function () {
     70 			spy.reset();
     71 		});
     72 		
     73 		after(function () {
     74 			spy.restore();
     75 		});
     76 		
     77 		it("should delay for given amounts of time without limit", function* () {
     78 			var intervals = [1, 2];
     79 			var gen = Zotero.Utilities.Internal.delayGenerator(intervals);
     80 			
     81 			// When intervals are exhausted, keep using last interval
     82 			var testIntervals = intervals.slice();
     83 			testIntervals.push(intervals[intervals.length - 1]);
     84 			
     85 			for (let i of testIntervals) {
     86 				let val = yield gen.next().value;
     87 				assert.isTrue(val);
     88 				assert.isTrue(spy.calledWith(i));
     89 				spy.reset();
     90 			}
     91 		});
     92 		
     93 		it("should return false when maxTime is reached", function* () {
     94 			var intervals = [5, 10];
     95 			var gen = Zotero.Utilities.Internal.delayGenerator(intervals, 30);
     96 			
     97 			// When intervals are exhausted, keep using last interval
     98 			var testIntervals = intervals.slice();
     99 			testIntervals.push(intervals[intervals.length - 1]);
    100 			
    101 			for (let i of testIntervals) {
    102 				let val = yield gen.next().value;
    103 				assert.isTrue(val);
    104 				assert.isTrue(spy.calledWith(i));
    105 				spy.reset();
    106 			}
    107 			
    108 			// Another interval would put us over maxTime, so return false immediately
    109 			let val = yield gen.next().value;
    110 			assert.isFalse(val);
    111 			assert.isFalse(spy.called);
    112 		});
    113 	});
    114 	
    115 	
    116 	describe("#extractIdentifiers()", function () {
    117 		it("should extract ISBN-10", async function () {
    118 			var id = "0838985890";
    119 			var identifiers = ZUI.extractIdentifiers(id);
    120 			assert.lengthOf(identifiers, 1);
    121 			assert.lengthOf(Object.keys(identifiers[0]), 1);
    122 			assert.propertyVal(identifiers[0], "ISBN", id);
    123 		});
    124 		
    125 		it("should extract ISBN-13", async function () {
    126 			var identifiers = ZUI.extractIdentifiers("978-0838985892");
    127 			assert.lengthOf(identifiers, 1);
    128 			assert.lengthOf(Object.keys(identifiers[0]), 1);
    129 			assert.propertyVal(identifiers[0], "ISBN", "9780838985892");
    130 		});
    131 		
    132 		it("should extract multiple ISBN-13s", async function () {
    133 			var identifiers = ZUI.extractIdentifiers("978-0838985892 9781479347711 ");
    134 			assert.lengthOf(identifiers, 2);
    135 			assert.lengthOf(Object.keys(identifiers[0]), 1);
    136 			assert.lengthOf(Object.keys(identifiers[1]), 1);
    137 			assert.propertyVal(identifiers[0], "ISBN", "9780838985892");
    138 			assert.propertyVal(identifiers[1], "ISBN", "9781479347711");
    139 		});
    140 		
    141 		it("should extract DOI", async function () {
    142 			var id = "10.4103/0976-500X.85940";
    143 			var identifiers = ZUI.extractIdentifiers(id);
    144 			assert.lengthOf(identifiers, 1);
    145 			assert.lengthOf(Object.keys(identifiers[0]), 1);
    146 			assert.propertyVal(identifiers[0], "DOI", id);
    147 		});
    148 		
    149 		it("should extract PMID", async function () {
    150 			var identifiers = ZUI.extractIdentifiers("1 PMID:24297125,222 3-4 1234567890, 123456789");
    151 			assert.lengthOf(identifiers, 4);
    152 			assert.lengthOf(Object.keys(identifiers[0]), 1);
    153 			assert.lengthOf(Object.keys(identifiers[1]), 1);
    154 			assert.lengthOf(Object.keys(identifiers[2]), 1);
    155 			assert.lengthOf(Object.keys(identifiers[3]), 1);
    156 			assert.propertyVal(identifiers[0], "PMID", "1");
    157 			assert.propertyVal(identifiers[1], "PMID", "24297125");
    158 			assert.propertyVal(identifiers[2], "PMID", "222");
    159 			assert.propertyVal(identifiers[3], "PMID", "123456789");
    160 		});
    161 		
    162 		it("should extract multiple old and new style arXivs", async function () {
    163 			var identifiers = ZUI.extractIdentifiers("0706.0044 arXiv:0706.00441v1,12345678,hep-ex/9809001v1, math.GT/0309135.");
    164 			assert.lengthOf(identifiers, 4);
    165 			assert.lengthOf(Object.keys(identifiers[0]), 1);
    166 			assert.lengthOf(Object.keys(identifiers[1]), 1);
    167 			assert.lengthOf(Object.keys(identifiers[2]), 1);
    168 			assert.lengthOf(Object.keys(identifiers[3]), 1);
    169 			assert.propertyVal(identifiers[0], "arXiv", "0706.0044");
    170 			assert.propertyVal(identifiers[1], "arXiv", "0706.00441");
    171 			assert.propertyVal(identifiers[2], "arXiv", "hep-ex/9809001");
    172 			assert.propertyVal(identifiers[3], "arXiv", "math.GT/0309135");
    173 		});
    174 	});
    175 })