From 8e08df786b0cf68f55e3639be2c84b82340825f4 Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Mon, 13 May 2024 12:10:20 +0200 Subject: [PATCH] Provides full coverage for shared functions (shared.js) --- src/shared.js | 4 ++-- test/shared.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/shared.js b/src/shared.js index ae005b7..fecc96f 100644 --- a/src/shared.js +++ b/src/shared.js @@ -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 } // ensure that if possible, line breaks are done at reasonable places diff --git a/test/shared.js b/test/shared.js index 0263c02..ac77e0d 100644 --- a/test/shared.js +++ b/test/shared.js @@ -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); @@ -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 ', @@ -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); + }); +});