[WIP] tests transform it into BDD Style

This commit is contained in:
NADAL Jean-Baptiste
2024-02-13 18:41:42 +01:00
parent c3d3a85f08
commit bac26441c5
6 changed files with 1635 additions and 731 deletions

View File

@@ -33,80 +33,136 @@ using namespace Raytracer;
/* ------------------------------------------------------------------------- */
TEST_CASE("[02][02][Canvas] Creating a canvas", "[Canvas]")
SCENARIO("Creating a canvas", "[features/canvas.feature]")
{
Canvas c(10, 20);
REQUIRE(c.width() == 10);
REQUIRE(c.height() == 20);
for (int i = 0; i < 10; ++i)
GIVEN("c <-canvas(10, 20)")
{
for (int j = 0; j < 20; ++j)
Canvas c(10, 20);
THEN("c.width = 10")
{
REQUIRE(c.pixel_at(2, 3) == Color(0, 0, 0));
REQUIRE(c.width() == 10);
}
AND_THEN("c.height = 20")
{
REQUIRE(c.height() == 20);
}
}
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[02][02][Canvas] Writing pixels to a canvas", "[Canvas]")
{
Canvas c(10, 20);
Color red(1, 0, 0);
c.write_pixel(2, 3, red);
REQUIRE(c.pixel_at(2, 3) == red);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[02][02][Canvas] Constructing the PPM pixel data", "[Canvas]")
{
std::string ppm, the_ref_ppm;
Canvas c(5, 3);
Color c1(1.5, 0, 0);
Color c2(0, 0.5, 0);
Color c3(-0.5, 0, 1);
c.write_pixel(0, 0, c1);
c.write_pixel(2, 1, c2);
c.write_pixel(4, 2, c3);
ppm = c.to_ppm();
the_ref_ppm = "P3\n5 3\n255\n";
the_ref_ppm += "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n";
the_ref_ppm += "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0\n";
the_ref_ppm += "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255\n";
REQUIRE(ppm == the_ref_ppm);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[02][02][Canvas] Split long lines in PPM files", "[Canvas]")
{
std::string ppm, the_ref_ppm;
Canvas c(10, 2);
for (int j = 0; j < 2; ++j)
{
for (int i = 0; i < 10; ++i)
{
c.write_pixel(i, j, Color(1, 0.8, 0.6));
for (int j = 0; j < 20; ++j)
{
AND_THEN("every pixel of c is color(0,0,0)")
{
REQUIRE(c.pixel_at(2, 3) == Color(0, 0, 0));
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Writing pixels to a canvas", "[features/canvas.feature]")
{
GIVEN("c <-canvas(10, 20)")
{
Canvas c(10, 20);
AND_GIVEN("red <-color(1, 0, 0)")
{
Color red(1, 0, 0);
WHEN("write_pixel(c,2,3,red)")
{
c.write_pixel(2, 3, red);
THEN("pixel_at(c,2,3) = red")
{
REQUIRE(c.pixel_at(2, 3) == red);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Constructing the PPM pixel data", "[features/canvas.feature]")
{
std::string ppm, the_ref_ppm;
GIVEN("c <-canvas(5, 3)")
{
Canvas c(5, 3);
AND_GIVEN(" c1 color(1.5, 0, 0)")
{
Color c1(1.5, 0, 0);
AND_GIVEN(" c2 color(0, 0.5, 0)")
{
Color c2(0, 0.5, 0);
AND_GIVEN(" c3 color(-0.5, 0, 1))")
{
Color c3(-0.5, 0, 1);
WHEN("write_pixel(c, 0, 0, c1)")
{
c.write_pixel(0, 0, c1);
AND_WHEN("write_pixel(c, 2, 1, c2)")
{
c.write_pixel(2, 1, c2);
AND_WHEN("write_pixel(c, 4, 2, c3)")
{
c.write_pixel(4, 2, c3);
AND_WHEN("ppm <- canvas_to_ppm(c)")
{
ppm = c.to_ppm();
the_ref_ppm = "P3\n5 3\n255\n";
the_ref_ppm += "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n";
the_ref_ppm += "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0\n";
the_ref_ppm += "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255\n";
THEN("line 4-6 of ppm are 255 0 0 ....")
{
REQUIRE(ppm == the_ref_ppm);
}
}
}
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Split long lines in PPM files", "[features/canvas.feature]")
{
std::string ppm, the_ref_ppm;
GIVEN("c <-canvas(10, 2)")
{
Canvas c(10, 2);
WHEN("every pixel of c is set to color(1,0.8,0.6)")
{
for (int j = 0; j < 2; ++j)
{
for (int i = 0; i < 10; ++i)
{
c.write_pixel(i, j, Color(1, 0.8, 0.6));
}
}
AND_WHEN("ppm <- canvas_to_ppm(c)")
{
ppm = c.to_ppm();
the_ref_ppm = "P3\n10 2\n255\n";
the_ref_ppm += "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n";
the_ref_ppm += "153 255 204 153 255 204 153 255 204 153 255 204 153\n";
the_ref_ppm += "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n";
the_ref_ppm += "153 255 204 153 255 204 153 255 204 153 255 204 153\n";
THEN("line 4-7 of ppm are 255 204 153 255 204 ...")
{
REQUIRE(ppm == the_ref_ppm);
}
}
}
}
ppm = c.to_ppm();
the_ref_ppm = "P3\n10 2\n255\n";
the_ref_ppm += "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n";
the_ref_ppm += "153 255 204 153 255 204 153 255 204 153 255 204 153\n";
the_ref_ppm += "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n";
the_ref_ppm += "153 255 204 153 255 204 153 255 204 153 255 204 153\n";
REQUIRE(ppm == the_ref_ppm);
}