2015年2月12日木曜日

開発環境

  • OS X Yosemite - Apple (OS)
  • Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
  • Java (プログラミング言語)
  • javac (コンパイラ)
  • java (application launcher)

Head First Object-Oriented Analysis and Design: A Brain Friendly Guide to OOA&D (Brett McLaughlin (著)、 Gary Pollice (著)、 David West (著) 、 O'Reilly Media)のChapter 9. Iterating and Testing: The Software is Still for the Customer、TEST PUZZLE(No. 5937)を解いてみる。

その他参考書籍

TEST PUZZLE(No. 5937)

コード(BBEdit, Emacs)

UnitTest.java

public class UnitTester {
    public static void main(String[] args){
        UnitTester unitTester = new UnitTester();
        Unit unit = new Unit(1);
        unitTester.testSetGetPropery(unit, "type", "infantry",
                                     "infantry");
        unitTester.testSetGetSpecificProperty(unit, "hitPoints", 25,
                                              25);
        unitTester.testChangeExistPropertyValue(unit, "hitPoints", 15,
                                                15);
    }
    
    public void testSetGetPropery(Unit unit, String type, Object value,
                                  Object output){
        System.out.println("Setting/Getting the type property.");
        unit.setProperty(type, value);
        if (unit.getProperty(type) == output){
            System.out.println("Test passed");
        } else {
            System.out.println("Test failed: " +
                               unit.getProperty(type));
        }
    }
    
    public void testSetGetSpecificProperty(Unit unit, String name,
                                           Object value,
                                           Object output) {
        System.out.println("Setting/Getting a unit-specific property");
        unit.setProperty(name, value);
        if (unit.getProperty(name) == output){
            System.out.println("Test passed");
        } else {
            System.out.println("Test failed: " +
                               unit.getProperty(name));
        }
    }

    public void testChangeExistPropertyValue(Unit unit, String name,
                                             Object value,
                                             Object output){
        System.out.println("Changing an existing property's value.");
        unit.setProperty(name, value);
        if (unit.getProperty(name) == output){
            System.out.println("Test passed");
        } else {
            System.out.println("Test failed: " +
                               unit.getProperty(name));
        }
    }

    public void testNonExistProperty(Unit unit, String name) {
        System.out.println("TGetting a non-existent property's value");
        if (unit.getProperty(name) == null) {
            System.out.println("Test passed");
        } else {
            System.out.println("Test failed: " +
                               unit.getProperty(name));
        }
    }    
}

Weapon.java

public enum Weapon{
    Bazooka,
    Gatling
}

入出力結果(Terminal)

$ jrun.sh UnitTester
Setting/Getting the type property.
Test passed
Setting/Getting a unit-specific property
Test passed
Changing an existing property's value.
Test passed
$

0 コメント:

コメントを投稿