Skip to content

[clang-doc] Add basic e2e test #93928

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

Merged
merged 10 commits into from
Jun 19, 2024
Merged

Large diffs are not rendered by default.

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

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,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,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,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,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_);
}
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.
};
30 changes: 30 additions & 0 deletions clang-tools-extra/test/clang-doc/Inputs/clang-doc-project1/Shape.h
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,17 @@
[
{
"directory": "$test_dir/build",
Copy link
Contributor

Choose a reason for hiding this comment

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

Would the following work? It would be really nice if we could make this operate w/o the sed command.

Suggested change
"directory": "$test_dir/build",
"directory": "./build",

or

Suggested change
"directory": "$test_dir/build",
"directory": ".",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried getting relative paths to work but it didn't seem to work

"command": "clang++ -o Calculator.o -I../include $test_dir/src/Calculator.cpp",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"command": "clang++ -o Calculator.o -I../include $test_dir/src/Calculator.cpp",
"command": "clang++ -o Calculator.o -I../include ../src/Calculator.cpp",

This can be relative too. Same in other lines.

"file": "$test_dir/src/Calculator.cpp"
},
{
"directory": "$test_dir/build",
"command": "clang++ -o Circle.o -I../include $test_dir/src/Circle.cpp",
"file": "$test_dir/src/Circle.cpp"
},
{
"directory": "$test_dir/build",
"command": "clang++ -o Rectangle.o -I../include $test_dir/src/Rectangle.cpp",
"file": "$test_dir/src/Rectangle.cpp"
}
]
87 changes: 87 additions & 0 deletions clang-tools-extra/test/clang-doc/Inputs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Append using posix-style a file name or directory to Base
function append(Base, New) {
if (!New)
return Base;
if (Base)
Base += "/";
Base += New;
return Base;
}

// Get relative path to access FilePath from CurrentDirectory
function computeRelativePath(FilePath, CurrentDirectory) {
var Path = FilePath;
while (Path) {
if (CurrentDirectory == Path)
return FilePath.substring(Path.length + 1);
Path = Path.substring(0, Path.lastIndexOf("/"));
}

var Dir = CurrentDirectory;
var Result = "";
while (Dir) {
if (Dir == FilePath)
break;
Dir = Dir.substring(0, Dir.lastIndexOf("/"));
Result = append(Result, "..")
}
Result = append(Result, FilePath.substring(Dir.length))
return Result;
}

function genLink(Ref, CurrentDirectory) {
var Path = computeRelativePath(Ref.Path, CurrentDirectory);
if (Ref.RefType == "namespace")
Path = append(Path, "index.html");
else
Path = append(Path, Ref.Name + ".html")

ANode = document.createElement("a");
ANode.setAttribute("href", Path);
var TextNode = document.createTextNode(Ref.Name);
ANode.appendChild(TextNode);
return ANode;
}

function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
// Out will store the HTML elements that Index requires to be generated
var Out = [];
if (Index.Name) {
var SpanNode = document.createElement("span");
var TextNode = document.createTextNode(Index.Name);
SpanNode.appendChild(genLink(Index, CurrentDirectory));
Out.push(SpanNode);
}
if (Index.Children.length == 0)
return Out;
// Only the outermost list should use ol, the others should use ul
var ListNodeName = IsOutermostList ? "ol" : "ul";
var ListNode = document.createElement(ListNodeName);
for (Child of Index.Children) {
var LiNode = document.createElement("li");
ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false);
for (Node of ChildNodes)
LiNode.appendChild(Node);
ListNode.appendChild(LiNode);
}
Out.push(ListNode);
return Out;
}

function createIndex(Index) {
// Get the DOM element where the index will be created
var IndexDiv = document.getElementById("sidebar-left");
// Get the relative path of this file
CurrentDirectory = IndexDiv.getAttribute("path");
var IndexNodes = genHTMLOfIndex(Index, CurrentDirectory, true);
for (Node of IndexNodes)
IndexDiv.appendChild(Node);
}

// Runs after DOM loads
document.addEventListener("DOMContentLoaded", function() {
// JsonIndex is a variable from another file that contains the index
// in JSON format
var Index = JSON.parse(JsonIndex);
createIndex(Index);
});
Loading