Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GoogleTest Vector Comparison Diff

Compares contents of std::vector objects, outputs a basic diff if the comparison fails

Screenshot from 2024-08-29 16-40-26

Example

person.h

#pragma once

class Person {
 public:
  const std::string name;
  const int age;

  Person(std::string name, int value) : name(name), age(age) {}
  std::string to_string() const;
  bool operator==(const Person& p) const;
};

std::string to_string(const Person& p);

person.cc

std::string Person::to_string() const {
  return "name: " + name + ", age: " + std::to_string( age );
}

bool Person::operator==(const Person& p) const {
  return name == p.name && age == p.age;
}

std::string to_string(const Person& p) {
  return p.to_string();
}

my-class-test.h

#pragma once

#include <gtest/gtest.h>

class MyTest : public testing::Test { }

my-class-test.cc

#include "my-class-test.h"
#include "my-class.h"

#include <string>
#include <vector>

TEST_F(MyTest, TestBasic) {
  MyClass instance;

  people = instance.get_people();

  std::vector<Person> expected = {
    Person("Gregory House", 45),
    Person("Lisa Cuddy", 57),
    Person("Robert Chase", 26),
    Person("James Wilson", 46)
  }

  EXPECT_PRED_FORMAT2(TestCustom::compare_vectors<Person>, expected, people);
}

About

Vector comparison for GoogleTest with basic diff

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages