const { translateComments, translateCommentsInChallenge, translateGeneric } = require('.'); const { ENGLISH_CHALLENGE_NO_FILES } = require('./__fixtures__/challenge-objects'); const { SIMPLE_TRANSLATION } = require('./__mocks__/mock-comments'); let logSpy; describe('translation parser', () => { beforeEach(() => { logSpy = jest.spyOn(console, 'warn').mockImplementation(); }); afterEach(() => { logSpy.mockRestore(); }); describe('translateGeneric', () => { it('returns an object containing translated text', () => { expect.assertions(1); const seed = `// Add your code below this line Add your code above this line `; const transSeed = `// (Chinese) Add your code below this line (Chinese) Add your code above this line `; const knownComments = Object.keys(SIMPLE_TRANSLATION); const config = { knownComments, dict: SIMPLE_TRANSLATION, lang: 'chinese' }; const actual = translateGeneric( { text: seed, commentCounts: new Map() }, config, '((? { expect.assertions(1); const seed = `// Add your code below this line // Add your code above this line // Add your code below this line `; const expectedCommentCounts = new Map(); expectedCommentCounts .set('(Chinese) Add your code below this line (Chinese)', 2) .set('(Chinese) Add your code above this line (Chinese)', 1); const knownComments = Object.keys(SIMPLE_TRANSLATION); const config = { knownComments, dict: SIMPLE_TRANSLATION, lang: 'chinese' }; const actual = translateGeneric( { text: seed, commentCounts: new Map() }, config, '((? { it('returns a clone of the challenge if there are no comments', () => { expect( translateCommentsInChallenge( ENGLISH_CHALLENGE_NO_FILES, 'chinese', SIMPLE_TRANSLATION ) ).toEqual(ENGLISH_CHALLENGE_NO_FILES); }); }); describe('translateComments', () => { it('replaces single line English comments with their translations', () => { const seed = `// Add your code below this line Add your code above this line `; const transSeed = `// (Chinese) Add your code below this line (Chinese) Add your code above this line `; expect( translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(transSeed); }); it('does not translate urls', () => { const seed = `http:// Add your code below this line Add your code above this line `; expect( translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(seed); const seedS = `https:// Add your code below this line Add your code above this line `; expect( translateComments(seedS, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(seedS); }); it('replaces inline English comments with their translations', () => { const seed = `inline comment // Add your code below this line Add your code above this line `; const transSeed = `inline comment // (Chinese) Add your code below this line (Chinese) Add your code above this line `; expect( translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(transSeed); }); it('replaces multiple English comments with their translations', () => { const seed = `inline comment // Add your code below this line // Add your code below this line `; const transSeed = `inline comment // (Chinese) Add your code below this line (Chinese) // (Chinese) Add your code below this line (Chinese) `; expect( translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(transSeed); }); it('replaces multiline English comments with their translations', () => { const seed = `multiline comment /* Add your code below this line */ /* Add your code above this line */ change code below this line `; const transSeed = `multiline comment /* (Chinese) Add your code below this line (Chinese) */ /* (Chinese) Add your code above this line (Chinese) */ change code below this line `; expect( translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(transSeed); }); it('replaces repeated multiline comments with their translations', () => { const seed = `multiline comment /* Add your code below this line */ /* Add your code below this line */ change code below this line `; const transSeed = `multiline comment /* (Chinese) Add your code below this line (Chinese) */ /* (Chinese) Add your code below this line (Chinese) */ change code below this line `; expect( translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(transSeed); }); it('ignores empty comments', () => { expect.assertions(1); const seed = '//'; expect( translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(seed); }); it('only replaces text inside comments, not between them', () => { const seed = `multiline comment /* Add your code below this line */ /* Add your code above this line */ Add your code below this line /* */ `; const transSeed = `multiline comment /* (Chinese) Add your code below this line (Chinese) */ /* (Chinese) Add your code above this line (Chinese) */ Add your code below this line /* */ `; expect( translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(transSeed); const seedTwo = `multiline /* */ Add your code below this line /* */ `; expect( translateComments(seedTwo, 'chinese', SIMPLE_TRANSLATION, 'js').text ).toBe(seedTwo); }); it('replaces English html comments with their translations', () => { const seed = `