allow tests to be embedded directly into .ship files
authoradam <adam@megacz.com>
Tue, 7 Aug 2007 14:33:01 +0000 (15:33 +0100)
committeradam <adam@megacz.com>
Tue, 7 Aug 2007 14:33:01 +0000 (15:33 +0100)
src/edu/berkeley/fleet/Main.java
src/edu/berkeley/fleet/doc/Doc.java
src/edu/berkeley/fleet/doc/ShipDescription.java
src/edu/berkeley/fleet/slipway/Slipway.java

index 5310d58..78dd785 100644 (file)
@@ -67,7 +67,7 @@ public class Main {
             run(fleet, is);
 
         } else if (command.equals("expand")) {
-            fleet.expand(new ShipDescription(new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
+            fleet.expand(new ShipDescription(args.get(0), new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
 
         } else if (command.equals("doc")) {
             Doc.print();
@@ -87,48 +87,56 @@ public class Main {
         }
     }
 
+    static void runTest(Fleet fleet, Reader reader, String title) throws Exception {
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        edu.berkeley.fleet.assembler.Main.assemble(fleet, reader, baos);
+        if (edu.berkeley.fleet.assembler.Parser.skip) {
+            System.out.println("\r[" + ANSI.yellow("SKIP") +
+                               "] "  + ANSI.yellow(title));
+            return;
+        }
+        FleetProcess fp = fleet.run(baos.toByteArray());
+        try {
+            ArrayList<Long> expect = edu.berkeley.fleet.assembler.Parser.expect;
+            String output = "";
+            // FIXME: check for extraneous stuff at the end
+            String verdict = "[    ]";
+            boolean failed = false;
+            while(true) {
+                if (output.length() > 60 && !failed)
+                    output = "..."+output.substring(output.length()-57, output.length());
+                if (expect.size() == 0) verdict = "["+ANSI.green("PASS")+"]";
+                System.out.print("\r" + verdict + " " + ANSI.yellow(title) + ": " + output);
+                if (failed) break;
+                if (expect.size() == 0) break;
+                long l = fp.readWord();
+                long l2 = expect.remove(0);
+                if (l!=l2) {
+                    verdict = "["+ANSI.red("FAIL")+"]";
+                    output += ANSI.red("0x"+Long.toString(l, 16)) +
+                        ANSI.yellow(" (expected ")+ANSI.green("0x"+Long.toString(l2, 16))+ANSI.yellow(")");
+                    failed = true;
+                    continue;
+                } else {
+                    output += ("0x"+Long.toString(l2, 16) + " ");
+                }
+            }
+            System.out.println();
+        } finally {
+            fp.terminate();
+        }
+    }
+
     static void test(Fleet fleet, File f) throws Exception {
         if (f.isDirectory()) {
             for(String s : f.list())
                 test(fleet, new File(f.getPath() + File.separatorChar + s));
+            return;
         } else if (f.getPath().endsWith(".fleet")) {
-            ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            edu.berkeley.fleet.assembler.Main.assemble(fleet, new InputStreamReader(new FileInputStream(f)), baos);
-            if (edu.berkeley.fleet.assembler.Parser.skip) {
-                System.out.println("\r[" + ANSI.yellow("SKIP") +
-                                   "] "  + ANSI.yellow(f.getPath()));
-                return;
-            }
-            FleetProcess fp = fleet.run(baos.toByteArray());
-            try {
-                ArrayList<Long> expect = edu.berkeley.fleet.assembler.Parser.expect;
-                String output = "";
-                // FIXME: check for extraneous stuff at the end
-                String verdict = "[    ]";
-                boolean failed = false;
-                while(true) {
-                    if (output.length() > 60 && !failed)
-                        output = "..."+output.substring(output.length()-57, output.length());
-                    if (expect.size() == 0) verdict = "["+ANSI.green("PASS")+"]";
-                    System.out.print("\r" + verdict + " " + ANSI.yellow(f.getPath()) + ": " + output);
-                    if (failed) break;
-                    if (expect.size() == 0) break;
-                    long l = fp.readWord();
-                    long l2 = expect.remove(0);
-                    if (l!=l2) {
-                        verdict = "["+ANSI.red("FAIL")+"]";
-                        output += ANSI.red("0x"+Long.toString(l, 16)) +
-                            ANSI.yellow(" (expected ")+ANSI.green("0x"+Long.toString(l2, 16))+ANSI.yellow(")");
-                        failed = true;
-                        continue;
-                    } else {
-                        output += ("0x"+Long.toString(l2, 16) + " ");
-                    }
-                }
-                System.out.println();
-            } finally {
-                fp.terminate();
-            }
+            runTest(fleet, new InputStreamReader(new FileInputStream(f)), f.getPath());
+        } else if (f.getPath().endsWith(".ship")) {
+            ShipDescription sd = new ShipDescription(f.getName(), new BufferedReader(new InputStreamReader(new FileInputStream(f))));
+            runTest(fleet, new StringReader(sd.getSection("test")), sd.getName());
         }
     }
 
index 48ca5a5..caa70b0 100644 (file)
@@ -14,7 +14,7 @@ public class Doc {
         for(String s = br.readLine(); s!=null; s = br.readLine())
             pw.println(s);
         for(String f : new File("ships").list()) {
-            print(pw, new ShipDescription(new BufferedReader(new InputStreamReader(new FileInputStream(new File("ships/"+f))))));
+            print(pw, new ShipDescription(f, new BufferedReader(new InputStreamReader(new FileInputStream(new File("ships/"+f))))));
         }
         pw.println("\\end{document}");
         pw.close();
index ed4a4c6..37d78ef 100644 (file)
@@ -10,14 +10,18 @@ public class ShipDescription implements Iterable<BenkoBoxDescription> {
     public String getSection(String sectionName) { return sections.get(sectionName); }
     public Iterator<BenkoBoxDescription> iterator() { return benkoBoxes.iterator(); }
 
-    public ShipDescription(BufferedReader r) throws IOException {
-        String sectionName = "";
+    public ShipDescription(String name, BufferedReader r) throws IOException {
+        if (name.endsWith(".ship"))
+            name = name.substring(0, name.length() - ".ship".length());
+        while(name.indexOf('/') != -1) name = name.substring(name.indexOf('/')+1);
+        this.name = name;
+        String sectionName = null;
         StringBuffer sb = new StringBuffer();
         while(true) {
             String s = r.readLine();
-            if (s==null) break;
-            if (s.startsWith("==")) {
-                sections.put(sectionName, sb.toString());
+            if (s==null || s.startsWith("==")) {
+                if (sectionName != null) sections.put(sectionName, sb.toString());
+                if (s==null) break;
                 sb = new StringBuffer();
                 sectionName = s.trim();
                 while(sectionName.startsWith("="))
index 42270c2..44e775f 100644 (file)
@@ -52,7 +52,7 @@ public class Slipway extends Fleet {
 
     public Ship createShip(String type, String name) {
         try {
-            ShipDescription sd = new ShipDescription(new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
+            ShipDescription sd = new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
             SlipwayShip ship = new SlipwayShip(this, name, type, sd);
             ships.put(name, ship);
             shiplist.add(ship);