Skip to content

Commit

Permalink
Generating and testing for warnings when parsing Twine 2 HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Cox authored and Dan Cox committed Jan 15, 2024
1 parent fd984e3 commit 93ed8f1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/Twine2HTML/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import { decode } from 'html-entities';
*
* See: Twine 2 HTML Output Specification
* (https://github.com/iftechfoundation/twine-specs/blob/master/twine-2-htmloutput-spec.md)
*
* Produces warnings for:
* - Missing name attribute on `<tw-storydata>` element.
* - Missing IFID attribute on `<tw-storydata>` element.
* - Malformed IFID attribute on `<tw-storydata>` element.
* @param {string} content - Twine 2 HTML content to parse.
* @returns {Story} Story
* @throws {TypeError} If content is not a string.
* @throws {Error} If content is not Twine 2 HTML.
* @throws {Error} If passage is missing name.
* @throws {Error} If passage is missing PID.
* @returns {Story} Story object based on Twine 2 HTML content.
* @throws {TypeError} Content is not a string.
* @throws {Error} Not Twine 2 HTML content!
* @throws {Error} Cannot parse passage data without name!
* @throws {Error} Passages are required to have PID!
*/
function parse (content) {
// Create new story.
Expand Down Expand Up @@ -60,7 +65,7 @@ function parse (content) {
story.name = storyData.attributes.name;
} else {
// Name is a required field. Warn user.
console.warn('Warning: The name attribute is required!');
console.warn('Warning: The name attribute is missing from tw-storydata!');
}

/**
Expand All @@ -74,7 +79,13 @@ function parse (content) {
story.IFID = storyData.attributes.ifid;
} else {
// Name is a required filed. Warn user.
console.warn('Warning: The IFID attribute is required!');
console.warn('Warning: The ifid attribute is missing from tw-storydata!');
}

// Check if the IFID has valid formatting.
if (story.IFID.match(/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/) === null) {
// IFID is not valid.
console.warn('Warning: The IFID is not in valid UUIDv4 formatting on tw-storydata!');
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/Twine2HTML/Twine2HTML.Parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,34 @@ describe('Twine2HTMLParser', () => {
expect(story.getPassageByName('"Test"').text).toBe('Success');
});
});

describe('Warnings', () => {
beforeEach(() => {
// Mock console.warn.
jest.spyOn(console, 'warn').mockImplementation();
});

afterEach(() => {
// Restore all mocks.
jest.restoreAllMocks();
});

it('Should generate a warning if name attribute is missing from tw-storydata', () => {
const s = '<tw-storydata ifid=\'E70FC479-01D9-4E44-AC6A-AFF9F5E1C475\'></tw-storydata>';
parseTwine2HTML(s);
expect(console.warn).toHaveBeenCalledWith('Warning: The name attribute is missing from tw-storydata!');
});

it('Should generate a warning if ifid attribute is missing from tw-storydata', () => {
const s = '<tw-storydata name=\'Test\'></tw-storydata>';
parseTwine2HTML(s);
expect(console.warn).toHaveBeenCalledWith('Warning: The ifid attribute is missing from tw-storydata!');
});

it('Should generate a warning if ifid on tw-storydata is malformed', () => {
const s = '<tw-storydata ifid=\'1234\'></tw-storydata>';
parseTwine2HTML(s);
expect(console.warn).toHaveBeenCalledWith('Warning: The IFID is not in valid UUIDv4 formatting on tw-storydata!');
});
});
});

0 comments on commit 93ed8f1

Please sign in to comment.