@needle-tools/needle-component-compiler 1.7.3 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Changelog.md +8 -0
- package/DEV.bat +1 -0
- package/{RUNTEST.bat → RUN_MANUAL_TEST.bat} +0 -0
- package/RUN_TESTS.bat +1 -0
- package/Readme.md +2 -1
- package/package.json +21 -3
- package/src/component-compiler.js +146 -71
- package/src/component-compiler.ts +147 -67
- package/src/test.ts +27 -3
- package/test/codegen/abstract method/expected.cs +14 -0
- package/test/codegen/abstract method/input.js +27 -0
- package/test/codegen/abstract method/input.ts +4 -0
- package/test/codegen/abstract method/output.cs +14 -0
- package/test/codegen/basic/expected.cs +13 -0
- package/test/codegen/basic/input.js +26 -0
- package/test/codegen/basic/input.ts +2 -0
- package/test/codegen/basic/output.cs +13 -0
- package/test/codegen/basic-no-export/expected.cs +0 -0
- package/test/codegen/basic-no-export/input.js +27 -0
- package/test/codegen/basic-no-export/input.ts +4 -0
- package/test/codegen/basic-no-export/output.cs +0 -0
- package/test/codegen/bool/expected.cs +14 -0
- package/test/codegen/bool/input.js +26 -0
- package/test/codegen/bool/input.ts +3 -0
- package/test/codegen/bool/output.cs +14 -0
- package/test/codegen/fields with default values/expected.cs +17 -0
- package/test/codegen/fields with default values/input.js +31 -0
- package/test/codegen/fields with default values/input.ts +6 -0
- package/test/codegen/fields with default values/output.cs +17 -0
- package/test/codegen/method ignore inline type declaration/expected.cs +14 -0
- package/test/codegen/method ignore inline type declaration/input.js +27 -0
- package/test/codegen/method ignore inline type declaration/input.ts +3 -0
- package/test/codegen/method ignore inline type declaration/output.cs +14 -0
- package/test/codegen/nonserialized ignore field/expected.cs +13 -0
- package/test/codegen/nonserialized ignore field/input.js +26 -0
- package/test/codegen/nonserialized ignore field/input.ts +4 -0
- package/test/codegen/nonserialized ignore field/output.cs +13 -0
- package/test/codegen/nonserialized on property does not affect method/expected.cs +14 -0
- package/test/codegen/nonserialized on property does not affect method/input.js +34 -0
- package/test/codegen/nonserialized on property does not affect method/input.ts +8 -0
- package/test/codegen/nonserialized on property does not affect method/output.cs +13 -0
- package/test/codegen/number/expected.cs +14 -0
- package/test/codegen/number/input.js +26 -0
- package/test/codegen/number/input.ts +3 -0
- package/test/codegen/number/output.cs +14 -0
- package/test/codegen/object/expected.cs +14 -0
- package/test/codegen/object/input.js +26 -0
- package/test/codegen/object/input.ts +3 -0
- package/test/codegen/object/output.cs +14 -0
- package/test/codegen/private.methods/expected.cs +13 -0
- package/test/codegen/private.methods/input.js +27 -0
- package/test/codegen/private.methods/input.ts +3 -0
- package/test/codegen/private.methods/output.cs +13 -0
- package/test/codegen/protected.methods/expected.cs +13 -0
- package/test/codegen/protected.methods/input.js +27 -0
- package/test/codegen/protected.methods/input.ts +3 -0
- package/test/codegen/protected.methods/output.cs +13 -0
- package/test/codegen/public method with args/expected.cs +14 -0
- package/test/codegen/public method with args/input.js +27 -0
- package/test/codegen/public method with args/input.ts +3 -0
- package/test/codegen/public method with args/output.cs +14 -0
- package/test/codegen/public method with multiple args/expected.cs +14 -0
- package/test/codegen/public method with multiple args/input.js +27 -0
- package/test/codegen/public method with multiple args/input.ts +3 -0
- package/test/codegen/public method with multiple args/output.cs +14 -0
- package/test/codegen/public.methods/expected.cs +14 -0
- package/test/codegen/public.methods/input.js +27 -0
- package/test/codegen/public.methods/input.ts +3 -0
- package/test/codegen/public.methods/output.cs +14 -0
- package/test/codegen/static method/expected.cs +13 -0
- package/test/codegen/static method/input.js +27 -0
- package/test/codegen/static method/input.ts +3 -0
- package/test/codegen/static method/output.cs +14 -0
- package/test/codegen/string/expected.cs +14 -0
- package/test/codegen/string/input.js +26 -0
- package/test/codegen/string/input.ts +3 -0
- package/test/codegen/string/output.cs +14 -0
- package/test/codegen/stringArray/expected.cs +14 -0
- package/test/codegen/stringArray/input.js +26 -0
- package/test/codegen/stringArray/input.ts +3 -0
- package/test/codegen/stringArray/output.cs +14 -0
- package/test/component.basic.test.ts +52 -0
- package/test/component.methods.test.ts +185 -0
- package/test/component.nonserialized.test.ts +45 -0
- package/test/component.primitives.test.ts +121 -0
- package/test/helpers.ts +72 -0
- package/tsconfig.json +8 -0
- package/workspace.code-workspace +7 -7
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { compareCodegen, compareCodegenWithDefaultContext } from './helpers';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
describe('Typescript with fields', () => {
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
it('should generate component with number', () => {
|
|
8
|
+
compareCodegenWithDefaultContext("number",
|
|
9
|
+
// INPUT
|
|
10
|
+
`export class MyComponent extends Behaviour {
|
|
11
|
+
public myField: number;
|
|
12
|
+
}
|
|
13
|
+
`,
|
|
14
|
+
// EXPECTED
|
|
15
|
+
`public partial class MyComponent : UnityEngine.MonoBehaviour
|
|
16
|
+
{
|
|
17
|
+
public float @myField;
|
|
18
|
+
}`
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
it('should generate component with string', () => {
|
|
25
|
+
compareCodegenWithDefaultContext("string",
|
|
26
|
+
// INPUT
|
|
27
|
+
`export class MyComponent extends Behaviour {
|
|
28
|
+
public myField: string;
|
|
29
|
+
}
|
|
30
|
+
`,
|
|
31
|
+
// EXPECTED
|
|
32
|
+
`public partial class MyComponent : UnityEngine.MonoBehaviour
|
|
33
|
+
{
|
|
34
|
+
public string @myField;
|
|
35
|
+
}`
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
it('should generate component with string array', () => {
|
|
43
|
+
compareCodegenWithDefaultContext("stringArray",
|
|
44
|
+
// INPUT
|
|
45
|
+
`export class MyComponent extends Behaviour {
|
|
46
|
+
public myField: string[];
|
|
47
|
+
}
|
|
48
|
+
`,
|
|
49
|
+
// EXPECTED
|
|
50
|
+
`public partial class MyComponent : UnityEngine.MonoBehaviour
|
|
51
|
+
{
|
|
52
|
+
public string[] @myField;
|
|
53
|
+
}`
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
it('should generate component with boolean', () => {
|
|
61
|
+
compareCodegenWithDefaultContext("bool",
|
|
62
|
+
// INPUT
|
|
63
|
+
`export class MyComponent extends Behaviour {
|
|
64
|
+
public myField: boolean;
|
|
65
|
+
}
|
|
66
|
+
`,
|
|
67
|
+
// EXPECTED
|
|
68
|
+
`public partial class MyComponent : UnityEngine.MonoBehaviour
|
|
69
|
+
{
|
|
70
|
+
public bool @myField;
|
|
71
|
+
}`
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
it('should generate component with object', () => {
|
|
79
|
+
compareCodegenWithDefaultContext("object",
|
|
80
|
+
// INPUT
|
|
81
|
+
`export class MyComponent extends Behaviour {
|
|
82
|
+
public myField: object;
|
|
83
|
+
}
|
|
84
|
+
`,
|
|
85
|
+
// EXPECTED
|
|
86
|
+
`public partial class MyComponent : UnityEngine.MonoBehaviour
|
|
87
|
+
{
|
|
88
|
+
public UnityEngine.Object @myField;
|
|
89
|
+
}`
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
it('should generate component with default values', () => {
|
|
97
|
+
compareCodegenWithDefaultContext("fields with default values",
|
|
98
|
+
// INPUT
|
|
99
|
+
`export class MyComponent extends Behaviour {
|
|
100
|
+
public myField: number = 42;
|
|
101
|
+
public myField2: string = "hello";
|
|
102
|
+
public myField3: boolean = true;
|
|
103
|
+
public myField4: object = null;
|
|
104
|
+
}
|
|
105
|
+
`,
|
|
106
|
+
// EXPECTED
|
|
107
|
+
`public partial class MyComponent : UnityEngine.MonoBehaviour
|
|
108
|
+
{
|
|
109
|
+
public float @myField = 42f;
|
|
110
|
+
public string @myField2 = "hello";
|
|
111
|
+
public bool @myField3 = true;
|
|
112
|
+
public UnityEngine.Object @myField4 = null;
|
|
113
|
+
}`
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
});
|
package/test/helpers.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { compile } from "../src/component-compiler";
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import { debuglog } from "util";
|
|
5
|
+
|
|
6
|
+
class TestOptions {
|
|
7
|
+
ignoreWhiteSpace: boolean = true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function testCompile(code: string): string | null {
|
|
11
|
+
|
|
12
|
+
const res = compile(code, "test", null, false);
|
|
13
|
+
const output = res?.length > 0 ? res[0] : "";
|
|
14
|
+
|
|
15
|
+
return output;
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function compareByLine(output: string, expected: string) {
|
|
20
|
+
const splitCode = /\r?\n/;
|
|
21
|
+
|
|
22
|
+
if (output == null) {
|
|
23
|
+
|
|
24
|
+
expect(expected).to.equal(output);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
|
|
28
|
+
const outputLines = output.split(splitCode);
|
|
29
|
+
const expectedLines = expected.split(splitCode);
|
|
30
|
+
for (let i = 0; i < outputLines.length; i++) {
|
|
31
|
+
const outputLine = outputLines[i].trim();
|
|
32
|
+
const expectedLine = expectedLines[i].trim();
|
|
33
|
+
expect(outputLine).to.equal(expectedLine, `Line ${i} does not match`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function compareCodegen(id: string, input: string, expected: string) {
|
|
40
|
+
const baseDir = "test/codegen";
|
|
41
|
+
if (!fs.existsSync(baseDir))
|
|
42
|
+
fs.mkdirSync(baseDir);
|
|
43
|
+
const dir = `${baseDir}/${id}`;
|
|
44
|
+
if (!fs.existsSync(dir))
|
|
45
|
+
fs.mkdirSync(dir);
|
|
46
|
+
fs.writeFileSync(`${dir}\\input.ts`, input);
|
|
47
|
+
const result = testCompile(input);
|
|
48
|
+
if (result !== null)
|
|
49
|
+
fs.writeFileSync(`${dir}\\output.cs`, result);
|
|
50
|
+
fs.writeFileSync(`${dir}\\expected.cs`, expected);
|
|
51
|
+
|
|
52
|
+
compareByLine(result, expected);
|
|
53
|
+
|
|
54
|
+
// expect(result).to.equal(expected);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function compareCodegenWithDefaultContext(id: string, input: string, expected: string) {
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
expected = `// NEEDLE_CODEGEN_START
|
|
61
|
+
// auto generated code - do not edit directly
|
|
62
|
+
|
|
63
|
+
#pragma warning disable
|
|
64
|
+
|
|
65
|
+
namespace Needle.Typescript.GeneratedComponents
|
|
66
|
+
{
|
|
67
|
+
\t` + expected + `
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// NEEDLE_CODEGEN_END`;
|
|
71
|
+
return compareCodegen(id, input, expected);
|
|
72
|
+
}
|
package/tsconfig.json
ADDED
package/workspace.code-workspace
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
{
|
|
2
|
-
"folders": [
|
|
3
|
-
{
|
|
4
|
-
"path": "."
|
|
5
|
-
}
|
|
6
|
-
],
|
|
7
|
-
"settings": {}
|
|
1
|
+
{
|
|
2
|
+
"folders": [
|
|
3
|
+
{
|
|
4
|
+
"path": "."
|
|
5
|
+
}
|
|
6
|
+
],
|
|
7
|
+
"settings": {}
|
|
8
8
|
}
|