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

Tests/shared #3

Merged
merged 6 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ export function foldLine (str, maxLen = 76) {
let match;

while (pos < len) {
let escaped = false;

curLine = str.substring(pos, pos + maxLen);

// ensure that the line never ends with a partial escaping
// make longer lines if needed
while (curLine.substring(-1) === '\\' && pos + curLine.length < len) {
curLine += str.charAt(pos + curLine.length);
if (curLine.endsWith('\\') && pos + curLine.length < len) {
escaped = true;
curLine += str.charAt(pos + curLine.length); // Append the next character
}

// ensure that if possible, line breaks are done at reasonable places
Expand All @@ -134,7 +137,7 @@ export function foldLine (str, maxLen = 76) {
if ((match = /.*\s+/.exec(curLine)) && /\S/.test(match[0])) {
// use everything before and including the last white space character (if anything)
curLine = match[0];
} else if ((match = /.*[\x21-\x2f0-9\x5b-\x60\x7b-\x7e]+/.exec(curLine)) && /[^\x21-\x2f0-9\x5b-\x60\x7b-\x7e]/.test(match[0])) {
} else if (!escaped && (match = /.*[\x21-\x2f0-9\x5b-\x60\x7b-\x7e]+/.exec(curLine)) && /[^\x21-\x2f0-9\x5b-\x60\x7b-\x7e]/.test(match[0])) {
// use everything before and including the last "special" character (if anything)
curLine = match[0];
}
Expand Down
81 changes: 80 additions & 1 deletion test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import { readFile as fsReadFile } from 'node:fs';
import { fileURLToPath } from 'node:url';
import * as chai from 'chai';
import { formatCharset, parseHeader, generateHeader, foldLine, parseNPluralFromHeadersSafely } from '../src/shared.js';
import { formatCharset, parseHeader, generateHeader, foldLine, parseNPluralFromHeadersSafely, compareMsgid } from '../src/shared.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -89,6 +89,43 @@ X-Poedit-SourceCharset: UTF-8`;
expect(folded.length).to.equal(3);
});

it('should fold the line into multiple lines with the right length', () => {
const line = Array.from({ length: 76 }, () => 'a').join('') + 'aaaaa\\aaaa';
const folded = foldLine(line);
expect(folded.length).to.equal(2);
expect(folded[0].length).to.equal(76);
expect(line).to.equal(folded.join(''));
expect(folded).to.deep.equal([
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'aaaaa\\aaaa'
]);
});

it('should fold the line into multiple lines with the right length (escaped character)', () => {
const line = Array.from({ length: 75 }, () => 'a').join('') + '\\aaaaaa\\aaaa';
const folded = foldLine(line);
expect(folded.length).to.equal(2);
expect(folded[0].length).to.equal(77);
expect(line).to.equal(folded.join(''));
expect(folded).to.deep.equal([
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\a',
'aaaaa\\aaaa'
]);
});


it('should fold the line into multiple lines with the right length (escaped forward slash)', () => {
const line = Array.from({ length: 75 }, () => 'a').join('') + '\\\\aaaaa\\aaaa';
const folded = foldLine(line);
expect(folded.length).to.equal(2);
expect(folded[0].length).to.equal(77);
expect(line).to.equal(folded.join(''));
expect(folded).to.deep.equal([
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\\',
'aaaaa\\aaaa'
]);
});

it('should fold at default length', () => {
const expected = ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pretium ',
'a nunc ac fringilla. Nulla laoreet tincidunt tincidunt. Proin tristique ',
Expand Down Expand Up @@ -199,3 +236,45 @@ X-Poedit-SourceCharset: UTF-8`;
});
});
});

describe('Strings Sorting function', () => {
it('should return -1 when left msgid is less than right msgid', () => {
const result = compareMsgid({ msgid: 'a' }, { msgid: 'b' });
expect(result).to.equal(-1);
});

it('should return 1 when left msgid is greater than right msgid', () => {
const result = compareMsgid({ msgid: 'b' }, { msgid: 'a' });
expect(result).to.equal(1);
});

it('should return 0 when left msgid is equal to right msgid', () => {
const result = compareMsgid({ msgid: 'a' }, { msgid: 'a' });
expect(result).to.equal(0);
});

it('should return -1 when msgid is the uppercased version of the other msgid', () => {
const result = compareMsgid({ msgid: 'A' }, { msgid: 'a' });
expect(result).to.equal(-1);
});

it('should return 1 when the msgid is a number and other is a string', () => {
const result = compareMsgid({ msgid: 'A' }, { msgid: '1' });
expect(result).to.equal(1);
});

it('should return the right result using buffer comparison', () => {
const result = compareMsgid({ msgid: Buffer.from('a') }, { msgid: Buffer.from('b') });
expect(result).to.equal(-1);
});

it('should return the right result using buffer (both directions)', () => {
const result = compareMsgid({ msgid: Buffer.from('c') }, { msgid: Buffer.from('b') });
expect(result).to.equal(1);
});

it('should return the right result using buffer comparison (checking uppercase)', () => {
const result = compareMsgid({ msgid: Buffer.from('A') }, { msgid: Buffer.from('a') });
expect(result).to.equal(-1);
});
});