mardi 31 mars 2020

State Design Pattern GTEST issues

I've been trying to work with a state pattern design where an order switches between Incart, Pending and Complete. The code compiles, but when I test I keep getting segmentation faults and I cannot wrap my head around what exactly I am doing wrong. I could really use some help in fixing my errors. This is my code for the following classes

Order.h

#ifndef ORDER_H
#define ORDER_H
#include "State.h"
#include "ShippingMethod.h"
#include "Paper.h"
#include <string>
#include <vector>

class State;

class Order {
 public:
    Order()
    Order(int weight);
    virtual ~Order();
    void setState(State* state);
    void addProduct(Paper*p);
    void edit(Paper*p, int v);
    void submit();
    void cancel(Paper *p);
    void addToWaitlist();
    std::string orderStatus();
    std::string getShippingMethod() {
        return method;
    }
    int OrderSize();
    int productLocation(Paper *p);

 private:
    State* currentState;
    ShippingMethod * shipMethod;
    std::string method;
    std::vector<Paper*> orders;
};
#endif

Order.cpp

#include "Order.h"
#include "State.h"
#include "Paper.h"
#include "ShippingMethod.h"
#include "Inventory.h"
#include <string>

class State;

Order::Order(int weight) {
  shipMethod = new ShippingMethod();
  shipMethod->setShippingMethod(weight);
  method = shipMethod->getShippingMethod();
  currentState == NULL;
}

Order::~Order() {
    for (unsigned int i = 0; i < orders.size(); i++) {
        delete orders[i];
        orders[i] = NULL;
     }
    delete currentState;
    delete shipMethod;
    shipMethod == NULL;
    currentState == NULL;
}

void Order::setState(State* s) {
        currentState = s;
}

void Order::addProduct(Paper *p) {
    if (currentState->addProduct()) {
       orders.push_back(p);
    } else {
        std::cout << "Cannot add product to a complete order" << std::endl;
    }
}

void Order::edit(Paper *p, int v) {
    int loc;
    if (currentState->cancel()) {
    loc = Order::productLocation(p);
    orders.at(loc)->setQuantity(v);
    std::cout << "Order has been edited" << std::endl;
    } else {
        std::cout << "Cannot order complete order" <<std::endl;
    }
}

void Order::submit() {
    if (currentState->submit()) {
        std::cout<< "Order has been submitted" << std::endl;
    } else {
        std::cout << "Order has already been submitted" << std::endl;
    }
}

void Order::cancel(Paper *p) {
  if (currentState->cancel()) {
    int loc;
    loc = Order::productLocation(p);
    orders.erase(orders.begin() + loc);
    std::cout<< "Order has been cancelled" << std::endl;
  } else {
      std::cout << "Order has been completed.. cannot be cancelled" <<std::endl;
  }
}

void Order::addToWaitlist() {
    if (currentState->addToWaitlist()) {
        std::cout <<"Order has been added to waitlist";
    } else {
        std::cout << "Order has already been added to waitlist ";
    }
}

int Order::productLocation(Paper *p) {
    int index;
    for (unsigned int i = 0; i < orders.size(); i++) {
        if (orders[i]->getProductName() == p->getProductName()) {
            index = i;
        }
    }
    return index;
}

std::string Order::orderStatus() {
      this->currentState->display();
}

int Order::OrderSize() {
    return orders.size();
}

State.h

#ifndef STATE_H
#define STATE_H
#include "Order.h"
#include "Paper.h"
#include <iostream>
#include <vector>
#include <string>

class Order;

class State {
 public:
    virtual bool addProduct() = 0;
    virtual bool edit() = 0;
    virtual bool submit() = 0;
    virtual bool cancel() = 0;
    virtual bool addToWaitlist() = 0;
    virtual std::string display() = 0;
};
#endif

Incart.h

#ifndef INCART_H
#define INCART_H
#include "State.h"
#include "Paper.h"
#include <string>


class Incart : public State {
 public:
    Incart(Order* o);
    virtual ~Incart();
    bool addProduct();
    bool edit();
    bool submit();
    bool cancel();
    bool addToWaitlist();
    std::string display() {
        return "In Cart";
    }

 private:
    Order* currentState;
};
#endif

Incart.cpp

#include "State.h"
#include "Order.h"
#include "Incart.h"
#include "Pending.h"
#include <string>
#include <iostream>

Incart::Incart(Order *o) {
    currentState = o;
}

Incart::~Incart() {
    currentState = NULL;
}

bool Incart::addProduct() {
    return true;
}

bool Incart::edit() {
    return true;
}

bool Incart::submit() {
    this->currentState->setState(new Pending(currentState));
    return true;
}

bool Incart::cancel() {
    return true;
}

bool Incart::addToWaitlist() {
    currentState->setState(new Pending(currentState));
    return true;
}

TestOrder.cpp

#include "Order.h"
#include "Paper.h"
#include "ShippingMethod.h"
#include "State.h"
#include "Pending.h"
#include "Complete.h"
#include "Incart.h"
#include "gtest/gtest.h"

TEST(TestOrder, testConstructor) {
    Paper* p = new Paper("Kleenex", 10, 10);
    Order* o = new Order(100);
    EXPECT_TRUE(o != NULL);
}

TEST(TestOrder, testPendingState) {
    Paper* p = new Paper("Kleenex", 10, 10);
    Order* o = new Order(100);
    o->setState(new Pending(o));
//SEG FAULT HERE    EXPECT_EQ("Pending", o->orderStatus());

}

TEST(TestOrder, testCompleteState) {
}

TEST(TestOrder, testShippingMethod) {
}

TEST(TestOrder, testSubmit) {
}

TEST(TestOrder, TestAddOrder) {
}
TEST(TestOrder, TestCancelOrder) {
}

TEST(TestOrder, TestEditOrder) {
}

TEST(TestOrder, addToWaitlistOrder) {
}

Aucun commentaire:

Enregistrer un commentaire