001: package org.drools.eclipse.editors.completion;
002:
003: import java.util.List;
004:
005: import junit.framework.TestCase;
006:
007: import org.drools.compiler.DrlParser;
008: import org.drools.compiler.DroolsParserException;
009: import org.drools.lang.descr.EvalDescr;
010: import org.drools.lang.descr.FieldBindingDescr;
011: import org.drools.lang.descr.FieldConstraintDescr;
012: import org.drools.lang.descr.FromDescr;
013: import org.drools.lang.descr.LiteralRestrictionDescr;
014: import org.drools.lang.descr.PackageDescr;
015: import org.drools.lang.descr.PatternDescr;
016: import org.drools.lang.descr.RestrictionConnectiveDescr;
017: import org.drools.lang.descr.RuleDescr;
018: import org.drools.lang.descr.VariableRestrictionDescr;
019:
020: /**
021: * Test to check the results from parsing incomplete rule fragments.
022: *
023: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
024: */
025: public class IncompleteParsingTest extends TestCase {
026:
027: private RuleDescr parseRuleString(String s) {
028: PackageDescr packageDescr = parseString(s);
029: if (packageDescr != null) {
030: List rules = packageDescr.getRules();
031: if (rules != null && rules.size() == 1) {
032: return (RuleDescr) rules.get(0);
033: }
034: }
035: return null;
036: }
037:
038: private PackageDescr parseString(String s) {
039: DrlParser parser = new DrlParser();
040: try {
041: return parser.parse(s);
042: } catch (DroolsParserException exc) {
043: exc.printStackTrace();
044: }
045: return null;
046: }
047:
048: public void testParsingColumn() {
049: String input = "rule MyRule \n" + " when \n" + " ";
050: RuleDescr rule = parseRuleString(input);
051: assertEquals(0, rule.getLhs().getDescrs().size());
052:
053: /**
054: * This is how the parsed tree should look like:
055: *
056: * RuleDescr
057: * PatternDescr [objectType = "Class"]
058: * FieldConstraintDescr [fieldName = "condition"]
059: * LiteralRestrictionDescr [evaluator = "==", text = "true"]
060: */
061: }
062:
063: public void testParsingColumn1() {
064: String input = "rule MyRule \n" + " when \n"
065: + " Class( condition == true ) \n" + " ";
066: RuleDescr rule = parseRuleString(input);
067: assertEquals(1, rule.getLhs().getDescrs().size());
068: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
069: .get(0);
070: assertEquals("Class", pattern.getObjectType());
071: assertTrue(pattern.getEndLine() != -1
072: || pattern.getEndColumn() != -1);
073: assertEquals(1, pattern.getDescrs().size());
074: assertEquals(input.indexOf("Class"), pattern
075: .getStartCharacter());
076: assertEquals(input.indexOf("("), pattern
077: .getLeftParentCharacter());
078: assertEquals(input.indexOf(")"), pattern
079: .getRightParentCharacter());
080: assertEquals(input.indexOf(")"), pattern.getEndCharacter());
081: FieldConstraintDescr field = (FieldConstraintDescr) pattern
082: .getDescrs().get(0);
083: assertEquals("condition", field.getFieldName());
084: assertEquals(1, field.getRestrictions().size());
085: LiteralRestrictionDescr restriction = (LiteralRestrictionDescr) field
086: .getRestrictions().get(0);
087: assertEquals("==", restriction.getEvaluator());
088: assertEquals("true", restriction.getText());
089: }
090:
091: public void testParsingColumn2() {
092: String input =
093:
094: "rule MyRule \n"
095: + " when \n"
096: + " class: Class( condition == true, condition2 == null ) \n"
097: + " ";
098: RuleDescr rule = parseRuleString(input);
099: assertEquals(1, rule.getLhs().getDescrs().size());
100: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
101: .get(0);
102: assertTrue(pattern.getEndCharacter() != -1);
103: }
104:
105: public void testParsingColumn3() {
106: String input = "rule MyRule \n" + " when \n" + " Cl";
107: RuleDescr rule = parseRuleString(input);
108: assertEquals(0, rule.getLhs().getDescrs().size());
109:
110: }
111:
112: public void testParsingColumn4() {
113: String input = "rule MyRule \n" + " when \n"
114: + " Class( condition == true ) \n" + " Cl";
115: RuleDescr rule = parseRuleString(input);
116: assertEquals(1, rule.getLhs().getDescrs().size());
117: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
118: .get(0);
119: assertTrue(pattern.getEndCharacter() != -1);
120:
121: }
122:
123: public void testParsingColumn5() {
124: String input = "rule MyRule \n" + " when \n" + " class:";
125: RuleDescr rule = parseRuleString(input);
126: assertEquals(1, rule.getLhs().getDescrs().size());
127: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
128: .get(0);
129: assertEquals("class", pattern.getIdentifier());
130: assertNull(pattern.getObjectType());
131: assertEquals(-1, pattern.getEndCharacter());
132:
133: }
134:
135: public void testParsingColumn6() {
136: String input = "rule MyRule \n" + " when \n" + " class: Cl";
137: RuleDescr rule = parseRuleString(input);
138: assertEquals(1, rule.getLhs().getDescrs().size());
139: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
140: .get(0);
141: assertEquals("class", pattern.getIdentifier());
142: assertEquals("Cl", pattern.getObjectType());
143: assertTrue(pattern.getEndLine() == -1
144: && pattern.getEndColumn() == -1);
145: assertEquals(0, pattern.getDescrs().size());
146: assertEquals(-1, pattern.getEndCharacter());
147:
148: }
149:
150: public void testParsingColumn7() {
151: String input = "rule MyRule \n" + " when \n" + " class:Cl";
152: RuleDescr rule = parseRuleString(input);
153: assertEquals(1, rule.getLhs().getDescrs().size());
154: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
155: .get(0);
156: assertEquals("class", pattern.getIdentifier());
157: assertEquals("Cl", pattern.getObjectType());
158: assertTrue(pattern.getEndLine() == -1
159: && pattern.getEndColumn() == -1);
160: assertEquals(0, pattern.getDescrs().size());
161: assertEquals(-1, pattern.getEndCharacter());
162:
163: }
164:
165: public void testParsingColumn8() {
166: /** Inside of condition: start */
167: String input = "rule MyRule \n" + " when \n" + " Class (";
168: RuleDescr rule = parseRuleString(input);
169: assertEquals(1, rule.getLhs().getDescrs().size());
170: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
171: .get(0);
172: assertEquals("Class", pattern.getObjectType());
173: assertTrue(pattern.getEndLine() == -1
174: && pattern.getEndColumn() == -1);
175: assertEquals(0, pattern.getDescrs().size());
176: assertEquals(-1, pattern.getEndCharacter());
177:
178: }
179:
180: public void testParsingColumn9() {
181: String input = "rule MyRule \n" + " when \n" + " Class ( na";
182: RuleDescr rule = parseRuleString(input);
183: assertEquals(1, rule.getLhs().getDescrs().size());
184: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
185: .get(0);
186: assertEquals("Class", pattern.getObjectType());
187: assertEquals(-1, pattern.getEndCharacter());
188: assertEquals(1, pattern.getDescrs().size());
189: FieldConstraintDescr field = (FieldConstraintDescr) pattern
190: .getDescrs().get(0);
191: assertEquals("na", field.getFieldName());
192: assertEquals(-1, field.getEndCharacter());
193:
194: }
195:
196: public void testParsingColumn10() {
197: String input = "rule MyRule \n" + " when \n"
198: + " Class ( name['xyz'].subname.subsubn";
199: RuleDescr rule = parseRuleString(input);
200: assertEquals(1, rule.getLhs().getDescrs().size());
201: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
202: .get(0);
203: assertEquals("Class", pattern.getObjectType());
204: assertEquals(-1, pattern.getEndCharacter());
205: assertEquals(1, pattern.getDescrs().size());
206: FieldConstraintDescr field = (FieldConstraintDescr) pattern
207: .getDescrs().get(0);
208: assertEquals("name['xyz'].subname.subsubn", field
209: .getFieldName());
210: assertEquals(-1, field.getEndCharacter());
211:
212: }
213:
214: public void testParsingColumn11() {
215: String input = "rule MyRule \n" + " when \n"
216: + " Class ( condition == true, ";
217: RuleDescr rule = parseRuleString(input);
218: assertEquals(1, rule.getLhs().getDescrs().size());
219: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
220: .get(0);
221: assertEquals("Class", pattern.getObjectType());
222: assertEquals(-1, pattern.getEndCharacter());
223: assertEquals(1, pattern.getDescrs().size());
224: FieldConstraintDescr field = (FieldConstraintDescr) pattern
225: .getDescrs().get(0);
226: assertEquals(-1, field.getEndCharacter());
227:
228: }
229:
230: public void testParsingColumn12() {
231: String input = "rule MyRule \n" + " when \n"
232: + " Class ( c : condition, ";
233: RuleDescr rule = parseRuleString(input);
234: assertEquals(1, rule.getLhs().getDescrs().size());
235: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
236: .get(0);
237: assertEquals("Class", pattern.getObjectType());
238: assertEquals(-1, pattern.getEndCharacter());
239: assertEquals(1, pattern.getDescrs().size());
240: FieldBindingDescr fieldBinding = (FieldBindingDescr) pattern
241: .getDescrs().get(0);
242: assertEquals(-1, fieldBinding.getEndCharacter());
243:
244: }
245:
246: public void testParsingColumn13() {
247: String input = "rule MyRule \n" + " when \n"
248: + " Class ( condition == true, na";
249: RuleDescr rule = parseRuleString(input);
250: assertEquals(1, rule.getLhs().getDescrs().size());
251: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
252: .get(0);
253: assertEquals("Class", pattern.getObjectType());
254: assertEquals(-1, pattern.getEndCharacter());
255: assertEquals(2, pattern.getDescrs().size());
256: FieldConstraintDescr field = (FieldConstraintDescr) pattern
257: .getDescrs().get(0);
258: assertEquals(-1, field.getEndCharacter());
259: assertEquals("condition", field.getFieldName());
260: field = (FieldConstraintDescr) pattern.getDescrs().get(1);
261: assertEquals("na", field.getFieldName());
262: assertEquals(-1, field.getEndCharacter());
263:
264: }
265:
266: public void FAILINGtestParsingColumn14() {
267: String input = "rule MyRule \n" + " when \n"
268: + " Class ( name:";
269: RuleDescr rule = parseRuleString(input);
270: assertEquals(1, rule.getLhs().getDescrs().size());
271: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
272: .get(0);
273: assertEquals("Class", pattern.getObjectType());
274: assertEquals(-1, pattern.getEndCharacter());
275: assertEquals(1, pattern.getDescrs().size());
276: FieldBindingDescr binding1 = (FieldBindingDescr) pattern
277: .getDescrs().get(0);
278: assertEquals("name", binding1.getIdentifier());
279: assertNull(binding1.getFieldName());
280:
281: }
282:
283: public void testParsingColumn15() {
284: String input = "rule MyRule \n" + " when \n"
285: + " Class ( property ";
286: RuleDescr rule = parseRuleString(input);
287: assertEquals(1, rule.getLhs().getDescrs().size());
288: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
289: .get(0);
290: assertEquals("Class", pattern.getObjectType());
291: assertEquals(-1, pattern.getEndCharacter());
292: assertEquals(1, pattern.getDescrs().size());
293: FieldConstraintDescr field = (FieldConstraintDescr) pattern
294: .getDescrs().get(0);
295: assertEquals("property", field.getFieldName());
296: assertEquals(0, field.getRestrictions().size());
297: assertEquals(-1, field.getEndCharacter());
298:
299: }
300:
301: public void testParsingColumn16() {
302: String input = "rule MyRule \n" + " when \n"
303: + " Class ( name: property ";
304: RuleDescr rule = parseRuleString(input);
305: assertEquals(1, rule.getLhs().getDescrs().size());
306: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
307: .get(0);
308: assertEquals("Class", pattern.getObjectType());
309: assertEquals(-1, pattern.getEndCharacter());
310: assertEquals(1, pattern.getDescrs().size());
311: FieldBindingDescr binding = (FieldBindingDescr) pattern
312: .getDescrs().get(0);
313: assertEquals("name", binding.getIdentifier());
314: assertEquals("property", binding.getFieldName());
315:
316: }
317:
318: public void testParsingColumn17() {
319: String input = "rule MyRule \n"
320: + " when \n"
321: + " Class ( name1: property1 == \"value1\", name2: property2 ";
322: RuleDescr rule = parseRuleString(input);
323: assertEquals(1, rule.getLhs().getDescrs().size());
324: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
325: .get(0);
326: assertEquals("Class", pattern.getObjectType());
327: assertEquals(-1, pattern.getEndCharacter());
328: assertEquals(3, pattern.getDescrs().size());
329: FieldBindingDescr binding = (FieldBindingDescr) pattern
330: .getDescrs().get(0);
331: assertEquals("name1", binding.getIdentifier());
332: assertEquals("property1", binding.getFieldName());
333: FieldConstraintDescr field = (FieldConstraintDescr) pattern
334: .getDescrs().get(1);
335: assertEquals("property1", field.getFieldName());
336: assertEquals(1, field.getRestrictions().size());
337: LiteralRestrictionDescr literal = (LiteralRestrictionDescr) field
338: .getRestrictions().get(0);
339: assertEquals("==", literal.getEvaluator());
340: assertEquals("value1", literal.getText());
341: binding = (FieldBindingDescr) pattern.getDescrs().get(2);
342: assertEquals("name2", binding.getIdentifier());
343: assertEquals("property2", binding.getFieldName());
344:
345: }
346:
347: public void testParsingColumn18() {
348: String input = "rule MyRule \n" + " when \n"
349: + " Class(name:property==";
350: RuleDescr rule = parseRuleString(input);
351: assertEquals(1, rule.getLhs().getDescrs().size());
352: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
353: .get(0);
354: assertEquals("Class", pattern.getObjectType());
355: assertEquals(-1, pattern.getEndCharacter());
356: assertEquals(2, pattern.getDescrs().size());
357: FieldBindingDescr binding = (FieldBindingDescr) pattern
358: .getDescrs().get(0);
359: assertEquals("name", binding.getIdentifier());
360: assertEquals("property", binding.getFieldName());
361: FieldConstraintDescr field = (FieldConstraintDescr) pattern
362: .getDescrs().get(1);
363: assertEquals("property", field.getFieldName());
364: assertEquals(1, field.getRestrictions().size());
365:
366: }
367:
368: public void testParsingColumn19() {
369: String input = "rule MyRule \n" + " when \n"
370: + " Class( property == otherPropertyN";
371: RuleDescr rule = parseRuleString(input);
372: assertEquals(1, rule.getLhs().getDescrs().size());
373: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
374: .get(0);
375: assertEquals("Class", pattern.getObjectType());
376: assertEquals(-1, pattern.getEndCharacter());
377: assertEquals(1, pattern.getDescrs().size());
378: FieldConstraintDescr field = (FieldConstraintDescr) pattern
379: .getDescrs().get(0);
380: assertEquals("property", field.getFieldName());
381: assertEquals(1, field.getRestrictions().size());
382: VariableRestrictionDescr variable = (VariableRestrictionDescr) field
383: .getRestrictions().get(0);
384: assertEquals("==", variable.getEvaluator());
385: assertEquals("otherPropertyN", variable.getIdentifier());
386: assertEquals(-1, field.getEndCharacter());
387: }
388:
389: public void testParsingColumn20() {
390: String input = "rule MyRule \n" + " when \n"
391: + " Class( property == \"someth";
392: RuleDescr rule = parseRuleString(input);
393: assertEquals(1, rule.getLhs().getDescrs().size());
394: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
395: .get(0);
396: assertEquals("Class", pattern.getObjectType());
397: assertEquals(-1, pattern.getEndCharacter());
398: assertEquals(1, pattern.getDescrs().size());
399: FieldConstraintDescr field = (FieldConstraintDescr) pattern
400: .getDescrs().get(0);
401: assertEquals("property", field.getFieldName());
402: assertEquals(1, field.getRestrictions().size());
403: LiteralRestrictionDescr literal = (LiteralRestrictionDescr) field
404: .getRestrictions().get(0);
405: // KRISV: for now, it would be really messy to make this work. String is a
406: // lexer rule (not parser), and changing that or controling the behavior of it
407: // is not simple. Can we leave the way it is for now?
408: //
409: // TODO literal should be a LiteralRestrictionDescr with filled in evaluator and text, not null
410: // assertEquals("==", literal.getEvaluator());
411: // assertEquals("someth", literal.getText());
412: // TODO this method does not yet exist
413: // assertEquals(-1, field.getEndCharacter());
414:
415: }
416:
417: public void testParsingColumn21() {
418: String input = "rule MyRule \n" + " when \n"
419: + " Class( property contains ";
420: RuleDescr rule = parseRuleString(input);
421: assertEquals(1, rule.getLhs().getDescrs().size());
422: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
423: .get(0);
424: assertEquals("Class", pattern.getObjectType());
425: assertEquals(-1, pattern.getEndCharacter());
426: assertEquals(1, pattern.getDescrs().size());
427: FieldConstraintDescr field = (FieldConstraintDescr) pattern
428: .getDescrs().get(0);
429: assertEquals("property", field.getFieldName());
430: assertEquals(1, field.getRestrictions().size());
431: // KRISV: you are right
432: //
433: // now I would like to access the evaluator 'contains', but this seems
434: // not possible because the parser cannot create this descr yet
435: // since it does not know what class to create (VariableRestrictionDescr
436: // or LiteralRestrictionDescr or ?)
437: // so maybe I should just extract this info myself, based on the
438: // starting character of this FieldConstraintDescr?
439: // TODO this method does not yet exist
440: assertEquals(-1, field.getEndCharacter());
441: }
442:
443: public void testParsingColumn22() {
444: String input = "rule MyRule \n" + " when \n"
445: + " Class( property matches \"someth";
446: RuleDescr rule = parseRuleString(input);
447: assertEquals(1, rule.getLhs().getDescrs().size());
448: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
449: .get(0);
450: assertEquals("Class", pattern.getObjectType());
451: assertEquals(-1, pattern.getEndCharacter());
452: assertEquals(1, pattern.getDescrs().size());
453: FieldConstraintDescr field = (FieldConstraintDescr) pattern
454: .getDescrs().get(0);
455: assertEquals("property", field.getFieldName());
456: assertEquals(1, field.getRestrictions().size());
457: LiteralRestrictionDescr literal = (LiteralRestrictionDescr) field
458: .getRestrictions().get(0);
459: // KRISV: see comments above
460: //
461: // TODO literal should be a LiteralRestrictionDescr with filled in evaluator and text, not null
462: // assertEquals("matches", literal.getEvaluator());
463: // assertEquals("someth", literal.getText());
464: // TODO this method does not yet exist
465: // assertEquals(-1, field.getEndCharacter());
466: }
467:
468: public void testParsingColumn23() {
469: String input = "rule MyRule \n" + " when \n"
470: + " eval ( ";
471: RuleDescr rule = parseRuleString(input);
472: assertEquals(1, rule.getLhs().getDescrs().size());
473: EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs().get(0);
474: assertEquals(input.indexOf("eval"), eval.getStartCharacter());
475: assertEquals(-1, eval.getEndCharacter());
476: }
477:
478: public void testParsingColumn24() {
479: String input = "rule MyRule \n" + " when \n"
480: + " Class ( property > 0 & ";
481: RuleDescr rule = parseRuleString(input);
482: assertEquals(1, rule.getLhs().getDescrs().size());
483: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
484: .get(0);
485: assertEquals("Class", pattern.getObjectType());
486: assertEquals(-1, pattern.getEndCharacter());
487: assertEquals(1, pattern.getDescrs().size());
488: FieldConstraintDescr field = (FieldConstraintDescr) pattern
489: .getDescrs().get(0);
490: assertEquals("property", field.getFieldName());
491: assertEquals(1, field.getRestrictions().size());
492: LiteralRestrictionDescr literal = (LiteralRestrictionDescr) field
493: .getRestrictions().get(0);
494: assertEquals(">", literal.getEvaluator());
495: assertEquals("0", literal.getText());
496: RestrictionConnectiveDescr connective = (RestrictionConnectiveDescr) field
497: .getRestriction();
498: assertEquals(RestrictionConnectiveDescr.AND, connective
499: .getConnective());
500: }
501:
502: public void testParsingColumn25() {
503: String input = "rule MyRule \n" + " when \n"
504: + " Class ( ) from a";
505: RuleDescr rule = parseRuleString(input);
506: assertEquals(1, rule.getLhs().getDescrs().size());
507: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
508: .get(0);
509: assertEquals("Class", pattern.getObjectType());
510: FromDescr from = (FromDescr) pattern.getSource();
511: assertEquals(-1, from.getEndCharacter());
512: assertTrue(pattern.getEndCharacter() != -1);
513: }
514:
515: public void testParsingColumn26() {
516: String input = "rule MyRule \n" + " when \n"
517: + " Class ( property > 0 ) from myGlobal.getList() \n"
518: + " ";
519: RuleDescr rule = parseRuleString(input);
520: assertEquals(1, rule.getLhs().getDescrs().size());
521: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
522: .get(0);
523: FromDescr from = (FromDescr) pattern.getSource();
524: assertTrue(from.getEndCharacter() != -1);
525: }
526:
527: public void testParsingColumn27() {
528: String input = "rule MyRule \n"
529: + " when \n"
530: + " Class ( property > 0 ) from getDroolsFunction() \n"
531: + " ";
532: RuleDescr rule = parseRuleString(input);
533: assertEquals(1, rule.getLhs().getDescrs().size());
534: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
535: .get(0);
536: FromDescr from = (FromDescr) pattern.getSource();
537: assertTrue(from.getEndCharacter() != -1);
538: }
539:
540: public void testParsingCharactersStartEnd() {
541: String input = "package test; \n" + "rule MyRule \n"
542: + " when \n" + " Class( condition == true ) \n"
543: + " then \n" + " System.out.println(\"Done\") \n"
544: + "end \n";
545: RuleDescr rule = parseRuleString(input);
546: assertEquals(input.indexOf("rule"), rule.getStartCharacter());
547: assertEquals(input.indexOf("end") + 2, rule.getEndCharacter());
548: PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
549: .get(0);
550: assertEquals(input.indexOf("Class"), pattern
551: .getStartCharacter());
552: assertEquals(input.indexOf("("), pattern
553: .getLeftParentCharacter());
554: assertEquals(input.indexOf(")"), pattern
555: .getRightParentCharacter());
556: assertEquals(input.indexOf(")"), pattern.getEndCharacter());
557: }
558: }
|