Skip to content

Commit

Permalink
Updated test for 2.4 Song
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcdonnell2 authored Oct 20, 2023
1 parent 3024063 commit bbce04c
Showing 1 changed file with 66 additions and 64 deletions.
130 changes: 66 additions & 64 deletions _sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,70 +340,72 @@ Use the Code Lens button or this |Java Visualizer| to step through the code.
}

====
import static org.junit.Assert.*;

import org.junit.*;

import java.io.*;

public class RunestoneTests extends CodeTestHelper
{
public String expected =
"Old MacDonald had a farm\n"
+ "E-I-E-I-O\n"
+ "And on that farm he had a cow\n"
+ "E-I-E-I-O\n"
+ "With a moo moo here,\n"
+ "And a moo moo there,\n"
+ "Old MacDonald had a farm\n"
+ "E-I-E-I-O\n"
+ "Old MacDonald had a farm\n"
+ "E-I-E-I-O\n"
+ "And on that farm he had a duck\n"
+ "E-I-E-I-O\n"
+ "With a quack quack here,\n"
+ "And a quack quack there,\n"
+ "Old MacDonald had a farm\n"
+ "E-I-E-I-O";

public RunestoneTests()
{
super("Song");
}

@Test
public void test1()
{
String output = getMethodOutput("main");

boolean passed = output.contains(expected);

passed = getResults(expected, output, "Still have the old output", passed);
assertTrue(passed);
}

@Test
public void test2()
{
String output = getMethodOutput("main");

boolean passed = output.contains(expected) && !output.equals(expected);

passed = getResults(expected, output, "Verse added", passed);
assertTrue(passed);
}

@Test
public void test3()
{
String code = getCode();
int numVerses = countOccurences(code, "verse(");
boolean passed = numVerses >= 4;
// + 1 because of verse method definition
passed = getResults("3 or more", "" + numVerses, "Number of verses", passed);
assertTrue(passed);
}
}
// Test for 2.4.4 Song
import static org.junit.Assert.*;
import org.junit.*;;
import java.io.*;

public class RunestoneTests extends CodeTestHelper
{
public String verse1 = "Old MacDonald had a farm\nE-I-E-I-O\nAnd on that farm he had a cow\nE-I-E-I-O\nWith a moo moo here,\nAnd a moo moo there,\nOld MacDonald had a farm\nE-I-E-I-O";
public String verse2 = "Old MacDonald had a farm\nE-I-E-I-O\nAnd on that farm he had a duck\nE-I-E-I-O\nWith a quack quack here,\nAnd a quack quack there,\nOld MacDonald had a farm\nE-I-E-I-O";

public String verse3 = "Old MacDonald had a farm\nE-I-E-I-O\nAnd on that farm he had a ...\nE-I-E-I-O\nWith a ... ... here,\nAnd a ... ... there,\nOld MacDonald had a farm\nE-I-E-I-O";

public RunestoneTests() {
super("Song");
}

@Test
public void test1()
{
String output = getMethodOutput("main");
output = output.replace(verse1, "").trim();
output = output.replace(verse2, "").trim();

boolean passed = output.length() > 0;

passed = getResults(verse3, output, "Contains new verse", passed);
assertTrue(passed);
}

@Test
public void test2()
{
String output = getMethodOutput("main");

boolean passed1 = output.contains(verse1);
boolean passed2 = output.contains(verse2);
boolean passed = passed1 && passed2;

String exp = "Verse 1: true\nVerse 2: true";
String act = "Verse 1: " + passed1 + "\nVerse 2: " + passed2;

passed = getResults(exp, act, "Contains original verses", passed);
assertTrue(passed);
}

@Test
public void testCode1() {
String[] lines = getCode().split("\n");
String expect = "s.verse(";
String output = "";
int count = 0;

for (int i = 0; i < lines.length; i++) {
if (lines[i].contains(expect)) {
output += lines[i].trim() + "\n";
count++;
}
}

String expected = "s.verse(\"cow\", \"moo\");\ns.verse(\"duck\",\"quack\");\ns.verse(\"...\", \"...\");";

boolean passed = count >= 3;
passed = getResults(expected, output, "Added third call to verse", passed);
assertTrue(passed);
}
}

|Exercise| **Check your understanding**

Expand Down

0 comments on commit bbce04c

Please sign in to comment.