@alloy-js/csharp 0.18.0-dev.2 → 0.18.0-dev.20

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.
Files changed (153) hide show
  1. package/LICENSE +7 -0
  2. package/dist/src/components/ClassDeclaration.d.ts +74 -0
  3. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  4. package/dist/src/components/{Class.js → ClassDeclaration.js} +59 -32
  5. package/dist/src/components/ClassMethod.d.ts +25 -4
  6. package/dist/src/components/ClassMethod.d.ts.map +1 -1
  7. package/dist/src/components/ClassMethod.js +22 -3
  8. package/dist/src/components/EnumDeclaration.d.ts +34 -0
  9. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  10. package/dist/src/components/{Enum.js → EnumDeclaration.js} +25 -5
  11. package/dist/src/components/Parameters.d.ts +6 -1
  12. package/dist/src/components/Parameters.d.ts.map +1 -1
  13. package/dist/src/components/Parameters.js +3 -2
  14. package/dist/src/components/doc/comment.d.ts +70 -0
  15. package/dist/src/components/doc/comment.d.ts.map +1 -0
  16. package/dist/src/components/doc/comment.js +88 -0
  17. package/dist/src/components/doc/comment.test.d.ts +2 -0
  18. package/dist/src/components/doc/comment.test.d.ts.map +1 -0
  19. package/dist/src/components/doc/comment.test.js +348 -0
  20. package/dist/src/components/doc/from-markdown.d.ts +6 -0
  21. package/dist/src/components/doc/from-markdown.d.ts.map +1 -0
  22. package/dist/src/components/doc/from-markdown.js +58 -0
  23. package/dist/src/components/doc/from-markdown.test.d.ts +2 -0
  24. package/dist/src/components/doc/from-markdown.test.d.ts.map +1 -0
  25. package/dist/src/components/doc/from-markdown.test.js +83 -0
  26. package/dist/src/components/index.d.ts +12 -3
  27. package/dist/src/components/index.d.ts.map +1 -1
  28. package/dist/src/components/index.js +13 -4
  29. package/dist/src/components/interface/declaration.d.ts +47 -0
  30. package/dist/src/components/interface/declaration.d.ts.map +1 -0
  31. package/dist/src/components/interface/declaration.js +77 -0
  32. package/dist/src/components/interface/declaration.test.d.ts +2 -0
  33. package/dist/src/components/interface/declaration.test.d.ts.map +1 -0
  34. package/dist/src/components/interface/declaration.test.js +153 -0
  35. package/dist/src/components/interface/method.d.ts +32 -0
  36. package/dist/src/components/interface/method.d.ts.map +1 -0
  37. package/dist/src/components/interface/method.js +69 -0
  38. package/dist/src/components/interface/method.test.d.ts +2 -0
  39. package/dist/src/components/interface/method.test.d.ts.map +1 -0
  40. package/dist/src/components/interface/method.test.js +254 -0
  41. package/dist/src/components/interface/property.d.ts +38 -0
  42. package/dist/src/components/interface/property.d.ts.map +1 -0
  43. package/dist/src/components/interface/property.js +67 -0
  44. package/dist/src/components/interface/property.test.d.ts +2 -0
  45. package/dist/src/components/interface/property.test.d.ts.map +1 -0
  46. package/dist/src/components/interface/property.test.js +165 -0
  47. package/dist/src/components/property/property.d.ts +57 -0
  48. package/dist/src/components/property/property.d.ts.map +1 -0
  49. package/dist/src/components/property/property.js +70 -0
  50. package/dist/src/components/property/property.test.d.ts +2 -0
  51. package/dist/src/components/property/property.test.d.ts.map +1 -0
  52. package/dist/src/components/property/property.test.js +218 -0
  53. package/dist/src/components/record/declaration.d.ts +35 -0
  54. package/dist/src/components/record/declaration.d.ts.map +1 -0
  55. package/dist/src/components/record/declaration.js +90 -0
  56. package/dist/src/components/record/declaration.test.d.ts +2 -0
  57. package/dist/src/components/record/declaration.test.d.ts.map +1 -0
  58. package/dist/src/components/record/declaration.test.js +94 -0
  59. package/dist/src/components/stc/index.d.ts +2 -2
  60. package/dist/src/components/stc/index.d.ts.map +1 -1
  61. package/dist/src/components/stc/index.js +2 -2
  62. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts +8 -0
  63. package/dist/src/components/type-parameters/type-parameter-constraints.d.ts.map +1 -0
  64. package/dist/src/components/type-parameters/type-parameter-constraints.js +44 -0
  65. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts +2 -0
  66. package/dist/src/components/type-parameters/type-parameter-constraints.test.d.ts.map +1 -0
  67. package/dist/src/components/type-parameters/type-parameter-constraints.test.js +67 -0
  68. package/dist/src/components/type-parameters/type-parameter.d.ts +20 -0
  69. package/dist/src/components/type-parameters/type-parameter.d.ts.map +1 -0
  70. package/dist/src/components/type-parameters/type-parameter.js +22 -0
  71. package/dist/src/components/type-parameters/type-parameters.d.ts +17 -0
  72. package/dist/src/components/type-parameters/type-parameters.d.ts.map +1 -0
  73. package/dist/src/components/type-parameters/type-parameters.js +54 -0
  74. package/dist/src/components/type-parameters/type-parameters.test.d.ts +2 -0
  75. package/dist/src/components/type-parameters/type-parameters.test.d.ts.map +1 -0
  76. package/dist/src/components/type-parameters/type-parameters.test.js +48 -0
  77. package/dist/src/components/var/declaration.d.ts +35 -0
  78. package/dist/src/components/var/declaration.d.ts.map +1 -0
  79. package/dist/src/components/var/declaration.js +40 -0
  80. package/dist/src/components/var/declaration.test.d.ts +2 -0
  81. package/dist/src/components/var/declaration.test.d.ts.map +1 -0
  82. package/dist/src/components/var/declaration.test.js +73 -0
  83. package/dist/src/modifiers.d.ts +10 -4
  84. package/dist/src/modifiers.d.ts.map +1 -1
  85. package/dist/src/modifiers.js +9 -32
  86. package/dist/src/name-policy.d.ts +1 -1
  87. package/dist/src/name-policy.d.ts.map +1 -1
  88. package/dist/src/name-policy.js +2 -0
  89. package/dist/test/class-declaration.test.d.ts +2 -0
  90. package/dist/test/class-declaration.test.d.ts.map +1 -0
  91. package/dist/test/class-declaration.test.js +463 -0
  92. package/dist/test/class-method.test.js +37 -14
  93. package/dist/test/enum.test.js +12 -12
  94. package/dist/test/namespace.test.js +8 -8
  95. package/dist/test/project-directory.test.d.ts +2 -0
  96. package/dist/test/project-directory.test.d.ts.map +1 -0
  97. package/dist/test/{projectdirectory.test.js → project-directory.test.js} +8 -8
  98. package/dist/test/sourcefile.test.js +4 -4
  99. package/dist/test/using.test.js +9 -9
  100. package/dist/test/vitest.setup.d.ts +2 -0
  101. package/dist/test/vitest.setup.d.ts.map +1 -0
  102. package/dist/test/vitest.setup.js +1 -0
  103. package/dist/tsconfig.tsbuildinfo +1 -1
  104. package/package.json +21 -21
  105. package/src/components/{Class.tsx → ClassDeclaration.tsx} +108 -43
  106. package/src/components/ClassMethod.tsx +53 -12
  107. package/src/components/{Enum.tsx → EnumDeclaration.tsx} +30 -6
  108. package/src/components/Parameters.tsx +10 -3
  109. package/src/components/doc/comment.test.tsx +337 -0
  110. package/src/components/doc/comment.tsx +152 -0
  111. package/src/components/doc/from-markdown.test.tsx +103 -0
  112. package/src/components/doc/from-markdown.tsx +58 -0
  113. package/src/components/index.ts +12 -3
  114. package/src/components/interface/declaration.test.tsx +143 -0
  115. package/src/components/interface/declaration.tsx +105 -0
  116. package/src/components/interface/method.test.tsx +250 -0
  117. package/src/components/interface/method.tsx +105 -0
  118. package/src/components/interface/property.test.tsx +144 -0
  119. package/src/components/interface/property.tsx +107 -0
  120. package/src/components/property/property.test.tsx +187 -0
  121. package/src/components/property/property.tsx +146 -0
  122. package/src/components/record/declaration.test.tsx +73 -0
  123. package/src/components/record/declaration.tsx +109 -0
  124. package/src/components/stc/index.ts +2 -2
  125. package/src/components/type-parameters/type-parameter-constraints.test.tsx +93 -0
  126. package/src/components/type-parameters/type-parameter-constraints.tsx +46 -0
  127. package/src/components/type-parameters/type-parameter.tsx +35 -0
  128. package/src/components/type-parameters/type-parameters.test.tsx +46 -0
  129. package/src/components/type-parameters/type-parameters.tsx +63 -0
  130. package/src/components/var/declaration.test.tsx +59 -0
  131. package/src/components/var/declaration.tsx +47 -0
  132. package/src/modifiers.ts +25 -42
  133. package/src/name-policy.ts +5 -0
  134. package/temp/api.json +5227 -685
  135. package/test/class-declaration.test.tsx +418 -0
  136. package/test/class-method.test.tsx +24 -14
  137. package/test/enum.test.tsx +11 -11
  138. package/test/namespace.test.tsx +4 -4
  139. package/test/{projectdirectory.test.tsx → project-directory.test.tsx} +4 -4
  140. package/test/sourcefile.test.tsx +2 -2
  141. package/test/using.test.tsx +9 -9
  142. package/test/vitest.setup.ts +1 -0
  143. package/vitest.config.ts +3 -0
  144. package/dist/src/components/Class.d.ts +0 -26
  145. package/dist/src/components/Class.d.ts.map +0 -1
  146. package/dist/src/components/Enum.d.ts +0 -15
  147. package/dist/src/components/Enum.d.ts.map +0 -1
  148. package/dist/test/class.test.d.ts +0 -2
  149. package/dist/test/class.test.d.ts.map +0 -1
  150. package/dist/test/class.test.js +0 -298
  151. package/dist/test/projectdirectory.test.d.ts +0 -2
  152. package/dist/test/projectdirectory.test.d.ts.map +0 -1
  153. package/test/class.test.tsx +0 -292
@@ -0,0 +1,73 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { List, refkey } from "@alloy-js/core";
3
+ import { expect, it } from "vitest";
4
+ import { TestNamespace } from "../../../test/utils.js";
5
+ import { SourceFile } from "../SourceFile.js";
6
+ import { VarDeclaration } from "./declaration.js";
7
+ it("declares var without type", () => {
8
+ expect(_$createComponent(TestNamespace, {
9
+ get children() {
10
+ return _$createComponent(VarDeclaration, {
11
+ name: "testVar",
12
+ children: "42"
13
+ });
14
+ }
15
+ })).toRenderTo(`
16
+ var testVar = 42;
17
+ `);
18
+ });
19
+ it("declares var with type", () => {
20
+ expect(_$createComponent(TestNamespace, {
21
+ get children() {
22
+ return _$createComponent(VarDeclaration, {
23
+ name: "testVar",
24
+ type: "int",
25
+ children: "42"
26
+ });
27
+ }
28
+ })).toRenderTo(`
29
+ int testVar = 42;
30
+ `);
31
+ });
32
+ it("name variables camel case", () => {
33
+ expect(_$createComponent(TestNamespace, {
34
+ get children() {
35
+ return _$createComponent(VarDeclaration, {
36
+ name: "test_var",
37
+ children: "42"
38
+ });
39
+ }
40
+ })).toRenderTo(`
41
+ var testVar = 42;
42
+ `);
43
+ });
44
+ it("links refkey", () => {
45
+ const key = refkey();
46
+ expect(_$createComponent(TestNamespace, {
47
+ get children() {
48
+ return _$createComponent(SourceFile, {
49
+ path: "test.cs",
50
+ get children() {
51
+ return _$createComponent(List, {
52
+ get children() {
53
+ return [_$createComponent(VarDeclaration, {
54
+ name: "testVar",
55
+ refkey: key,
56
+ children: "42"
57
+ }), _$createComponent(VarDeclaration, {
58
+ name: "testVar2",
59
+ children: key
60
+ })];
61
+ }
62
+ });
63
+ }
64
+ });
65
+ }
66
+ })).toRenderTo(`
67
+ namespace TestCode
68
+ {
69
+ var testVar = 42;
70
+ var testVar2 = testVar;
71
+ }
72
+ `);
73
+ });
@@ -1,8 +1,14 @@
1
- export type AccessModifier = "public" | "protected" | "private" | "internal" | "protected-internal" | "private-protected" | "file";
2
- export declare function getAccessModifier(accessModifier?: AccessModifier): string;
3
- export type MethodModifier = "abstract" | "sealed" | "static" | "virtual";
4
- export declare function getMethodModifier(methodModifier?: MethodModifier): string;
1
+ /** Access modifiers. */
2
+ export interface AccessModifiers {
3
+ readonly public?: boolean;
4
+ readonly protected?: boolean;
5
+ readonly private?: boolean;
6
+ readonly internal?: boolean;
7
+ readonly file?: boolean;
8
+ }
9
+ export declare const getAccessModifier: (data: AccessModifiers) => string;
5
10
  export declare function getAsyncModifier(async?: boolean): string;
