Skip to content

Commit

Permalink
run prettier on generation
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Dec 18, 2017
1 parent 699eb87 commit d025437
Show file tree
Hide file tree
Showing 11 changed files with 928 additions and 847 deletions.
26 changes: 13 additions & 13 deletions generation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ To correlate changes to the generation with changes in the generated code, pleas

## Development

- `npm install`
- `npm run build` builds every file specified in the `index.js`
- `npm test`
* `npm install`
* `npm run build` builds every file specified in the `index.js`
* `npm test`

## Testing

- We test with integration level tests by having a fixture Objective-C file which we generate and import before the tests.
- We only add unit tests for code that is used in production, e.g. helper functions.
- We then test against this file to ensure that the code generation result works with the specified interface.
- We decided to base our tests around snapshot tests with the goal to spot mistakes in the return values of the functions without over-specification.
- If you add functionality that affects the API surface of detox, please also make sure to include [End to End tests for it](../detox/test/e2e).
* We test with integration level tests by having a fixture Objective-C file which we generate and import before the tests.
* We only add unit tests for code that is used in production, e.g. helper functions.
* We then test against this file to ensure that the code generation result works with the specified interface.
* We decided to base our tests around snapshot tests with the goal to spot mistakes in the return values of the functions without over-specification.
* If you add functionality that affects the API surface of detox, please also make sure to include [End to End tests for it](../detox/test/e2e).

## Resources

Parsing, ASTs, and code generation are hard topics but don't worry; we got your back.
Here are some resources that might come in handy:

