Skip to content

Commit

Permalink
fix: refactor typings interface
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed May 22, 2019
1 parent e9b1291 commit 7846704
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 187 deletions.
211 changes: 64 additions & 147 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ const expectedRipSlipTypescript = [
" [k: string]: any;",
"}",
].join("\n");
const expectedJibberTypescript = "export type TJibber = (niptip: TNiptip): Promise<IRipslip>;";
const expectedTypescript = [
expectedNipTipTypescript,
expectedRipSlipTypescript,
expectedJibberTypescript,
].join("\n");

const expectedNipTipRust = "";
Expand Down Expand Up @@ -69,7 +71,9 @@ const expectedRipSlipRust = [
" reepadoop: Option<f64>,",
"}",
].join("\n");
const expectedRust = expectedRipSlipRust;

const expectedJibberRust = "pub fn jibber(&mut self, niptip: Niptip) -> RpcRequest<Ripslip>;";
const expectedRust = [expectedRipSlipRust, expectedJibberRust].join("\n");

describe("MethodTypings", () => {

Expand All @@ -85,184 +89,98 @@ describe("MethodTypings", () => {
expect(methodTypings).toBeInstanceOf(MethodTypings);
});

describe("getTypeDefinitionsForMethod", () => {
describe("getMethodTypings", () => {

it("throws if types not generated yet", () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
expect(() => methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "typescript")).toThrow();
});

describe("typscript", () => {

it("returns a string of typings for a method", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "typescript"))
.toBe(expectedTypescript);
});

});

describe("rust", () => {

it("returns a string of typings where the typeNames are unique", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "rust")).toBe(expectedRust);
});

expect(() => methodTypings.getMethodTypings(testOpenRPCDocument.methods[0], "typescript")).toThrow();
});

});

describe("getTypingsForMethod", () => {

it("throws if types not generated yet", () => {
it("returns a string of typings for a method", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
expect(() => methodTypings.getTypingsForMethod(testOpenRPCDocument.methods[0], "typescript")).toThrow();
});

describe("typscript", () => {

it("returns a string of typings for a method", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getTypingsForMethod(testOpenRPCDocument.methods[0], "typescript")).toEqual({
methodAliasName: "TJibber",
params: [
{
typeId: "jibber/0",
typeName: "TNiptip",
typing: expectedNipTipTypescript,
},
],
result: {
typeId: "jibber/result",
typeName: "IRipslip",
typing: expectedRipSlipTypescript,
await methodTypings.generateTypings();

expect(methodTypings.getMethodTypings(testOpenRPCDocument.methods[0], "typescript")).toEqual({
methodAliasName: "TJibber",
methodTyping: "export type TJibber = (niptip: TNiptip): Promise<IRipslip>;",
params: [
{
typeId: "jibber/0",
typeName: "TNiptip",
typing: expectedNipTipTypescript,
},
});
],
result: {
typeId: "jibber/result",
typeName: "IRipslip",
typing: expectedRipSlipTypescript,
},
});

});

describe("rust", () => {

it("returns a string of typings where the typeNames are unique", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getTypingsForMethod(testOpenRPCDocument.methods[0], "rust")).toEqual({
methodAliasName: "Jibber",
params: [
{
typeId: "jibber/0",
typeName: "Niptip",
typing: expectedNipTipRust,
},
],
result: {
typeId: "jibber/result",
typeName: "Ripslip",
typing: expectedRipSlipRust,
expect(methodTypings.getMethodTypings(testOpenRPCDocument.methods[0], "rust")).toEqual({
methodAliasName: "Jibber",
methodTyping: "pub fn jibber(&mut self, niptip: Niptip) -> RpcRequest<Ripslip>;",
params: [
{
typeId: "jibber/0",
typeName: "Niptip",
typing: expectedNipTipRust,
},
});
],
result: {
typeId: "jibber/result",
typeName: "Ripslip",
typing: expectedRipSlipRust,
},
});

});

});

describe("getAllUniqueTypings", () => {
describe("toString", () => {

it("throws if types not generated yet", () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
expect(() => methodTypings.getAllUniqueTypings("typescript")).toThrow();
});

describe("typscript", () => {

it("returns a string of typings where the typeNames are unique", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getAllUniqueTypings("typescript")).toBe(expectedTypescript);
});

expect(() => methodTypings.toString("typescript")).toThrow();
});

describe("rust", () => {

it("returns a string of typings where the typeNames are unique", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getAllUniqueTypings("rust")).toBe(expectedRust);
});
it("returns a string of typings where the typeNames are unique", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.toString("typescript")).toBe(expectedTypescript);
expect(methodTypings.toString("rust")).toBe(expectedRust);
});
});

