Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-doc] Add basic e2e test #93928

Merged
merged 10 commits into from
Jun 19, 2024
3 changes: 1 addition & 2 deletions clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ Example usage for a project using a compile commands database:
llvm::outs() << "Generating assets for docs...\n";
Err = G->get()->createResources(CDCtx);
if (Err) {
llvm::errs() << toString(std::move(Err)) << "\n";
return 1;
llvm::outs() << "warning: " << toString(std::move(Err)) << "\n";
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"directory": "$test_dir/Inputs/basic-project",
"command": "clang++ -o Calculator.o -I./include ./src/Calculator.cpp",
"file": "./src/Calculator.cpp"
},
{
"directory": "$test_dir/Inputs/basic-project",
"command": "clang++ -o Circle.o -I./include ./src/Circle.cpp",
"file": "./src/Circle.cpp"
},
{
"directory": "$test_dir/Inputs/basic-project",
"command": "clang++ -o Rectangle.o -I./include ./src/Rectangle.cpp",
"file": "./src/Rectangle.cpp"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

/**
* @brief A simple calculator class.
*
* Provides basic arithmetic operations.
*/
class Calculator {
public:
/**
* @brief Adds two integers.
*
* @param a First integer.
* @param b Second integer.
* @return int The sum of a and b.
*/
int add(int a, int b);

/**
* @brief Subtracts the second integer from the first.
*
* @param a First integer.
* @param b Second integer.
* @return int The result of a - b.
*/
int subtract(int a, int b);

/**
* @brief Multiplies two integers.
*
* @param a First integer.
* @param b Second integer.
* @return int The product of a and b.
*/
int multiply(int a, int b);

/**
* @brief Divides the first integer by the second.
*
* @param a First integer.
* @param b Second integer.
* @return double The result of a / b.
* @throw std::invalid_argument if b is zero.
*/
double divide(int a, int b);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "Shape.h"

/**
* @brief Circle class derived from Shape.
*
* Represents a circle with a given radius.
*/
class Circle : public Shape {
public:
/**
* @brief Constructs a new Circle object.
*
* @param radius Radius of the circle.
*/
Circle(double radius);

/**
* @brief Calculates the area of the circle.
*
* @return double The area of the circle.
*/
double area() const override;

/**
* @brief Calculates the perimeter of the circle.
*
* @return double The perimeter of the circle.
*/
double perimeter() const override;

private:
double radius_; ///< Radius of the circle.
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "Shape.h"

/**
* @brief Rectangle class derived from Shape.
*
* Represents a rectangle with a given width and height.
*/
class Rectangle : public Shape {
public:
/**
* @brief Constructs a new Rectangle object.
*
* @param width Width of the rectangle.
* @param height Height of the rectangle.
*/
Rectangle(double width, double height);

/**
* @brief Calculates the area of the rectangle.
*
* @return double The area of the rectangle.
*/
double area() const override;

/**
* @brief Calculates the perimeter of the rectangle.
*
* @return double The perimeter of the rectangle.
*/
double perimeter() const override;

private:
double width_; ///< Width of the rectangle.
double height_; ///< Height of the rectangle.
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

/**
* @brief Abstract base class for shapes.
*
* Provides a common interface for different types of shapes.
*/
class Shape {
public:
/**
* @brief Virtual destructor.
*/
virtual ~Shape() {}

/**
* @brief Calculates the area of the shape.
*
* @return double The area of the shape.
*/
virtual double area() const = 0;

/**
* @brief Calculates the perimeter of the shape.
*
* @return double The perimeter of the shape.
*/
virtual double perimeter() const = 0;
};


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "Calculator.h"
#include <stdexcept>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests must be freestanding and cannot include system headers.


int Calculator::add(int a, int b) {
return a + b;
}

int Calculator::subtract(int a, int b) {
return a - b;
}

int Calculator::multiply(int a, int b) {
return a * b;
}

double Calculator::divide(int a, int b) {
if (b == 0) {
throw std::invalid_argument("Division by zero");
}
return static_cast<double>(a) / b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "Circle.h"

Circle::Circle(double radius) : radius_(radius) {}

double Circle::area() const {
return 3.141 * radius_ * radius_;
}

double Circle::perimeter() const {
return 3.141 * radius_;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "Rectangle.h"

Rectangle::Rectangle(double width, double height)
: width_(width), height_(height) {}

double Rectangle::area() const {
return width_ * height_;
}

double Rectangle::perimeter() const {
return 2 * (width_ + height_);
}
Loading