Add `git/render.js` with `nodeDescription` (#262)

Test Plan:
Unit tests added, with full coverage of reachable cases.

wchargin-branch: git-render
This commit is contained in:
William Chargin 2018-05-10 14:02:19 -07:00 committed by GitHub
parent 2a88bbc091
commit d21ad1312b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 0 deletions

30
src/plugins/git/render.js Normal file
View File

@ -0,0 +1,30 @@
// @flow
import type {Address} from "../../core/address";
import type {Graph} from "../../core/graph";
import type {NodeType, SubmoduleCommitPayload} from "./types";
/**
* Describe a node provided by the Git plugin.
*/
export function nodeDescription(graph: Graph<any, any>, address: Address) {
const type: NodeType = (address.type: any);
switch (type) {
case "COMMIT":
return `commit ${address.id}`;
case "TREE":
return `tree ${address.id}`;
case "BLOB":
return `blob ${address.id}`;
case "SUBMODULE_COMMIT": {
const payload: SubmoduleCommitPayload = graph.node(address).payload;
return `submodule commit ${payload.hash} in ${payload.url}`;
}
case "TREE_ENTRY":
return `entry ${address.id}`;
default:
// eslint-disable-next-line no-unused-expressions
(type: empty);
throw new Error(`unknown type: ${type}`);
}
}

View File

@ -0,0 +1,70 @@
// @flow
import type {
CommitNodePayload,
BlobNodePayload,
TreeNodePayload,
TreeEntryNodePayload,
SubmoduleCommitPayload,
} from "./types";
import type {Node} from "../../core/graph";
import {Graph} from "../../core/graph";
import {_makeAddress, commitAddress} from "./address";
import {nodeDescription} from "./render";
import {submoduleCommitId, treeEntryId} from "./types";
describe("nodeDescription", () => {
it("describes commits", () => {
const node: Node<CommitNodePayload> = {
address: commitAddress("cafebabe"),
payload: (({}: any): {||}),
};
expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual(
"commit cafebabe"
);
});
it("describes trees", () => {
const node: Node<TreeNodePayload> = {
address: _makeAddress("TREE", "deadbeef"),
payload: (({}: any): {||}),
};
expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual(
"tree deadbeef"
);
});
it("describes blobs", () => {
const node: Node<BlobNodePayload> = {
address: _makeAddress("BLOB", "01010101"),
payload: (({}: any): {||}),
};
expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual(
"blob 01010101"
);
});
it("describes submodule commits", () => {
const hash = "15615651";
const url = "https://github.com/sourcecred/example-git-submodule.git";
const node: Node<SubmoduleCommitPayload> = {
address: _makeAddress("SUBMODULE_COMMIT", submoduleCommitId(hash, url)),
payload: {hash, url},
};
expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual(
`submodule commit ${hash} in ${url}`
);
});
it("describes tree entries", () => {
const tree = "76476476323";
const name = "healing";
const node: Node<TreeEntryNodePayload> = {
address: _makeAddress("TREE_ENTRY", treeEntryId(tree, name)),
payload: {name},
};
expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual(
`entry ${tree}:${name}`
);
});
});