import fs from 'fs'; import { vol } from 'memfs'; import { buildResourceItem, readResourcesXMLAsync } from '../../android/Resources'; import { setStringItem } from '../../android/Strings'; import { escapeAndroidString, format, unescapeAndroidString, writeXMLAsync } from '../XML'; jest.mock('fs'); export const sampleStringsXML = ` exp\\'o &bo<y>' `; describe(readResourcesXMLAsync, () => { beforeAll(async () => { const directoryJSON = { './android/app/src/main/res/values/strings.xml': sampleStringsXML, }; vol.fromJSON(directoryJSON, '/app'); }); afterAll(async () => { vol.reset(); }); it(`can write the escaped name and then read it back in unescaped format`, async () => { const stringsPath = '/app/android/app/src/main/res/values/strings.xml'; let stringsJSON = await readResourcesXMLAsync({ path: stringsPath }); expect(stringsJSON.resources.string.filter((e) => e.$.name === 'app_name')[0]._).toBe( `exp'o &bo'` ); stringsJSON = setStringItem( [buildResourceItem({ name: 'app_name', value: `'E&x

o"@\n` })], stringsJSON ); // Test that it's written in escaped form // expect(format(stringsJSON)).toBe(true); expect(format(stringsJSON).includes(`\\'E&x<p>o\\"\\@\\n`)).toBe(true); // And parsed in unescaped form expect(stringsJSON.resources.string.filter((e) => e.$.name === 'app_name')[0]._).toBe( `\\'E&x

o\\"\\@\\n` ); }); }); describe('read and write', () => { // reading removes // const example = ` exp\\'o `; beforeAll(async () => { const directoryJSON = { './android/app/src/main/res/values/strings.xml': example, }; vol.fromJSON(directoryJSON, '/app'); }); afterAll(async () => { vol.reset(); }); it(`can write the escaped name and then read it back in unescaped format`, async () => { const stringsPath = '/app/android/app/src/main/res/values/strings.xml'; const stringsJSON = await readResourcesXMLAsync({ path: stringsPath }); await writeXMLAsync({ path: stringsPath, xml: stringsJSON }); expect(await fs.promises.readFile(stringsPath, 'utf-8')).toBe(example); }); }); describe(escapeAndroidString, () => { it(`can escape Android strings`, () => { expect(escapeAndroidString(`@`)).toBe(`\\@`); expect(escapeAndroidString(`'''`)).toBe(`\\'\\'\\'`); expect(escapeAndroidString('"E&x

o"@\n\r\t')).toBe(`\\"E&x

o\\"\\@\\n\\r\\t`); }); }); describe(unescapeAndroidString, () => { it(`can remove escape sequences from Android strings`, () => { expect(unescapeAndroidString(`test\\test`)).toBe('testtest'); expect(unescapeAndroidString(`test\\'test`)).toBe("test'test"); expect(unescapeAndroidString(`test\\\\'test`)).toBe("test\\'test"); }); });