Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property based tests to assess load reverses dump #398

Merged
merged 1 commit into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"browserify": "^14.3.0",
"codemirror": "^5.13.4",
"eslint": "^4.1.1",
"fast-check": "0.0.13",
"istanbul": "^0.4.5",
"mocha": "^3.3.0",
"uglify-js": "^3.0.1"
Expand Down
47 changes: 47 additions & 0 deletions test/25-dumper-fuzzy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

var assert = require('assert');
var fc = require('fast-check');
var yaml = require('../');

// Generate valid YAML instances for yaml.safeDump
var key = fc.string16bits();
var values = [
key, fc.boolean(), fc.integer(), fc.double(),
fc.constantFrom(null, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)
];
var yamlArbitrary = fc.object({ key: key, values: values });

// Generate valid options for yaml.safeDump configuration
var dumpOptionsArbitrary = fc.record({
skipInvalid: fc.boolean(),
sortKeys: fc.boolean(),
noRefs: fc.boolean(),
noCompatMode: fc.boolean(),
condenseFlow: fc.boolean(),
indent: fc.integer(1, 80),
flowLevel: fc.integer(-1, 10),
styles: fc.record({
'!!null': fc.constantFrom('lowercase', 'canonical', 'uppercase', 'camelcase'),
'!!int': fc.constantFrom('decimal', 'binary', 'octal', 'hexadecimal'),
'!!bool': fc.constantFrom('lowercase', 'uppercase', 'camelcase'),
'!!float': fc.constantFrom('lowercase', 'uppercase', 'camelcase')
}, { with_deleted_keys: true })
}, { with_deleted_keys: true })
.map(function (instance) {
if (instance.condenseFlow === true && instance.flowLevel !== undefined) { instance.flowLevel = -1; }
return instance;
});

suite('Properties', function () {
test('Load from dumped should be the original object', function () {
fc.assert(fc.property(
yamlArbitrary,
dumpOptionsArbitrary,
function (obj, dumpOptions) {
var yamlContent = yaml.safeDump(obj, dumpOptions);
assert.ok(typeof yamlContent === 'string');
assert.deepStrictEqual(yaml.safeLoad(yamlContent), obj);
}));
});
});