Skip to content

Commit

Permalink
Merge pull request #3 from danieljharvey/neat-sorting-thing
Browse files Browse the repository at this point in the history
Neat sorting thing
  • Loading branch information
danieljharvey committed Apr 4, 2020
2 parents 5d30237 + 9dbde08 commit 1f05069
Show file tree
Hide file tree
Showing 8 changed files with 512 additions and 229 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
yarn-error.log
dist/
output/*.ts
Expand Down
189 changes: 68 additions & 121 deletions src/convert.test.ts
Original file line number Diff line number Diff line change
@@ -1,131 +1,78 @@
// import { arbitraryQuery } from "../output/output";
import * as Codegen from "./convert";
import * as Types from "./types";

describe("includesOneOf", () => {
it("Makes any sense whatsoever", () => {
expect(Codegen.includesOneOf("dog", ["dog"])).toBeTruthy();
});
it("Works with multiple", () => {
expect(Codegen.includesOneOf("dog", ["log", "dog"])).toBeTruthy();
});
it("Works with multiple even with nonsense everywhere", () => {
expect(Codegen.includesOneOf("_____dog___", ["log", "dog"])).toBeTruthy();
});
it("Doesn't find something that's not there", () => {
expect(Codegen.includesOneOf("dog", ["log"])).toBeFalsy();
});
});
const stable: Types.Output = {
kind: "Object",
name: "Stable" as Types.TypeName,
output: "fc.record({horse: arbitraryHorse })" as Types.Generated,
deps: ["Horse" as Types.TypeName]
};
const horse: Types.Output = {
kind: "Object",
name: "Horse" as Types.TypeName,
output: "fc.record({saddle: arbitrarySaddle})" as Types.Generated,
deps: ["Saddle" as Types.TypeName]
};
const saddle: Types.Output = {
kind: "Object",
name: "Saddle" as Types.TypeName,
output: "fc.record({chair: arbitraryChair})" as Types.Generated,
deps: []
};
const things: Types.Output = {
kind: "Union",
name: "Things" as Types.TypeName,
output: "fc.oneof(arbitraryStable, arbitrarySaddle)" as Types.Generated,
deps: ["Stable" as Types.TypeName, "Saddle" as Types.TypeName]
};

describe("sortASTs", () => {
it("Puts mentions later", () => {
const stable: Types.Output = {
kind: "Object",
name: "Stable" as Types.TypeName,
output: "fc.record({horse: arbitraryHorse })" as Types.Generated,
deps: [],
};
const horse: Types.Output = {
kind: "Object",
name: "Horse" as Types.TypeName,
output: "fc.record({saddle: arbitrarySaddle})" as Types.Generated,
deps: [],
};
const saddle: Types.Output = {
kind: "Object",
name: "Saddle" as Types.TypeName,
output: "fc.record({chair: arbitraryChair})" as Types.Generated,
deps: [],
};
const things: Types.Output = {
kind: "Union",
name: "Things" as Types.TypeName,
output: "fc.oneof(arbitraryStable, arbitrarySaddle)" as Types.Generated,
deps: [],
};
const expectedOrder = [saddle, horse, stable, things];
expect(Codegen.sortASTs([things, stable, horse, saddle])).toStrictEqual(
expectedOrder
);
expect(Codegen.sortASTs([stable, horse, saddle, things])).toStrictEqual(
expectedOrder
);
});

it("Does the twitter ones", () => {
const Query: Types.Output = {
kind: "Object",
name: "Query" as Types.TypeName,
output: `fc.record({Tweet: arbitraryTweet,Tweets: fc.array(arbitraryTweet),TweetsMeta: arbitraryMeta,User: arbitraryUser,Notifications: fc.array(arbitraryNotification),NotificationsMeta: arbitraryMeta})` as Types.Generated,
deps: [],
};
const Tweet: Types.Output = {
kind: "Object",
name: "Tweet" as Types.TypeName,
output: `fc.record({id: arbitraryID,body: arbitraryString,date: arbitraryDate,Author: arbitraryUser,Stats: arbitraryStat})` as Types.Generated,
deps: [],
};
const User: Types.Output = {
kind: "Object",
name: "User" as Types.TypeName,
output: `fc.record({id: arbitraryID,username: arbitraryString,first_name: arbitraryString,last_name: arbitraryString,full_name: arbitraryString,name: arbitraryString,avatar_url: arbitraryUrl})` as Types.Generated,
deps: [],
};
const Stat: Types.Output = {
kind: "Object",
name: "Stat" as Types.TypeName,
output: `fc.record({views: arbitraryInt,likes: arbitraryInt,retweets: arbitraryInt,responses: arbitraryInt})` as Types.Generated,
deps: [],
};
const Meta: Types.Output = {
kind: "Object",
name: "Meta" as Types.TypeName,
output: `fc.record({count: arbitraryInt})` as Types.Generated,
deps: [],
};

const Notification: Types.Output = {
kind: "Object",
name: "Notification" as Types.TypeName,
output: `fc.record({id: arbitraryID,date: arbitraryDate,type: arbitraryString})` as Types.Generated,
deps: [],
};

const Mutation: Types.Output = {
kind: "Object",
name: "Mutation" as Types.TypeName,
output: `fc.record({createTweet: arbitraryTweet,deleteTweet: arbitraryTweet,markTweetRead: arbitraryBoolean})` as Types.Generated,
deps: [],
};

const StatOrNotification: Types.Output = {
kind: "Union",
name: "StatOrNotification" as Types.TypeName,
output: `fc.oneof(arbitraryStat, arbitraryNotification)` as Types.Generated,
deps: [],
};

const expectedTweetOrder = [
User,
Stat,
Tweet,
Meta,
Notification,
Query,
Mutation,
StatOrNotification,
const expectedOrder = [
"Saddle" as Types.TypeName,
"Horse" as Types.TypeName,
"Stable" as Types.TypeName,
"Things" as Types.TypeName
];
expect(
Codegen.sortASTs([
Query,
Tweet,
User,
Stat,
Meta,
Notification,
Mutation,
StatOrNotification,
])
).toStrictEqual(expectedTweetOrder);
Codegen.sortASTs([things, stable, horse, saddle]).map(a => a.name)
).toStrictEqual(expectedOrder);
expect(
Codegen.sortASTs([stable, horse, saddle, things]).map(a => a.name)
).toStrictEqual(expectedOrder);
});
});

describe("moveASTs", () => {
it("Moves everything with zero deps", () => {
const used: Types.Output[] = [];
const remaining = [stable, horse, saddle, things];
const result = Codegen.moveASTs(used, remaining);
if (result._tag === "Right") {
expect(result.payload.used.length).toEqual(1);
expect(result.payload.remaining.length).toEqual(3);
} else {
throw "moveASTs failed";
}
});
it("Moves everything with deps that have already been moved across", () => {
const used = [saddle];
const remaining = [horse, stable, things];
const result = Codegen.moveASTs(used, remaining);
if (result._tag === "Right") {
expect(result.payload.used.length).toEqual(2);
expect(result.payload.remaining.length).toEqual(2);
} else {
throw "moveASTs failed";
}
});
it("Bruce forces it to a satifying conclusion", () => {
const remaining = [stable, horse, saddle, things];
const result = Codegen.magicSort(remaining, 100);
if (result._tag === "Right") {
expect(result && result.payload.length).toEqual(4);
} else {
throw "fail!";
}
});
});

0 comments on commit 1f05069

Please sign in to comment.