简体   繁体   English

在另一个文件中包含一个文件

[英]Including a file in another

I'm trying to use mocha for some Test Driven Development. 我正在尝试将mocha用于某些测试驱动开发。 Still at the beginning of this foray into Software Engineering, I'm having a problem including Graph.js in test.js: 仍然是在进入软件工程的初期,我在test.js中遇到了包括Graph.js的问题:

project/Graph/Graph.js: 项目/Graph/Graph.js:

function Graph(){
    this.nodes = {};
    this.id = 0;
}
Graph.prototype.newId = function(){
    return this.id++;
};
Graph.prototype.addNode = function(data){
    var node = new Node(graph, this.newId());
    node.data = data || {};
    this.nodes[node.id] = node;
    return edge.id;
};
Graph.prototype.removeNode = function(node){
    for(var key in this.nodes){
        for(var key2 in this.nodes[key].to){
            if(this.nodes[key].edges.to[key2] == this){
                delete this.nodes[key].edges.to[key2];
            }
        }
        for(var key2 in this.nodes[key].from){
            if(this.nodes[key].edges.from[key2] == this){
                delete this.nodes[key].edges.from[key2];
            }
        }
    }
    delete this.nodes[node.id];
    return 0;
};
Graph.prototype.addEdge = function(source, target){
    id = this.newId();
    source.edges.to[id] = target;
    target.edges.from[id] = source;
    return id;
};
Graph.prototype.removeEdge = function(edgeId){
    delete this.edges[edgeId];
};

function Node(graph, id){
    this.id = id;
    this.graph = graph;
    this.edges = {};
    this.edges.from = {};
    this.edges.to = {};
};

exports.Graph = Graph;
exports.Node = Node;

project/testing/test.js: 项目/测试/test.js:

require('../Graph/Graph.js');

suite('Graph.js', function(){
        setup(function(){
                var graph = new Graph();

                var n0 = graph.addNode({text: "Hello"}),
                    n1 = graph.addNode({text: "Sweet"}),
                    n2 = graph.addNode({text: "Worlds!"});
                var e0 = graph.addEdge(n0, n1),
                    e1 = graph.addEdge(n1, n2);

            });

        test('traverse graph', function(){
                var currentNode = n0;
                var str = ''
                while(Object.keys(currentNode.edges.to).length > 0){
                    str += currentNode.data.text + ' ';
                    currentNode = currentNode.edges.to[Object.keys(currentNode.edges.to)[0]];
                }
                assert.equals(str, 'Hello Sweet Worlds! ');
            });
    });

With the command 用命令

localhost:testing lowerkey$ mocha -u tdd -R nyan

I get the following result: 我得到以下结果:

 0   -__,------,
 1   -__|  /\_/\ 
 0   -_~|_( o .o) 
     -_ ""  "" 

  ✖ 1 of 1 test failed:

  1) Graph.js "before each" hook:
     ReferenceError: Graph is not defined
      at Context.<anonymous> (/Users/lowerkey/Desktop/TryThree/testing/test.js:5:19)
      at Hook.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:200:32)
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:201:10)
      at Runner.hook (/usr/local/lib/node_modules/mocha/lib/runner.js:212:5)
      at process.startup.processNextTick.process._tickCallback (node.js:244:9)

Update With var graphjs = require(...); var graphjs = require(...); 更新 var graphjs = require(...); and var graph = new graphjs.Graph(); var graph = new graphjs.Graph(); I get graph (lowercase) not defined. 我得到未定义的图形(小写)。

You have to get the Graph variable from the exports 您必须从exports获取Graph变量

var Graph = require('../Graph/Graph.js').Graph;
...
var graph = new Graph();

I reorganized the two files fixing several errors: 我重新整理了两个文件,修复了几个错误:

Here is are the two complete files: 这是两个完整的文件:

Graph.js: Graph.js:

function Graph(){
    this.nodes = {};
    this.id = 0;
    this.edges = {};
}
Graph.prototype.newId = function(){
    return this.id++;
};
Graph.prototype.addNode = function(data){
    var node = new Node(this, this.newId());
    node.data = data || {};
    this.nodes[node.id] = node;
    return node.id;
};
Graph.prototype.removeNode = function(id){
    for(var key in this.edges){
        if(this.edges[key].source.id == id || this.edges[key].target.id == id){
            delete this.edges[key];
        }
    }
    delete this.nodes[id];
    return 0;
};
Graph.prototype.nodeIds = function(){
    var ids = [];
    for(var key in this.nodes){
        if(this.nodes[key] !== undefined){
            ids.push(key);
        }
    }
    return ids;
};
Graph.prototype.addEdge = function(sourceId, targetId){
    var id = this.newId();
    this.edges[id] = {target: sourceId, source: targetId};
    return id;
};
Graph.prototype.removeEdge = function(edgeId){
    delete this.edges[edgeId];
};
Graph.prototype.edgeIds = function(){
    var ids = [];
    for(var key in this.edges){
        if(this.edges[key] != undefined){
            ids.push(key);
        }
    }
    return ids;
};
function Node(graph, id){
    this.id = id;
    this.graph = graph;
    this.edges = {};
}
Node.prototype.nodesFrom = function(){
    var ids = [];
    for(var key in this.graph.edges){
        if(this.graph.edges[key].source == this.id){
            ids.push(this.graph.edges[key].target);
        }
    }
    return ids;
};
Node.prototype.nodesTo = function(){
    var ids = [];
    for(var key in this.graph.edges){
        if(this.graph.edges[key].target == this.id){
            ids.push(this.graph.edges[key].source);
        }
    }
    return ids;
};

exports.tests = function(){
    suite('Graph.js', function(){
        function createGraph(){
            var graph = new Graph();
            var n0Id = graph.addNode({text: "Hello"}),
                n1Id = graph.addNode({text: "Sweet"}),
                n2Id = graph.addNode({text: "Worlds!"});
            var e0Id = graph.addEdge(n0Id, n1Id),
                e1Id = graph.addEdge(n1Id, n2Id);

            return graph;
        }

        test('create graph', function(){
            var graph = new Graph();
            assert.isDefined(graph);
        });
        test('removeNode', function(){
            var graph = createGraph();
            graph.removeNode(0);
            assert.deepEqual(graph.nodeIds(), ['1', '2']);
        });
        test('removeEdge', function(){
            var graph = createGraph();
            graph.removeEdge(3);
            assert.deepEqual(graph.edgeIds(), ['4']);
        });
        test('nodesFrom', function(){
            var graph = createGraph();
            assert.deepEqual(graph.nodes[1].nodesFrom(), [0]);
        });
        test('nodesTo', function(){
            var graph = createGraph();
            assert.deepEqual(graph.nodes[1].nodesTo(), [2]);
        })
    });
};

test.js: test.js:

assert = require('chai').assert;
require('../Graph/Graph.js').tests();

This way, the tests will be included in every source file, to be run by the project test suite. 这样,测试将包含在每个源文件中,并由项目测试套件运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM