More CMI tests

This commit is contained in:
Jonathan Putney
2019-11-12 21:41:39 -05:00
parent b427943036
commit d2cc923f94
6 changed files with 793 additions and 68 deletions

View File

@@ -3,10 +3,11 @@ import {BaseCMI, CMIArray, CMIScore} from './common';
import {aicc_constants} from '../constants/api_constants';
import {aicc_regex} from '../regex';
import {scorm12_error_codes} from '../constants/error_codes';
import {ValidationError} from '../exceptions';
import {check12ValidFormat} from './scorm12_cmi';
import {throwReadOnlyError} from './scorm12_cmi';
import {throwWriteOnlyError} from './scorm12_cmi';
import {
check12ValidFormat,
throwReadOnlyError,
throwWriteOnlyError,
} from './scorm12_cmi';
const constants = aicc_constants;
const regex = aicc_regex;
@@ -36,6 +37,41 @@ export class CMI extends Scorm12CMI.CMI {
this.student_data?.initialize();
this.evaluation?.initialize();
}
/**
* toJSON for cmi
*
* @return {
* {
* suspend_data: string,
* launch_data: string,
* comments: string,
* comments_from_lms: string,
* core: CMICore,
* objectives: CMIObjectives,
* student_data: CMIStudentData,
* student_preference: CMIStudentPreference,
* interactions: CMIInteractions
* }
* }
*/
toJSON() {
this.jsonString = true;
const result = {
'suspend_data': this.suspend_data,
'launch_data': this.launch_data,
'comments': this.comments,
'comments_from_lms': this.comments_from_lms,
'core': this.core,
'objectives': this.objectives,
'student_data': this.student_data,
'student_preference': this.student_preference,
'interactions': this.interactions,
'evaluation': this.evaluation,
};
delete this.jsonString;
return result;
}
}
/**
@@ -58,6 +94,19 @@ class CMIEvaluation extends BaseCMI {
super.initialize();
this.comments?.initialize();
}
/**
* toJSON for cmi.evaluation object
* @return {{comments: CMIEvaluationComments}}
*/
toJSON() {
this.jsonString = true;
const result = {
'comments': this.comments,
};
delete this.jsonString;
return result;
}
}
/**
@@ -114,6 +163,29 @@ class AICCCMIStudentData extends Scorm12CMI.CMIStudentData {
this.#tries_during_lesson = tries_during_lesson :
throwReadOnlyError();
}
/**
* toJSON for cmi.student_data object
* @return {
* {
* mastery_score: string,
* max_time_allowed: string,
* time_limit_action: string,
* tries: CMITries
* }
* }
*/
toJSON() {
this.jsonString = true;
const result = {
'mastery_score': this.mastery_score,
'max_time_allowed': this.max_time_allowed,
'time_limit_action': this.time_limit_action,
'tries': this.tries,
};
delete this.jsonString;
return result;
}
}
/**
@@ -138,7 +210,14 @@ export class CMITriesObject extends BaseCMI {
constructor() {
super();
this.score = new CMIScore();
this.score = new CMIScore(
{
score_children: constants.score_children,
score_range: regex.score_range,
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
});
}
/**
@@ -187,6 +266,27 @@ export class CMITriesObject extends BaseCMI {
this.#time = time;
}
}
/**
* toJSON for cmi.student_data.tries.n object
* @return {
* {
* status: string,
* time: string,
* score: CMIScore
* }
* }
*/
toJSON() {
this.jsonString = true;
const result = {
'status': this.status,
'time': this.time,
'score': this.score,
};
delete this.jsonString;
return result;
}
}
/**
@@ -257,6 +357,27 @@ export class CMIEvaluationCommentsObject extends BaseCMI {
this.#time = time;
}
}
/**
* toJSON for cmi.evaulation.comments.n object
* @return {
* {
* content: string,
* location: string,
* time: string
* }
* }
*/
toJSON() {
this.jsonString = true;
const result = {
'content': this.content,
'location': this.location,
'time': this.time,
};
delete this.jsonString;
return result;
}
}
/**
@@ -289,4 +410,21 @@ export class NAV extends BaseCMI {
this.#event = event;
}
}
/**
* toJSON for nav object
* @returns {
* {
* event: string
* }
* }
*/
toJSON() {
this.jsonString = true;
const result = {
'event': this.event,
};
delete this.jsonString;
return result;
}
}

View File

