All files / ext.wikilambda.edit/components/default-view-types ZTester.vue

97.95% Statements 48/49
88.88% Branches 16/18
100% Functions 16/16
97.95% Lines 48/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 28624x 24x 24x 24x     24x                                             125x               114x               125x                     125x               116x               116x               114x               114x               40x   40x         40x         40x 40x     40x     40x                     40x 40x 40x   1x     1x       1x   39x       39x         39x                 65x 37x           12x     12x 3x       24x   24x     24x 24x 24x     24x       24x     24x       24x     24x       24x       116x 116x                                     24x                                                                                                                          
<!--
	WikiLambda Vue component for Z20/Tester objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-tester">
		<!-- Function selection block -->
		<div
			class="ext-wikilambda-tester-function"
			data-testid="z-test-function-select-container"
		>
			<div class="ext-wikilambda-key-block">
				<label>{{ functionLabel }}</label>
			</div>
			<wl-z-object-key-value
				:key="functionRowId"
				:row-id="functionRowId"
				:skip-key="true"
				:skip-indent="true"
				:edit="edit"
			></wl-z-object-key-value>
		</div>
		<!-- Tester call block -->
		<div
			class="ext-wikilambda-tester-content"
			role="ext-wikilambda-tester-call"
		>
			<div class="ext-wikilambda-key-block">
				<label>{{ testerCallLabel }}</label>
			</div>
			<wl-z-object-key-value
				:key="testerCallRowId"
				:skip-key="true"
				:row-id="testerCallRowId"
				:error-id="testerCallRowId"
				:edit="edit"
			></wl-z-object-key-value>
		</div>
		<!-- Tester result validation block -->
		<div
			class="ext-wikilambda-tester-content"
			role="ext-wikilambda-tester-validation"
		>
			<div class="ext-wikilambda-key-block">
				<label>{{ testerValidationLabel }}</label>
			</div>
			<wl-z-object-key-value
				:key="testerValidationRowId"
				:skip-key="true"
				:row-id="testerValidationRowId"
				:error-id="testerValidationRowId"
				:edit="edit"
			></wl-z-object-key-value>
		</div>
	</div>
</template>
 
