chore: remove old parser
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
e3511f2930
commit
a3a678b7af
@ -0,0 +1,259 @@
|
||||
const ENGLISH_CERTIFICATE = {
|
||||
id: '561add10cb82ac38a17513bc',
|
||||
title: 'Responsive Web Design Certificate',
|
||||
challengeType: 7,
|
||||
isPrivate: true,
|
||||
tests: [
|
||||
{ id: 'bd7158d8c442eddfaeb5bd18', title: 'Build a Tribute Page' },
|
||||
{ id: '587d78af367417b2b2512b03', title: 'Build a Survey Form' },
|
||||
{
|
||||
id: '587d78af367417b2b2512b04',
|
||||
title: 'Build a Product Landing Page'
|
||||
},
|
||||
{
|
||||
id: '587d78b0367417b2b2512b05',
|
||||
title: 'Build a Technical Documentation Page'
|
||||
},
|
||||
{
|
||||
id: 'bd7158d8c242eddfaeb5bd13',
|
||||
title: 'Build a Personal Portfolio Webpage'
|
||||
}
|
||||
],
|
||||
solutions: ['// solution required\n'],
|
||||
description: '',
|
||||
instructions: '',
|
||||
files: []
|
||||
};
|
||||
|
||||
const ENGLISH_CHALLENGE = {
|
||||
id: 'id',
|
||||
title: 'Title',
|
||||
challengeType: 0,
|
||||
videoUrl: 'https://scrimba.com/',
|
||||
forumTopicId: 12345,
|
||||
tests: [
|
||||
{
|
||||
text: 'Test text',
|
||||
testString: 'assertions'
|
||||
},
|
||||
{
|
||||
text: 'Test text2',
|
||||
testString: 'assertions2'
|
||||
}
|
||||
],
|
||||
solutions: ['solution html string'],
|
||||
description: 'description html string',
|
||||
instructions: 'instructions html string',
|
||||
files: [
|
||||
{
|
||||
key: 'indexhtml',
|
||||
ext: 'html',
|
||||
name: 'index',
|
||||
contents: 'seed html string',
|
||||
head: 'head string',
|
||||
tail: 'tail string'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const ENGLISH_CHALLENGE_TWO_SOLUTIONS = {
|
||||
id: 'id',
|
||||
title: 'Title',
|
||||
challengeType: 0,
|
||||
videoUrl: 'https://scrimba.com/',
|
||||
forumTopicId: 12345,
|
||||
tests: [
|
||||
{
|
||||
text: 'Test text',
|
||||
testString: 'assertions'
|
||||
},
|
||||
{
|
||||
text: 'Test text2',
|
||||
testString: 'assertions2'
|
||||
}
|
||||
],
|
||||
solutions: ['solution html string', 'second solution html string'],
|
||||
description: 'description html string',
|
||||
instructions: 'instructions html string',
|
||||
files: [
|
||||
{
|
||||
key: 'indexhtml',
|
||||
ext: 'html',
|
||||
name: 'index',
|
||||
contents: 'seed html string',
|
||||
head: '',
|
||||
tail: ''
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const ENGLISH_CHALLENGE_NO_FILES = {
|
||||
id: 'id',
|
||||
title: 'Title',
|
||||
challengeType: 0,
|
||||
videoUrl: 'https://scrimba.com/',
|
||||
forumTopicId: 12345,
|
||||
tests: [
|
||||
{
|
||||
text: 'Test text',
|
||||
testString: 'assertions'
|
||||
},
|
||||
{
|
||||
text: 'Test text2',
|
||||
testString: 'assertions2'
|
||||
}
|
||||
],
|
||||
solutions: ['solution html string'],
|
||||
description: 'description html string',
|
||||
instructions: 'instructions html string',
|
||||
files: []
|
||||
};
|
||||
|
||||
const ENGLISH_VIDEO_CHALLENGE = {
|
||||
id: 'id',
|
||||
title: 'Title',
|
||||
challengeType: 0,
|
||||
videoId: 'abc123',
|
||||
forumTopicId: 12345,
|
||||
question: 'english question',
|
||||
description: 'description html string',
|
||||
instructions: 'instructions html string'
|
||||
};
|
||||
|
||||
const TRANSLATED_CERTIFICATE = {
|
||||
id: '561add10cb82ac38a17513bc',
|
||||
title: '响应式网页设计证书',
|
||||
challengeType: 7,
|
||||
isPrivate: true,
|
||||
videoUrl: '',
|
||||
tests: [
|
||||
{ id: 'bd7158d8c442eddfaeb5bd18', title: 'Build a Tribute Page' },
|
||||
{ id: '587d78af367417b2b2512b03', title: 'Build a Survey Form' },
|
||||
{
|
||||
id: '587d78af367417b2b2512b04',
|
||||
title: 'Build a Product Landing Page'
|
||||
},
|
||||
{
|
||||
id: '587d78b0367417b2b2512b05',
|
||||
title: 'Build a Technical Documentation Page'
|
||||
},
|
||||
{
|
||||
id: 'bd7158d8c242eddfaeb5bd13',
|
||||
title: 'Build a Personal Portfolio Webpage'
|
||||
}
|
||||
],
|
||||
solutions: ['// solution required\n'],
|
||||
description: '',
|
||||
instructions: '',
|
||||
files: []
|
||||
};
|
||||
|
||||
const TRANSLATED_CHALLENGE = {
|
||||
id: 'id',
|
||||
title: 'Translated title',
|
||||
challengeType: 0,
|
||||
videoUrl: 'https://scrimba.com/',
|
||||
forumTopicId: 9876,
|
||||
tests: [
|
||||
{
|
||||
text: 'Translated test text',
|
||||
testString: 'Translated assertions, should be ignored'
|
||||
},
|
||||
{
|
||||
text: 'Translated test text2',
|
||||
testString: 'Translated assertions, should be ignored2'
|
||||
}
|
||||
],
|
||||
solutions: ['Translated solution html string, should be ignored'],
|
||||
description: 'Translated description html string',
|
||||
instructions: 'Translated instructions html string',
|
||||
files: [
|
||||
{
|
||||
key: 'indexhtml',
|
||||
ext: 'html',
|
||||
name: 'index',
|
||||
contents: 'Translated seed html string, should be ignored',
|
||||
head: 'Translated head string, should be ignored',
|
||||
tail: 'Translated tail string, should be ignored'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const TRANSLATED_CHALLENGE_NO_TITLE = {
|
||||
id: 'id',
|
||||
challengeType: 0,
|
||||
videoUrl: 'https://scrimba.com/',
|
||||
forumTopicId: 9876,
|
||||
tests: [
|
||||
{
|
||||
text: 'Translated test text',
|
||||
testString: 'Translated assertions, should be ignored'
|
||||
},
|
||||
{
|
||||
text: 'Translated test text2',
|
||||
testString: 'Translated assertions, should be ignored2'
|
||||
}
|
||||
],
|
||||
solutions: ['Translated solution html string, should be ignored'],
|
||||
description: 'Translated description html string',
|
||||
instructions: 'Translated instructions html string',
|
||||
files: [
|
||||
{
|
||||
key: 'indexhtml',
|
||||
ext: 'html',
|
||||
name: 'index',
|
||||
contents: 'Translated seed html string, should be ignored',
|
||||
head: 'Translated head string, should be ignored',
|
||||
tail: 'Translated tail string, should be ignored'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const TRANSLATED_VIDEO_CHALLENGE = {
|
||||
id: 'id',
|
||||
title: 'Title',
|
||||
challengeType: 0,
|
||||
videoId: 'abc123',
|
||||
forumTopicId: 12345,
|
||||
question: 'translated question',
|
||||
description: 'translated description html string',
|
||||
instructions: 'translated instructions html string'
|
||||
};
|
||||
|
||||
const WRONG_NUM_TESTS_CHALLENGE = {
|
||||
id: 'id',
|
||||
title: 'Translated title',
|
||||
challengeType: 0,
|
||||
videoUrl: 'https://scrimba.com/',
|
||||
forumTopicId: 12345,
|
||||
tests: [
|
||||
{
|
||||
text: 'Translated test text',
|
||||
testString: 'Translated assertions, should be ignored'
|
||||
}
|
||||
],
|
||||
solutions: ['Translated solution html string, should be ignored'],
|
||||
description: 'Translated description html string',
|
||||
instructions: 'Translated instructions html string',
|
||||
files: [
|
||||
{
|
||||
key: 'indexhtml',
|
||||
ext: 'html',
|
||||
name: 'index',
|
||||
contents: 'Translated seed html string, should be ignored',
|
||||
head: '',
|
||||
tail: ''
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
exports.ENGLISH_CERTIFICATE = ENGLISH_CERTIFICATE;
|
||||
exports.ENGLISH_CHALLENGE = ENGLISH_CHALLENGE;
|
||||
exports.ENGLISH_CHALLENGE_TWO_SOLUTIONS = ENGLISH_CHALLENGE_TWO_SOLUTIONS;
|
||||
exports.ENGLISH_CHALLENGE_NO_FILES = ENGLISH_CHALLENGE_NO_FILES;
|
||||
exports.ENGLISH_VIDEO_CHALLENGE = ENGLISH_VIDEO_CHALLENGE;
|
||||
exports.TRANSLATED_CERTIFICATE = TRANSLATED_CERTIFICATE;
|
||||
exports.TRANSLATED_CHALLENGE = TRANSLATED_CHALLENGE;
|
||||
exports.TRANSLATED_CHALLENGE_NO_TITLE = TRANSLATED_CHALLENGE_NO_TITLE;
|
||||
exports.TRANSLATED_VIDEO_CHALLENGE = TRANSLATED_VIDEO_CHALLENGE;
|
||||
exports.WRONG_NUM_TESTS_CHALLENGE = WRONG_NUM_TESTS_CHALLENGE;
|
@ -0,0 +1,16 @@
|
||||
const SIMPLE_TRANSLATION = {
|
||||
'Add your code below this line': {
|
||||
chinese: '(Chinese) Add your code below this line (Chinese)'
|
||||
},
|
||||
'Add your code above this line': {
|
||||
chinese: '(Chinese) Add your code above this line (Chinese)'
|
||||
},
|
||||
'change code below this line': {
|
||||
chinese: '(Chinese) change code below this line (Chinese)'
|
||||
},
|
||||
'change code above this line': {
|
||||
chinese: '(Chinese) change code above this line (Chinese)'
|
||||
}
|
||||
};
|
||||
|
||||
exports.SIMPLE_TRANSLATION = SIMPLE_TRANSLATION;
|
146
tools/challenge-parser/translation-parser/translation-parser.js
Normal file
146
tools/challenge-parser/translation-parser/translation-parser.js
Normal file
@ -0,0 +1,146 @@
|
||||
const { isEmpty } = require('lodash');
|
||||
const clone = require('lodash/cloneDeep');
|
||||
|
||||
exports.translateComments = (text, lang, dict, codeLang) => {
|
||||
const knownComments = Object.keys(dict);
|
||||
const config = { knownComments, dict, lang };
|
||||
switch (codeLang) {
|
||||
case 'js':
|
||||
case 'jsx':
|
||||
return transMultiline(transInline(text, config), config);
|
||||
case 'html':
|
||||
return transScript(transHTML(transCSS(text, config), config), config);
|
||||
default:
|
||||
return text;
|
||||
}
|
||||
};
|
||||
|
||||
exports.translateCommentsInChallenge = (challenge, lang, dict) => {
|
||||
const challClone = clone(challenge);
|
||||
if (!challClone.files) {
|
||||
console.warn(`Challenge ${challClone.title} has no comments to translate`);
|
||||
} else {
|
||||
Object.keys(challClone.files).forEach(key => {
|
||||
if (challClone.files[key].contents) {
|
||||
challClone.files[key].contents = this.translateComments(
|
||||
challenge.files[key].contents,
|
||||
lang,
|
||||
dict,
|
||||
challClone.files[key].ext
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
return challClone;
|
||||
};
|
||||
|
||||
exports.mergeChallenges = (engChal, transChal) => {
|
||||
const hasTests =
|
||||
(engChal.tests && transChal.tests) ||
|
||||
(engChal.question && transChal.question);
|
||||
const challenge = {
|
||||
...engChal,
|
||||
description: transChal.description,
|
||||
instructions: transChal.instructions,
|
||||
originalTitle: engChal.title,
|
||||
// TODO: throw in production?
|
||||
title: isEmpty(transChal.title) ? engChal.title : transChal.title,
|
||||
forumTopicId: transChal.forumTopicId
|
||||
};
|
||||
if (!hasTests)
|
||||
throw Error(
|
||||
`Both challenges must have tests or questions.
|
||||
title: ${engChal.title}
|
||||
translated title: ${transChal.title}`
|
||||
);
|
||||
// TODO: this should break the build when we go to production, but
|
||||
// not for testing.
|
||||
if (transChal.tests && transChal.tests.length !== engChal.tests.length) {
|
||||
console.error(
|
||||
`Challenges in both languages must have the same number of tests.
|
||||
title: ${engChal.title}
|
||||
translated title: ${transChal.title}`
|
||||
);
|
||||
return challenge;
|
||||
}
|
||||
|
||||
// throw Error(
|
||||
// `Challenges in both languages must have the same number of tests.
|
||||
// title: ${engChal.title}
|
||||
// translated title: ${transChal.title}`
|
||||
// );
|
||||
|
||||
if (transChal.tests) {
|
||||
const translatedTests =
|
||||
engChal.challengeType === 7
|
||||
? transChal.tests.map(({ title }, i) => ({
|
||||
title,
|
||||
id: engChal.tests[i].id
|
||||
}))
|
||||
: transChal.tests.map(({ text }, i) => ({
|
||||
text,
|
||||
testString: engChal.tests[i].testString
|
||||
}));
|
||||
challenge.tests = translatedTests;
|
||||
} else {
|
||||
challenge.question = transChal.question;
|
||||
}
|
||||
|
||||
// certificates do not have forumTopicIds
|
||||
if (challenge.challengeType === 7) delete challenge.forumTopicId;
|
||||
return challenge;
|
||||
};
|
||||
|
||||
// bare urls could be interpreted as comments, so we have to lookbehind for
|
||||
// http:// or https://
|
||||
function transInline(text, config) {
|
||||
return translateGeneric(text, config, '((?<!https?:)//\\s*)', '(\\s*$)');
|
||||
}
|
||||
|
||||
function transMultiline(text, config) {
|
||||
return translateGeneric(text, config, '(/\\*\\s*)', '(\\s*\\*/)');
|
||||
}
|
||||
|
||||
// CSS has to be handled separately since it is looking for comments inside tags
|
||||
function transCSS(text, config) {
|
||||
const regex = /<style>.*?<\/style>/gms;
|
||||
const matches = text.matchAll(regex);
|
||||
|
||||
for (const [match] of matches) {
|
||||
text = text.replace(match, transMultiline(match, config));
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function transScript(text, config) {
|
||||
const regex = /<script>.*?<\/script>/gms;
|
||||
const matches = text.matchAll(regex);
|
||||
|
||||
for (const [match] of matches) {
|
||||
text = text.replace(
|
||||
match,
|
||||
transMultiline(transInline(match, config), config)
|
||||
);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function transHTML(text, config) {
|
||||
return translateGeneric(text, config, '(<!--\\s*)', '(\\s*-->)');
|
||||
}
|
||||
|
||||
function translateGeneric(text, config, regexBefore, regexAfter) {
|
||||
const { knownComments, dict, lang } = config;
|
||||
const regex = new RegExp(regexBefore + '(.*?)' + regexAfter, 'gms');
|
||||
const matches = text.matchAll(regex);
|
||||
|
||||
for (const [match, before, comment, after] of matches) {
|
||||
if (knownComments.includes(comment)) {
|
||||
text = text.replace(match, `${before}${dict[comment][lang]}${after}`);
|
||||
} else {
|
||||
console.warn(`${comment} does not appear in the comment dictionary`);
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
@ -0,0 +1,427 @@
|
||||
/* global expect jest */
|
||||
const {
|
||||
mergeChallenges,
|
||||
translateComments,
|
||||
translateCommentsInChallenge
|
||||
} = require('./translation-parser');
|
||||
const {
|
||||
ENGLISH_CERTIFICATE,
|
||||
ENGLISH_CHALLENGE,
|
||||
ENGLISH_CHALLENGE_NO_FILES,
|
||||
ENGLISH_CHALLENGE_TWO_SOLUTIONS,
|
||||
ENGLISH_VIDEO_CHALLENGE,
|
||||
TRANSLATED_CERTIFICATE,
|
||||
TRANSLATED_CHALLENGE,
|
||||
TRANSLATED_CHALLENGE_NO_TITLE,
|
||||
TRANSLATED_VIDEO_CHALLENGE
|
||||
// WRONG_NUM_TESTS_CHALLENGE
|
||||
} = require('./__fixtures__/challenge-objects');
|
||||
const { SIMPLE_TRANSLATION } = require('./__mocks__/mock-comments');
|
||||
|
||||
const COMBINED_CHALLENGE = mergeChallenges(
|
||||
ENGLISH_CHALLENGE,
|
||||
TRANSLATED_CHALLENGE
|
||||
);
|
||||
|
||||
const COMBINED_CHALLENGE_NO_TITLE = mergeChallenges(
|
||||
ENGLISH_CHALLENGE,
|
||||
TRANSLATED_CHALLENGE_NO_TITLE
|
||||
);
|
||||
|
||||
const COMBINED_CHALLENGE_TWO_SOLUTIONS = mergeChallenges(
|
||||
ENGLISH_CHALLENGE_TWO_SOLUTIONS,
|
||||
TRANSLATED_CHALLENGE
|
||||
);
|
||||
|
||||
const COMBINED_CERTIFICATE = mergeChallenges(
|
||||
ENGLISH_CERTIFICATE,
|
||||
TRANSLATED_CERTIFICATE
|
||||
);
|
||||
|
||||
const COMBINED_VIDEO_CHALLENGE = mergeChallenges(
|
||||
ENGLISH_VIDEO_CHALLENGE,
|
||||
TRANSLATED_VIDEO_CHALLENGE
|
||||
);
|
||||
|
||||
let logSpy;
|
||||
|
||||
describe('translation parser', () => {
|
||||
beforeEach(() => {
|
||||
logSpy = jest.spyOn(console, 'warn').mockImplementation();
|
||||
});
|
||||
afterEach(() => {
|
||||
logSpy.mockRestore();
|
||||
});
|
||||
describe('mergeChallenges', () => {
|
||||
it('takes the description from the second challenge', () => {
|
||||
expect(COMBINED_CHALLENGE.description).toBe(
|
||||
TRANSLATED_CHALLENGE.description
|
||||
);
|
||||
});
|
||||
it('takes the head and tail from the first challenge', () => {
|
||||
expect(COMBINED_CHALLENGE.files[0].head).toBe(
|
||||
ENGLISH_CHALLENGE.files[0].head
|
||||
);
|
||||
expect(COMBINED_CHALLENGE.files[0].tail).toBe(
|
||||
ENGLISH_CHALLENGE.files[0].tail
|
||||
);
|
||||
});
|
||||
it('takes the instructions from the second challenge', () => {
|
||||
expect(COMBINED_CHALLENGE.instructions).toBe(
|
||||
TRANSLATED_CHALLENGE.instructions
|
||||
);
|
||||
});
|
||||
it('takes the seed from the first challenge', () => {
|
||||
expect(COMBINED_CHALLENGE.files[0].contents).toBe(
|
||||
ENGLISH_CHALLENGE.files[0].contents
|
||||
);
|
||||
});
|
||||
it('takes the solution from the first challenge', () => {
|
||||
expect(COMBINED_CHALLENGE.solutions[0]).toBe(
|
||||
ENGLISH_CHALLENGE.solutions[0]
|
||||
);
|
||||
});
|
||||
it('takes multiple solutions', () => {
|
||||
expect(COMBINED_CHALLENGE_TWO_SOLUTIONS.solutions).toEqual(
|
||||
ENGLISH_CHALLENGE_TWO_SOLUTIONS.solutions
|
||||
);
|
||||
});
|
||||
it('takes the testStrings from the first challenge', () => {
|
||||
const actualStrings = COMBINED_CHALLENGE.tests.map(
|
||||
({ testString }) => testString
|
||||
);
|
||||
const expectedStrings = ENGLISH_CHALLENGE.tests.map(
|
||||
({ testString }) => testString
|
||||
);
|
||||
for (let i = 0; i < actualStrings.length; i++) {
|
||||
expect(actualStrings[i]).toBe(expectedStrings[i]);
|
||||
}
|
||||
});
|
||||
it('takes the test text from the second challenge', () => {
|
||||
const actualStrings = COMBINED_CHALLENGE.tests.map(({ text }) => text);
|
||||
const expectedStrings = TRANSLATED_CHALLENGE.tests.map(
|
||||
({ text }) => text
|
||||
);
|
||||
for (let i = 0; i < actualStrings.length; i++) {
|
||||
expect(actualStrings[i]).toBe(expectedStrings[i]);
|
||||
}
|
||||
});
|
||||
it('takes the title from the second challenge', () => {
|
||||
expect(COMBINED_CHALLENGE.title).toBe(TRANSLATED_CHALLENGE.title);
|
||||
});
|
||||
|
||||
// TODO: throw in production?
|
||||
it("takes the first challenge's title if the second is missing", () => {
|
||||
expect(COMBINED_CHALLENGE_NO_TITLE.title).toBe(ENGLISH_CHALLENGE.title);
|
||||
});
|
||||
|
||||
// 'originalTitle' is just used to create the dashedName (which must be
|
||||
// the same in both challenges, but only gets added after parsing)
|
||||
it('creates originalTitle from the first challenge', () => {
|
||||
expect(COMBINED_CHALLENGE.originalTitle).toBe(ENGLISH_CHALLENGE.title);
|
||||
});
|
||||
// TODO: reinstate this after alpha testing.
|
||||
// it('throws an error if the numbers of tests do not match', () => {
|
||||
// expect(() =>
|
||||
// mergeChallenges(ENGLISH_CHALLENGE, WRONG_NUM_TESTS_CHALLENGE)
|
||||
// ).toThrow();
|
||||
// });
|
||||
it('takes the forum id from the second challenge', () => {
|
||||
expect(COMBINED_CHALLENGE.forumTopicId).toBe(
|
||||
TRANSLATED_CHALLENGE.forumTopicId
|
||||
);
|
||||
});
|
||||
it('takes the ids from the first certificate', () => {
|
||||
const actualIds = COMBINED_CERTIFICATE.tests.map(({ id }) => id);
|
||||
const expectedIds = ENGLISH_CERTIFICATE.tests.map(({ id }) => id);
|
||||
for (let i = 0; i < actualIds.length; i++) {
|
||||
expect(actualIds[i]).toBe(expectedIds[i]);
|
||||
}
|
||||
});
|
||||
|
||||
it('takes the titles from the second certificate', () => {
|
||||
const actualTitles = COMBINED_CERTIFICATE.tests.map(({ title }) => title);
|
||||
const expectedTitles = TRANSLATED_CERTIFICATE.tests.map(
|
||||
({ title }) => title
|
||||
);
|
||||
for (let i = 0; i < actualTitles.length; i++) {
|
||||
expect(actualTitles[i]).toBe(expectedTitles[i]);
|
||||
}
|
||||
});
|
||||
it('certificates do not have a forumTopicId property', () => {
|
||||
expect(Object.keys(COMBINED_CERTIFICATE).includes('forumTopicId')).toBe(
|
||||
false
|
||||
);
|
||||
});
|
||||
it('takes the question from the second challenge', () => {
|
||||
expect(COMBINED_VIDEO_CHALLENGE.question).toBe(
|
||||
TRANSLATED_VIDEO_CHALLENGE.question
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('translateCommentsInChallenge', () => {
|
||||
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')).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')).toBe(
|
||||
seed
|
||||
);
|
||||
const seedS = `https:// Add your code below this line
|
||||
Add your code above this line `;
|
||||
expect(
|
||||
translateComments(seedS, 'chinese', SIMPLE_TRANSLATION, 'js')
|
||||
).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')).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')).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')).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')).toBe(
|
||||
transSeed
|
||||
);
|
||||
});
|
||||
|
||||
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')).toBe(
|
||||
transSeed
|
||||
);
|
||||
|
||||
const seedTwo = `multiline /* */ Add your code below this line /* */ `;
|
||||
expect(
|
||||
translateComments(seedTwo, 'chinese', SIMPLE_TRANSLATION, 'js')
|
||||
).toBe(seedTwo);
|
||||
});
|
||||
|
||||
it('replaces English html comments with their translations', () => {
|
||||
const seed = `<div> <!-- Add your code below this line -->
|
||||
<!-- Add your code above this line --> <span>change code below this line</span> `;
|
||||
const transSeed = `<div> <!-- (Chinese) Add your code below this line (Chinese) -->
|
||||
<!-- (Chinese) Add your code above this line (Chinese) --> <span>change code below this line</span> `;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(transSeed);
|
||||
});
|
||||
|
||||
it('replaces css comments with their translations', () => {
|
||||
const seed = `<style>
|
||||
/* Add your code below this line */
|
||||
</style>`;
|
||||
const transSeed = `<style>
|
||||
/* (Chinese) Add your code below this line (Chinese) */
|
||||
</style>`;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(transSeed);
|
||||
});
|
||||
|
||||
it('replaces multiple css comments with their translations', () => {
|
||||
const seed = `<style>
|
||||
/* Add your code below this line */
|
||||
/* Add your code below this line */
|
||||
</style>`;
|
||||
const transSeed = `<style>
|
||||
/* (Chinese) Add your code below this line (Chinese) */
|
||||
/* (Chinese) Add your code below this line (Chinese) */
|
||||
</style>`;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(transSeed);
|
||||
});
|
||||
|
||||
it('ignores css comments outside style tags', () => {
|
||||
const seed = `/* Add your code below this line */`;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(seed);
|
||||
});
|
||||
|
||||
it('ignores css comments between style tags', () => {
|
||||
const seed = `<style>
|
||||
</style>
|
||||
/* Add your code below this line */
|
||||
<style>
|
||||
</style>`;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(seed);
|
||||
});
|
||||
|
||||
it('only replaces inside English html comments', () => {
|
||||
const seed = `<div> <!-- --> Add your code below this line <!-- -->`;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(seed);
|
||||
});
|
||||
|
||||
it('replaces English JSX comments with their translations', () => {
|
||||
const seed = `{ /* Add your code below this line */ }
|
||||
{ /* Add your code above this line */ } <span>change code below this line</span> `;
|
||||
const transSeed = `{ /* (Chinese) Add your code below this line (Chinese) */ }
|
||||
{ /* (Chinese) Add your code above this line (Chinese) */ } <span>change code below this line</span> `;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'jsx')
|
||||
).toBe(transSeed);
|
||||
});
|
||||
|
||||
it('replaces English script comments with their translations', () => {
|
||||
const seed = `<script>
|
||||
// Add your code below this line
|
||||
</script>`;
|
||||
const transSeed = `<script>
|
||||
// (Chinese) Add your code below this line (Chinese)
|
||||
</script>`;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(transSeed);
|
||||
});
|
||||
|
||||
it('replaces multiple script comments with their translations', () => {
|
||||
const seed = `<script>
|
||||
/* Add your code below this line */
|
||||
// Add your code below this line
|
||||
</script>`;
|
||||
const transSeed = `<script>
|
||||
/* (Chinese) Add your code below this line (Chinese) */
|
||||
// (Chinese) Add your code below this line (Chinese)
|
||||
</script>`;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(transSeed);
|
||||
});
|
||||
|
||||
it('ignores html comments inside JavaScript', () => {
|
||||
const seed = `<div> <!-- Add your code below this line
|
||||
Add your code above this line --> <span>change code below this line</span> `;
|
||||
expect(translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js')).toBe(
|
||||
seed
|
||||
);
|
||||
});
|
||||
|
||||
it('ignores html comments inside jsx', () => {
|
||||
const seed = `<div> <!-- Add your code below this line
|
||||
Add your code above this line --> <span>change code below this line</span> `;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'jsx')
|
||||
).toBe(seed);
|
||||
});
|
||||
|
||||
it('only replaces exact matches (js)', () => {
|
||||
const seedMulti = `/* Add your code below this line
|
||||
Add your code above this line */ <span>change code below this line</span> `;
|
||||
const seedInline = `// Add your code below this line, please`;
|
||||
expect(
|
||||
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'js')
|
||||
).toBe(seedMulti);
|
||||
expect(
|
||||
translateComments(seedInline, 'chinese', SIMPLE_TRANSLATION, 'js')
|
||||
).toBe(seedInline);
|
||||
});
|
||||
|
||||
it('only replaces exact matches (jsx)', () => {
|
||||
const seedMulti = `{ /* Add your code below this line
|
||||
Add your code above this line */ } <span>change code below this line</span> `;
|
||||
expect(
|
||||
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'jsx')
|
||||
).toBe(seedMulti);
|
||||
});
|
||||
|
||||
it('only replaces exact matches (html)', () => {
|
||||
const seed = `<div> <!-- Add your code below this line
|
||||
Add your code above this line --> <span>change code below this line</span> `;
|
||||
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
|
||||
).toBe(seed);
|
||||
});
|
||||
|
||||
it('only translates jsx comments once', () => {
|
||||
const seed = `{ /* Add your code below this line */ }`;
|
||||
const transSeed = `{ /* (Chinese) Add your code below this line (Chinese) */ }`;
|
||||
expect(
|
||||
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'jsx')
|
||||
).toBe(transSeed);
|
||||
});
|
||||
|
||||
it('warns if the comment is not in the dictionary', () => {
|
||||
const seedJSX = `{ /* this is not a comment */ }`;
|
||||
const seedInline = `// this is not a comment `;
|
||||
const seedMulti = `/* this is not a comment */`;
|
||||
const seedCSS = `<style>
|
||||
/* this is not a comment */
|
||||
</style>`;
|
||||
const seedHTML = `<div> <!-- this is not a comment --> `;
|
||||
const seedScript = `<script> // this is not a comment </script>`;
|
||||
|
||||
translateComments(seedJSX, 'chinese', SIMPLE_TRANSLATION, 'jsx');
|
||||
expect(logSpy).toBeCalledTimes(1);
|
||||
translateComments(seedInline, 'chinese', SIMPLE_TRANSLATION, 'js');
|
||||
expect(logSpy).toBeCalledTimes(2);
|
||||
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'js');
|
||||
expect(logSpy).toBeCalledTimes(3);
|
||||
translateComments(seedCSS, 'chinese', SIMPLE_TRANSLATION, 'html');
|
||||
expect(logSpy).toBeCalledTimes(4);
|
||||
translateComments(seedHTML, 'chinese', SIMPLE_TRANSLATION, 'html');
|
||||
expect(logSpy).toBeCalledTimes(5);
|
||||
translateComments(seedScript, 'chinese', SIMPLE_TRANSLATION, 'html');
|
||||
expect(logSpy).toBeCalledTimes(6);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user