@@ -96,7 +96,7 @@ export class CMIScore extends BaseCMI {
this.#_children = score_children ?
score_children :
scorm12_constants.score_children;
this.#_score_range = score_range ? score_range : false;
this.#_score_range = !score_range ? false : scorm12_regex.score_range;
this.#max = max ? max : '100';
this.#_invalid_error_code = invalidErrorCode ?
invalidErrorCode :

View File

@@ -236,7 +236,9 @@ export class CMI extends BaseCMI {
* @param {string} comments_from_lms
*/
set comments_from_lms(comments_from_lms) {
!this.initialized ? this.#comments_from_lms = comments_from_lms : throwReadOnlyError();
!this.initialized ?
this.#comments_from_lms = comments_from_lms :
throwReadOnlyError();
}
}
@@ -250,7 +252,7 @@ class CMICore extends BaseCMI {
constructor() {
super();
this.score = new Scorm12CMIScore(
this.score = new CMIScore(
{
score_children: constants.score_children,
score_range: regex.score_range,
@@ -327,7 +329,9 @@ class CMICore extends BaseCMI {
* @param {string} student_name
*/
set student_name(student_name) {
!this.initialized ? this.#student_name = student_name : throwReadOnlyError();
!this.initialized ?
this.#student_name = student_name :
throwReadOnlyError();
}
/**
@@ -474,7 +478,7 @@ class CMICore extends BaseCMI {
* student_name: string,
* entry: string,
* exit: string,
* score: Scorm12CMIScore,
* score: CMIScore,
* student_id: string,
* lesson_mode: string,
* lesson_location: string,
@@ -572,7 +576,9 @@ export class CMIStudentData extends BaseCMI {
* @param {string} mastery_score
*/
set mastery_score(mastery_score) {
!this.initialized ? this.#mastery_score = mastery_score : throwReadOnlyError();
!this.initialized ?
this.#mastery_score = mastery_score :
throwReadOnlyError();
}
/**
@@ -588,7 +594,9 @@ export class CMIStudentData extends BaseCMI {
* @param {string} max_time_allowed
*/
set max_time_allowed(max_time_allowed) {
!this.initialized ? this.#max_time_allowed = max_time_allowed : throwReadOnlyError();
!this.initialized ?
this.#max_time_allowed = max_time_allowed :
throwReadOnlyError();
}
/**
@@ -604,7 +612,9 @@ export class CMIStudentData extends BaseCMI {
* @param {string} time_limit_action
*/
set time_limit_action(time_limit_action) {
!this.initialized ? this.#time_limit_action = time_limit_action : throwReadOnlyError();
!this.initialized ?
this.#time_limit_action = time_limit_action :
throwReadOnlyError();
}
/**
@@ -809,13 +819,13 @@ export class CMIInteractionsObject extends BaseCMI {
this.correct_responses?.initialize();
}
#id: '';
#time: '';
#type: '';
#weighting: '';
#student_response: '';
#result: '';
#latency: '';
#id = '';
#time = '';
#type = '';
#weighting = '';
#student_response = '';
#result = '';
#latency = '';
/**
* Getter for #id. Should only be called during JSON export.
@@ -991,11 +1001,18 @@ export class CMIObjectivesObject extends BaseCMI {
constructor() {
super();
this.score = new Scorm12CMIScore();
this.score = new CMIScore(
{
score_children: constants.score_children,
score_range: regex.score_range,
invalidErrorCode: scorm12_error_codes.INVALID_SET_VALUE,
invalidTypeCode: scorm12_error_codes.TYPE_MISMATCH,
invalidRangeCode: scorm12_error_codes.VALUE_OUT_OF_RANGE,
});
}
#id: '';
#status: '';
#id = '';
#status = '';
/**
* Getter for #id
@@ -1039,7 +1056,7 @@ export class CMIObjectivesObject extends BaseCMI {
* {
* id: string,
* status: string,
* score: Scorm12CMIScore
* score: CMIScore
* }
* }
*/
@@ -1066,7 +1083,7 @@ export class CMIInteractionsObjectivesObject extends BaseCMI {
super();
}
#id: '';
#id = '';
/**
* Getter for #id
@@ -1115,14 +1132,14 @@ export class CMIInteractionsCorrectResponsesObject extends BaseCMI {
super();
}
#pattern: '';
#pattern = '';
/**
* Getter for #pattern
* @return {""}
* @return {string}
*/
get pattern() {
return this.#pattern;
return (!this.jsonString) ? throwWriteOnlyError() : this.#pattern;
}
/**
@@ -1152,10 +1169,3 @@ export class CMIInteractionsCorrectResponsesObject extends BaseCMI {
return result;
}
}
/**
* Basic extension of CMIScore
*/
class Scorm12CMIScore extends CMIScore {
toJSON = super.toJSON;
}

View File

@@ -3,7 +3,7 @@
export const scorm12_regex = {
CMIString256: '^.{0,255}$',
CMIString4096: '^.{0,4096}$',
CMITime: '^([0-2]{1}[0-9]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{1,6})?$', // eslint-disable-line
CMITime: '^(?:[01]\\d|2[0123]):(?:[012345]\\d):(?:[012345]\\d)$', // eslint-disable-line
CMITimespan: '^([0-9]{2,}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,2})?$', // eslint-disable-line
CMIInteger: '^\\d+$',
CMISInteger: '^-?([0-9]+)$',
@@ -13,12 +13,12 @@ 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
NAVEvent: '^previous$|^continue$',
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
NAVEvent: '^(previous|continue)$',
// Data ranges
score_range: '0#100',
@@ -58,14 +58,14 @@ 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
NAVBoolean: '^unknown$|^true$|^false$',
NAVTarget: '^previous$|^continue$|^choice.{target=\\S{0,200}[a-zA-Z0-9]}$',
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
NAVBoolean: '^(unknown|true|false$)',
NAVTarget: '^(previous|continue|choice.{target=\\S{0,200}[a-zA-Z0-9]})$',
// Data ranges
scaled_range: '-1#1',