Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
test_user.cpp
Go to the documentation of this file.
1#include "user.hpp"
2#include <doctest.h>
3#include <iostream>
4#include <sstream>
5
6// --- User class unit tests ---
7
8TEST_CASE("User constructor initializes fields") {
9 User user("octocat", "The Octocat", "GitHub", "San Francisco");
10
11 CHECK(user.login == "octocat");
12 CHECK(user.name == "The Octocat");
13 CHECK(user.company == "GitHub");
14 CHECK(user.location == "San Francisco");
15}
16
17TEST_CASE("User::print() outputs correct format") {
18 User user("octocat", "The Octocat", "GitHub", "San Francisco");
19
20 std::ostringstream output;
21 std::streambuf *old_cout_buf = std::cout.rdbuf(output.rdbuf());
22
23 user.print();
24
25 std::cout.rdbuf(old_cout_buf); // restore std::cout
26
27 std::string result = output.str();
28 CHECK(result.find("Login: octocat") != std::string::npos);
29 CHECK(result.find("Name: The Octocat") != std::string::npos);
30 CHECK(result.find("Company: GitHub") != std::string::npos);
31 CHECK(result.find("Location: San Francisco") != std::string::npos);
32}
33
34// --- fetch_github_user integration tests ---
35
36TEST_CASE("fetch_github_user returns valid user for known GitHub account") {
37 User user = User::fetch_github_user("octocat");
38
39 CHECK(user.login == "octocat");
40
41 // These fields can change, so we check they are not empty instead
42 CHECK_FALSE(user.name.empty());
43 CHECK_FALSE(user.company.empty());
44 CHECK_FALSE(user.location.empty());
45}
46
47TEST_CASE("fetch_github_user throws for nonexistent username") {
48 std::string invalid_username = "this_user_should_not_exist_123456789";
49
50 CHECK_THROWS_AS(User::fetch_github_user(invalid_username),
51 std::runtime_error);
52}
#define CHECK_FALSE(...)
Definition doctest.h:2971
#define CHECK(...)
Definition doctest.h:2970
#define TEST_CASE(name)
Definition doctest.h:2937
#define CHECK_THROWS_AS(expr,...)
Definition doctest.h:2973
static const char * output
Structure representing a GitHub user.
Definition user.h:9
const char * name
Full name.
Definition user.h:11
const char * login
GitHub username.
Definition user.h:10
const char * location
User's location.
Definition user.h:13
const char * company
Company name.
Definition user.h:12