diff --git a/publicodes/source/cyclesLib/graph.ts b/publicodes/source/cyclesLib/graph.ts index 625dc640f..c94a6eba0 100644 --- a/publicodes/source/cyclesLib/graph.ts +++ b/publicodes/source/cyclesLib/graph.ts @@ -60,7 +60,8 @@ export function flattenOneLevelRemplaceLoops(naiveGraph: Graph) { const ROLLNodesTypes = new Proxy( {}, { - get: (target, name) => (name in target ? target[name] : new Set()) + get: (target, name) => + name in target ? target[name] : (target[name] = new Set()) } ) ROLLEdges.forEach(e => { diff --git a/publicodes/test/cycles.test.js b/publicodes/test/cycles.test.js index 70aba30d9..46796d8ac 100644 --- a/publicodes/test/cycles.test.js +++ b/publicodes/test/cycles.test.js @@ -6,15 +6,72 @@ import { flattenOneLevelRemplaceLoops } from '../source/cyclesLib/graph' import { cyclicDependencies } from '../source/cyclesLib' -describe('Remplace loops flatten-o-tron 2500 ™', () => { - it(`should replace 2 nodes referencing each other with formule and replacedBy by - 4 nodes without loop`, () => { +describe('ROLL flatten-o-tron 2500 ™', () => { + it('should replace 2 ROLL nodes with 4 nodes without loop', () => { const g = new graphlib.Graph() g.setEdge('b', 'c', { type: DependencyType.formule }) g.setEdge('c', 'b', { type: DependencyType.replacedBy }) const flattenedGraph = flattenOneLevelRemplaceLoops(g) + + expect(flattenedGraph.nodes()).to.deep.equal([ + 'b [depType: 0]', + 'c [depType: 0]', + 'c [depType: 1]', + 'b [depType: 1]' + ]) + expect(flattenedGraph.edges()).to.deep.equal([ + { v: 'b [depType: 0]', w: 'c [depType: 0]' }, + { v: 'c [depType: 1]', w: 'b [depType: 1]' } + ]) + }) + + it('should replace 2 ROLL nodes in context of a larger graph', () => { + const g = new graphlib.Graph() + + g.setEdge('a', 'b', { type: DependencyType.formule }) + g.setEdge('a', 'c', { type: DependencyType.formule }) + g.setEdge('b', 'c', { type: DependencyType.formule }) + g.setEdge('c', 'b', { type: DependencyType.replacedBy }) + g.setEdge('b', 'd', { type: DependencyType.formule }) + g.setEdge('c', 'd', { type: DependencyType.formule }) + + const flattenedGraph = flattenOneLevelRemplaceLoops(g) + + expect(flattenedGraph.nodes()).to.deep.equal([ + 'a', + 'b [depType: 1]', + 'b [depType: 0]', + 'c [depType: 1]', + 'c [depType: 0]', + 'd' + ]) + expect(flattenedGraph.edges()).to.have.deep.members([ + { v: 'a', w: 'b [depType: 0]' }, + { v: 'a', w: 'b [depType: 1]' }, + { v: 'a', w: 'c [depType: 0]' }, + { v: 'a', w: 'c [depType: 1]' }, + { v: 'b [depType: 0]', w: 'c [depType: 0]' }, + { v: 'c [depType: 1]', w: 'b [depType: 1]' }, + { v: 'b [depType: 0]', w: 'd' }, + { v: 'b [depType: 1]', w: 'd' }, + { v: 'c [depType: 0]', w: 'd' }, + { v: 'c [depType: 1]', w: 'd' } + ]) + }) + + it('should not replace any nodes in a 2-level formule + remplace loop', () => { + const g = new graphlib.Graph() + + g.setEdge('a', 'b', { type: DependencyType.formule }) + g.setEdge('b', 'c', { type: DependencyType.formule }) + g.setEdge('c', 'a', { type: DependencyType.replacedBy }) + + const flattenedGraph = flattenOneLevelRemplaceLoops(g) + + expect(flattenedGraph.nodes()).to.deep.equal(['a', 'b', 'c']) + expect(flattenedGraph.edges()).to.deep.equal(g.edges()) }) })