- [astexplorer](https://astexplorer.net): Allows you to understand Abstract Syntax Trees by showing you code and the resulting AST side-by-side.
- [Babel Handbook](https://github.com/thejameskyle/babel-handbook):
- [babel-types](https://github.com/babel/babel/tree/master/packages/babel-types): A builder library for ASTs.
- [babel-template](https://github.com/babel/babel/tree/master/packages/babel-template): A useful tool we do not use yet for generating bigger ASTs with less code by the usage of string interpolation.
- [regex101](https://regex101.com): You might need to extract parts of a string; This tool helps you to quickly build the right Regular Expression for the job.
* [astexplorer](https://astexplorer.net): Allows you to understand Abstract Syntax Trees by showing you code and the resulting AST side-by-side.
* [Babel Handbook](https://github.com/thejameskyle/babel-handbook):
* [babel-types](https://github.com/babel/babel/tree/master/packages/babel-types): A builder library for ASTs.
* [babel-template](https://github.com/babel/babel/tree/master/packages/babel-template): A useful tool we do not use yet for generating bigger ASTs with less code by the usage of string interpolation.
* [regex101](https://regex101.com): You might need to extract parts of a string; This tool helps you to quickly build the right Regular Expression for the job.
98 changes: 49 additions & 49 deletions generation/__tests__/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,52 @@ const remove = require("remove");
const androidGenerator = require("../adapters/android");

describe("Android generation", () => {
let ExampleClass;
let exampleContent;
beforeAll(() => {
// Generate the code to test
fs.mkdirSync("./__tests__/generated-android");

const files = {
"./fixtures/example.java": "./__tests__/generated-android/example.js"
};

console.log('==> generating android files');
androidGenerator(files);

console.log('==> loading android files');
// Load
ExampleClass = require("./generated-android/example.js");
exampleContent = fs.readFileSync(
"./__tests__/generated-android/example.js",
"utf8"
);
});

afterAll(() => {
// Clean up
remove.removeSync("./__tests__/generated-android");
});

describe("methods", () => {
it("should expose the functions", () => {
expect(ExampleClass.multiClick).toBeInstanceOf(Function);
});

it("should generate type checks", () => {
expect(() => {
ExampleClass.multiClick("FOO");
}).toThrowErrorMatchingSnapshot();
});

it("should return adapter calls", () => {
const result = ExampleClass.multiClick(3);
expect(result.method).toBe('multiClick');
expect(result.target.value).toBe('com.wix.detox.espresso.DetoxAction');
expect(result.args[0].type).toBe('Integer');
expect(result.args[0].value).toBe(3);

expect(result).toMatchSnapshot();
});
});
});
let ExampleClass;
let exampleContent;
beforeAll(() => {
// Generate the code to test
fs.mkdirSync("./__tests__/generated-android");

const files = {
"./fixtures/example.java": "./__tests__/generated-android/example.js"
};

console.log("==> generating android files");
androidGenerator(files);

console.log("==> loading android files");
// Load
ExampleClass = require("./generated-android/example.js");
exampleContent = fs.readFileSync(
"./__tests__/generated-android/example.js",
"utf8"
);
});

afterAll(() => {
// Clean up
remove.removeSync("./__tests__/generated-android");
});

describe("methods", () => {
it("should expose the functions", () => {
expect(ExampleClass.multiClick).toBeInstanceOf(Function);
});

it("should generate type checks", () => {
expect(() => {
ExampleClass.multiClick("FOO");
}).toThrowErrorMatchingSnapshot();
});

it("should return adapter calls", () => {
const result = ExampleClass.multiClick(3);
expect(result.method).toBe("multiClick");
expect(result.target.value).toBe("com.wix.detox.espresso.DetoxAction");
expect(result.args[0].type).toBe("Integer");
expect(result.args[0].value).toBe(3);

expect(result).toMatchSnapshot();
});
});
});
81 changes: 45 additions & 36 deletions generation/__tests__/global-functions.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
const globals = require("../core/global-functions");

describe("globals", () => {
describe("sanitize_greyDirection", () => {
it('should return numbers for strings', () => {
expect(globals.sanitize_greyDirection('left')).toBe(1);
expect(globals.sanitize_greyDirection("right")).toBe(2);
expect(globals.sanitize_greyDirection("up")).toBe(3);
expect(globals.sanitize_greyDirection("down")).toBe(4);
});
describe("sanitize_greyDirection", () => {
it("should return numbers for strings", () => {
expect(globals.sanitize_greyDirection("left")).toBe(1);
expect(globals.sanitize_greyDirection("right")).toBe(2);
expect(globals.sanitize_greyDirection("up")).toBe(3);
expect(globals.sanitize_greyDirection("down")).toBe(4);
});

it('should fail with unknown value', () => {
expect(() => {
globals.sanitize_greyDirection("kittens");
}).toThrowErrorMatchingSnapshot();
});
});
it("should fail with unknown value", () => {
expect(() => {
globals.sanitize_greyDirection("kittens");
}).toThrowErrorMatchingSnapshot();
});
});

describe("sanitize_greyContentEdge", () => {
it("should return numbers for strings", () => {
expect(globals.sanitize_greyContentEdge("left")).toBe(0);
expect(globals.sanitize_greyContentEdge("right")).toBe(1);
expect(globals.sanitize_greyContentEdge("top")).toBe(2);
expect(globals.sanitize_greyContentEdge("bottom")).toBe(3);
});
describe("sanitize_greyContentEdge", () => {
it("should return numbers for strings", () => {
expect(globals.sanitize_greyContentEdge("left")).toBe(0);
expect(globals.sanitize_greyContentEdge("right")).toBe(1);
expect(globals.sanitize_greyContentEdge("top")).toBe(2);
expect(globals.sanitize_greyContentEdge("bottom")).toBe(3);
});

it("should fail with unknown value", () => {
expect(() => {
globals.sanitize_greyContentEdge("kittens");
}).toThrowErrorMatchingSnapshot();
});
});
it("should fail with unknown value", () => {
expect(() => {
globals.sanitize_greyContentEdge("kittens");
}).toThrowErrorMatchingSnapshot();
});
});

describe("sanitize_uiAccessibilityTraits", () => {
it("should return numbers for traits", () => {
expect(globals.sanitize_uiAccessibilityTraits(["button", "link"])).toBe(3);
expect(globals.sanitize_uiAccessibilityTraits(["summary", "allowsDirectInteraction"])).toBe(16896);
});
describe("sanitize_uiAccessibilityTraits", () => {
it("should return numbers for traits", () => {
expect(globals.sanitize_uiAccessibilityTraits(["button", "link"])).toBe(
3
);
expect(
globals.sanitize_uiAccessibilityTraits([
"summary",
"allowsDirectInteraction"
])
).toBe(16896);
});

it("should throw if unknown trait is accessed", () => {
expect(() => globals.sanitize_uiAccessibilityTraits(["unknown"])).toThrowErrorMatchingSnapshot();
});
});
});
it("should throw if unknown trait is accessed", () => {
expect(() =>
globals.sanitize_uiAccessibilityTraits(["unknown"])
).toThrowErrorMatchingSnapshot();
});
});
});
22 changes: 13 additions & 9 deletions generation/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const { methodNameToSnakeCase } = require('../helpers');
const { methodNameToSnakeCase } = require("../helpers");

describe("helpers", () => {
describe("methodNameToSnakeCase", () => {
it("should not fail with empty string", () => {
expect(() => methodNameToSnakeCase("")).not.toThrow();
});
describe("methodNameToSnakeCase", () => {
it("should not fail with empty string", () => {
expect(() => methodNameToSnakeCase("")).not.toThrow();
});

it("should return the correct snake case method name", () => {
expect(methodNameToSnakeCase("actionForScrollInDirection:amount:xOriginStartPercentage:yOriginStartPercentage:")).toMatchSnapshot();
});
});
it("should return the correct snake case method name", () => {
expect(
methodNameToSnakeCase(
"actionForScrollInDirection:amount:xOriginStartPercentage:yOriginStartPercentage:"
)
).toMatchSnapshot();
});
});
});
Loading

0 comments on commit d025437

Please sign in to comment.