From b7de15c90070dcbbf393fb5a8961967fdddc19ab Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Fri, 14 Jul 2017 17:20:38 +0200 Subject: [PATCH] =?UTF-8?q?:gear:=20Explorer=20un=20monde=20simplifi=C3=A9?= =?UTF-8?q?=20pour=20le=20moteur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + test/tree.test.js | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 test/tree.test.js diff --git a/package.json b/package.json index 03388dcd6..d4240ea87 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "core-js": "^2.4.1", "css-loader": "^0.28.1", "eslint": "^4.4.1", + "daggy": "^1.1.0", "eslint-plugin-react": "^7.0.1", "express": "^4.15.3", "file-loader": "^0.11.1", diff --git a/test/tree.test.js b/test/tree.test.js new file mode 100644 index 000000000..78f231d26 --- /dev/null +++ b/test/tree.test.js @@ -0,0 +1,93 @@ +import R from 'ramda' +import {expect} from 'chai' +import daggy from 'daggy' + +describe('simplified tree walks', function() { + + // Notre domaine peut se simplifier à une liste d'équations à trous: + // a: 45 + // b: a + c + // d: a + 4 + // e: b + d + // Disons que je veux connaitre "e", alors il va me manquer "c" + // Si je connais "c", alors je peux calculer "e" + // Et mon ambition est aussi de pouvoir visualiser le calcul en HTML + // Donc j'ai une structure plate que je transforme en arbre (ce n'est pas + // le focus de la présente exploration), je veux pouvoir demander des choses + // diverses à cet arbre: l'évaluer, repérer les trous, le transformer en HTML + + // Plus tard je vais avoir des trucs plus sophistiqués, par exemple: + // b: a + (bleu: b, vert: c) + // qui est équivalent à: + // b: b-bleu + b-vert + // b-bleu: a + b + // b-vert: a + c + // Le but du jeu est de pouvoir le représenter de façon compacte, mais + // d'avoir un arbre simple à manipuler + + let evaluate = tree => tree.evaluate() + let missing = tree => tree.missing() + + const Tree = daggy.taggedSum('Tree', + { + Number: ['number'], + Sum: ['children'], + Variable: ['name'] + }) + + Tree.prototype.evaluate = function (f) { + return this.cata({ + Number: (number) => parseInt(number), + Sum: (children) => R.reduce(R.add,0,R.map(evaluate,children)), + }) + } + + Tree.prototype.missing = function (f) { + return this.cata({ + Number: (number) => [], + Variable: (name) => [name], + Sum: (children) => R.reduce(R.concat,[],R.map(missing,children)), + }) + } + + it('should provide a protocol for evaluation', function() { + let tree = Tree.Number("45"), + result = tree.evaluate() + expect(result).to.equal(45) + }); + + it('should evaluate expressions', function() { + let tree = Tree.Sum([Tree.Number("45"),Tree.Number("25")]), + result = tree.evaluate() + expect(result).to.equal(70) + }); + + it('should evaluate nested expressions', function() { + let tree = Tree.Sum([ + Tree.Sum([Tree.Number("35"),Tree.Number("10")]), + Tree.Number("25")]), + result = tree.evaluate() + expect(result).to.equal(70) + }); + + it('should provide a protocol for missing variables', function() { + let tree = Tree.Variable("a"), + result = tree.missing() + expect(result).to.deep.equal(["a"]) + }); + + it('should locate missing variables in expressions', function() { + let tree = Tree.Sum([Tree.Number("45"),Tree.Variable("a")]), + result = tree.missing() + expect(result).to.deep.equal(["a"]) + }); + + it('should locate missing variables in nested expressions', function() { + let tree = Tree.Sum([ + Tree.Sum([Tree.Number("35"),Tree.Variable("a")]), + Tree.Number("25")]), + result = tree.missing() + expect(result).to.deep.equal(["a"]) + }); + +});