public static string IfStatement(string str, int count) { string block = ""; //search for bool elements List <string> matches = new List <string> { }; foreach (JSElement ele in createdJSEl) { if (ele.type == "Boolean") { matches.Add(ele.name); //ifVal = ele.name; } } string ifVal = ""; if (matches.Count != 0) { ifVal = matches[rand.Next(matches.Count)]; } //if there were no matches in created, make a new element if (ifVal == "") { JSElement newEl = new JSElement("bool_" + varCount, "Boolean"); block += "var " + newEl.name + " = new " + newEl.type + "();\n"; ifVal = newEl.name; varCount++; createdJSEl.Add(newEl); } string action = DoSomething("", count); block += "if(" + ifVal + ") {\n" + action + "}\n"; return(block); }
public static string GenerateRandElement(string str, int count) { //choose random element index int r = rand.Next(0, gram.Count); //create element Element e = gram[r]; e.name = e.type + "_" + varCount; varCount++; //choose random way to append to dom string[] domAppend = { "createTextNode", "appendChild", "insertBefore", "replaceChild" }; //initialize element in string String block = "var " + e.name + " = " + "document.createElement(\"" + e.type + "\");\n"; //optional - make element by calling method from another element /*if(rand.Next() > 0.5)//happens ~1/2 the time * { * * }*/ //append element to dom string appendTo = ""; if (e.name.IndexOf("_0") != -1) //append first element to body { appendTo = "body"; } else { appendTo = createdEl[rand.Next(createdEl.Count)].name; } block += "document." + appendTo + "." + domAppend[rand.Next(domAppend.Length)] + "(" + e.name + ");\n"; //if has children, can replace or remove children //random number of attributes will be assigned int numA = rand.Next(e.attributes.Count); for (int j = 0; j < numA; j++) { //choose random attribute EleAtt at = e.attributes[rand.Next(e.attributes.Count)]; string valA = ""; if (at.values != null) { //choose random value from array of values valA = at.values[rand.Next(at.values.Length)]; } block += e.name + "." + at.name + " = \"" + valA + "\";\n"; } //add element to list of created elements createdEl.Add(e); //random number of methods to be called int numM = rand.Next(e.methods.Count); for (int j = 0; j < numM; j++) { //choose random method + call method on element int randM = rand.Next(e.methods.Count); string[] p = e.methods[randM].parameters.ToArray(); //for each parameter, find another element with attributes or methods of the correct type block += e.name + "." + e.methods[randM].name.Trim() + "("; //start of method string string block2 = ""; foreach (string param in p) { string value = ""; //if the method requires parameters if (param != "") { //list of all possible elements string[] values = { }; //search for created element of correct type - should be fixed so that not the last in list foreach (Element el in createdEl) { //see if any methods return the correct type foreach (EleMethod m in el.methods) { foreach (string s in m.re) { if (s.Trim().Equals(param.Trim())) { value = el.name + "." + m + "add parameters here"; } } } //see if any attributes are correct type foreach (EleAtt a in el.attributes) { if (a.type == param) { value = el.name + "." + a; } } } //see if any matches in createdJSEL if (values.Length == 0) { foreach (JSElement jel in createdJSEl) { if (jel.type == param) { if (jel.name == null) { jel.name = jel.type + "_" + varCount; varCount++; } value = jel.name; } } } //if there were no matches in createdEl, make a new js object with attributes or methods of correct type if (values.Length == 0) { JSElement newEl = new JSElement(param + "_" + varCount, param); block2 += "var " + newEl.name + " = new " + newEl.type + "();\n"; value = newEl.name; varCount++; createdJSEl.Add(newEl); } else { //randomly choose from list of possible elements value = values[rand.Next(values.Length)]; } } //commas between parameters in method string block += value + ", "; } //end of method string block = block.Substring(0, block.Length - 2); //to remove ", " block += ");\n"; block = block2 + block; } return(block); }