<script>
const Constants = require( '../../Constants.js' ),
	isValidZidFormat = require( '../../mixins/typeUtils.js' ).methods.isValidZidFormat,
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters;
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-tester',
	components: {
		// Leave components as an empty object to add the ZObjectKeyValue later
	},
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		}
	},
	computed: $.extend( mapGetters( [
		'createObjectByType',
		'getLabel',
		'getZTesterFunctionRowId',
		'getZTesterCallRowId',
		'getZTesterValidationRowId',
		'getZReferenceTerminalValue',
		'isCreateNewPage',
		'getStoredObject'
	] ), {
		/**
		 * Returns the row Id of the target function key: Z20K1
		 *
		 * @return {number|undefined}
		 */E
		functionRowId: function () {
			return this.getZTesterFunctionRowId( this.rowId );
		},
 
		/**
		 * Returns the human readable label for "Function"
		 *
		 * @return {string}
		 */
		functionLabel: function () {
			return this.getLabel( Constants.Z_TESTER_FUNCTION );
		},
I
		/**
		 * Returns the Zid of the selected function/Z20K1
		 *
		 * @return {string}
		 */
		functionZid: function () {
			return this.getZReferenceTerminalValue( this.functionRowId );
		},
 
		/**
		 * Returns the stored function object given the selected function Zid.
		 * This computed property is needed so that it can be observed and
		 * update the test call/Z20K2 with the appropriate function call and
		 * its arguments. Before it's fetched it will return undefined
		 *
		 * @return {Object|undefined}
		 */
		storedFunction: function () {
			return this.getStoredObject( this.functionZid );
		},
 
		/**
		 * Returns the row Id of the tester call: Z20K2
		 *
		 * @return {number|undefined}
		 */
		testerCallRowId: function () {
			return this.getZTesterCallRowId( this.rowId );
		},
 
		/**
		 * Returns the row Id of the tester validation function call: Z20K3
		 *
		 * @return {number|undefined}
		 */
		testerValidationRowId: function () {
			return this.getZTesterValidationRowId( this.rowId );
		},
 
		/**
		 * Returns the human readable label for the tester call
		 *
		 * @return {string}
		 */
		testerCallLabel: function () {
			return this.getLabel( Constants.Z_TESTER_CALL );
		},
 
		/**
		 * Returns the human readable label for the tester validation
		 *
		 * @return {string}
		 */
		testerValidationLabel: function () {
			return this.getLabel( Constants.Z_TESTER_VALIDATION );
		}
	} ),
	methods: $.extend( mapActions( [
		'fetchZids',
		'setZFunctionCallArguments'
	] ), {
		/**
		 * Initializes Test call/Z20K2 with a function call to the given functionZid
		 */
		initializeTestCall: function () {
			if ( this.isCreateNewPage && !!this.functionZid ) {
				// Set test call function call Zid
				this.$emit( 'set-value', {
					keyPath: [
						Constants.Z_TESTER_CALL,
						Constants.Z_FUNCTION_CALL_FUNCTION,
						Constants.Z_REFERENCE_ID
					],
					value: this.functionZid
				} );
				// Set test call function arguments
				this.setZFunctionCallArguments( {
					parentId: this.testerCallRowId,
					functionZid: this.functionZid
				} );
				// Get function output type and dismiss anything that's not a reference
				const outputType = this.storedFunction[ Constants.Z_PERSISTENTOBJECT_VALUE ][
					Constants.Z_FUNCTION_RETURN_TYPE ];
				if ( !outputType || ( typeof outputType !== 'string' ) || !isValidZidFormat( outputType ) ) {
					return;
				}
				this.fetchZids( { zids: [ outputType ] } ).then( () => {
					this.initializeTestValidation( outputType );
				} );
			}
		},
		/**
		 * Initializes Test validator/Z20K3 with a function call to the equality function
		 * of the type returned by the Test call/Z20K2, which is passed as input parameter.
		 *
		 * @param {string} outputType
		 */
		initializeTestValidation: function ( outputType ) {
			const type = this.getStoredObject( outputType );
			const equalityZid = type[ Constants.Z_PERSISTENTOBJECT_VALUE ][ Constants.Z_TYPE_EQUALITY ];
			if ( !equalityZid || ( typeof equalityZid !== 'string' ) || !isValidZidFormat( equalityZid ) ) {
				// Set to blank Function Call if the new test call output doesn't have an equality function
				const blankFunctionCall = this.createObjectByType( { type: Constants.Z_FUNCTION_CALL } );
				this.$emit( 'set-value', {
					keyPath: [ Constants.Z_TESTER_VALIDATION ],
					value: blankFunctionCall
				} );
				return;
			}
			this.fetchZids( { zids: [ equalityZid ] } ).then( () => {
				// Set test validation function call Zid
				this.$emit( 'set-value', {
					keyPath: [
						Constants.Z_TESTER_VALIDATION,
						Constants.Z_FUNCTION_CALL_FUNCTION,
						Constants.Z_REFERENCE_ID
					],
					value: equalityZid
				} );
				// Set tester validation function arguments
				this.setZFunctionCallArguments( {
					parentId: this.testerValidationRowId,
					functionZid: equalityZid
				} );
			} );
		}
	} ),
	watch: {
		storedFunction: function ( newFunction ) {
			if ( newFunction ) {
				this.initializeTestCall();
			}
		}
	},
	beforeCreate: function () {
		// Need to delay require of ZObjectKeyValue to avoid loop
		this.$options.components[ 'wl-z-object-key-value' ] = require( './ZObjectKeyValue.vue' );
	},
	mounted: function () {
		if ( this.storedFunction ) {
			this.initializeTestCall();
		}
	}
};
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-tester {
	.ext-wikilambda-tester-content {
		padding-top: @spacing-75;
 
		> .ext-wikilambda-key-block {
			margin-bottom: 0;
 
			label {
				font-weight: bold;
				color: @color-base;
			}
		}
	}
 
	.ext-wikilambda-tester-function {
		.ext-wikilambda-key-block {
			margin-bottom: 0;
 
			label {
				font-weight: bold;
				color: @color-base;
			}
		}
	}
}
</style>