diff --git a/cypress/integration/persons.js b/cypress/integration/persons.js index b3b68ce..6b4d764 100644 --- a/cypress/integration/persons.js +++ b/cypress/integration/persons.js @@ -848,3 +848,65 @@ describe('Multiple authors', function () { cy.get('#author_2_givenName').should('have.value', 'Joe'); }); }); + +describe('One contributor with a role', function () { + it('can be exported in both codemeta v2.0 and v3.0 versions', function () { + cy.get('#name').type('My Test Software'); + + cy.get('#contributor_add').click(); + cy.get('#contributor_1_givenName').type('Jane'); + + cy.get('#contributor_1_role_add').click(); + cy.get('#contributor_1_roleName_0').type('Developer'); + cy.get('#contributor_1_startDate_0').type('2024-03-04'); + cy.get('#contributor_1_endDate_0').type('2024-04-03'); + + cy.get('#generateCodemetaV2').click(); + cy.get('#codemetaText').then((elem) => JSON.parse(elem.text())) + .should('deep.equal', { + "@context": "https://doi.org/10.5063/schema/codemeta-2.0", + "type": "SoftwareSourceCode", + "name": "My Test Software", + "contributor": [ + { + "type": "Person", + "givenName": "Jane" + }, + { + "type": "schema:Role", + "schema:contributor": { + "type": "Person", + "givenName": "Jane" + }, + "schema:roleName": "Developer", + "schema:startDate": "2024-03-04", + "schema:endDate": "2024-04-03" + } + ] + }); + + cy.get('#generateCodemetaV3').click(); + cy.get('#codemetaText').then((elem) => JSON.parse(elem.text())) + .should('deep.equal', { + "@context": "https://w3id.org/codemeta/3.0", + "type": "SoftwareSourceCode", + "name": "My Test Software", + "contributor": [ + { + "type": "Person", + "givenName": "Jane" + }, + { + "type": "Role", + "schema:contributor": { + "type": "Person", + "givenName": "Jane" + }, + "roleName": "Developer", + "startDate": "2024-03-04", + "endDate": "2024-04-03" + } + ] + }); + }); +}); diff --git a/js/codemeta_generation.js b/js/codemeta_generation.js index 6366558..c270c2b 100644 --- a/js/codemeta_generation.js +++ b/js/codemeta_generation.js @@ -169,7 +169,7 @@ function generatePerson(idPrefix) { return doc; } -function generateRole(id) { +function generateRole(id, property) { const doc = { "@type": "Role" }; @@ -179,26 +179,26 @@ function generateRole(id) { return doc; } -function generateRoles(idPrefix, person) { +function generateRoles(idPrefix, property, person) { const roles = []; const roleNodes = document.querySelectorAll(`ul[id^=${idPrefix}_role_`); roleNodes.forEach(roleNode => { - const role = generateRole(roleNode.id); - role["schema:author"] = person; // Prefix with "schema:" to prevent it from expanding into a list + const role = generateRole(roleNode.id, property); + role[`schema:${property}`] = person; // Prefix with "schema:" to prevent it from expanding into a list roles.push(role); }); return roles; } -function generatePersons(prefix) { +function generatePersons(property) { var persons = []; - var nbPersons = getNbPersons(prefix); + var nbPersons = getNbPersons(property); for (let personId = 1; personId <= nbPersons; personId++) { - const idPrefix = `${prefix}_${personId}`; + const idPrefix = `${property}_${personId}`; const person = generatePerson(idPrefix); persons.push(person); - const roles = generateRoles(idPrefix, person); + const roles = generateRoles(idPrefix, property, person); if (roles.length > 0) { persons = persons.concat(roles); }