6
11
  /** Resolve the modifier prefix */
7
12
  export declare function computeModifiersPrefix(modifiers: Array<string | undefined>): string;
13
+ export declare function makeModifiers<T>(obj: Array<keyof T>): (data: T) => string;
8
14
  //# sourceMappingURL=modifiers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"modifiers.d.ts","sourceRoot":"","sources":["../../src/modifiers.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,cAAc,GACtB,QAAQ,GACR,WAAW,GACX,SAAS,GACT,UAAU,GACV,oBAAoB,GACpB,mBAAmB,GACnB,MAAM,CAAC;AAgBX,wBAAgB,iBAAiB,CAAC,cAAc,CAAC,EAAE,cAAc,GAAG,MAAM,CAEzE;AAED,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAC;AAa1E,wBAAgB,iBAAiB,CAAC,cAAc,CAAC,EAAE,cAAc,GAAG,MAAM,CAEzE;AAED,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAExD;AAED,kCAAkC;AAClC,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,KAAK,CAAC,MAAM,GAAG,SAAS,CAAC,GACnC,MAAM,CAGR"}
1
+ {"version":3,"file":"modifiers.d.ts","sourceRoot":"","sources":["../../src/modifiers.ts"],"names":[],"mappings":"AAGA,yBAAyB;AACzB,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,eAAO,MAAM,iBAAiB,mCAM5B,CAAC;AAEH,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAExD;AAED,kCAAkC;AAClC,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,KAAK,CAAC,MAAM,GAAG,SAAS,CAAC,GACnC,MAAM,CAGR;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,IAC1C,MAAM,CAAC,YAMhB"}
@@ -1,43 +1,20 @@
1
1
  // the possible C# access modifiers
