From 4b7b1c91ec41cb0e00c797da58ae769a3c1cd8b1 Mon Sep 17 00:00:00 2001 From: Raymond Zhou <56318341+rayzhou-bit@users.noreply.github.com> Date: Thu, 13 Apr 2023 14:31:03 -0700 Subject: [PATCH] feat: further number parse changes (#305) --- .../ProblemEditor/data/OLXParser.test.js | 11 +-- .../ProblemEditor/data/ReactStateOLXParser.js | 16 ++++ .../data/ReactStateOLXParser.test.js | 16 +++- .../data/mockData/editorTestData.js | 12 +++ .../data/mockData/olxTestData.js | 76 ++++++++++++++----- 5 files changed, 104 insertions(+), 27 deletions(-) diff --git a/src/editors/containers/ProblemEditor/data/OLXParser.test.js b/src/editors/containers/ProblemEditor/data/OLXParser.test.js index 9a46ae1ae..8d16ccc02 100644 --- a/src/editors/containers/ProblemEditor/data/OLXParser.test.js +++ b/src/editors/containers/ProblemEditor/data/OLXParser.test.js @@ -18,7 +18,8 @@ import { shuffleProblemOLX, scriptProblemOlX, labelDescriptionQuestionOLX, - encodingTestOLX, + htmlEntityTestOLX, + numberParseTestOLX, } from './mockData/olxTestData'; import { ProblemTypeKeys } from '../../../data/constants/problem'; @@ -235,14 +236,14 @@ describe('OLXParser for problem with solution tag', () => { describe('Check OLXParser for proper encoding', () => { it('should not encode html entities', () => { - const olxparser = new OLXParser(encodingTestOLX.rawOLX); + const olxparser = new OLXParser(htmlEntityTestOLX.rawOLX); const problemType = olxparser.getProblemType(); const question = olxparser.parseQuestions(problemType); - expect(question).toBe(encodingTestOLX.question); + expect(question).toBe(htmlEntityTestOLX.question); }); it('should not parse hex numbers and leading zeros', () => { - const olxparser = new OLXParser(encodingTestOLX.rawOLX); + const olxparser = new OLXParser(numberParseTestOLX.rawOLX); const answer = olxparser.parseMultipleChoiceAnswers('multiplechoiceresponse', 'choicegroup', 'choice'); - expect(answer).toEqual(encodingTestOLX.data); + expect(answer).toEqual(numberParseTestOLX.data); }); }); diff --git a/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.js b/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.js index c28bb2d36..4e926b99e 100644 --- a/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.js +++ b/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.js @@ -8,10 +8,18 @@ class ReactStateOLXParser { const parserOptions = { ignoreAttributes: false, alwaysCreateTextNode: true, + numberParseOptions: { + leadingZeros: false, + hex: false, + }, }; const questionParserOptions = { ignoreAttributes: false, alwaysCreateTextNode: true, + numberParseOptions: { + leadingZeros: false, + hex: false, + }, preserveOrder: true, }; const questionBuilderOptions = { @@ -19,6 +27,10 @@ class ReactStateOLXParser { attributeNamePrefix: '@_', suppressBooleanAttributes: false, format: true, + numberParseOptions: { + leadingZeros: false, + hex: false, + }, preserveOrder: true, }; const builderOptions = { @@ -26,6 +38,10 @@ class ReactStateOLXParser { attributeNamePrefix: '@_', suppressBooleanAttributes: false, format: true, + numberParseOptions: { + leadingZeros: false, + hex: false, + }, }; this.questionParser = new XMLParser(questionParserOptions); this.parser = new XMLParser(parserOptions); diff --git a/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.test.js b/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.test.js index 6ecb97cdd..4c4c2743e 100644 --- a/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.test.js +++ b/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.test.js @@ -7,6 +7,7 @@ import { textInputWithFeedbackAndHintsOLX, multipleChoiceWithFeedbackAndHintsOLX, textInputWithFeedbackAndHintsOLXWithMultipleAnswers, + numberParseTestOLX, } from './mockData/olxTestData'; import { checkboxesWithFeedbackAndHints, @@ -16,10 +17,11 @@ import { numericInputWithFeedbackAndHints, numericInputWithAnswerRange, textInputWithFeedbackAndHintsWithMultipleAnswers, + numberParseTest, } from './mockData/editorTestData'; import ReactStateOLXParser from './ReactStateOLXParser'; -describe('Check React Sate OLXParser problem', () => { +describe('Check React State OLXParser problem', () => { test('Test checkbox with feedback and hints problem type', () => { const olxparser = new OLXParser(checkboxesOLXWithFeedbackAndHintsOLX.rawOLX); const problem = olxparser.getParsedOLXData(); @@ -91,4 +93,16 @@ describe('Check React Sate OLXParser problem', () => { const buildOLX = stateParser.buildOLX(); expect(buildOLX.replace(/\s/g, '')).toEqual(textInputWithFeedbackAndHintsOLXWithMultipleAnswers.buildOLX.replace(/\s/g, '')); }); + describe('encode/decode', () => { + test('does not change hex values to dec and does not remove leading 0s', () => { + const olxparser = new OLXParser(numberParseTestOLX.rawOLX); + const problem = olxparser.getParsedOLXData(); + const stateParser = new ReactStateOLXParser({ + problem, + editorObject: numberParseTest, + }); + const buildOLX = stateParser.buildOLX(); + expect(buildOLX.replace(/\s/g, '')).toEqual(numberParseTestOLX.buildOLX.replace(/\s/g, '')); + }); + }); }); diff --git a/src/editors/containers/ProblemEditor/data/mockData/editorTestData.js b/src/editors/containers/ProblemEditor/data/mockData/editorTestData.js index 771dee05c..1bc41d712 100644 --- a/src/editors/containers/ProblemEditor/data/mockData/editorTestData.js +++ b/src/editors/containers/ProblemEditor/data/mockData/editorTestData.js @@ -116,3 +116,15 @@ export const textInputWithFeedbackAndHintsWithMultipleAnswers = { ], question: '

You can use this template as a guide to the simple editor markdown and OLX markup to use for text input with hints and feedback problems. Edit this component to replace this template with your own assessment.

You can add an optional tip or note related to the prompt like this.', }; + +export const numberParseTest = { + solution: '', + answers: { + A: `0x10`, // eslint-disable-line + B: `0x0f`, // eslint-disable-line + C: `0x07`, // eslint-disable-line + D: `0009`, // eslint-disable-line + }, + hints: [], + question: '

What is the content of the register x2 after executing the following three lines of instructions?

', +}; diff --git a/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js b/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js index e7b21557f..2ee4202b7 100644 --- a/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js +++ b/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js @@ -695,17 +695,15 @@ export const labelDescriptionQuestionOLX = { question: '

boiling eggs: water system

Watch out, boiling water is hot', }; -export const encodingTestOLX = { +export const htmlEntityTestOLX = { rawOLX: `

What is the content of the register x2 after executing the following three lines of instructions?

Address          assembly instructions
0x0              addi x1, x0, 1
0x4              slli x2, x1, 4
0x8              sub x1, x2, x1

- 0x10 - 0x0f - 0x07 - 0009 + answerA + answerB
@@ -719,28 +717,14 @@ export const encodingTestOLX = { answers: [ { id: 'A', - // eslint-disable-next-line - title: `0x10`, + title: 'answerA', correct: false, }, { id: 'B', - // eslint-disable-next-line - title: `0x0f`, + title: 'answerB', correct: true, }, - { - id: 'C', - // eslint-disable-next-line - title: `0x07`, - correct: false, - }, - { - id: 'D', - // eslint-disable-next-line - title: `0009`, - correct: false, - }, ], }, // eslint-disable-next-line @@ -748,3 +732,53 @@ export const encodingTestOLX = { // eslint-disable-next-line solutionExplanation: `

Address          assembly instructions    comment

0x0              addi x1, x0, 1           x1 = 0x1

0x4              slli x2, x1, 4           x2 = x1 << 4 = 0x10

0x8              sub x1, x2, x1           x1 = x2 - x1 = 0x10 - 0x01 = 0xf

`, }; + +export const numberParseTestOLX = { + rawOLX: ` + +

What is the content of the register x2 after executing the following three lines of instructions?

+ + 0x10 + 0x0f + 0x07 + 0009 + +
+
`, + data: { + answers: [ + { + id: 'A', + title: `0x10`, // eslint-disable-line + correct: false, + }, + { + id: 'B', + title: `0x0f`, // eslint-disable-line + correct: true, + }, + { + id: 'C', + title: `0x07`, // eslint-disable-line + correct: false, + }, + { + id: 'D', + title: `0009`, // eslint-disable-line + correct: false, + }, + ], + }, + question: '

What is the content of the register x2 after executing the following three lines of instructions?

', + buildOLX: ` + +

What is the content of the register x2 after executing the following three lines of instructions?

+ + 0x10 + 0x0f + 0x07 + 0009 + +
+
`, +};