PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/UnitVerbs.cpp
00001 /*
00002  *  UnitVerbs.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 10 Aug 2011
00006  *
00007  */
00008 #include "Verbs.h"
00009         
00010 #include "Interpreter.h"
00011 #include "Thing.h"
00012 #include "Exceptions.h"
00013 #include "World.h"
00014 #include "Console.h"
00015 #include "Turtle.h"
00016 #include "FileProcessor.h"
00017 
00018 #include <boost/lexical_cast.hpp>
00019 #include <boost/algorithm/string.hpp>
00020 
00021 using namespace std;
00022 using namespace boost;
00023 using namespace boost::filesystem;
00024 
00025 namespace phlogo {
00026 
00027 void Verbs::test(TokenStream *ts) {
00028     
00029         string code = getWord(ts);
00030     string expected = getWord(ts);
00031         
00032     _itp->run(_world, code);
00033     
00034     shared_ptr<Thing> result(new Thing());
00035     if (_world->getConsole()->getString() != expected) {
00036         result->set("failed [" + expected + " != " + _world->getConsole()->getString() + "]");
00037     }
00038     else
00039         result->set("passed");
00040     _world->setResult(result);
00041     
00042 }
00043 
00044 class LUnitProcessor : public LineProcessor {
00045     
00046 public:
00047     LUnitProcessor(Interpreter *itp, World *world) :  
00048         _itp(itp), _world(world), _name(""), _passcount(0), _total(0) {}
00049     
00050     virtual void process(const std::string &line);
00051     
00052     int getPasscount() { return _passcount; }
00053     int getTotal() { return _total; }
00054     
00055 private:
00056     Interpreter *_itp;
00057     World *_world;
00058     string _name;
00059     int _passcount;
00060     int _total;
00061 };
00062     
00063 void LUnitProcessor::process(const std::string &s) {
00064     
00065     string line = s;
00066     if (line[0] == '=') {
00067         // execute the test.
00068         size_t test_start = line.find_first_not_of("\t ", 1);
00069         if (test_start != string::npos) {
00070             string test = line.substr(test_start);
00071             replace_all(test, "\\n", "\n");
00072             if (_world->getConsole()->getString() != test)
00073                 cout << _name << ": failed [" << test << " != " + _world->getConsole()->getString() << "]" << endl;
00074             else
00075                 _passcount++;
00076             _total++;
00077             _world->getConsole()->reset();
00078         }
00079     }
00080     else if (line[0] == '>') {
00081         // execute the test.
00082         size_t test_start = line.find_first_not_of("\t ", 1);
00083         if (test_start != string::npos) {
00084             string test = line.substr(test_start);
00085             string gdata = static_cast<JSONTurtle *>(_world->getTurtle())->endGData();
00086             if (gdata != test)
00087                 cout << _name << ": failed [" << test << " != " + gdata << "]" << endl;
00088             else
00089                 _passcount++;
00090             _total++;
00091             _world->getTurtle()->clear();
00092         }
00093     }
00094     else {
00095         if (line[0] == '\t' || line[0] == ' ') {
00096             // statement
00097             // execute the statement.
00098             FileProcessor::parseLine(line, _itp, _world);
00099         }
00100         else {
00101             // suite name.
00102             trim(line);
00103             if (line.length() > 0) {
00104                 _name = line;
00105             }
00106         }
00107     }
00108     
00109 }
00110     
00111 void Verbs::testsuite(TokenStream *ts) {
00112     
00113     path p = "test";
00114     path fn = getWord(ts) + ".lunit";
00115 
00116     LUnitProcessor processor(_itp, _world);
00117     FileProcessor::parseFile(FileProcessor::locateFile(p, fn), &processor);
00118     
00119     shared_ptr<Thing> result(new Thing());
00120     if (processor.getPasscount() == processor.getTotal())
00121         result->set("passed [" + lexical_cast<string>(processor.getTotal()) + "]");
00122     else
00123         result->set("failed [" + lexical_cast<string>(processor.getTotal() - processor.getPasscount()) + "]");
00124     _world->setResult(result);
00125 
00126 }
00127 
00128 }
 All Classes Functions