diff --git a/_sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst b/_sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst index bbcb4b57..a59115fb 100644 --- a/_sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst +++ b/_sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst @@ -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**