test: add HTML and SVG exporter unit tests

- Add HTMLExporterTest covering HTML structure, rectangle/circle div generation,
  empty and nested collections, and constructor
- Add SVGExporterTest covering SVG structure, rectangle/circle elements,
  color attributes, nested collections, and constructor
This commit is contained in:
2026-03-27 14:29:38 +01:00
parent 211f15658b
commit da70c9f568
2 changed files with 263 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
package ovh.gasser.newshapes.exporters;
import ovh.gasser.newshapes.HTMLExporter;
import ovh.gasser.newshapes.shapes.SCircle;
import ovh.gasser.newshapes.shapes.SCollection;
import ovh.gasser.newshapes.shapes.SRectangle;
import ovh.gasser.newshapes.ui.visitors.HTMLDraftman;
import java.awt.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class HTMLExporterTest {
@Test
void testExportCreatesValidHtmlStructure() throws Exception {
SCollection model = SCollection.of(
SRectangle.create(10, 20, 100, 50, Color.RED)
);
StringWriter htmlBuffer = new StringWriter();
StringWriter cssBuffer = new StringWriter();
PrintWriter htmlWriter = new PrintWriter(htmlBuffer);
PrintWriter cssWriter = new PrintWriter(cssBuffer);
HTMLDraftman draftman = new HTMLDraftman(htmlWriter, cssWriter);
draftman.generateHTML(model);
String htmlOutput = htmlBuffer.toString();
String cssOutput = cssBuffer.toString();
assertTrue(htmlOutput.contains("<!DOCTYPE html>"), "HTML should contain DOCTYPE");
assertTrue(htmlOutput.contains("<html"), "HTML should contain html tag");
assertTrue(htmlOutput.contains("<head>"), "HTML should contain head tag");
assertTrue(htmlOutput.contains("<body>"), "HTML should contain body tag");
assertTrue(htmlOutput.contains("</body>"), "HTML should close body tag");
assertTrue(htmlOutput.contains("style.css"), "HTML should reference stylesheet");
}
@Test
void testExportRectangleGeneratesCorrectDiv() throws Exception {
SRectangle rect = SRectangle.create(10, 20, 100, 50, Color.BLUE);
SCollection model = SCollection.of(rect);
StringWriter htmlBuffer = new StringWriter();
StringWriter cssBuffer = new StringWriter();
PrintWriter htmlWriter = new PrintWriter(htmlBuffer);
PrintWriter cssWriter = new PrintWriter(cssBuffer);
HTMLDraftman draftman = new HTMLDraftman(htmlWriter, cssWriter);
draftman.generateHTML(model);
String htmlOutput = htmlBuffer.toString();
String cssOutput = cssBuffer.toString();
assertTrue(htmlOutput.contains("rec"), "HTML should contain rectangle div");
assertTrue(cssOutput.contains("top:20px"), "CSS should contain correct top position");
assertTrue(cssOutput.contains("left:10px"), "CSS should contain correct left position");
assertTrue(cssOutput.contains("width:100px"), "CSS should contain correct width");
assertTrue(cssOutput.contains("height:50px"), "CSS should contain correct height");
}
@Test
void testExportCircleGeneratesCorrectDiv() throws Exception {
SCircle circle = SCircle.create(50, 50, 30, Color.GREEN);
SCollection model = SCollection.of(circle);
StringWriter htmlBuffer = new StringWriter();
StringWriter cssBuffer = new StringWriter();
PrintWriter htmlWriter = new PrintWriter(htmlBuffer);
PrintWriter cssWriter = new PrintWriter(cssBuffer);
HTMLDraftman draftman = new HTMLDraftman(htmlWriter, cssWriter);
draftman.generateHTML(model);
String htmlOutput = htmlBuffer.toString();
String cssOutput = cssBuffer.toString();
assertTrue(htmlOutput.contains("circle"), "HTML should contain circle class");
assertTrue(cssOutput.contains("border-radius"), "CSS should contain border-radius for circle");
}
@Test
void testExportEmptyCollection() throws Exception {
SCollection model = SCollection.of();
StringWriter htmlBuffer = new StringWriter();
StringWriter cssBuffer = new StringWriter();
PrintWriter htmlWriter = new PrintWriter(htmlBuffer);
PrintWriter cssWriter = new PrintWriter(cssBuffer);
HTMLDraftman draftman = new HTMLDraftman(htmlWriter, cssWriter);
draftman.generateHTML(model);
String htmlOutput = htmlBuffer.toString();
assertTrue(htmlOutput.contains("<!DOCTYPE html>"), "HTML should contain DOCTYPE");
assertTrue(htmlOutput.contains("<body>"), "HTML should contain body tag");
assertTrue(htmlOutput.contains("</body>"), "HTML should close body tag");
}
@Test
void testExportNestedCollection() throws Exception {
SCollection nested = SCollection.of(
SRectangle.create(0, 0, 10, 10, Color.RED),
SRectangle.create(20, 0, 10, 10, Color.BLUE)
);
SCollection model = SCollection.of(nested);
StringWriter htmlBuffer = new StringWriter();
StringWriter cssBuffer = new StringWriter();
PrintWriter htmlWriter = new PrintWriter(htmlBuffer);
PrintWriter cssWriter = new PrintWriter(cssBuffer);
HTMLDraftman draftman = new HTMLDraftman(htmlWriter, cssWriter);
draftman.generateHTML(model);
String htmlOutput = htmlBuffer.toString();
assertTrue(htmlOutput.contains("rec"), "HTML should contain rectangles");
}
@Test
void testHTMLExporterConstructorWithModel() {
SCollection model = SCollection.of(SRectangle.create(1, 2, 3, 4));
HTMLExporter exporter = new HTMLExporter(model);
assertNotNull(exporter);
}
}

