Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit-tests for colorspace.js #8611

Merged
merged 1 commit into from
Jul 28, 2017

Conversation

apoorv-mishra
Copy link
Contributor

@apoorv-mishra apoorv-mishra commented Jul 5, 2017

Hi, I have added some tests to check the isDefaultDecode(...) function in colorspace.js. I'll be adding the tests for other functions too. Please review and let me know the necessary changes to be made.

Thanks.

Fixes #7261.

it('should be true if decode is not an array', function() {
let test1 = new Test('I am a string', 0);
let result1 = true;
let returnValue1 = ColorSpace.isDefaultDecode(test1.decode, test1.n);
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Jul 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the tests that you've currently added in the patch, I'm not sure why the code needs to be so verbose in these test-cases!?
Something along these lines ought to be perfectly sufficient:

expect(ColorSpace.isDefaultDecode('string', 0)).toBeTruthy(); // or .toBeFalsy() where applicable

or, if that doesn't fit on one line (e.g. below when you're passing arrays as arguments):

let someVariableName = [...];
expect(ColorSpace.isDefaultDecode(someVariableName, 0)).toBeTruthy();

import { ColorSpace } from '../../src/core/colorspace';

describe('colorspace', function () {
class Test {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is necessary; see the suggestion in https://github.com/mozilla/pdf.js/pull/8611/files#r125623641.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait!, don't you need to import ColorSpace?

let result5 = false;
let returnValue5 = ColorSpace.isDefaultDecode(test5.decode, test5.n);

let test6 = new Test([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1], 8);
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Jul 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice, I don't think the number of components will usually go beyond 4, so you can probably use slightly shorter arrays here (and below too).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

@apoorv-mishra
Copy link
Contributor Author

Hi @Snuffleupagus , I made the appropriate changes. Please have a look and let me know.


describe('colorspace', function () {
it('should be true if decode is not an array', function() {
expect(ColorSpace.isDefaultDecode('I am a string', 0)).toBe(true);
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Jul 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As indicated in #8611 (comment), please use toBeTruthy()/toBeFalsy() instead.

Also, please don't forget to squash the commits, see https://github.com/mozilla/pdf.js/wiki/Squashing-Commits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

expect(ColorSpace.isDefaultDecode([0, 1], 1)).toBe(true);
expect(ColorSpace.isDefaultDecode([1, 0], 1)).toBe(false);
expect(ColorSpace.isDefaultDecode([1, 1], 1)).toBe(false);
expect(ColorSpace.isDefaultDecode([0, 1, 0, 1, 0, 1, 1, 1], 4)).toBe(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean for one of the last three cases to be truthy instead, since otherwise I'm not sure why you need so many (almost) identical test-cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they are meant to be falsy. I would change them, they look too verbose.

@apoorv-mishra
Copy link
Contributor Author

@Snuffleupagus , I made the changes and squashed the commits. Please have a look and let me know.

@apoorv-mishra
Copy link
Contributor Author

Hi @Snuffleupagus , let me know if the changes look fine so that I may proceed further with this work and add tests for other functions too. Thanks.

@Snuffleupagus
Copy link
Collaborator

let me know if the changes look fine so that I may proceed further with this work and add tests for other functions too. Thanks.

Since it's your first patch, and you explicitly asked for initial feedback, I provided some basic comments. However, I'm not sure if it's really necessary to review every small part piecewise at this point, so I'd suggest that we'll simply review it once you feel that the patch is complete :-)
One general piece of advice though: you've got a couple of merge commits which shouldn't be there (squash to get rid of them). I'd recommend that you use git rebase, rather than git merge, to avoid that in the future. Also, when you're updating the patch please change the commit message to "Add unit-tests for colorspace.js" instead.

As for what additional tests you need to add, I think that it would be nice to at least have a few simple unit-tests for e.g. the following methods: ColorSpace.parse (this will cover ColorSpace.parseToIR and ColorSpace.fromIR automatically), getRgb, getRgbItem, fillRgb.
I'd suggest searching for these methods in the src/core/*.js files, if you want examples of how they are used.

@apoorv-mishra apoorv-mishra force-pushed the colorspace-tests branch 2 times, most recently from 60b2d52 to 016add5 Compare July 6, 2017 12:31
@apoorv-mishra apoorv-mishra changed the title Added tests for colorspace.js Add unit-tests for colorspace.js Jul 26, 2017
Copy link
Contributor

@timvandermeij timvandermeij left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far! It's really good to have unit tests for this. I have added some comments about stylistic issues, so please resolve those to be consistent with the other parts of the codebase.

* limitations under the License.
*/

import {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put these on one line since they fit within the 80 character limit.

Ref
} from '../../src/core/primitives';
import { Stream, StringStream } from '../../src/core/stream';
import {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this on one line since they fit within the 80 character limit.

for (let elem in array) {
let obj = array[elem];
let ref = obj.ref,
data = obj.data;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single lines should only contain one statement, so if you put this on a new line, make it let data = obj.data;.

expect(ColorSpace.isDefaultDecode('string', 0)).toBeTruthy();
});
it('should be true if length of decode array is not correct',
function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to other places in the patch as well.

In the other parts of the codebase, we format this as follows, so please do that here as well:

it('should be true if length of decode array is not correct',
    function () {
  expect(ColorSpace.isDefaultDecode([0], 1)).toBeTruthy();
  expect(ColorSpace.isDefaultDecode([0, 1, 0], 1)).toBeTruthy();
});

In other words, the function is intented with four spaces instead of two, while the body of the function is still indented with two spaces.


let testSrc = new Uint8Array([27, 125, 250, 131]);
let testDest = new Uint8Array(4 * 4 * 3);
let expectedDest = new Uint8Array(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to other places in the patch as well.

Format this as follows:

let expectedDest = new Uint8Array([
  27, 27, 27,
  ...,
  131, 131, 131,
]);


expect(colorSpace.getRgb(new Float32Array([0.2]), 0)).toEqual(
new Uint8Array(
[51, 51, 51]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to other places in the patch as well.

Move this back to the previous line.


let colorSpace = ColorSpace.parse(cs, xref, res);

let testSrc = new Uint8Array([27, 125, 250,
Copy link
Contributor

@timvandermeij timvandermeij Jul 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to other places in the patch as well.

Put the part from the [ onward on the next line.

expect(testDest).toEqual(expectedDest);
});
});
describe('CalRGBCS', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to other places in the patch as well.

Please make sure that there is a newline before each describe block for readability.

@timvandermeij
Copy link
Contributor

/botio unittest

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_unittest from @timvandermeij received. Current queue size: 0

Live output at: http://54.215.176.217:8877/0468a8a39ec70a2/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_unittest from @timvandermeij received. Current queue size: 0

Live output at: http://54.67.70.0:8877/7d9ef8d6b32836a/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/7d9ef8d6b32836a/output.txt

Total script time: 2.55 mins

  • Unit Tests: Passed

@pdfjsbot
Copy link

From: Bot.io (Windows)


Success

Full output at http://54.215.176.217:8877/0468a8a39ec70a2/output.txt

Total script time: 12.10 mins

  • Unit Tests: Passed

@apoorv-mishra
Copy link
Contributor Author

Hi @timvandermeij , I resolved the stylistic issues as mentioned. Please have a look.

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides a couple of quick inline comments, is there any reason why these tests wouldn't work if run on Node.js?
If not, please add colorspace_spec.js to the https://github.com/mozilla/pdf.js/blob/master/test/unit/clitests.json file.


describe('DeviceGrayCS', function () {
it('should handle the case when cs is a Name object', function () {
let cs = new Name('DeviceGray');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to comment on this once here, but it also applies in a number of places below:
Please use Name.get('DeviceGray') instead, since that only creates one instance of the Name object regardless of how many times it's called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

params.set('BlackPoint', [0, 0, 0]);
params.set('Gamma', 2.0);

let cs = new Array(2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, and elsewhere too, using new Array isn't necessary. Please just do:

let cs = [
  Name.get('CalGray'),
  params,
];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Added unit-tests for DeviceGray, DeviceRGB and DeviceCMYK

Added unit-tests for CalGray

Added unit-tests for CalRGB

Removed redundant code

Added unit-tests for LabCS

Added unit-tests for IndexedCS

Update comment

Change lookup to Uint8Array as mentioned in pdf specs(these tests will pass after PR mozilla#8666 is merged).

Added unit-tests for AlternateCS

Resolved code-style issues

Fixed code-style issues

Addressed issues pointed out in mozilla#8611 (review)
@timvandermeij
Copy link
Contributor

/botio unittest

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_unittest from @timvandermeij received. Current queue size: 0

Live output at: http://54.67.70.0:8877/17bb92ab174b2f8/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_unittest from @timvandermeij received. Current queue size: 0

Live output at: http://54.215.176.217:8877/a033a228a610cd9/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/17bb92ab174b2f8/output.txt

Total script time: 2.55 mins

  • Unit Tests: Passed

@pdfjsbot
Copy link

From: Bot.io (Windows)


Success

Full output at http://54.215.176.217:8877/a033a228a610cd9/output.txt

Total script time: 12.36 mins

  • Unit Tests: Passed

@timvandermeij
Copy link
Contributor

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @timvandermeij received. Current queue size: 0

Live output at: http://54.67.70.0:8877/2c946f5b8ea04cb/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @timvandermeij received. Current queue size: 0

Live output at: http://54.215.176.217:8877/66646a9eb6886a1/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/2c946f5b8ea04cb/output.txt

Total script time: 16.54 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: Passed

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://54.215.176.217:8877/66646a9eb6886a1/output.txt

Total script time: 29.02 mins

  • Font tests: Passed
  • Unit tests: FAILED
  • Regression tests: Passed

@timvandermeij timvandermeij merged commit 783d42e into mozilla:master Jul 28, 2017
@timvandermeij
Copy link
Contributor

timvandermeij commented Jul 28, 2017

Thank you for providing the smoke tests!

(The one unit tests failure is unrelated to this patch and passed before.)

movsb pushed a commit to movsb/pdf.js that referenced this pull request Jul 14, 2018
Added unit-tests for DeviceGray, DeviceRGB and DeviceCMYK

Added unit-tests for CalGray

Added unit-tests for CalRGB

Removed redundant code

Added unit-tests for LabCS

Added unit-tests for IndexedCS

Update comment

Change lookup to Uint8Array as mentioned in pdf specs(these tests will pass after PR mozilla#8666 is merged).

Added unit-tests for AlternateCS

Resolved code-style issues

Fixed code-style issues

Addressed issues pointed out in mozilla#8611 (review)
movsb pushed a commit to movsb/pdf.js that referenced this pull request Jul 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants