Procházet zdrojové kódy

MPIDE-28: ✅: some event generator tests

Kyle P Davis před 10 roky
rodič
revize
e722c3b112

+ 12 - 0
src/lib/generator/test/.jshintrc

@@ -0,0 +1,12 @@
+{
+	"extends": "../../../../.jshintrc",
+	"globals": {
+		"assert": true,
+		"describe": true,
+		"it": true,
+		"before": true,
+		"beforeEach": true,
+		"afterEach": true,
+		"after": true
+	}
+}

+ 9 - 0
src/lib/generator/test/_bootstrap.js

@@ -0,0 +1,9 @@
+"use strict";
+
+// explicitely enable diffs for failing mocha tests
+Error.prototype.showDiff = true; //eslint-disable-line no-extend-native
+
+// enable on-the-fly ES6 conversion for tests
+require("babel/register")({
+	stage: 0,
+});

+ 182 - 0
src/lib/generator/test/generator.js

@@ -0,0 +1,182 @@
+"use strict";
+
+let assert = require("assert"),
+	Generator = require("../modellang");
+
+//EXAMPLE
+//	BEHAVIOR: foobarbaz = foo bar baz;
+let parsed = {
+	systems: {},
+	behaviors: {
+		foobarbaz: {type: "Behavior", id: "foobarbaz", refs: ["foo", "bar", "baz"],
+			body: [
+				{type: "Sequence", scope: {min: 1, max: 1},
+					body: [
+						{type: "Behavior", id: "foo", scope: {min: 1, max: 1}},
+						{type: "Behavior", id: "bar", scope: {min: 1, max: 1}},
+						{type: "Behavior", id: "baz", scope: {min: 1, max: 1}},
+					],
+				},
+			],
+		},
+		foo: {type: "Behavior", id: "foo", refs: [], body: []},
+		bar: {type: "Behavior", id: "bar", refs: [], body: []},
+		baz: {type: "Behavior", id: "baz", refs: [], body: []},
+	},
+	interactions: [],
+	triggers: [],
+	errors: [],
+};
+
+let g = new Generator(parsed);
+
+describe("Generator", function() {
+
+	describe("generate", function() {
+
+		function getGenerated(obj, min, max, scope) {
+			return [
+				for (evt of g.generate(obj, min, max, scope))
+					evt.map(s => s.replace(/_\S+$/, ""))
+			];
+		}
+
+		describe("Behavior (leaf/atomic)", function() {
+
+			let obj = g.behaviors.foo;
+
+			it("should return 1 trace if min=1, max=1", function() {
+				assert.deepEqual(getGenerated(obj, 1, 1), [
+					["foo"],
+				]);
+			});
+
+			it("should return 2 traces if min=1, max=2", function() {
+				assert.deepEqual(getGenerated(obj, 1, 2), [
+					["foo"],
+					["foo", "foo"],
+				]);
+			});
+
+			it("should return 3 traces if min=1, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 1, 3), [
+					["foo"],
+					["foo", "foo"],
+					["foo", "foo", "foo"],
+				]);
+			});
+
+			it("should return 2 traces if min=2, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 2, 3), [
+					["foo", "foo"],
+					["foo", "foo", "foo"],
+				]);
+			});
+
+			it("should return 1 trace if min=3, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 3, 3), [
+					["foo", "foo", "foo"],
+				]);
+			});
+
+			it("should not use scope", function() {
+				assert.deepEqual(getGenerated(obj, 1, 1, 1), getGenerated(obj, 1, 1, 10));
+				assert.deepEqual(getGenerated(obj, 1, 2, 1), getGenerated(obj, 1, 2, 10));
+				assert.deepEqual(getGenerated(obj, 1, 3, 1), getGenerated(obj, 1, 3, 10));
+				assert.deepEqual(getGenerated(obj, 2, 3, 1), getGenerated(obj, 2, 3, 10));
+				assert.deepEqual(getGenerated(obj, 3, 3, 1), getGenerated(obj, 3, 3, 10));
+			});
+
+		});
+
+		describe("Sequence", function() {
+
+			let obj = g.behaviors.foobarbaz.body[0];
+
+			it("should return 1 trace if min=1, max=1", function() {
+				assert.deepEqual(getGenerated(obj, 1, 1), [
+					["foo", "bar", "baz"],
+				]);
+			});
+
+			it("should return 2 traces if min=1, max=2", function() {
+				assert.deepEqual(getGenerated(obj, 1, 2), [
+					["foo", "bar", "baz"],
+					["foo", "bar", "baz", "foo", "bar", "baz"],
+				]);
+			});
+
+			it("should return 3 traces if min=1, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 1, 3), [
+					["foo", "bar", "baz"],
+					["foo", "bar", "baz", "foo", "bar", "baz"],
+					["foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"],
+				]);
+			});
+
+			it("should return 2 traces if min=2, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 2, 3), [
+					["foo", "bar", "baz", "foo", "bar", "baz"],
+					["foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"],
+				]);
+			});
+
+			it("should return 1 trace if min=3, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 3, 3), [
+					["foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"],
+				]);
+			});
+
+			it("should not use scope", function() {
+				assert.deepEqual(getGenerated(obj, 1, 1, 1), getGenerated(obj, 1, 1, 10));
+				assert.deepEqual(getGenerated(obj, 1, 2, 1), getGenerated(obj, 1, 2, 10));
+				assert.deepEqual(getGenerated(obj, 1, 3, 1), getGenerated(obj, 1, 3, 10));
+				assert.deepEqual(getGenerated(obj, 2, 3, 1), getGenerated(obj, 2, 3, 10));
+				assert.deepEqual(getGenerated(obj, 3, 3, 1), getGenerated(obj, 3, 3, 10));
+			});
+
+		});
+
+		describe("Behavior (parent/complex)", function() {
+
+			let obj = g.behaviors.foobarbaz;
+
+			it("should return 1 trace if min=1, max=1", function() {
+				assert.deepEqual(getGenerated(obj, 1, 1), [
+					["foobarbaz", "foo", "bar", "baz"],
+				]);
+			});
+
+			it("should return 2 traces if min=1, max=2", function() {
+				assert.deepEqual(getGenerated(obj, 1, 2), [
+					["foobarbaz", "foo", "bar", "baz"],
+					["foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz"],
+				]);
+			});
+
+			it("should return 3 traces if min=1, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 1, 3), [
+					["foobarbaz", "foo", "bar", "baz"],
+					["foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz"],
+					["foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz"],
+				]);
+			});
+
+			it("should return 2 traces if min=2, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 2, 3), [
+					["foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz"],
+					["foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz"],
+				]);
+			});
+
+			it("should return 1 trace if min=3, max=3", function() {
+				assert.deepEqual(getGenerated(obj, 3, 3), [
+					["foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz", "foobarbaz", "foo", "bar", "baz"],
+				]);
+			});
+
+		});
+
+	});
+
+});

+ 3 - 0
src/lib/generator/test/mocha.opts

@@ -0,0 +1,3 @@
+--reporter spec
+--require ./test/_bootstrap.js
+--recursive