Skip to content
Permalink
4bc58bfaca
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
78 lines (67 sloc) 2.99 KB
#include <iostream>
#include <subprocess.hpp>
#include <internal/exists.hpp>
#include <internal/simple_parse.hpp>
#include "buildlist.hpp"
#include "question.hpp"
TEST_CASE("Assignment 2") {
const auto question = GENERATE(
Question { .id = "q1", .desc = "Q1: References" },
Question { .id = "q2", .desc = "Q2: Pointers" },
Question { .id = "q3", .desc = "Q3: Exception" }
);
DYNAMIC_SECTION(question.desc) {
DYNAMIC_SECTION("Compile " + question.id) {
// I'm not sure if running make before running our tests is a good idea or
// not yet. We'll see what responses are like. -Brandon
subprocess::popen make("make", {"-C", question.directory()});
const auto make_exit_status = make.wait();
INFO(make.stdout().rdbuf());
CHECK(make_exit_status == 0);
}
DYNAMIC_SECTION("Test a simple phrase for " + question.id) {
INFO("Make sure that the output is dumped one word per line");
CHECK_THAT(
*question.exec("Vim is better than emacs").disjunction({}),
Catch::Equals(std::vector<std::string>({ "Vim", "is", "better", "than", "emacs" }))
);
}
DYNAMIC_SECTION("Check a simple phrase with valgrind") {
INFO("Check that valgrind passes on your input and returns with no memory errors.");
CHECK(question.passes_valgrind("Emacs is better than vim"));
}
DYNAMIC_SECTION("Check that buildList (in " + question.wlist_path() + ") meets question criteria.") {
if (!exists(question.wlist_path())) {
FAIL("File not found: " + question.wlist_path());
}
const auto w_list_file = simple_parse(question.wlist_path());
if (w_list_file == nullptr) {
FAIL("Failed to parse wList.cpp file. Check the file's syntax and make sure it compiles.");
}
const auto build_list = find_build_list_fn(*w_list_file);
if (!build_list.has_value()) {
FAIL("Make sure the \"buildList\" function exists in \"asgn2/q1/WList.cpp\"");
}
// Ew... casting. (The first-party example uses it too, so this may be
// required by the cppast API design.)
auto& build_list_fn = static_cast<const cppast::cpp_function&>(*build_list);
if (question.id == "q1" || question.id == "q3") {
INFO("\"buildList\" needs to take an \"std::list\" as a reference for q1.\"");
CHECK(function_takes_list_reference(build_list_fn));
}
if (question.id == "q2") {
INFO("\"buildList\" needs to return an \"std::list\" pointer.\"");
CHECK(function_returns_list_pointer(build_list_fn));
}
}
if (question.id == "q3") {
SECTION("Output should be different if non-alphabetic character is entered") {
INFO("Have buildList throw an exception, then output an error (instead of the user input). The non-alphabetic characters should not be printed back out.");
CHECK_THAT(
*question.exec("Vim = emacs").disjunction({}),
!Catch::Equals(std::vector<std::string>({ "Vim", "=", "emacs" }))
);
}
}
}
}