More test cases and breaking out valid/invalid values to be reusable

This commit is contained in:
Jonathan Putney
2019-11-13 13:09:01 -05:00
parent 54b56e905f
commit 755259afdf
13 changed files with 1417 additions and 845 deletions

View File

@@ -10,7 +10,7 @@ import {
import * as Utilities from './utilities';
import {scorm12_constants} from './constants/api_constants';
import {scorm12_error_codes} from './constants/error_codes';
import {scorm12_regex} from './regex';
import {scorm12_regex} from './constants/regex';
const constants = scorm12_constants;

View File

@@ -15,7 +15,7 @@ import {scorm2004_constants} from './constants/api_constants';
import {scorm2004_error_codes} from './constants/error_codes';
import {correct_responses} from './constants/response_constants';
import {valid_languages} from './constants/language_constants';
import {scorm2004_regex} from './regex';
import {scorm2004_regex} from './constants/regex';
const constants = scorm2004_constants;

View File

@@ -1,7 +1,7 @@
import * as Scorm12CMI from './scorm12_cmi';
import {BaseCMI, CMIArray, CMIScore} from './common';
import {aicc_constants} from '../constants/api_constants';
import {aicc_regex} from '../regex';
import {aicc_regex} from '../constants/regex';
import {scorm12_error_codes} from '../constants/error_codes';
import {
check12ValidFormat,

View File

@@ -2,7 +2,7 @@
import {scorm12_constants} from '../constants/api_constants';
import {scorm12_error_codes} from '../constants/error_codes';
import {ValidationError} from '../exceptions';
import {scorm12_regex} from '../regex';
import {scorm12_regex} from '../constants/regex';
/**
* Check if the value matches the proper format. If not, throw proper error code.
@@ -10,12 +10,20 @@ import {scorm12_regex} from '../regex';
* @param {string} value
* @param {string} regexPattern
* @param {number} errorCode
* @param {boolean} allowEmptyString
* @return {boolean}
*/
export function checkValidFormat(
value: String, regexPattern: String, errorCode: number) {
value: String,
regexPattern: String,
errorCode: number,
allowEmptyString?: boolean) {
const formatRegex = new RegExp(regexPattern);
if (!value || !value.match(formatRegex)) {
const matches = value.match(formatRegex);
if (allowEmptyString && value === '') {
return true;
}
if (value === undefined || !matches || matches[0] === '') {
throw new ValidationError(errorCode);
}
return true;
@@ -97,7 +105,7 @@ export class CMIScore extends BaseCMI {
score_children :
scorm12_constants.score_children;
this.#_score_range = !score_range ? false : scorm12_regex.score_range;
this.#max = max ? max : '100';
this.#max = (max || max === '') ? max : '100';
this.#_invalid_error_code = invalidErrorCode ?
invalidErrorCode :
scorm12_error_codes.INVALID_SET_VALUE;

View File

@@ -8,7 +8,7 @@ import {
} from './common';
import {scorm12_constants} from '../constants/api_constants';
import {scorm12_error_codes} from '../constants/error_codes';
import {scorm12_regex} from '../regex';
import {scorm12_regex} from '../constants/regex';
import {ValidationError} from '../exceptions';
const constants = scorm12_constants;

View File

@@ -7,7 +7,7 @@ import {
CMIScore,
} from './common';
import {scorm2004_constants} from '../constants/api_constants';
import {scorm2004_regex} from '../regex';
import {scorm2004_regex} from '../constants/regex';
import {scorm2004_error_codes} from '../constants/error_codes';
import {learner_responses} from '../constants/response_constants';
import {ValidationError} from '../exceptions';
@@ -40,11 +40,15 @@ function throwTypeMismatchError() {
* Helper method, no reason to have to pass the same error codes every time
* @param {*} value
* @param {string} regexPattern
* @param {boolean} allowEmptyString
* @return {boolean}
*/
function check2004ValidFormat(value: String, regexPattern: String) {
function check2004ValidFormat(
value: String,
regexPattern: String,
allowEmptyString?: boolean) {
return checkValidFormat(value, regexPattern,
scorm2004_error_codes.TYPE_MISMATCH);
scorm2004_error_codes.TYPE_MISMATCH, allowEmptyString);
}
/**
@@ -943,7 +947,7 @@ export class CMIInteractionsObject extends BaseCMI {
* @param {string} description
*/
set description(description) {
if (check2004ValidFormat(description, regex.CMILangString250)) {
if (check2004ValidFormat(description, regex.CMILangString250, true)) {
this.#description = description;
}
}
@@ -1098,7 +1102,7 @@ export class CMIObjectivesObject extends BaseCMI {
* @param {string} description
*/
set description(description) {
if (check2004ValidFormat(description, regex.CMILangString250)) {
if (check2004ValidFormat(description, regex.CMILangString250, true)) {
this.#description = description;
}
}
@@ -1112,7 +1116,8 @@ export class CMIObjectivesObject extends BaseCMI {
* success_status: string,
* completion_status: string,
* progress_measure: string,
* description: string
* description: string,
* score: Scorm2004CMIScore
* }
* }
*/
@@ -1124,6 +1129,7 @@ export class CMIObjectivesObject extends BaseCMI {
'completion_status': this.completion_status,
'progress_measure': this.progress_measure,
'description': this.description,
'score': this.score,
};
delete this.jsonString;
return result;
@@ -1186,9 +1192,9 @@ class Scorm2004CMIScore extends CMIScore {
this.jsonString = true;
const result = {
'scaled': this.scaled,
'raw': this.raw,
'min': this.min,
'max': this.max,
'raw': super.raw,
'min': super.min,
'max': super.max,
};
delete this.jsonString;
return result;
@@ -1208,6 +1214,9 @@ export class CMICommentsFromLearnerObject extends BaseCMI {
*/
constructor() {
super();
this.#comment = '';
this.#location = '';
this.#timestamp = '';
}
/**
@@ -1223,7 +1232,7 @@ export class CMICommentsFromLearnerObject extends BaseCMI {
* @param {string} comment
*/
set comment(comment) {
if (check2004ValidFormat(comment, regex.CMILangString4000)) {
if (check2004ValidFormat(comment, regex.CMILangString4000, true)) {
this.#comment = comment;
}
}
@@ -1263,6 +1272,27 @@ export class CMICommentsFromLearnerObject extends BaseCMI {
this.#timestamp = timestamp;
}
}
/**
* toJSON for cmi.comments_from_learner.n object
* @return {
* {
* comment: string,
* location: string,
* timestamp: string
* }
* }
*/
toJSON() {
this.jsonString = true;
const result = {
'comment': this.comment,
'location': this.location,
'timestamp': this.timestamp,
};
delete this.jsonString;
return result;
}
}
/**
@@ -1276,12 +1306,28 @@ export class CMICommentsFromLMSObject extends CMICommentsFromLearnerObject {
super();
}
/**
* Getter for #comment
* @return {string}
*/
get comment() {
return super.comment;
}
/**
* Setter for #comment. Can only be called before initialization.
* @param {string} comment
*/
set comment(comment) {
!this.initialized ? this.comment = comment : throwReadOnlyError();
!this.initialized ? super.comment = comment : throwReadOnlyError();
}
/**
* Getter for #location
* @return {string}
*/
get location() {
return super.location;
}
/**
@@ -1289,7 +1335,15 @@ export class CMICommentsFromLMSObject extends CMICommentsFromLearnerObject {
* @param {string} location
*/
set location(location) {
!this.initialized ? this.location = location : throwReadOnlyError();
!this.initialized ? super.location = location : throwReadOnlyError();
}
/**
* Getter for #timestamp
* @return {string}
*/
get timestamp() {
return super.timestamp;
}
/**
@@ -1297,7 +1351,21 @@ export class CMICommentsFromLMSObject extends CMICommentsFromLearnerObject {
* @param {string} timestamp
*/
set timestamp(timestamp) {
!this.initialized ? this.timestamp = timestamp : throwReadOnlyError();
!this.initialized ? super.timestamp = timestamp : throwReadOnlyError();
}
/**
* toJSON for cmi.comments_from_lms.n
* @return {
* {
* comment: string,
* location: string,
* timestamp: string
* }
* }
*/
toJSON() {
return super.toJSON();
}
}
@@ -1331,6 +1399,23 @@ export class CMIInteractionsObjectivesObject extends BaseCMI {
this.#id = id;
}
}
/**
* toJSON for cmi.interactions.n.objectives.n
* @return {
* {
* id: string
* }
* }
*/
toJSON() {
this.jsonString = true;
const result = {
'id': this.id,
};
delete this.jsonString;
return result;
}
}
/**
@@ -1363,6 +1448,23 @@ export class CMIInteractionsCorrectResponsesObject extends BaseCMI {
this.#pattern = pattern;
}
}
/**
* toJSON cmi.interactions.n.correct_responses.n object
* @return {
* {
* pattern: string
* }
* }
*/
toJSON() {
this.jsonString = true;
const result = {
'pattern': this.pattern,
};
delete this.jsonString;
return result;
}
}
/**

View File

@@ -0,0 +1,334 @@
const common_values = {
validResult: [
'correct',
'wrong',
'unanticipated',
'neutral',
],
invalidResult: [
'-10000',
'10000',
'invalid',
],
valid0To1Range: [
'0.0',
'0.25',
'0.5',
'1.0',
],
invalid0To1Range: [
'-1',
'-0.1',
'1.1',
'.25',
],
valid0To100Range: [
'1',
'50',
'100',
],
invalid0To100Range: [
'invalid',
'a100',
'-1',
],
validScaledRange: [
'1',
'0.5',
'0',
'-0.5',
'-1',
],
invalidScaledRange: [
'-101',
'25.1',
'50.5',
'75',
'100',
],
validIntegerScaledRange: [
'1',
'0',
'-1',
],
invalidIntegerScaledRange: [
'-101',
'-0.5',
'0.5',
'25.1',
'50.5',
'75',
'100',
],
};
export const scorm12_values = {
...common_values, ...{
validLessonStatus: [
'passed',
'completed',
'failed',
'incomplete',
'browsed',
],
invalidLessonStatus: [
'Passed',
'P',
'F',
'p',
'true',
'false',
'complete',
],
validExit: [
'time-out',
'suspend',
'logout',
],
invalidExit: [
'close',
'exit',
'crash',
],
validType: [
'true-false',
'choice',
'fill-in',
'matching',
'performance',
'sequencing',
'likert',
'numeric',
],
invalidType: [
'correct',
'wrong',
'logout',
],
validSpeedRange: [
'1',
'50',
'100',
'-1',
'-50',
'-100',
],
invalidSpeedRange: [
'invalid',
'a100',
'-101',
'101',
'-100000',
'100000',
],
validScoreRange: [
'1',
'50.25',
'100',
],
invalidScoreRange: [
'invalid',
'a100',
'-1',
'101',
'-100000',
'100000',
],
invalid0To100Range: [
'invalid',
'a100',
'-2',
],
validTime: [
'10:06:57',
'23:59:59',
'00:00:00',
],
invalidTime: [
'47:59:59',
'00:00:01.56',
'06:5:13',
'23:59:59.123',
'P1DT23H59M59S',
],
validTimestamp: [
'10:06:57',
'00:00:01.56',
'23:59:59',
'47:59:59',
],
invalidTimestamp: [
'06:5:13',
'23:59:59.123',
'P1DT23H59M59S',
],
},
};
export const scorm2004_values = {
...common_values, ...{
// valid field values
validTimestamps: [
'2019-06-25',
'2019-06-25T23:59',
'2019-06-25T23:59:59.99',
'1970-01-01',
],
invalidTimestamps: [
'2019-06-25T',
'2019-06-25T23:59:59.999',
'2019-06-25T25:59:59.99',
'2019-13-31',
'1969-12-31',
'-00:00:30',
'0:50:30',
'23:00:30.',
],
validCStatus: [
'completed',
'incomplete',
'not attempted',
'unknown',
],
invalidCStatus: [
'complete',
'passed',
'failed',
],
validSStatus: [
'passed',
'failed',
'unknown',
],
invalidSStatus: [
'complete',
'incomplete',
'P',
'f',
],
validExit: [
'time-out',
'suspend',
'logout',
'normal',
],
invalidExit: [
'close',
'exit',
'crash',
],
validType: [
'true-false',
'choice',
'fill-in',
'long-fill-in',
'matching',
'performance',
'sequencing',
'likert',
'numeric',
'other',
],
invalidType: [
'correct',
'wrong',
'logout',
],
validScoreRange: [
'1',
'50',
'100',
'-10000',
'-1',
'10000',
],
invalidScoreRange: [
'invalid',
'a100',
'-100000',
'100000',
],
validISO8601Durations: [
'P1Y34DT23H45M15S',
'PT1M45S',
'P0S',
'PT75M',
],
invalidISO8601Durations: [
'00:08:45',
'-P1H',
'1y45D',
'0',
],
validComment: [
'{lang=en-98} learner comment',
'{lang=eng-98-9} learner comment',
'{lang=eng-98-9fhgj}' + 'x'.repeat(4000),
'learner comment',
'learner comment}',
'{lang=i-xx}',
'{lang=i}',
'',
],
invalidComment: [
'{lang=i-}',
'{lang=i-x}',
'{lang=eng-98-9fhgj}{ learner comment',
'{learner comment',
'{lang=eng-98-9fhgj}' + 'x'.repeat(4001),
'{lang=eng-98-9fhgj}{' + 'x'.repeat(3999),
],
validDescription: [
'{lang=en-98} learner comment',
'{lang=eng-98-9} learner comment',
'{lang=eng-98-9fhgj}' + 'x'.repeat(250),
'learner comment',
'learner comment}',
'{lang=i-xx}',
'{lang=i}',
'',
],
invalidDescription: [
'{lang=i-}',
'{lang=i-x}',
'{lang=eng-98-9fhgj}{ learner comment',
'{learner comment',
'{lang=eng-98-9fhgj}' + 'x'.repeat(251),
'{lang=eng-98-9fhgj}{' + 'x'.repeat(249),
],
validNavRequest: [
'previous',
'continue',
'exit',
'exitAll',
'abandon',
'abandonAll',
'suspendAll',
],
invalidNavRequest: [
'close',
'quit',
'next',
'before',
],
},
};

View File

@@ -1,5 +1,7 @@
// @flow
import {scorm12_values, scorm2004_values} from './field_values';
export const scorm12_regex = {
CMIString256: '^.{0,255}$',
CMIString4096: '^.{0,4096}$',
@@ -13,11 +15,11 @@ export const scorm12_regex = {
CMIIndex: '[._](\\d+).',
// Vocabulary Data Type Definition
CMIStatus: '^(passed|completed|failed|incomplete|browsed)$',
CMIStatus2: '^(passed|completed|failed|incomplete|browsed|not attempted)$',
CMIExit: '^(time-out|suspend|logout|)$',
CMIType: '^(true-false|choice|fill-in|matching|performance|sequencing|likert|numeric)$',
CMIResult: '^(correct|wrong|unanticipated|neutral|([0-9]{0,3})?(\\.[0-9]*)?)$', // eslint-disable-line
CMIStatus: '^(' + scorm12_values.validLessonStatus.join('|') + ')$',
CMIStatus2: '^(' + scorm12_values.validLessonStatus.join('|') + '|not attempted)$',
CMIExit: '^(' + scorm12_values.validExit.join('|') + '|)$',
CMIType: '^(' + scorm12_values.validType.join('|') + ')$',
CMIResult: '^(' + scorm12_values.validResult.join('|') + '|([0-9]{0,3})?(\\.[0-9]*)?)$', // eslint-disable-line
NAVEvent: '^(previous|continue)$',
// Data ranges
@@ -41,10 +43,10 @@ export const scorm2004_regex = {
CMIString4000: '^[\\u0000-\\uFFFF]{0,4000}$',
CMIString64000: '^[\\u0000-\\uFFFF]{0,64000}$',
CMILang: '^([a-zA-Z]{2,3}|i|x)(\-[a-zA-Z0-9\-]{2,8})?$|^$', // eslint-disable-line
CMILangString250: '^(\{lang=([a-zA-Z]{2,3}|i|x)(\-[a-zA-Z0-9\-]{2,8})?\})?([^\{].{0,250}$)?', // eslint-disable-line
CMILangString250: '^(\{lang=([a-zA-Z]{2,3}|i|x)(\-[a-zA-Z0-9\-]{2,8})?\})?((?!\{.*$).{0,250}$)?$', // eslint-disable-line
CMILangcr: '^((\{lang=([a-zA-Z]{2,3}|i|x)?(\-[a-zA-Z0-9\-]{2,8})?\}))(.*?)$', // eslint-disable-line
CMILangString250cr: '^((\{lang=([a-zA-Z]{2,3}|i|x)?(\-[a-zA-Z0-9\-]{2,8})?\})?(.{0,250})?)?$', // eslint-disable-line
CMILangString4000: '^(\{lang=([a-zA-Z]{2,3}|i|x)(\-[a-zA-Z0-9\-]{2,8})?\})?([^\{].{0,4000}$)?', // eslint-disable-line
CMILangString4000: '^(\{lang=([a-zA-Z]{2,3}|i|x)(\-[a-zA-Z0-9\-]{2,8})?\})?((?!\{.*$).{0,4000}$)?$', // eslint-disable-line
CMITime: '^(19[7-9]{1}[0-9]{1}|20[0-2]{1}[0-9]{1}|203[0-8]{1})((-(0[1-9]{1}|1[0-2]{1}))((-(0[1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]{1}))(T([0-1]{1}[0-9]{1}|2[0-3]{1})((:[0-5]{1}[0-9]{1})((:[0-5]{1}[0-9]{1})((\\.[0-9]{1,2})((Z|([+|-]([0-1]{1}[0-9]{1}|2[0-3]{1})))(:[0-5]{1}[0-9]{1})?)?)?)?)?)?)?)?$',
CMITimespan: '^P(?:([.,\\d]+)Y)?(?:([.,\\d]+)M)?(?:([.,\\d]+)W)?(?:([.,\\d]+)D)?(?:T?(?:([.,\\d]+)H)?(?:([.,\\d]+)M)?(?:([.,\\d]+)S)?)?$',
CMIInteger: '^\\d+$',
@@ -58,12 +60,12 @@ export const scorm2004_regex = {
CMIIndexStore: '.N(\\d+).',
// Vocabulary Data Type Definition
CMICStatus: '^(completed|incomplete|not attempted|unknown)$',
CMISStatus: '^(passed|failed|unknown)$',
CMIExit: '^(time-out|suspend|logout|normal|)$',
CMIType: '^(true-false|choice|(long-)?fill-in|matching|performance|sequencing|likert|numeric|other)$',
CMIResult: '^(correct|incorrect|unanticipated|neutral|-?([0-9]{1,4})(\\.[0-9]{1,18})?)$',
NAVEvent: '^(previous|continue|exit|exitAll|abandon|abandonAll|suspendAll|\{target=\\S{0,200}[a-zA-Z0-9]\}choice|jump)$', // eslint-disable-line
CMICStatus: '^(' + scorm2004_values.validCStatus.join('|') + ')$',
CMISStatus: '^(' + scorm2004_values.validSStatus.join('|') + ')$',
CMIExit: '^(' + scorm2004_values.validExit.join('|') + ')$',
CMIType: '^(' + scorm2004_values.validType.join('|') + ')$',
CMIResult: '^(' + scorm2004_values.validResult.join('|') + '|-?([0-9]{1,4})(\\.[0-9]{1,18})?)$',
NAVEvent: '^(' + scorm2004_values.validNavRequest.join('|') + '|\{target=\\S{0,200}[a-zA-Z0-9]\}choice|jump)$', // eslint-disable-line
NAVBoolean: '^(unknown|true|false$)',
NAVTarget: '^(previous|continue|choice.{target=\\S{0,200}[a-zA-Z0-9]})$',

View File

@@ -1,5 +1,5 @@
// @flow
import {scorm2004_regex} from '../regex';
import {scorm2004_regex} from './regex';
export const learner_responses = {
'true-false': {