diff options
author | Mike Gerwitz <mike.gerwitz@rtspecialty.com> | 2018-02-16 12:19:37 -0500 |
---|---|---|
committer | Mike Gerwitz <mike.gerwitz@rtspecialty.com> | 2018-02-19 15:21:14 -0500 |
commit | 47f0f4039b9c6d4a9898714dcca4692fd8af154a (patch) | |
tree | 2b92cae5fa2c0e57b474a4652aee2da7a7358513 /progtest/src/reader | |
parent | 0c020b736d8d46f18e2e671d7b928e146db5572e (diff) | |
download | tame-47f0f4039b9c6d4a9898714dcca4692fd8af154a.tar.gz tame-47f0f4039b9c6d4a9898714dcca4692fd8af154a.tar.bz2 tame-47f0f4039b9c6d4a9898714dcca4692fd8af154a.zip |
progtest: Initial working console runner
Diffstat (limited to 'progtest/src/reader')
-rw-r--r-- | progtest/src/reader/ConstResolver.js | 110 | ||||
-rw-r--r-- | progtest/src/reader/DateResolver.js | 61 | ||||
-rw-r--r-- | progtest/src/reader/TestReader.js | 40 | ||||
-rw-r--r-- | progtest/src/reader/YamlTestReader.js | 79 |
4 files changed, 290 insertions, 0 deletions
diff --git a/progtest/src/reader/ConstResolver.js b/progtest/src/reader/ConstResolver.js new file mode 100644 index 0000000..b8861f6 --- /dev/null +++ b/progtest/src/reader/ConstResolver.js @@ -0,0 +1,110 @@ +/** + * Constant resolver for test case reader + * + * Copyright (C) 2018 R-T Specialty, LLC. + * + * This file is part of TAME. + * + * TAME is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +"use strict"; + + +const { Trait } = require( 'easejs' ); +const TestReader = require( './TestReader' ); + + +/** + * Resolve program constants by replacing them with their numeric value + * + * The result is a loaded YAML file that contains only numeric input. All + * non-numeric input is interpreted as a constant. + * + * Any non-numeric value is considered to be a constant, so it is important + * to perform all other data transformations before applying constant + * resolution. + */ +module.exports = Trait( 'ConstResolver' ) + .implement( TestReader ) + .extend( +{ + /** + * Program from which to load constants + * + * @type {Program} + */ + 'private _program': null, + + + /** + * Initialize with program from which to load constants + * + * @param {Program} program source program + */ + __mixin( program ) + { + this._program = program; + }, + + + /** + * Load test cases and resolve constants + * + * @param {*} src data source + * + * @return {Array<TestCase>} array of test cases + */ + 'abstract override public loadCases'( yaml ) + { + const data = this.__super( yaml ); + const { consts } = this._program.rater; + + return data.map( + testcase => testcase.mapEachValue( + value => this._resolve( value, consts ) + ) + ); + }, + + + /** + * Resolve constant + * + * If the constant is not known (via CONSTS), an error is thrown. + * + * @param {number|Array} input scalar or array of inputs + * @param {Object} consts constant mapping + * + * @throws {Error} if constant is unknown in CONSTS + * + * @return {number|Array} resolved value(s) + */ + 'private _resolve'( input, consts ) + { + // already a number, return as-is + if ( !isNaN( +input ) ) + { + return input; + } + + const result = consts[ input ]; + if ( result === undefined ) + { + throw Error( `unknown constant: ${input}` ); + } + + return result; + } +} ); diff --git a/progtest/src/reader/DateResolver.js b/progtest/src/reader/DateResolver.js new file mode 100644 index 0000000..319254e --- /dev/null +++ b/progtest/src/reader/DateResolver.js @@ -0,0 +1,61 @@ +/** + * Date resolver for test case reader + * + * Copyright (C) 2018 R-T Specialty, LLC. + * + * This file is part of TAME. + * + * TAME is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +"use strict"; + + +const { Trait } = require( 'easejs' ); +const TestReader = require( './TestReader' ); + + +/** + * Resolve dates of the format MM/DD/YYYY to Unix timestamps + * + * This allows easily readable dates to be included in test cases without + * having to worry about Unix timestamps. For higher precision, Unix + * timestamps must be used. + */ +module.exports = Trait( 'DateResolver' ) + .implement( TestReader ) + .extend( +{ + /** + * Load test cases and resolve dates + * + * Dates will be replaced with Unix timestamps. + * + * @param {*} src data source + * + * @return {Array<TestCase>} array of test cases + */ + 'abstract override public loadCases'( src ) + { + const data = this.__super( src ); + + return data.map( + testcase => testcase.mapEachValue( + value => ( /^\d{2}\/\d{2}\/\d{4}$/.test( value ) ) + ? ( ( new Date( value ) ).getTime() / 1000 ) + : value + ) + ); + } +} ); diff --git a/progtest/src/reader/TestReader.js b/progtest/src/reader/TestReader.js new file mode 100644 index 0000000..717b12c --- /dev/null +++ b/progtest/src/reader/TestReader.js @@ -0,0 +1,40 @@ +/** + * Test case reader + * + * Copyright (C) 2018 R-T Specialty, LLC. + * + * This file is part of TAME. + * + * TAME is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +"use strict"; + +const { Interface } = require( 'easejs' ); + + +module.exports = Interface( 'TestReader', +{ + /** + * Load test cases from an implementation-defined data source SRC + * + * The produced object will be an array of cases, each containing a + * `description`, `data`, and `expect`. + * + * @param {*} src data source + * + * @return {Array<TestCase>} array of test cases + */ + 'public loadCases': [ 'src' ], +} ); diff --git a/progtest/src/reader/YamlTestReader.js b/progtest/src/reader/YamlTestReader.js new file mode 100644 index 0000000..a28c1f3 --- /dev/null +++ b/progtest/src/reader/YamlTestReader.js @@ -0,0 +1,79 @@ +/** + * YAML test case reader + * + * Copyright (C) 2018 R-T Specialty, LLC. + * + * This file is part of TAME. + * + * TAME is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +"use strict"; + +const { Class } = require( 'easejs' ); +const TestReader = require( './TestReader' ); + + +module.exports = Class( 'YamlTestReader' ) + .implement( TestReader ) + .extend( +{ + /** + * YAML parser + * + * @type {Object} + */ + 'private _yamlParser': null, + + /** + * TestCase constructor + * + * @type {function(Object)} + */ + 'private _createTestCase': null, + + + /** + * Initialize with YAML parser + * + * The parser must conform to the API of `js-yaml`. + * + * @param {Object} yaml_parser YAML parser + * @param {function(Object)} test_case_ctor TestCase constructor + */ + constructor( yaml_parser, test_case_ctor ) + { + this._yamlParser = yaml_parser; + this._createTestCase = test_case_ctor; + }, + + + /** + * Load test cases from a YAML string + * + * The produced object will be an array of cases, each containing a + * `description`, `data`, and `expect`. + * + * @param {string} src source YAML + * + * @return {Array<TestCase>} array of test cases + */ + 'virtual public loadCases'( yaml ) + { + const data = this._yamlParser.safeLoad( yaml ) + .map( this._createTestCase ); + + return data; + }, +} ); |