2
2
  // https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
3
3
 
4
- // maps the above access modifier value to its C# syntax.
5
- // note that the C# keyword includes a trailing space
6
- const accessModifierLookup = {
7
- public: "public ",
8
- protected: "protected ",
9
- private: "private ",
10
- internal: "internal ",
11
- "protected-internal": "protected internal ",
12
- "private-protected": "private protected ",
13
- file: "file "
14
- };
4
+ /** Access modifiers. */
15
5
 
16
- // returns the C# syntax for the specified access modifier.
17
- // if no access modifier is specified, the empty string is returned.
18
- export function getAccessModifier(accessModifier) {
19
- return accessModifier ? accessModifierLookup[accessModifier] : "";
20
- }
21
- // maps the above method modifier value to its C# syntax.
22
- // note that the C# keyword includes a trailing space
23
- const methodModifierLookup = {
24
- abstract: "abstract ",
25
- sealed: "sealed ",
26
- static: "static ",
27
- virtual: "virtual "
28
- };
29
-
30
- // returns the C# syntax for the specified method modifier.
31
- // if no method modifier is specified, the empty string is returned.
32
- export function getMethodModifier(methodModifier) {
33
- return methodModifier ? methodModifierLookup[methodModifier] : "";
34
- }
6
+ export const getAccessModifier = makeModifiers(["public", "protected", "private", "internal", "file"]);
35
7
  export function getAsyncModifier(async) {
36
- return async ? "async " : "";
8
+ return async ? "async" : "";
37
9
  }
38
10
 
39
11
  /** Resolve the modifier prefix */
40
12
  export function computeModifiersPrefix(modifiers) {
41
13
  const resolved = modifiers.filter(x => x);
42
- return resolved.length > 0 ? resolved.join("") : "";
14
+ return resolved.length > 0 ? resolved.join(" ") + " " : "";
15
+ }
16
+ export function makeModifiers(obj) {
17
+ return data => {
18
+ return obj.map(key => data[key] ? key : undefined).filter(x => x).join(" ");
19
+ };
43
20
  }
@@ -1,5 +1,5 @@
1
1
  import * as core from "@alloy-js/core";
