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

Provides full coverage for shared functions (shared.js) #99

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ export function foldLine (str, maxLen = 76) {

// 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);
while (curLine.endsWith('\\') && pos + curLine.length < len) {
curLine += str.charAt(pos + curLine.length + 1); // Append the next character
erikyo marked this conversation as resolved.
Show resolved Hide resolved
}

// ensure that if possible, line breaks are done at reasonable places
Expand Down
30 changes: 29 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,17 @@ X-Poedit-SourceCharset: UTF-8`;
expect(folded.length).to.equal(3);
});

it('should ensure that the line never ends with a partial escaping', () => {
const line = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\'aaaaa\'aaaa';
const folded = foldLine(line);

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 +210,20 @@ 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);
});
});
erikyo marked this conversation as resolved.
Show resolved Hide resolved