1 module it.docs;
2 
3 import it;
4 import dpp.translation;
5 import unit_threaded: shouldEqual, Sandbox;
6 
7 import std.file: readText;
8 import std.string: indexOf;
9 
10 import unit_threaded.assertions: shouldBeIn;
11 
12 auto normalizeLines(string text) {
13     import std.string: strip, lineSplitter;
14     import std.algorithm: map;
15     import std.array: join;
16     return text.lineSplitter().map!(line => line.strip()).join('\n');
17 }
18 
19 
20 version(Windows) {
21     @("The Cpp documentation is preserved")
22     @safe unittest {
23         with(immutable IncludeSandbox()) {
24 
25             writeFile("hdr.hpp",
26                       `
27                           /** f1 doc */
28                           void f1() {}
29 
30                           // f2 non-doc
31                           void f2() {}
32 
33                           /// f3 doc
34                           void f3() {}
35 
36                           /** variable1 doc */
37                           int variable1 = 1 + 2;
38 
39                           /// variable2 doc
40                           int variable2 = 1 + 2;
41 
42                           // variable3 non-doc
43                           int variable3 = 1 + 2;
44 
45 
46                           /** Struct1 doc */
47                           struct Struct1 {
48 
49                             /** prop1 doc */
50                             int prop1;
51 
52                             /** method1 doc */
53                             void method1();
54                           };
55 
56 
57                           /** Enum1 doc */
58                           enum Enum1 {
59                             /** x doc */
60                             x,
61                             /// y doc
62                             y,
63                             // z non doc
64                             z,
65                           };
66 
67                           /** Type1 doc */
68                           typedef int Type1;`);
69             writeFile("main.dpp",
70                       `
71                           #include "hdr.hpp"
72                       `);
73             runPreprocessOnly(
74                 "--source-output-path",
75                 inSandboxPath("foo/bar"),
76                 "main.dpp",
77             );
78 
79             auto originalText = readText(inSandboxPath("foo/bar/main.d"));
80             auto needle = `extern(C++)
81             {
82                 /** Type1 doc */
83                 alias Type1 = int;
84                 /** Enum1 doc */
85                 enum Enum1
86                 {
87                     /** x doc */
88                     x = 0,
89                     /// y doc
90                     y = 1,
91                     /// y doc
92                     z = 2,
93                 }
94                 enum x = Enum1.x;
95                 enum y = Enum1.y;
96                 enum z = Enum1.z;
97                 /** Struct1 doc */
98                 struct Struct1
99                 {
100                     /** prop1 doc */
101                     int prop1;
102                     /** method1 doc */
103                     pragma(mangle, "?method1@Struct1@@QEAAXXZ") void method1() @nogc nothrow;
104                 }
105 
106                 pragma(mangle, "?variable3@@3HA") extern __gshared int variable3;
107                 /// variable2 doc
108                 pragma(mangle, "?variable2@@3HA") extern __gshared int variable2;
109                 /** variable1 doc */
110                 pragma(mangle, "?variable1@@3HA") extern __gshared int variable1;
111                 /// f3 doc
112                 pragma(mangle, "?f3@@YAXXZ") void f3() @nogc nothrow;
113 
114                 pragma(mangle, "?f2@@YAXXZ") void f2() @nogc nothrow;
115                 /** f1 doc */
116                 pragma(mangle, "?f1@@YAXXZ") void f1() @nogc nothrow;
117             }`;
118         needle.normalizeLines().shouldBeIn(originalText[originalText.indexOf("extern(C++)")..$].normalizeLines());
119 
120         }
121     }
122 }
123 
124 @("The C documentation is preserved")
125 @safe unittest {
126     with(immutable IncludeSandbox()) {
127         writeFile("hdr.h",
128                   `
129                       /** f1 doc */
130                       void f1() {}
131 
132                       // f2 non-doc
133                       void f2() {}
134 
135                       /// f3 doc
136                       void f3() {}
137 
138                       /** variable1 doc */
139                       int variable1 = 1 + 2;
140 
141                       /// variable2 doc
142                       int variable2 = 1 + 2;
143 
144                       // variable3 non-doc
145                       int variable3 = 1 + 2;
146 
147 
148                       /** Struct1 doc */
149                       struct Struct1 {
150 
151                         /** prop1 doc */
152                         int prop1;
153                       };
154 
155 
156                       /** Enum1 doc */
157                       enum Enum1 {
158                         /** x doc */
159                         x,
160                         /// y doc
161                         y,
162                         // z non doc
163                         z,
164                       };
165 
166                       /** Type1 doc */
167                       typedef int Type1;
168                   `);
169         writeFile("main.dpp",
170                   `
171                       #include "hdr.h"
172                   `);
173         runPreprocessOnly(
174             "--source-output-path",
175             inSandboxPath("foo/bar"),
176             "main.dpp",
177         );
178 
179         auto originalText = readText(inSandboxPath("foo/bar/main.d"));
180         auto needle = `extern(C)
181         {
182             /** Type1 doc */
183             alias Type1 = int;
184             /** Enum1 doc */
185             enum Enum1
186             {
187                 /** x doc */
188                 x = 0,
189                 /// y doc
190                 y = 1,
191                 /// y doc
192                 z = 2,
193             }
194             enum x = Enum1.x;
195             enum y = Enum1.y;
196             enum z = Enum1.z;
197             /** Struct1 doc */
198             struct Struct1
199             {
200                 /** prop1 doc */
201                 int prop1;
202             }
203 
204             extern __gshared int variable3;
205             /// variable2 doc
206             extern __gshared int variable2;
207             /** variable1 doc */
208             extern __gshared int variable1;
209             /// f3 doc
210             void f3() @nogc nothrow;
211 
212             void f2() @nogc nothrow;
213             /** f1 doc */
214             void f1() @nogc nothrow;
215         }`;
216         needle.normalizeLines().shouldBeIn(originalText[originalText.indexOf("extern(C)")..$].normalizeLines());
217     }
218 }