describe("getMethodTypeAlias", () => {
describe("getMethodAliasTyping", () => {

it("throws if types not generated yet", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
expect(() => methodTypings.getMethodTypeAlias(testOpenRPCDocument.methods[0], "typescript")).toThrow();
expect(() => methodTypings.getMethodAliasTyping(testOpenRPCDocument.methods[0], "typescript")).toThrow();
});

describe("typescript", () => {

it("returns the function signature for a method", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getMethodTypeAlias(testOpenRPCDocument.methods[0], "typescript"))
.toBe("export type TJibber = (niptip: TNiptip): Promise<IRipslip>;");
});

it("works when there are no params", async () => {
const copytestOpenRPCDocument = cloneDeep(testOpenRPCDocument);
copytestOpenRPCDocument.methods[0].params = [];
const methodTypings = new MethodTypings(copytestOpenRPCDocument);
await methodTypings.generateTypings();
it("returns the function signature for a method", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getMethodTypeAlias(copytestOpenRPCDocument.methods[0], "typescript"))
.toBe("export type TJibber = (): Promise<IRipslip>;");
});
expect(methodTypings.getMethodAliasTyping(testOpenRPCDocument.methods[0], "typescript"))
.toBe("export type TJibber = (niptip: TNiptip): Promise<IRipslip>;");

expect(methodTypings.getMethodAliasTyping(testOpenRPCDocument.methods[0], "rust"))
.toBe("pub fn jibber(&mut self, niptip: Niptip) -> RpcRequest<Ripslip>;");
});

describe("rust", () => {
it("works when there are no params", async () => {
const copytestOpenRPCDocument = cloneDeep(testOpenRPCDocument);
copytestOpenRPCDocument.methods[0].params = [];
const methodTypings = new MethodTypings(copytestOpenRPCDocument);
await methodTypings.generateTypings();

it("does the same as getFunctionSignature", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();
expect(methodTypings.getMethodAliasTyping(copytestOpenRPCDocument.methods[0], "typescript"))
.toBe("export type TJibber = (): Promise<IRipslip>;");

expect(methodTypings.getMethodTypeAlias(testOpenRPCDocument.methods[0], "rust"))
.toBe(methodTypings.getMethodTypeAlias(testOpenRPCDocument.methods[0], "rust"));
});

it("returns the function signature for a method", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getMethodTypeAlias(testOpenRPCDocument.methods[0], "rust"))
.toBe("pub fn jibber(&mut self, niptip: Niptip) -> RpcRequest<Ripslip>;");
});

it("works when there are no params", async () => {
const copytestOpenRPCDocument = cloneDeep(testOpenRPCDocument);
copytestOpenRPCDocument.methods[0].params = [];

const methodTypings = new MethodTypings(copytestOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getMethodTypeAlias(copytestOpenRPCDocument.methods[0], "rust"))
.toBe("pub fn jibber(&mut self) -> RpcRequest<Ripslip>;");
});
expect(methodTypings.getMethodAliasTyping(copytestOpenRPCDocument.methods[0], "rust"))
.toBe("pub fn jibber(&mut self) -> RpcRequest<Ripslip>;");
});
});

Expand Down Expand Up @@ -339,8 +257,7 @@ describe("MethodTypings", () => {

const methodTypings = new MethodTypings(copytestOpenRPCDocument);
await methodTypings.generateTypings();
const typings = methodTypings.getAllUniqueTypings("rust");
expect(methodTypings.getAllUniqueTypings("rust"))
expect(methodTypings.toString("rust", { includeContentDescriptorTypings: true, includeMethodAliasTypings: false }))
.toBe([
"pub type Ripslip2 = String;",
"#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]",
Expand Down
Loading

0 comments on commit 7846704

Please sign in to comment.