2
- export type CSharpElements = "class" | "constant" | "enum" | "enum-member" | "function" | "interface" | "class-member-private" | "class-member-public" | "class-method" | "parameter" | "type-parameter";
2
+ export type CSharpElements = "class" | "constant" | "variable" | "enum" | "enum-member" | "function" | "interface" | "record" | "class-member-private" | "class-member-public" | "class-method" | "class-property" | "parameter" | "type-parameter";
3
3
  export declare function createCSharpNamePolicy(): core.NamePolicy<CSharpElements>;
4
4
  export declare function useCSharpNamePolicy(): core.NamePolicy<CSharpElements>;
5
5
  //# sourceMappingURL=name-policy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"name-policy.d.ts","sourceRoot":"","sources":["../../src/name-policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAC;AAIvC,MAAM,MAAM,cAAc,GACtB,OAAO,GACP,UAAU,GACV,MAAM,GACN,aAAa,GACb,UAAU,GACV,WAAW,GACX,sBAAsB,GACtB,qBAAqB,GACrB,cAAc,GACd,WAAW,GACX,gBAAgB,CAAC;AAGrB,wBAAgB,sBAAsB,IAAI,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAiBxE;AAGD,wBAAgB,mBAAmB,IAAI,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAErE"}
1
+ {"version":3,"file":"name-policy.d.ts","sourceRoot":"","sources":["../../src/name-policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAC;AAIvC,MAAM,MAAM,cAAc,GACtB,OAAO,GACP,UAAU,GACV,UAAU,GACV,MAAM,GACN,aAAa,GACb,UAAU,GACV,WAAW,GACX,QAAQ,GACR,sBAAsB,GACtB,qBAAqB,GACrB,cAAc,GACd,gBAAgB,GAChB,WAAW,GACX,gBAAgB,CAAC;AAGrB,wBAAgB,sBAAsB,IAAI,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAmBxE;AAGD,wBAAgB,mBAAmB,IAAI,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAErE"}
@@ -11,9 +11,11 @@ export function createCSharpNamePolicy() {
11
11
  case "enum":
12
12
  case "enum-member":
13
13
  case "interface":
14
+ case "record":
14
15
  case "class-member-public":
15
16
  case "class-method":
16
17
  case "type-parameter":
18
+ case "class-property":
17
19
  return changecase.pascalCase(name);
18
20
  case "constant":
19
21
  return changecase.constantCase(name);
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=class-declaration.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class-declaration.test.d.ts","sourceRoot":"","sources":["../../test/class-declaration.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,463 @@
1
+ import { createComponent as _$createComponent, mergeProps as _$mergeProps, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import * as core from "@alloy-js/core";
3
+ import { List, refkey } from "@alloy-js/core";
4
+ import * as coretest from "@alloy-js/core/testing";
5
+ import { describe, expect, it } from "vitest";
6
+ import * as csharp from "../src/index.js";
7
+ import { ClassDeclaration, ClassMember, Property, SourceFile } from "../src/index.js";
8
+ import * as utils from "./utils.js";
9
+ it("declares class with no members", () => {
10
+ expect(_$createComponent(utils.TestNamespace, {
11
+ get children() {
12
+ return _$createComponent(ClassDeclaration, {
13
+ name: "TestClass"
14
+ });
15
+ }
16
+ })).toRenderTo(`
17
+ class TestClass;
18
+ `);
19
+ });
20
+ describe("modifiers", () => {
21
+ it.each(["public", "private"])("%s", mod => {
22
+ expect(_$createComponent(utils.TestNamespace, {
23
+ get children() {
24
+ return _$createComponent(ClassDeclaration, _$mergeProps({
25
+ [mod]: true
26
+ }, {
27
+ name: "TestClass"
28
+ }));
29
+ }
30
+ })).toRenderTo(`
31
+ ${mod} class TestClass;
32
+ `);
33
+ });
34
+ it.each(["partial", "abstract", "static", "sealed"])("%s", mod => {
35
+ expect(_$createComponent(utils.TestNamespace, {
36
+ get children() {
37
+ return _$createComponent(ClassDeclaration, _$mergeProps({
38
+ [mod]: true
39
+ }, {
40
+ name: "TestClass"
41
+ }));
42
+ }
43
+ })).toRenderTo(`
44
+ ${mod} class TestClass;
45
+ `);
46
+ });
47
+ it("combines modifiers", () => {
48
+ expect(_$createComponent(utils.TestNamespace, {
49
+ get children() {
50
+ return _$createComponent(ClassDeclaration, {
51
+ "public": true,
52
+ abstract: true,
53
+ partial: true,
54
+ name: "TestClass"
55
+ });
56
+ }
57
+ })).toRenderTo(`
58
+ public abstract partial class TestClass;
59
+ `);
60
+ });
61
+ });
62
+ describe("base", () => {
63
+ it("define base class", () => {
64
+ expect(_$createComponent(utils.TestNamespace, {
65
+ get children() {
66
+ return _$createComponent(csharp.ClassDeclaration, {
67
+ name: "TestClass",
68
+ baseType: "Foo"
69
+ });
70
+ }
71
+ })).toRenderTo(`
72
+ class TestClass : Foo;
73
+ `);
74
+ });
75
+ it("define multiple interface types", () => {
76
+ expect(_$createComponent(utils.TestNamespace, {
77
+ get children() {
78
+ return _$createComponent(csharp.ClassDeclaration, {
79
+ name: "TestClass",
80
+ interfaceTypes: ["Foo", "Bar"]
81
+ });
82
+ }
83
+ })).toRenderTo(`
84
+ class TestClass : Foo, Bar;
85
+ `);
86
+ });
87
+ it("define base class and multiple interface types", () => {
88
+ expect(_$createComponent(utils.TestNamespace, {
89
+ get children() {
90
+ return _$createComponent(csharp.ClassDeclaration, {
91
+ name: "TestClass",
92
+ baseType: "BaseClass",
93
+ interfaceTypes: ["Foo", "Bar"]
94
+ });
95
+ }
96
+ })).toRenderTo(`
97
+ class TestClass : BaseClass, Foo, Bar;
98
+ `);
99
+ });
100
+ });
101
+ it("declares class with some members", () => {
102
+ const res = utils.toSourceText(_$createComponent(csharp.ClassDeclaration, {
103
+ "public": true,
104
+ name: "TestClass",
105
+ get children() {
106
+ return _$createComponent(core.StatementList, {
107
+ get children() {
108
+ return [_$createComponent(csharp.ClassMember, {
109
+ "public": true,
110
+ name: "MemberOne",
111
+ type: "string"
112
+ }), _$createComponent(csharp.ClassMember, {
113
+ "private": true,
114
+ name: "MemberTwo",
115
+ type: "int"
116
+ })];
117
+ }
118
+ });
119
+ }
120
+ }));
121
+ expect(res).toBe(coretest.d`
122
+ namespace TestCode
123
+ {
124
+ public class TestClass
125
+ {
126
+ public string MemberOne;
127
+ private int memberTwo;
128
+ }
129
+ }
130
+ `);
131
+ });
132
+ it("declares class with some methods", () => {
133
+ const res = utils.toSourceText(_$createComponent(csharp.ClassDeclaration, {
134
+ "public": true,
135
+ name: "TestClass",
136
+ get children() {
137
+ return _$createComponent(core.List, {
138
+ get children() {
139
+ return [_$createComponent(csharp.ClassMethod, {
140
+ "public": true,
141
+ name: "MethodOne"
142
+ }), _$createComponent(csharp.ClassMethod, {
143
+ "private": true,
144
+ virtual: true,
145
+ name: "MethodTwo"
146
+ })];
147
+ }
148
+ });
149
+ }
150
+ }));
151
+ expect(res).toBe(coretest.d`
152
+ namespace TestCode
153
+ {
154
+ public class TestClass
155
+ {
156
+ public void MethodOne() {}
157
+ private virtual void MethodTwo() {}
158
+ }
159
+ }
160
+ `);
161
+ });
162
+ it("uses refkeys for members, params, and return type", () => {
163
+ const inputTypeRefkey = core.refkey();
164
+ const testResultTypeRefkey = core.refkey();
165
+ const enumTypeRefkey = core.refkey();
166
+ const params = [{
167
+ name: "IntParam",
168
+ type: "int"
169
+ }, {
170
+ name: "BodyParam",
171
+ type: inputTypeRefkey
172
+ }];
173
+ const res = core.render(_$createComponent(core.Output, {
174
+ get namePolicy() {
175
+ return csharp.createCSharpNamePolicy();
176
+ },
177
+ get children() {
178
+ return _$createComponent(csharp.Namespace, {
179
+ name: "TestCode",
180
+ get children() {
181
+ return _$createComponent(csharp.SourceFile, {
182
+ path: "Test.cs",
183
+ get children() {
184
+ return [_$createComponent(csharp.EnumDeclaration, {
185
+ "public": true,
186
+ name: "TestEnum",
187
+ refkey: enumTypeRefkey,
188
+ get children() {
189
+ return _$createComponent(core.List, {
190
+ comma: true,
191
+ hardline: true,
192
+ get children() {
193
+ return [_$createComponent(csharp.EnumMember, {
194
+ name: "One"
195
+ }), _$createComponent(csharp.EnumMember, {
196
+ name: "Two"
197
+ })];
198
+ }
199
+ });
200
+ }
201
+ }), _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassDeclaration, {
202
+ "public": true,
203
+ name: "TestInput",
204
+ refkey: inputTypeRefkey
205
+ }), _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassDeclaration, {
206
+ "public": true,
207
+ name: "TestResult",
208
+ refkey: testResultTypeRefkey
209
+ }), _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassDeclaration, {
210
+ "public": true,
211
+ name: "TestClass",
212
+ get children() {
213
+ return [_$createComponent(csharp.ClassMember, {
214
+ "private": true,
215
+ name: "MemberOne",
216
+ type: enumTypeRefkey
217
+ }), ";", _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassMethod, {
218
+ "public": true,
219
+ name: "MethodOne",
220
+ parameters: params,
221
+ returns: testResultTypeRefkey,
222
+ get children() {
223
+ return ["return new ", testResultTypeRefkey, "();"];
224
+ }
225
+ })];
226
+ }
227
+ })];
228
+ }
229
+ });
230
+ }
231
+ });
232
+ }
233
+ }));
234
+ expect(res.contents[0].contents).toBe(coretest.d`
235
+ namespace TestCode
236
+ {
237
+ public enum TestEnum
238
+ {
239
+ One,
240
+ Two
241
+ }
242
+ public class TestInput;
243
+ public class TestResult;
244
+ public class TestClass
245
+ {
246
+ private TestEnum memberOne;
247
+ public TestResult MethodOne(int intParam, TestInput bodyParam)
248
+ {
249
+ return new TestResult();
250
+ }
251
+ }
252
+ }
253
+ `);
254
+ });
255
+ describe("with type parameters", () => {
256
+ it("reference parameters", () => {
257
+ const typeParameters = [{
258
+ name: "T",
259
+ refkey: refkey()
260
+ }, {
261
+ name: "U",
262
+ refkey: refkey()
263
+ }];
264
+ expect(_$createComponent(utils.TestNamespace, {
265
+ get children() {
266
+ return _$createComponent(SourceFile, {
267
+ path: "Test.cs",
268
+ get children() {
269
+ return _$createComponent(ClassDeclaration, {
270
+ "public": true,
271
+ name: "TestClass",
272
+ typeParameters: typeParameters,
273
+ get children() {
274
+ return _$createComponent(List, {
275
+ get children() {
276
+ return [_$createComponent(Property, {
277
+ name: "PropA",
278
+ get type() {
279
+ return typeParameters[0].refkey;
280
+ },
281
+ get: true,
282
+ set: true
283
+ }), _$createComponent(Property, {
284
+ name: "PropB",
285
+ get type() {
286
+ return typeParameters[1].refkey;
287
+ },
288
+ get: true,
289
+ set: true
290
+ })];
291
+ }
292
+ });
293
+ }
294
+ });
295
+ }
296
+ });
297
+ }
298
+ })).toRenderTo(`
299
+ namespace TestCode
300
+ {
301
+ public class TestClass<T, U>
302
+ {
303
+ T PropA { get; set; }
304
+ U PropB { get; set; }
305
+ }
306
+ }
307
+ `);
308
+ });
309
+ it("defines with constraints", () => {
310
+ const typeParameters = [{
311
+ name: "T",
312
+ constraints: "IFoo"
313
+ }, {
314
+ name: "U",
315
+ constraints: "IBar"
316
+ }];
317
+ expect(_$createComponent(utils.TestNamespace, {
318
+ get children() {
319
+ return _$createComponent(ClassDeclaration, {
320
+ "public": true,
321
+ name: "TestClass",
322
+ typeParameters: typeParameters,
323
+ children: "// Body"
324
+ });
325
+ }
326
+ })).toRenderTo(`
327
+ public class TestClass<T, U>
328
+ where T : IFoo
329
+ where U : IBar
330
+ {
331
+ // Body
332
+ }
333
+ `);
334
+ });
335
+ });
336
+ it("declares class with invalid members", () => {
337
+ const decl = _$createComponent(csharp.ClassDeclaration, {
338
+ "public": true,
339
+ name: "TestClass",
340
+ get children() {
341
+ return [_$createComponent(csharp.EnumMember, {
342
+ name: "One"
343
+ }), ",", _$createIntrinsic("hbr", {}), _$createComponent(csharp.EnumMember, {
344
+ name: "Two"
345
+ })];
346
+ }
347
+ });
348
+ expect(() => utils.toSourceText(decl)).toThrow("can't define an enum member outside of an enum-decl scope");
349
+ });
350
+ it("declares class with constructor", () => {
351
+ const res = utils.toSourceText(_$createComponent(csharp.ClassDeclaration, {
352
+ "public": true,
353
+ name: "TestClass",
354
+ get children() {
355
+ return _$createComponent(csharp.ClassConstructor, {
356
+ "public": true
357
+ });
358
+ }
359
+ }));
360
+ expect(res).toBe(coretest.d`
361
+ namespace TestCode
362
+ {
363
+ public class TestClass
364
+ {
365
+ public TestClass() {}
366
+ }
367
+ }
368
+ `);
369
+ });
370
+ it("declares class with constructor params and assigns values to fields", () => {
371
+ const thisNameRefkey = core.refkey();
372
+ const thisSizeRefkey = core.refkey();
373
+ const paramNameRefkey = core.refkey();
374
+ const paramSizeRefkey = core.refkey();
375
+ const ctorParams = [{
376
+ name: "name",
377
+ type: "string",
378
+ refkey: paramNameRefkey
379
+ }, {
380
+ name: "size",
381
+ type: "int",
382
+ refkey: paramSizeRefkey
383
+ }];
384
+ const res = utils.toSourceText(_$createComponent(csharp.ClassDeclaration, {
385
+ "public": true,
386
+ name: "TestClass",
387
+ get children() {
388
+ return [_$createComponent(csharp.ClassMember, {
389
+ "private": true,
390
+ name: "name",
391
+ type: "string",
392
+ refkey: thisNameRefkey
393
+ }), ";", _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassMember, {
394
+ "private": true,
395
+ name: "size",
396
+ type: "int",
397
+ refkey: thisSizeRefkey
398
+ }), ";", _$createIntrinsic("hbr", {}), _$createComponent(csharp.ClassConstructor, {
399
+ "public": true,
400
+ parameters: ctorParams,
401
+ get children() {
402
+ return [thisNameRefkey, " = ", paramNameRefkey, ";", _$createIntrinsic("hbr", {}), thisSizeRefkey, " = ", paramSizeRefkey, ";"];
403
+ }
404
+ })];
405
+ }
406
+ }));
407
+
408
+ // TODO: assignments to members should have this. prefix
409
+ // e.g. this.name = name;
410
+ expect(res).toBe(coretest.d`
411
+ namespace TestCode
412
+ {
413
+ public class TestClass
414
+ {
415
+ private string name;
416
+ private int size;
417
+ public TestClass(string name, int size)
418
+ {
419
+ name = name;
420
+ size = size;
421
+ }
422
+ }
423
+ }
424
+ `);
425
+ });
426
+ it("specify doc comment", () => {
427
+ expect(_$createComponent(utils.TestNamespace, {
428
+ get children() {
429
+ return _$createComponent(ClassDeclaration, {
430
+ name: "Test",
431
+ doc: "This is a test"
432
+ });
433
+ }
434
+ })).toRenderTo(`
435
+ /// This is a test
436
+ class Test;
437
+ `);
438
+ });
439
+ it("supports class member doc comments", () => {
440
+ expect(_$createComponent(utils.TestNamespace, {
441
+ get children() {
442
+ return _$createComponent(ClassDeclaration, {
443
+ name: "Test",
444
+ doc: "This is a test",
445
+ get children() {
446
+ return _$createComponent(ClassMember, {
447
+ name: "Member",
448
+ "public": true,
449
+ type: "int",
450
+ doc: "This is a member"
451
+ });
452
+ }
453
+ });
454
+ }
455
+ })).toRenderTo(`
456
+ /// This is a test
457
+ class Test
458
+ {
459
+ /// This is a member
460
+ public int Member
461
+ }
462
+ `);
463
+ });