View File

@@ -0,0 +1,135 @@
package ovh.gasser.newshapes.exporters;
import ovh.gasser.newshapes.SVGExporter;
import ovh.gasser.newshapes.attributes.ColorAttributes;
import ovh.gasser.newshapes.shapes.SCircle;
import ovh.gasser.newshapes.shapes.SCollection;
import ovh.gasser.newshapes.shapes.SRectangle;
import ovh.gasser.newshapes.ui.visitors.SVGDraftman;
import java.awt.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class SVGExporterTest {
@Test
void testExportCreatesValidSvgStructure() throws Exception {
SCollection model = SCollection.of(
SRectangle.create(10, 20, 100, 50)
);
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);
SVGDraftman draftman = new SVGDraftman(writer);
draftman.generateSVG(model);
String output = buffer.toString();
assertTrue(output.contains("<?xml version=\"1.0\" encoding=\"utf-8\"?>"), "SVG should contain XML declaration");
assertTrue(output.contains("<svg xmlns=\"http://www.w3.org/2000/svg\""), "SVG should contain svg element");
assertTrue(output.contains("</svg>"), "SVG should close svg element");
}
@Test
void testExportRectangleGeneratesCorrectElement() throws Exception {
SRectangle rect = SRectangle.create(10, 20, 100, 50);
rect.addAttributes(new ColorAttributes(true, false, Color.BLUE, null));
SCollection model = SCollection.of(rect);
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);
SVGDraftman draftman = new SVGDraftman(writer);
draftman.generateSVG(model);
String output = buffer.toString();
assertTrue(output.contains("<rect"), "SVG should contain rect element");
assertTrue(output.contains("width=\"100\""), "SVG should contain correct width");
assertTrue(output.contains("height=\"50\""), "SVG should contain correct height");
assertTrue(output.contains("x=\"10\""), "SVG should contain correct x");
assertTrue(output.contains("y=\"20\""), "SVG should contain correct y");
}
@Test
void testExportCircleGeneratesCorrectElement() throws Exception {
SCircle circle = SCircle.create(50, 50, 30);
circle.addAttributes(new ColorAttributes(true, false, Color.RED, null));
SCollection model = SCollection.of(circle);
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);
SVGDraftman draftman = new SVGDraftman(writer);
draftman.generateSVG(model);
String output = buffer.toString();
assertTrue(output.contains("<circle"), "SVG should contain circle element");
assertTrue(output.contains("cx=\"80\""), "SVG should contain correct cx (x + radius)");
assertTrue(output.contains("cy=\"80\""), "SVG should contain correct cy (y + radius)");
assertTrue(output.contains("r=\"30\""), "SVG should contain correct radius");
}
@Test
void testExportEmptyCollection() throws Exception {
SCollection model = SCollection.of();
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);
SVGDraftman draftman = new SVGDraftman(writer);
draftman.generateSVG(model);
String output = buffer.toString();
assertTrue(output.contains("<?xml version=\"1.0\" encoding=\"utf-8\"?>"), "SVG should contain XML declaration");
assertTrue(output.contains("<svg"), "SVG should contain svg element");
assertTrue(output.contains("</svg>"), "SVG should close svg element");
}
@Test
void testExportNestedCollection() throws Exception {
SCollection nested = SCollection.of(
SRectangle.create(0, 0, 10, 10),
SRectangle.create(20, 0, 10, 10)
);
SCollection model = SCollection.of(nested);
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);
SVGDraftman draftman = new SVGDraftman(writer);
draftman.generateSVG(model);
String output = buffer.toString();
assertTrue(output.contains("<rect"), "SVG should contain rect elements");
assertTrue(output.contains("</svg>"), "SVG should close svg element");
}
@Test
void testExportWithColorAttributes() throws Exception {
SRectangle rect = SRectangle.create(0, 0, 50, 50);
rect.addAttributes(new ColorAttributes(true, true, Color.decode("#ff0000"), Color.decode("#000000")));
SCollection model = SCollection.of(rect);
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);
SVGDraftman draftman = new SVGDraftman(writer);
draftman.generateSVG(model);
String output = buffer.toString();
assertTrue(output.contains("fill=\"#ff0000\""), "SVG should contain fill color");
assertTrue(output.contains("stroke=\"#000000\""), "SVG should contain stroke color");
}
@Test
void testSVGExporterConstructorWithModel() {
SCollection model = SCollection.of(SRectangle.create(1, 2, 3, 4));
SVGExporter exporter = new SVGExporter(model);
assertNotNull(exporter);
}
}