Skip to content

Commit

Permalink
Merge pull request #76 from sebastian-sauer/feature/allow_property_wi…
Browse files Browse the repository at this point in the history
…th_textcontent
  • Loading branch information
davidparsson authored Sep 18, 2024
2 parents 26e2634 + 7e40b4c commit 6cb2ba4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
3 changes: 2 additions & 1 deletion spec/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe('JUnit Report builder', () => {
.className('suite1.test.Class2')
.name('Third test')
.file('./path-to/the-test-file.coffee')
.property('property name', 'property value');
.property('property name', 'property value')
.multilineProperty('property name 2', 'property value 2');

const suite2 = builder.testSuite().name('second.Suite');
suite2.testCase().failure('Failure message');
Expand Down
1 change: 1 addition & 0 deletions spec/expected_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<testcase classname="suite1.test.Class2" name="Third test" file="./path-to/the-test-file.coffee">
<properties>
<property name="property name" value="property value"/>
<property name="property name 2">property value 2</property>
</properties>
</testcase>
</testsuite>
Expand Down
14 changes: 14 additions & 0 deletions spec/test_case.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ describe('Test Case builder', () => {
});
});

it('should add the provided property with textContent as elements with textContent', () => {
testCase.multilineProperty('property name', 'property value');

testCase.build(parentElement);

expect(propertiesElement.ele).toHaveBeenCalledWith(
'property',
{
name: 'property name',
},
'property value',
);
});

it('should add a failure node when test failed', () => {
testCase.failure();

Expand Down
14 changes: 14 additions & 0 deletions spec/test_suite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ describe('Test Suite builder', () => {
});
});

it('should add the provided property with textContent as element with textContent', () => {
testSuite.multilineProperty('property name', 'property value');

testSuite.build(parentElement);

expect(propertiesElement.ele).toHaveBeenCalledWith(
'property',
{
name: 'property name',
},
'property value',
);
});

it('should add all the provided properties as elements', () => {
testSuite.property('name 1', 'value 1');
testSuite.property('name 2', 'value 2');
Expand Down
35 changes: 28 additions & 7 deletions src/test_node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import _ from 'lodash';
import xmlBuilder, { type XMLElement } from 'xmlbuilder';

/** Helper interface to describe one property */
interface Property {
/** name of the property */
name: string;
/** value of the property */
value: string;
/** if true the property will be serialized as node with textcontent */
isPropertyWithTextContent: boolean;
}

export abstract class TestNode {
private _elementName: string;
protected _attributes: any;
private _properties: any[];
private _properties: Property[];

/**
* @param elementName - the name of the XML element
Expand All @@ -21,7 +31,17 @@ export abstract class TestNode {
* @returns this
*/
property(name: string, value: string): this {
this._properties.push({ name: name, value: value });
this._properties.push({ name: name, value: value, isPropertyWithTextContent: false });
return this;
}

/**
* @param name
* @param value
* @returns this
*/
multilineProperty(name: string, value: string): this {
this._properties.push({ name: name, value: value, isPropertyWithTextContent: true });
return this;
}

Expand Down Expand Up @@ -122,11 +142,12 @@ export abstract class TestNode {
protected buildNode(element: XMLElement): XMLElement {
if (this._properties.length) {
var propertiesElement = element.ele('properties');
_.forEach(this._properties, (property: any) => {
propertiesElement.ele('property', {
name: property.name,
value: property.value,
});
_.forEach(this._properties, (property: Property) => {
if (property.isPropertyWithTextContent) {
propertiesElement.ele('property', { name: property.name }, property.value);
} else {
propertiesElement.ele('property', { name: property.name, value: property.value });
}
});
}
return element;
Expand Down

0 comments on commit 6cb2ba4

Please sign in to comment.