PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/org/d/model/FormatterTest.java

https://bitbucket.org/ariovistus/d-jvm-cc
Java | 672 lines | 622 code | 34 blank | 16 comment | 17 complexity | 55a2459eb3df821fd38ccffc2faddbde MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package org.d.model;
  6. import org.netbeans.modules.cnd.antlr.collections.AST;
  7. import java.io.StringReader;
  8. import java.text.MessageFormat;
  9. import junit.framework.TestCase;
  10. import org.d.generated.D2Lexer;
  11. import org.d.generated.D2Parser;
  12. import org.d.generated.DLexer;
  13. import org.d.generated.DParser;
  14. /**
  15. *
  16. * @author ellery
  17. */
  18. public class FormatterTest extends TestCase {
  19. public static DParser parser(String s) {
  20. DLexer lexer = new DLexer(new StringReader(s));
  21. DParser parser = new DParser(lexer);
  22. parser.initWithLines();
  23. return parser;
  24. }
  25. public static D2Parser parser2(String s) {
  26. D2Lexer lexer = new D2Lexer(new StringReader(s));
  27. D2Parser parser = new D2Parser(lexer);
  28. parser.initWithLines();
  29. return parser;
  30. }
  31. public static AST module(String s) {
  32. DParser parser = parser(s);
  33. parser.module();
  34. if (parser.errors.isEmpty()) {
  35. return parser.getAST();
  36. }
  37. return null;
  38. }
  39. public static AST module2(String s) {
  40. D2Parser parser = parser2(s);
  41. parser.module();
  42. if (parser.errors.isEmpty()) {
  43. return parser.getAST();
  44. }
  45. return null;
  46. }
  47. public static AST expression(String s) {
  48. DParser parser = parser(s);
  49. parser.exp();
  50. if (parser.errors.isEmpty()) {
  51. return parser.getAST();
  52. }
  53. return null;
  54. }
  55. public static AST expression2(String s) {
  56. D2Parser parser = parser2(s);
  57. parser.exp();
  58. if (parser.errors.isEmpty()) {
  59. return parser.getAST();
  60. }
  61. return null;
  62. }
  63. public static AST statement(String s) {
  64. DParser parser = parser(s);
  65. parser.statement();
  66. if (parser.errors.isEmpty()) {
  67. return parser.getAST();
  68. }
  69. return null;
  70. }
  71. public static AST statement2(String s) {
  72. D2Parser parser = parser2(s);
  73. parser.statement();
  74. if (parser.errors.isEmpty()) {
  75. return parser.getAST();
  76. }
  77. return null;
  78. }
  79. public static AST declDef(String s) {
  80. DParser parser = parser(s);
  81. parser.declDef();
  82. if (parser.errors.isEmpty()) {
  83. return parser.getAST();
  84. }
  85. return null;
  86. }
  87. public static AST declDef2(String s) {
  88. D2Parser parser = parser2(s);
  89. parser.declDef();
  90. if (parser.errors.isEmpty()) {
  91. return parser.getAST();
  92. }
  93. return null;
  94. }
  95. public FormatterTest(String testName) {
  96. super(testName);
  97. }
  98. @Override
  99. protected void setUp() throws Exception {
  100. super.setUp();
  101. }
  102. @Override
  103. protected void tearDown() throws Exception {
  104. super.tearDown();
  105. }
  106. /**
  107. * Test of format method, of class Formatter.
  108. */
  109. public void testFormat() {
  110. System.out.println("format");
  111. String[][] expressions = {
  112. {"a+b*c-d(e,f)^j;", "a + b * c - d(e, f) ^ j"},
  113. {"- a;", "-a"},
  114. {"a & - a;", "a & -a"},
  115. {"(a,b,c);", "(a, b, c)"},
  116. {"delete a;", "delete a"},
  117. {"cast( int )1.0;", "cast(int) 1.0"},
  118. {"int . max ;", "int.max"},
  119. {"new(1,2) int[][](1,2);", "new(1, 2) int[][](1, 2)"},
  120. {"a is null;", "a is null"},
  121. {"a !is null;", "a !is null"},
  122. {"a in null;", "a in null"},
  123. {"is(int);", "is(int)"},
  124. {"is(int i:j);", "is(int i: j)"},
  125. {"is( int[ ] i == j);", "is(int[] i == j)"},
  126. {"is( int == return);", "is(int == return)"},
  127. {"is( int == class);", "is(int == class)"},
  128. {"is( int == interface);", "is(int == interface)"},
  129. {"is( int == super);", "is(int == super)"},
  130. {"is( int == struct);", "is(int == struct)"},
  131. {"is( int == union);", "is(int == union)"},
  132. {"is( i == function);", "is(i == function)"},
  133. {"is( i == delegate);", "is(i == delegate)"},
  134. {"is( i == enum);", "is(i == enum)"},
  135. {"typeid(int);", "typeid(int)"},
  136. {"typeof(int.max);", "typeof(int.max)"},
  137. {"import(\"waga\");", "import(\"waga\")"},
  138. {"i!(i,j,k);", "i!(i, j, k)"},
  139. {"[1,2,3];", "[1, 2, 3]"},
  140. {"[1:\"a\",2:\"b\",3:\"c\"];", "[1: \"a\", 2: \"b\", 3: \"c\"]"},
  141. {"{ return 1; };", "{return 1;}"},
  142. {"delegate { return 1; };", "delegate {return 1;}"},
  143. {"delegate { };", "delegate {}"},
  144. {"delegate int{ return 1; };", "delegate int{return 1;}"},
  145. {"delegate int(int j){ return 1; };", "delegate int(int j){return 1;}"},
  146. {"(int j){ return 1; };", "(int j){return 1;}"},
  147. {"assert(false, \"hi!\");", "assert(false, \"hi!\")"},
  148. {"assert(true );", "assert(true)"},
  149. {"a[i ..j];", "a[i .. j]"},
  150. {"a[i ];", "a[i]"},
  151. {"a[i , j , k];", "a[i, j, k]"},
  152. {"a?b:c;", "a ? b : c"},
  153. {"&a;", "&a"},
  154. {"b&a;", "b & a"},
  155. {"+a;", "+a"},
  156. {"b+a;", "b + a"},
  157. {"-a;", "-a"},
  158. {"b-a;", "b - a"},
  159. {"~a;", "~a"},
  160. {"b~a;", "b ~ a"},
  161. {"*a;", "*a"},
  162. {"b*a;", "b * a"},
  163. {"++a;", "++a"},
  164. {"a++;", "a++"},
  165. {"--a;", "--a"},
  166. {"a--;", "a--"},
  167. {"!a--;", "!a--"},
  168. {"`a` `b`;", "`a` `b`"},
  169. {"__LINE__ is __DUMB__;", "__LINE__ is __DUMB__"},
  170. {"i!();", "i!()"},
  171. {"new class (1) T {};", "new class(1) T {\n}"},
  172. {"mixin(hi);", "mixin(hi)"},
  173. {"(int*).max;", "(int*).max"},};
  174. String[][] expressions2 = {
  175. {"a ^^ b;", "a ^^ b"},
  176. {"a ^^= b;", "a ^^= b"},
  177. {"__traits(me);", "__traits(me)"},
  178. {"__traits(me,you);", "__traits(me, you)"},
  179. {"__traits(me,you,us);", "__traits(me, you, us)"},
  180. {"q{ /*doh*/ }c;", "q{ /*doh*/ }c"},
  181. {"a ! in b;", "a !in b"},
  182. {"is(I J == K, L);", "is(I J == K, L)"},
  183. {"is(I J == K, L, M);", "is(I J == K, L, M)"},
  184. {"i!i;", "i!i"},
  185. {"to!int(`1`);", "to!int(`1`)"},};
  186. String[][] statements = {
  187. {"int i = 0;", "int i = 0;"},
  188. {"int i = 0, b = 1, c = 2;", "int i = 0, b = 1, c = 2;"},
  189. {"int* i = 1;", "int* i = 1;"},
  190. {"int delegate(int)* i = 1;", "int delegate(int)* i = 1;"},
  191. {"int delegate(int) delegate(double,int) i = 1;", "int delegate(int) delegate(double, int) i = 1;"},
  192. {"int [int*] i = 1;", "int[int*] i = 1;"},
  193. {"int[3] [ 4] i = 1;", "int[3][4] i = 1;"},
  194. {"int[] *i = 1;", "int[]* i = 1;"},
  195. {"typeof(1)[] i = 1;", "typeof(1)[] i = 1;"},
  196. {"MAHBIGLABEL: i;", "MAHBIGLABEL: i;"},
  197. {"mixin(\"It's a trap!\");", "mixin(\"It's a trap!\");"},
  198. {"mixin I.I!(I);", "mixin I.I!(I);"},
  199. {"mixin .I!(I);", "mixin .I!(I);"},
  200. {"mixin .I.I!(I);", "mixin .I.I!(I);"},
  201. {"mixin typeof(J).I.I!(I);", "mixin typeof(J).I.I!(I);"},
  202. {"mixin typeof(J).I.I!(I);", "mixin typeof(J).I.I!(I);"},
  203. {"mixin I.I!(I) FOO;", "mixin I.I!(I) FOO;"},
  204. {"mixin .I.I!(I) FOO;", "mixin .I.I!(I) FOO;"},
  205. {"mixin typeof(J).I.I!(I) FOO;", "mixin typeof(J).I.I!(I) FOO;"},
  206. {"abstract class X{}", "abstract class X {\n}"},
  207. {"abstract class X{int i;}", "abstract class X {\n int i;\n}"},
  208. {"static assert(1,-1);", "static assert(1, -1);"},
  209. {"static assert (1);", "static assert(1);"},
  210. {"version(1){ int i; };", "version(1) {\n int i;\n}"},
  211. {"version(i){ int i; };", "version(i) {\n int i;\n}"},
  212. {"version(i){ int i; }else{ int j;};", "version(i) {\n int i;\n} else {\n int j;\n}"},
  213. {"version(i){ int i; }else version(j){ int j;};", "version(i) {\n int i;\n} else version(j) {\n int j;\n}"},
  214. {"version(i) int i = 0;;", "version(i) int i = 0;"},
  215. {"debug{ int i; };", "debug {\n int i;\n}"},
  216. {"debug(1){ int i; };", "debug(1) {\n int i;\n}"},
  217. {"debug(i){ int i; };", "debug(i) {\n int i;\n}"},
  218. {"debug(i){ int i; }else{ int j;};", "debug(i) {\n int i;\n} else {\n int j;\n}"},
  219. {"debug(i){ int i; }else debug(j){ int j;};", "debug(i) {\n int i;\n} else debug(j) {\n int j;\n}"},
  220. {"static if(i){ int i; };", "static if(i) {\n int i;\n}"},
  221. {"static if(i){ int i; };", "static if(i) {\n int i;\n}"},
  222. {"static if(i){ int i; }else{ int j;};", "static if(i) {\n int i;\n} else {\n int j;\n}"},
  223. {"static if(i){ int i; }else static if(j){ int j;};", "static if(i) {\n int i;\n} else static if(j) {\n int j;\n}"},
  224. {"if(i){};", "if(i) {\n}"},
  225. {"if(int i = 0) {};", "if(int i = 0) {\n}"},
  226. {"if(auto i = 0){};", "if(auto i = 0) {\n}"},
  227. {"if(i){}else{};", "if(i) {\n} else {\n}"},
  228. {"if(i){}else if(j){ };", "if(i) {\n} else if(j) {\n}"},
  229. {"while(true){ true++; };", "while(true) {\n true++;\n}"},
  230. {"while(true) true++; ;", "while(true) true++;"},
  231. {"do{ true++; }while(true);", "do {\n true++;\n} while(true);"},
  232. {"do true++; while(true);", "do true++;\nwhile(true);"},
  233. {"for(;;){ writeln(i); writeln(i+1);};",
  234. "for(;;) {\n writeln(i);\n writeln(i + 1);\n}"
  235. },
  236. {"for(int i=0;;){ writeln(i); writeln(i+1);};",
  237. "for(int i = 0;;) {\n writeln(i);\n writeln(i + 1);\n}"
  238. },
  239. {"for(;i < 10;){ writeln(i); writeln(i+1);};",
  240. "for(; i < 10;) {\n writeln(i);\n writeln(i + 1);\n}"
  241. },
  242. {"for(;;i++){ writeln(i); writeln(i+1);};",
  243. "for(;; i++) {\n writeln(i);\n writeln(i + 1);\n}"
  244. },
  245. {"for(int i =0; i<10; i ++){ writeln(i); writeln(i+1);};",
  246. "for(int i = 0; i < 10; i++) {\n writeln(i);\n writeln(i + 1);\n}"
  247. },
  248. {"for(;;) i++;;", "for(;;) i++;"},
  249. {"for({} i < 10; i++) writeln(c);;",
  250. "for({\n} i < 10; i++) writeln(c);"
  251. },
  252. {"for({int i =1; char c = 'a';} i < 10; i++) writeln(c);;",
  253. "for({\n int i = 1;\n char c = 'a';\n} i < 10; i++) writeln(c);"
  254. },
  255. {"foreach(i;j) i++;;", "foreach(i; j) i++;"},
  256. {"foreach(ref i;j) i++;;", "foreach(ref i; j) i++;"},
  257. {"foreach(inout i;j) i++;;", "foreach(inout i; j) i++;"},
  258. {"foreach(int* i;j) i++;;", "foreach(int* i; j) i++;"},
  259. {"foreach(ref int* i;j) i++;;", "foreach(ref int* i; j) i++;"},
  260. {"foreach(i;j){i++;};", "foreach(i; j) {\n i++;\n}"},
  261. {"foreach_reverse(i;j) i++;;", "foreach_reverse(i; j) i++;"},
  262. {"switch(i){};", "switch(i) {\n}"},
  263. {"switch(i){ case 1,2: i++;};", "switch(i) {\n case 1, 2: i++;\n}"},
  264. {"switch(i){ case 1,2: i++; \nj--;};", "switch(i) {\n case 1, 2:\n i++;\n j--;\n}"},
  265. {"default:{ throw new Exception();};", "default: {\n throw new Exception();\n}"},
  266. {"switch(i){default: return -1;};", "switch(i) {\n default: return -1;\n}"},
  267. {"continue i;", "continue i;"},
  268. {"continue;", "continue;"},
  269. {"break;", "break;"},
  270. {"return;", "return;"},
  271. {"while(true) continue i;;", "while(true) continue i;"},
  272. {"while(true) break i;;", "while(true) break i;"},
  273. {"while(true) return i;;", "while(true) return i;"},
  274. {"while(true) goto i;;", "while(true) goto i;"},
  275. {"while(true) goto default;;", "while(true) goto default;"},
  276. {"while(true) goto case ;;", "while(true) goto case;"},
  277. {"while(true) goto case 1;;", "while(true) goto case 1;"},
  278. {"with(i){};", "with(i){\n}"},
  279. {"with(i)i++;", "with(i) i++;"},
  280. {"synchronized(i)i++;", "synchronized(i) i++;"},
  281. {"synchronized{i++;}", "synchronized {\n i++;\n}"},
  282. {"try{ i++; }catch{ i--; };", "try {\n i++;\n} catch {\n i--;\n}"},
  283. {"try{ i++; }catch(Exception ex){ i--; };", "try {\n i++;\n} catch(Exception ex) {\n i--;\n}"},
  284. {"try{ i++; }catch(Exception ex){ i--; }finally{};", "try {\n i++;\n} catch(Exception ex) {\n i--;\n} finally {\n}"},
  285. {"try{ i++; }finally{ i--; };", "try {\n i++;\n} finally {\n i--;\n}"},
  286. {"while(true) throw i;;", "while(true) throw i;"},
  287. {"scope(success) i++;", "scope(success) i++;"},
  288. {"asm{ cmp EAX 1;}", "asm {\n cmp EAX 1;\n}"}, //no, I don't know jack about asm syntax
  289. {"pragma(msg);", "pragma(msg);"},
  290. {"pragma(msg,\"hi!\");", "pragma(msg, \"hi!\");"},
  291. {"{};", "{\n}"},
  292. {"volatile int i;", "volatile int i;"},
  293. {"volatile{ int i;};", "volatile {\n int i;\n}"},
  294. {"LABEL: int i = 0;;", "LABEL: int i = 0;"},
  295. {"LABEL: ;;", "LABEL:;"},};
  296. String[][] statements2 = {
  297. {"final switch(i){};", "final switch(i) {\n}"},
  298. {"case 1: .. case 2: i++;", "case 1:\n..\ncase 2: i++;"},
  299. {"foreach(i; 1 .. 10) i++;", "foreach(i; 1 .. 10) i++;"},};
  300. String[][] decls = {
  301. {"enum I{}", "enum I {\n}"},
  302. {"enum I;", "enum I;"},
  303. {"enum I:int{}", "enum I: int {\n}"},
  304. {"enum {}", "enum {\n}"},
  305. {"enum :int{}", "enum: int {\n}"},
  306. {"enum I{ a}", "enum I {\n a,\n}"},
  307. {"enum I{ a,}", "enum I {\n a,\n}"},
  308. {"enum I{ a,b}", "enum I {\n a,\n b,\n}"},
  309. {"enum I{ a=1}", "enum I {\n a = 1,\n}"},
  310. {"enum I{ a = 1, b}", "enum I {\n a = 1,\n b,\n}"},
  311. {"class I{}", "class I {\n}"},
  312. {"class I:B{}", "class I: B {\n}"},
  313. {"class I(T){}", "class I(T) {\n}"},
  314. {"class I(T):B{}", "class I(T): B {\n}"},
  315. {"class I(T):B { static assert(green);}", "class I(T): B {\n static assert(green);\n}"},
  316. {"class I;", "class I;"},
  317. {"class I:B,C,D{}", "class I: B, C, D {\n}"},
  318. {"class I:private B{}", "class I: private B {\n}"},
  319. {"class I:B.C.D{}", "class I: B.C.D {\n}"},
  320. {"class I:B.C!(D){}", "class I: B.C!(D) {\n}"},
  321. {"interface I{}", "interface I {\n}"},
  322. {"interface I:B{}", "interface I: B {\n}"},
  323. {"interface I(T){}", "interface I(T) {\n}"},
  324. {"interface I(T):B{}", "interface I(T): B {\n}"},
  325. {"interface I(T):B{ static assert(green);}", "interface I(T): B {\n static assert(green);\n}"},
  326. {"interface I;", "interface I;"},
  327. {"interface I:B,C,D{}", "interface I: B, C, D {\n}"},
  328. {"interface I:private B{}", "interface I: private B {\n}"},
  329. {"interface I:B.C.D{}", "interface I: B.C.D {\n}"},
  330. {"interface I:B.C!(D){}", "interface I: B.C!(D) {\n}"},
  331. {"struct I{}", "struct I {\n}"},
  332. {"struct I(T){}", "struct I(T) {\n}"},
  333. {"struct I;", "struct I;"},
  334. {"struct{}", "struct {\n}"},
  335. {"union I{}", "union I {\n}"},
  336. {"union I(T){}", "union I(T) {\n}"},
  337. {"union I;", "union I;"},
  338. {"union{}", "union {\n}"},
  339. {"import i;", "import i;"},
  340. {"import i, j;", "import i, j;"},
  341. {"import i.i.i;", "import i.i.i;"},
  342. {"import I = i;", "import I = i;"},
  343. {"static import i;", "static import i;"},
  344. {"import i:j;", "import i: j;"},
  345. {"import i:j=k;", "import i: j = k;"},
  346. {"template T(){}", "template T() {\n}"},
  347. {"template T(alias i){}", "template T(alias i) {\n}"},
  348. {"template T(alias i:int){}", "template T(alias i: int) {\n}"},
  349. {"template T(alias i=int){}", "template T(alias i = int) {\n}"},
  350. {"template T(i...){}", "template T(i...) {\n}"},
  351. {"template T(int i:1){}", "template T(int i: 1) {\n}"},
  352. {"template T(int i=1){}", "template T(int i = 1) {\n}"},
  353. {"template T(i){}", "template T(i) {\n}"},
  354. {"template T(i:j){}", "template T(i: j) {\n}"},
  355. {"template T(i=j){}", "template T(i = j) {\n}"},
  356. {"template T(i, alias i, int i){}", "template T(i, alias i, int i) {\n}"},
  357. {"mixin(\"mixin(\\\"hi!\\\");\");", "mixin(\"mixin(\\\"hi!\\\");\");"},
  358. {"typedef i j;", "typedef i j;"},
  359. {"typedef deprecated i j;", "typedef deprecated i j;"},
  360. {"typedef i j =1;", "typedef i j = 1;"},
  361. {"typedef i j , k;", "typedef i j, k;"},
  362. {"typedef i j =1 , k=1;", "typedef i j = 1, k = 1;"},
  363. {"alias i j;", "alias i j;"},
  364. {"alias static i j;", "alias static i j;"},
  365. {"alias i j, k, m;", "alias i j, k, m;"},
  366. {"auto i = 1;", "auto i = 1;"},
  367. {"auto i = 1, j = 2;", "auto i = 1, j = 2;"},
  368. {"int i(int j){}", "int i(int j) {\n}"},
  369. {"int i(lazy int j){}", "int i(lazy int j) {\n}"},
  370. {"int i(in int j){}", "int i(in int j) {\n}"},
  371. {"int i(inout int j){}", "int i(inout int j) {\n}"},
  372. {"int i(out int j){}", "int i(out int j) {\n}"},
  373. {"int i(int j, int k = 1){}", "int i(int j, int k = 1) {\n}"},
  374. {"int i(int j, int k ...){}", "int i(int j, int k...) {\n}"},
  375. {"int i(int j, int ...){}", "int i(int j, int...) {\n}"},
  376. {"int i(int j, int ){}", "int i(int j, int) {\n}"},
  377. {"int i(...){}", "int i(...) {\n}"},
  378. {"int i() body{}", "int i()\nbody {\n}"},
  379. {"int i() in{}out{}body{}", "int i()\nin {\n} out {\n} body {\n}"},
  380. {"int i() in{}out(result){}body{}", "int i()\nin {\n} out(result) {\n} body {\n}"},
  381. {"this(){}", "this() {\n}"},
  382. {"this(int i){}", "this(int i) {\n}"},
  383. {"~this(){}", "~this() {\n}"},
  384. {"invariant{}", "invariant() {\n}"},
  385. {"unittest{}", "unittest {\n}"},
  386. {"new(){}", "new() {\n}"},
  387. {"new(int i){}", "new(int i) {\n}"},
  388. {"delete(){}", "delete() {\n}"},
  389. {"delete(int i){}", "delete(int i) {\n}"},
  390. {"static this(){}", "static this() {\n}"},
  391. {"static ~this(){}", "static ~this() {\n}"},
  392. {"static assert(i);", "static assert(i);"},
  393. {"static assert(i,j);", "static assert(i, j);"},
  394. {"static if(i){}", "static if(i) {\n}"},
  395. {"static if(i){}else {}", "static if(i) {\n} else {\n}"},
  396. {"private{}", "private{\n}"},
  397. {"static{}", "static{\n}"},
  398. {"static{int i;}", "static{\n int i;\n}"},
  399. {"private:int i;", "private:\n int i;"},
  400. {"public export:int i;", "public export:\n int i;"},
  401. {"static: int i;", "static:\n int i;"},
  402. {"static private: int i;", "static private:\n int i;"},
  403. {"align(16): int i;", "align(16):\n int i;"},
  404. {"align: int i;", "align:\n int i;"},
  405. {"align int i;", "align int i;"},
  406. {"export: int i;", "export:\n int i;"},
  407. {"private extern: int i;", "private extern:\n int i;"},
  408. {"private extern(C++): int i;", "private extern(C++):\n int i;"},
  409. {"private extern(Windows): int i;", "private extern(Windows):\n int i;"},
  410. {"pragma(msk): int i;", "pragma(msk):\n int i;"},
  411. {"pragma(msk,msy): int i;", "pragma(msk, msy):\n int i;"},
  412. {"debug = I;", "debug = I;"},
  413. {"debug = 1;", "debug = 1;"},
  414. {"version = i;", "version = i;"},
  415. {"version = 1;", "version = 1;"},
  416. {"debug{}", "debug {\n}"},
  417. {"debug(i){}", "debug(i) {\n}"},
  418. {"version(i){}", "version(i) {\n}"},
  419. {"int[] i = [1,2,3,];", "int[] i = [1, 2, 3];"},
  420. {"int[] i = [];", "int[] i = [];"},
  421. {"int[] i = [0:1,10:2,3,];", "int[] i = [0: 1, 10: 2, 3];"},
  422. {"Struct s = {1,2,3};", "Struct s = {1, 2, 3};"},
  423. {"Struct s = {};", "Struct s = {};"},
  424. {"Struct s = {i:1,J:2,3};", "Struct s = {i: 1, J: 2, 3};"},
  425. {"Struct[] s = [{1,2,3}, {4,5,6}];", "Struct[] s = [{1, 2, 3}, {4, 5, 6}];"},
  426. {"int[] i = void;", "int[] i = void;"},
  427. {".i* k = 1;", ".i* k = 1;"},
  428. {".i.i* k = 1;", ".i.i* k = 1;"},
  429. {"typeof(i).i.i* k = 1;", "typeof(i).i.i* k = 1;"},
  430. {"int[0..2] i = (1,2);", "int[0 .. 2] i = (1, 2);"},
  431. {"int (*i)(int) = cast(int(*)(int))function int(int){return 1;};", "int (*i)(int) = cast(int (*)(int)) function int(int){return 1;};"},};
  432. String[][] decls2 = {
  433. {"@property abstract auto int i;", "@property abstract auto int i;"},
  434. {"abstract private int i;", "abstract private int i;"},
  435. {"@Anno private int i;", "@Anno private int i;"},
  436. {"const deprecated enum int i;", "const deprecated enum int i;"},
  437. {"final __gshared immutable int i;", "final __gshared immutable int i;"},
  438. {"inout nothrow override pure int i;", "inout nothrow override pure int i;"},
  439. {"ref scope shared int i;", "ref scope shared int i;"},
  440. {"static synchronized __thread int i;", "static synchronized __thread int i;"},
  441. {"auto a(int i){}", "auto a(int i) {\n}"},
  442. {"enum I{ int i = 1,}", "enum I {\n int i = 1,\n}"},
  443. {"mixin template T(){}", "mixin template T() {\n}"},
  444. {"template T(this I){}", "template T(this I) {\n}"},
  445. {"template T(alias int i){}", "template T(alias int i) {\n}"},
  446. {"version(unittest){}", "version(unittest) {\n}"},
  447. {"class X(T) if(is(T == int)){}", "class X(T) if(is(T == int)) {\n}"},
  448. {"alias i this;", "alias i this;"},
  449. {"const(int) i;", "const(int) i;"},
  450. {"const shared(int) i;", "const shared(int) i;"},
  451. {"const (shared int) i;", "const(shared int) i;"},
  452. {"const (shared(int)) i;", "const(shared(int)) i;"},
  453. {"int i(int j) nothrow {}", "int i(int j) nothrow {\n}"},
  454. {"int i(int j) pure nothrow {}", "int i(int j) pure nothrow {\n}"},
  455. {"int i(I)(int j) nothrow if(is(I)) {}", "int i(I)(int j) nothrow if(is(I)) {\n}"},};
  456. for (String[] raw_formatted : expressions) {
  457. AST t = expression(raw_formatted[0]);
  458. String s = Formatter.format(t);
  459. // System.out.println(raw_formatted[1] + " " + s);
  460. assertEquals(raw_formatted[1], s);
  461. assertEquals(raw_formatted[1], Formatter.format2(t));
  462. }
  463. for (String[] raw_formatted : expressions2) {
  464. AST t = expression2(raw_formatted[0]);
  465. String s = Formatter.format2(t);
  466. // System.out.println(raw_formatted[1] + " " + s);
  467. assertEquals(raw_formatted[1], s);
  468. }
  469. for (String[] raw_formatted : statements) {
  470. AST t = statement(raw_formatted[0]);
  471. String s = Formatter.format(t);
  472. System.out.println(MessageFormat.format("\"{0}\" vs \"{1}\"", raw_formatted[1],s));
  473. assertEquals(raw_formatted[1], s);
  474. assertEquals(raw_formatted[1], Formatter.format2(t));
  475. }
  476. for (String[] raw_formatted : statements2) {
  477. AST t = statement2(raw_formatted[0]);
  478. String s = Formatter.format2(t);
  479. // System.out.println(MessageFormat.format("\"{0}\" vs \"{1}\"", raw_formatted[1],s));
  480. assertEquals(raw_formatted[1], s);
  481. }
  482. for (String[] raw_formatted : decls) {
  483. AST t = declDef(raw_formatted[0]);
  484. String s = Formatter.format(t);
  485. if (!s.equals(raw_formatted[1])) {
  486. System.out.println(MessageFormat.format("\"{0}\" vs \"{1}\"", raw_formatted[1], s));
  487. // Visitor.dump(t);
  488. }
  489. assertEquals(raw_formatted[1], s);
  490. assertEquals(raw_formatted[1], Formatter.format2(t));
  491. }
  492. for (String[] raw_formatted : decls2) {
  493. AST t = declDef2(raw_formatted[0]);
  494. String s = Formatter.format2(t);
  495. if (!s.equals(raw_formatted[1])) {
  496. System.out.println(MessageFormat.format("\"{0}\" vs \"{1}\"", raw_formatted[1], s));
  497. // Visitor.dump(t);
  498. }
  499. assertEquals(raw_formatted[1], s);
  500. }
  501. CodeStyle b4foreachParenT = new CodeStyle.DefaultCodeStyle(1) {
  502. @Override
  503. public boolean spaceBeforeForeachParen() {
  504. return true;
  505. }
  506. };
  507. CodeStyle b4foreachParenF = new CodeStyle.DefaultCodeStyle(1) {
  508. @Override
  509. public boolean spaceBeforeForeachParen() {
  510. return false;
  511. }
  512. };
  513. CodeStyle b4versionParenT = new CodeStyle.DefaultCodeStyle(1) {
  514. @Override
  515. public boolean spaceBeforeVersionParen() {
  516. return true;
  517. }
  518. };
  519. CodeStyle b4versionParenF = new CodeStyle.DefaultCodeStyle(1) {
  520. @Override
  521. public boolean spaceBeforeVersionParen() {
  522. return false;
  523. }
  524. };
  525. CodeStyle b4debugParenT = new CodeStyle.DefaultCodeStyle(1) {
  526. @Override
  527. public boolean spaceBeforeDebugParen() {
  528. return true;
  529. }
  530. };
  531. CodeStyle b4debugParenF = new CodeStyle.DefaultCodeStyle(1) {
  532. @Override
  533. public boolean spaceBeforeDebugParen() {
  534. return false;
  535. }
  536. };
  537. CodeStyle b4catchParenT = new CodeStyle.DefaultCodeStyle(1) {
  538. @Override
  539. public boolean spaceBeforeCatchParen() {
  540. return true;
  541. }
  542. };
  543. CodeStyle b4catchParenF = new CodeStyle.DefaultCodeStyle(1) {
  544. @Override
  545. public boolean spaceBeforeCatchParen() {
  546. return false;
  547. }
  548. };
  549. CodeStyle b4catchLBraceT = new CodeStyle.DefaultCodeStyle(1) {
  550. @Override
  551. public boolean spaceBeforeCatchLeftBrace() {
  552. return true;
  553. }
  554. };
  555. CodeStyle b4catchLBraceF = new CodeStyle.DefaultCodeStyle(1) {
  556. @Override
  557. public boolean spaceBeforeCatchLeftBrace() {
  558. return false;
  559. }
  560. };
  561. CodeStyle b4tryLBraceT = new CodeStyle.DefaultCodeStyle(1) {
  562. @Override
  563. public boolean spaceBeforeTryLeftBrace() {
  564. return true;
  565. }
  566. };
  567. CodeStyle b4tryLBraceF = new CodeStyle.DefaultCodeStyle(1) {
  568. @Override
  569. public boolean spaceBeforeTryLeftBrace() {
  570. return false;
  571. }
  572. };
  573. CodeStyle b4catchT = new CodeStyle.DefaultCodeStyle(1) {
  574. @Override
  575. public boolean spaceBeforeCatch() {
  576. return true;
  577. }
  578. };
  579. CodeStyle b4catchF = new CodeStyle.DefaultCodeStyle(1) {
  580. @Override
  581. public boolean spaceBeforeCatch() {
  582. return false;
  583. }
  584. };
  585. CodeStyle rndUnaryT = new CodeStyle.DefaultCodeStyle(1) {
  586. @Override
  587. public boolean spaceAroundUnaryOps() {
  588. return true;
  589. }
  590. };
  591. CodeStyle rndUnaryF = new CodeStyle.DefaultCodeStyle(1) {
  592. @Override
  593. public boolean spaceAroundUnaryOps() {
  594. return false;
  595. }
  596. };
  597. Object[] stmts = new Object[]{
  598. new Object[]{"foreach(i; j){};", b4foreachParenT, "foreach (i; j) {\n}"},
  599. new Object[]{"foreach(i; j){};", b4foreachParenF, "foreach(i; j) {\n}"},
  600. new Object[]{"foreach_reverse(i; j){};", b4foreachParenT, "foreach_reverse (i; j) {\n}"},
  601. new Object[]{"foreach_reverse(i; j){};", b4foreachParenF, "foreach_reverse(i; j) {\n}"},
  602. new Object[]{"version(i){};", b4versionParenT, "version (i) {\n}"},
  603. new Object[]{"version(i){};", b4versionParenF, "version(i) {\n}"},
  604. new Object[]{"debug(i){};", b4debugParenT, "debug (i) {\n}"},
  605. new Object[]{"debug(i){};", b4debugParenF, "debug(i) {\n}"},
  606. new Object[]{"debug{};", b4debugParenT, "debug {\n}"},
  607. new Object[]{"debug{};", b4debugParenF, "debug {\n}"},
  608. new Object[]{"try{}catch(Exception e){}", b4catchLBraceT, "try {\n} catch(Exception e) {\n}"},
  609. new Object[]{"try{}catch(Exception e){}", b4catchLBraceF, "try {\n} catch(Exception e){\n}"},
  610. new Object[]{"try{}catch(Exception e){}", b4catchT, "try {\n} catch(Exception e) {\n}"},
  611. new Object[]{"try{}catch(Exception e){}", b4catchF, "try {\n}catch(Exception e) {\n}"},
  612. new Object[]{"try{}catch(Exception e){}", b4tryLBraceT, "try {\n} catch(Exception e) {\n}"},
  613. new Object[]{"try{}catch(Exception e){}", b4tryLBraceF, "try{\n} catch(Exception e) {\n}"},
  614. new Object[]{"i++;", rndUnaryT, "i ++"},
  615. new Object[]{"i++;", rndUnaryF, "i++"},
  616. new Object[]{"j--;", rndUnaryT, "j --"},
  617. new Object[]{"j--;", rndUnaryF, "j--"},
  618. new Object[]{"++k;", rndUnaryT, "++ k"},
  619. new Object[]{"++k;", rndUnaryF, "++k"},
  620. new Object[]{"--l;", rndUnaryT, "-- l"},
  621. new Object[]{"--l;", rndUnaryF, "--l"},
  622. new Object[]{"*l;", rndUnaryT, "* l"},
  623. new Object[]{"*l;", rndUnaryF, "*l"},
  624. new Object[]{"&l;", rndUnaryT, "& l"},
  625. new Object[]{"&l;", rndUnaryF, "&l"},
  626. };
  627. for (Object ob : stmts) {
  628. String code = (String) (((Object[]) ob)[0]);
  629. CodeStyle style = (CodeStyle) (((Object[]) ob)[1]);
  630. String expected = (String) (((Object[]) ob)[2]);
  631. AST t = statement(code);
  632. Formatter f = new Formatter(style);
  633. f.visit(t);
  634. String result = f.getString();
  635. assertEquals(expected, result);
  636. }
  637. }
  638. }