2012-05-11 06:11:27 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2013-02-05 03:01:19 +09:00
|
|
|
/* globals expect, it, describe, PartialEvaluator, StringStream */
|
2012-05-11 06:11:27 +09:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('evaluator', function() {
|
|
|
|
function XrefMock(queue) {
|
|
|
|
this.queue = queue;
|
|
|
|
}
|
|
|
|
XrefMock.prototype = {
|
|
|
|
fetchIfRef: function() {
|
|
|
|
return this.queue.shift();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
function HandlerMock() {
|
|
|
|
this.inputs = [];
|
|
|
|
}
|
|
|
|
HandlerMock.prototype = {
|
|
|
|
send: function(name, data) {
|
|
|
|
this.inputs({name: name, data: data});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
function ResourcesMock() { }
|
|
|
|
ResourcesMock.prototype = {
|
|
|
|
get: function(name) {
|
|
|
|
return this[name];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-19 02:41:33 +09:00
|
|
|
function PdfManagerMock() { }
|
|
|
|
|
2012-05-11 06:11:27 +09:00
|
|
|
describe('splitCombinedOperations', function() {
|
|
|
|
it('should reject unknown operations', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-11 06:11:27 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('qTT');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, new ResourcesMock());
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(1);
|
|
|
|
expect(result.fnArray[0]).toEqual('save');
|
|
|
|
expect(result.argsArray[0].length).toEqual(0);
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle one operations', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-11 06:11:27 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('Q');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, new ResourcesMock());
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(1);
|
|
|
|
expect(result.fnArray[0]).toEqual('restore');
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle two glued operations', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-11 06:11:27 +09:00
|
|
|
'prefix');
|
|
|
|
var resources = new ResourcesMock();
|
|
|
|
resources.Res1 = {};
|
|
|
|
var stream = new StringStream('/Res1 DoQ');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, resources);
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(2);
|
|
|
|
expect(result.fnArray[0]).toEqual('paintXObject');
|
|
|
|
expect(result.fnArray[1]).toEqual('restore');
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle tree glued operations', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-11 06:11:27 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('qqq');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, new ResourcesMock());
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(3);
|
|
|
|
expect(result.fnArray[0]).toEqual('save');
|
|
|
|
expect(result.fnArray[1]).toEqual('save');
|
|
|
|
expect(result.fnArray[2]).toEqual('save');
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
2012-05-21 03:44:03 +09:00
|
|
|
|
|
|
|
it('should handle three glued operations #2', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-21 03:44:03 +09:00
|
|
|
'prefix');
|
|
|
|
var resources = new ResourcesMock();
|
|
|
|
resources.Res1 = {};
|
2013-01-11 11:00:44 +09:00
|
|
|
var stream = new StringStream('B*Bf*');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, resources);
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(3);
|
|
|
|
expect(result.fnArray[0]).toEqual('eoFillStroke');
|
|
|
|
expect(result.fnArray[1]).toEqual('fillStroke');
|
|
|
|
expect(result.fnArray[2]).toEqual('eoFill');
|
|
|
|
});
|
2012-05-21 03:44:03 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle glued operations and operands', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-21 03:44:03 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('q5 Ts');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, new ResourcesMock());
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(2);
|
|
|
|
expect(result.fnArray[0]).toEqual('save');
|
|
|
|
expect(result.fnArray[1]).toEqual('setTextRise');
|
|
|
|
expect(result.argsArray.length).toEqual(2);
|
|
|
|
expect(result.argsArray[1].length).toEqual(1);
|
|
|
|
expect(result.argsArray[1][0]).toEqual(5);
|
|
|
|
});
|
2012-05-21 03:44:03 +09:00
|
|
|
});
|
2012-05-21 04:05:23 +09:00
|
|
|
|
|
|
|
it('should handle glued operations and literals', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-21 04:05:23 +09:00
|
|
|
'prefix');
|
2013-01-11 11:00:44 +09:00
|
|
|
var stream = new StringStream('trueifalserinullq');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, new ResourcesMock());
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(3);
|
|
|
|
expect(result.fnArray[0]).toEqual('setFlatness');
|
|
|
|
expect(result.fnArray[1]).toEqual('setRenderingIntent');
|
|
|
|
expect(result.fnArray[2]).toEqual('save');
|
|
|
|
expect(result.argsArray.length).toEqual(3);
|
|
|
|
expect(result.argsArray[0].length).toEqual(1);
|
|
|
|
expect(result.argsArray[0][0]).toEqual(true);
|
|
|
|
expect(result.argsArray[1].length).toEqual(1);
|
|
|
|
expect(result.argsArray[1][0]).toEqual(false);
|
|
|
|
expect(result.argsArray[2].length).toEqual(0);
|
|
|
|
});
|
2013-01-11 11:00:44 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('validateNumberOfArgs', function() {
|
|
|
|
it('should execute if correct number of arguments', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2013-01-11 11:00:44 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('5 1 d0');
|
2013-04-09 07:14:56 +09:00
|
|
|
console.log('here!');
|
|
|
|
var promise = evaluator.getOperatorList(stream, new ResourcesMock());
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(result.argsArray[0][0]).toEqual(5);
|
|
|
|
expect(result.argsArray[0][1]).toEqual(1);
|
|
|
|
expect(result.fnArray[0]).toEqual('setCharWidth');
|
|
|
|
});
|
2013-01-11 11:00:44 +09:00
|
|
|
});
|
|
|
|
it('should execute if too many arguments', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2013-01-11 11:00:44 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('5 1 4 d0');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, new ResourcesMock());
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(result.argsArray[0][0]).toEqual(5);
|
|
|
|
expect(result.argsArray[0][1]).toEqual(1);
|
|
|
|
expect(result.argsArray[0][2]).toEqual(4);
|
|
|
|
expect(result.fnArray[0]).toEqual('setCharWidth');
|
|
|
|
});
|
2013-01-11 11:00:44 +09:00
|
|
|
});
|
|
|
|
it('should skip if too few arguments', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2013-01-11 11:00:44 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('5 d0');
|
2013-04-09 07:14:56 +09:00
|
|
|
var promise = evaluator.getOperatorList(stream, new ResourcesMock());
|
|
|
|
promise.then(function(data) {
|
|
|
|
var result = data.queue;
|
|
|
|
expect(result.argsArray).toEqual([]);
|
|
|
|
expect(result.fnArray).toEqual([]);
|
|
|
|
});
|
2012-05-21 04:05:23 +09:00
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|