From c2998d485f972ac591abd5a367bbe77d5380d030 Mon Sep 17 00:00:00 2001 From: Sebastian Sauer Date: Mon, 16 Sep 2024 20:04:18 +0200 Subject: [PATCH 1/3] Allow properties to use textcontent --- spec/e2e.spec.ts | 3 ++- spec/expected_report.xml | 1 + spec/test_case.spec.ts | 14 ++++++++++++++ spec/test_suite.spec.ts | 14 ++++++++++++++ src/test_node.ts | 27 +++++++++++++++++++-------- 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/spec/e2e.spec.ts b/spec/e2e.spec.ts index 4fbf3a4..1a968b1 100644 --- a/spec/e2e.spec.ts +++ b/spec/e2e.spec.ts @@ -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') + .property('property name 2', 'property value 2', true); const suite2 = builder.testSuite().name('second.Suite'); suite2.testCase().failure('Failure message'); diff --git a/spec/expected_report.xml b/spec/expected_report.xml index d0ffe04..b9ddebf 100755 --- a/spec/expected_report.xml +++ b/spec/expected_report.xml @@ -6,6 +6,7 @@ + property value 2 diff --git a/spec/test_case.spec.ts b/spec/test_case.spec.ts index 6abed5e..e8273c6 100644 --- a/spec/test_case.spec.ts +++ b/spec/test_case.spec.ts @@ -127,6 +127,20 @@ describe('Test Case builder', () => { }); }); + it('should add the provided property with textContent as elements with textContent', () => { + testCase.property('property name', 'property value', true); + + testCase.build(parentElement); + + expect(propertiesElement.ele).toHaveBeenCalledWith( + 'property', + { + name: 'property name', + }, + 'property value', + ); + }); + it('should add a failure node when test failed', () => { testCase.failure(); diff --git a/spec/test_suite.spec.ts b/spec/test_suite.spec.ts index 3dfa5b3..71f5cda 100644 --- a/spec/test_suite.spec.ts +++ b/spec/test_suite.spec.ts @@ -132,6 +132,20 @@ describe('Test Suite builder', () => { }); }); + it('should add the provided property with textContent as element with textContent', () => { + testSuite.property('property name', 'property value', true); + + 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'); diff --git a/src/test_node.ts b/src/test_node.ts index ce18ff7..d3b3f4e 100644 --- a/src/test_node.ts +++ b/src/test_node.ts @@ -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 @@ -20,8 +30,8 @@ export abstract class TestNode { * @param value * @returns this */ - property(name: string, value: string): this { - this._properties.push({ name: name, value: value }); + property(name: string, value: string, isPropertyWithTextContent: boolean = false): this { + this._properties.push({ name: name, value: value, isPropertyWithTextContent: isPropertyWithTextContent }); return this; } @@ -122,11 +132,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; From 9222fc4806bc126b0f44116357d3a633ffe7eb8d Mon Sep 17 00:00:00 2001 From: Sebastian Sauer Date: Tue, 17 Sep 2024 18:57:38 +0200 Subject: [PATCH 2/3] Add new textContentProperty method --- spec/e2e.spec.ts | 2 +- spec/test_case.spec.ts | 2 +- spec/test_suite.spec.ts | 2 +- src/test_node.ts | 14 ++++++++++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/spec/e2e.spec.ts b/spec/e2e.spec.ts index 1a968b1..9a044c5 100644 --- a/spec/e2e.spec.ts +++ b/spec/e2e.spec.ts @@ -29,7 +29,7 @@ describe('JUnit Report builder', () => { .name('Third test') .file('./path-to/the-test-file.coffee') .property('property name', 'property value') - .property('property name 2', 'property value 2', true); + .textContentProperty('property name 2', 'property value 2'); const suite2 = builder.testSuite().name('second.Suite'); suite2.testCase().failure('Failure message'); diff --git a/spec/test_case.spec.ts b/spec/test_case.spec.ts index e8273c6..6375858 100644 --- a/spec/test_case.spec.ts +++ b/spec/test_case.spec.ts @@ -128,7 +128,7 @@ describe('Test Case builder', () => { }); it('should add the provided property with textContent as elements with textContent', () => { - testCase.property('property name', 'property value', true); + testCase.textContentProperty('property name', 'property value'); testCase.build(parentElement); diff --git a/spec/test_suite.spec.ts b/spec/test_suite.spec.ts index 71f5cda..f644391 100644 --- a/spec/test_suite.spec.ts +++ b/spec/test_suite.spec.ts @@ -133,7 +133,7 @@ describe('Test Suite builder', () => { }); it('should add the provided property with textContent as element with textContent', () => { - testSuite.property('property name', 'property value', true); + testSuite.textContentProperty('property name', 'property value'); testSuite.build(parentElement); diff --git a/src/test_node.ts b/src/test_node.ts index d3b3f4e..9e268c4 100644 --- a/src/test_node.ts +++ b/src/test_node.ts @@ -30,8 +30,18 @@ export abstract class TestNode { * @param value * @returns this */ - property(name: string, value: string, isPropertyWithTextContent: boolean = false): this { - this._properties.push({ name: name, value: value, isPropertyWithTextContent: isPropertyWithTextContent }); + property(name: string, value: string): this { + this._properties.push({ name: name, value: value, isPropertyWithTextContent: false }); + return this; + } + + /** + * @param name + * @param value + * @returns this + */ + textContentProperty(name: string, value: string): this { + this._properties.push({ name: name, value: value, isPropertyWithTextContent: true }); return this; } From 7e40b4c7809f9e35a97ced6045757738e0514052 Mon Sep 17 00:00:00 2001 From: Sebastian Sauer Date: Wed, 18 Sep 2024 17:20:48 +0200 Subject: [PATCH 3/3] Rename method to multilineProperty --- spec/e2e.spec.ts | 2 +- spec/test_case.spec.ts | 2 +- spec/test_suite.spec.ts | 2 +- src/test_node.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/e2e.spec.ts b/spec/e2e.spec.ts index 9a044c5..ea89567 100644 --- a/spec/e2e.spec.ts +++ b/spec/e2e.spec.ts @@ -29,7 +29,7 @@ describe('JUnit Report builder', () => { .name('Third test') .file('./path-to/the-test-file.coffee') .property('property name', 'property value') - .textContentProperty('property name 2', 'property value 2'); + .multilineProperty('property name 2', 'property value 2'); const suite2 = builder.testSuite().name('second.Suite'); suite2.testCase().failure('Failure message'); diff --git a/spec/test_case.spec.ts b/spec/test_case.spec.ts index 6375858..797801e 100644 --- a/spec/test_case.spec.ts +++ b/spec/test_case.spec.ts @@ -128,7 +128,7 @@ describe('Test Case builder', () => { }); it('should add the provided property with textContent as elements with textContent', () => { - testCase.textContentProperty('property name', 'property value'); + testCase.multilineProperty('property name', 'property value'); testCase.build(parentElement); diff --git a/spec/test_suite.spec.ts b/spec/test_suite.spec.ts index f644391..0299315 100644 --- a/spec/test_suite.spec.ts +++ b/spec/test_suite.spec.ts @@ -133,7 +133,7 @@ describe('Test Suite builder', () => { }); it('should add the provided property with textContent as element with textContent', () => { - testSuite.textContentProperty('property name', 'property value'); + testSuite.multilineProperty('property name', 'property value'); testSuite.build(parentElement); diff --git a/src/test_node.ts b/src/test_node.ts index 9e268c4..33b4cf5 100644 --- a/src/test_node.ts +++ b/src/test_node.ts @@ -40,7 +40,7 @@ export abstract class TestNode { * @param value * @returns this */ - textContentProperty(name: string, value: string): this { + multilineProperty(name: string, value: string): this { this._properties.push({ name: name, value: value, isPropertyWithTextContent: true }); return this; }