Skip to content

Instantly share code, notes, and snippets.

@lithiumhead
Created January 31, 2018 17:28
Show Gist options
  • Save lithiumhead/d7f35f21a0f5a9af0ae6e5d536214d30 to your computer and use it in GitHub Desktop.
Save lithiumhead/d7f35f21a0f5a9af0ae6e5d536214d30 to your computer and use it in GitHub Desktop.
TEST_P() and code in same file - Test Fail & Formatted Output
#include <iostream>
#include <gtest/gtest.h>
struct BankAccount {
int balance = 0;
BankAccount() {
}
explicit BankAccount(const int balance)
: balance{balance} {
}
void deposit(int amount) {
balance += amount;
}
bool withdraw(int amount) {
// if (amount <= balance) {
// balance -= amount;
// return true;
// }
return false;
}
};
struct BankAccountTest : testing::Test {
BankAccount *account;
BankAccountTest() {
account = new BankAccount;
}
virtual ~BankAccountTest() {
delete account;
}
};
struct account_state {
int initial_balance;
int withdrawal_amount;
int final_balance;
bool success;
friend std::ostream& operator <<(std::ostream& os, const account_state & obj) {
return os
<< "initial_balance: " << obj.initial_balance
<< " withdrawal_amount: " << obj.withdrawal_amount
<< " final_balance: " << obj.final_balance
<< " success: " << obj.success;
}
};
struct withdrawAccountTest : BankAccountTest , testing::WithParamInterface<account_state> {
withdrawAccountTest() {
account->balance = GetParam().initial_balance;
}
};
TEST_P(withdrawAccountTest, FinalBalance) {
auto as = GetParam();
auto success = account->withdraw(as.withdrawal_amount);
EXPECT_EQ(as.final_balance,account->balance);
EXPECT_EQ(as.success,success);
}
INSTANTIATE_TEST_CASE_P(Default, withdrawAccountTest,
testing::Values(
account_state{100,50,50,true},
account_state{100,200,100,false}
));
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment