Commit be212852 authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ make expected behaviour and when / then clauses optional

+ fix issue with tabs causing generation to fail
parent 893abd5e
Loading
Loading
Loading
Loading
+27 −16
Original line number Diff line number Diff line
@@ -427,11 +427,14 @@ public class Generator {
			map.put(Placeholders.INITIAL, initialConditions);
		}
		
		//TODO:check if defined?
		if (sto.getExpectedBehaviour()!=null) {
			String expected = NodeModelUtils.getNode(sto.getExpectedBehaviour()).getText();
			expected = expected.replaceAll("\\s*Expected behaviour", "");
			expected = filterSource(expected, "\n", "ensure").trim();
			map.put(Placeholders.EXPECTED, expected);
		} else {
			map.put(Placeholders.EXPECTED, "");
		}

		map.put(Placeholders.FINAL, "");
		if (sto.getFinalConditions()!=null) {
@@ -442,22 +445,29 @@ public class Generator {
			map.put(Placeholders.FINAL, finalConditions);
		}

		if (sto.getExpectedBehaviour()!=null
				&& sto.getExpectedBehaviour().getWhenClause()!=null
				&& sto.getExpectedBehaviour().getThenClause()!=null
				) {
			String when = NodeModelUtils.getNode(sto.getExpectedBehaviour().getWhenClause()).getText();
			String then = NodeModelUtils.getNode(sto.getExpectedBehaviour().getThenClause()).getText();
			//TODO: a bit of a hack
			when = filterSource(when, "\n", "\\s\\s\\s\\s\\w");
		then = filterSource(then, "\n", "\\s\\s\\s\\s\\w");
			then = filterSource(then, "\n", "\\s+\\w");
	
			map.put(Placeholders.WHEN, "when {"+when+"\n}");
			map.put(Placeholders.THEN, "then {"+then+"\n}");
		}
		return map;
	}

	private String filterSource(String source, String prefix, String offsetKeyword) {
		Matcher matcher = Pattern.compile(prefix+"(.+?)"+offsetKeyword).matcher(source);
		matcher.find();
		String offset = matcher.group(1);
		String offset = "";
		if (matcher.find()) {
			offset = matcher.group(1);
			System.out.println("|"+offset+"|");
		}
		source = source
				.replaceAll("\t", "    ") //tabs
				.replaceAll("\n"+offset, "\n") //leading space
@@ -465,6 +475,7 @@ public class Generator {
				.replaceAll("[\\s\n\r]+"+prefix, prefix)
				.replaceAll(" \\((typed|predefined)\\) ", " ") //shall be optional
				.replaceAll(" entity ", " ") //shall be optional
				.replaceAll("\\^", "") //shall be optional
				.replaceAll(";", "") //shall be optional
				.replaceAll("\\(|\\)", "") //shall be optional
				;