-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from nigelng/feat/refactor-process-line
refactor process line to cover with unit tests
- Loading branch information
Showing
5 changed files
with
1,106 additions
and
796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { last, forEach } from 'ramda' | ||
|
||
import { isNilOrEmpty, isOpeningTag, getClosingTag, getTags } from './helpers' | ||
|
||
export default (line) => { | ||
const allTags = getTags(line) | ||
let lineResult = '' | ||
|
||
if (isNilOrEmpty(allTags)) { | ||
return 'No tag found' | ||
} | ||
|
||
const pendingOpeningTags = [] | ||
|
||
forEach((tag) => { | ||
// stop at the first unmatched tag | ||
if (!isNilOrEmpty(lineResult)) { | ||
return | ||
} | ||
|
||
if (isOpeningTag(tag)) { | ||
/* | ||
* This is an opening tag, so let's see what coming up next | ||
*/ | ||
pendingOpeningTags.push(tag) | ||
return | ||
} | ||
|
||
/* | ||
* This tag is a closing tag: | ||
* - if there is no previous opening tag, it's an orphaned closing tag | ||
* - if it does not match last opening tag, it's an unmatched closing tag | ||
* - otherwise happy day | ||
*/ | ||
|
||
const lastOpeningTag = last(pendingOpeningTags) | ||
|
||
if (isNilOrEmpty(lastOpeningTag)) { | ||
lineResult = `Expected # found ${tag}` | ||
return | ||
} | ||
|
||
const matchedClosingTag = getClosingTag(lastOpeningTag) | ||
|
||
if (matchedClosingTag !== tag) { | ||
lineResult = `Expected ${matchedClosingTag} found ${tag}` | ||
return | ||
} | ||
|
||
pendingOpeningTags.pop() | ||
}, allTags) | ||
|
||
if (!isNilOrEmpty(lineResult)) { | ||
return lineResult | ||
} | ||
|
||
return isNilOrEmpty(pendingOpeningTags) | ||
? 'Correctly tagged paragraph' | ||
: `Expect ${getClosingTag(pendingOpeningTags[0])} found #` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import processLine from '../src/processLine' | ||
|
||
describe('processLine', () => { | ||
it('should return "No tag found" if the input line is empty', () => { | ||
expect(processLine('')).toBe('No tag found') | ||
expect(processLine()).toBe('No tag found') | ||
}) | ||
|
||
it('should return "No tag found" if the input line contains no tag', () => { | ||
expect(processLine('abc def xyz')).toBe('No tag found') | ||
}) | ||
|
||
it('should return "Correctly tagged paragraph" if the input line contains valid matched tag', () => { | ||
expect(processLine('<A> abc def xyz </A>')).toBe('Correctly tagged paragraph') | ||
expect(processLine('<A><B><C> abc def xyz </C></B></A>')).toBe( | ||
'Correctly tagged paragraph' | ||
) | ||
}) | ||
|
||
it('should return "Expected <tag> found #" if the input line contains orphaned opening tag', () => { | ||
expect(processLine('<A> abc def xyz')).toBe('Expect </A> found #') | ||
}) | ||
|
||
it('should return "Expected <tag> found <other-tag>" if the input line contains mismatched opening tag', () => { | ||
expect(processLine('<A><B><C> abc def xyz </B></A>')).toBe( | ||
'Expected </C> found </B>' | ||
) | ||
}) | ||
|
||
it('should return "Expected # found <tag>" if the input line contains orphaned closing tag', () => { | ||
expect(processLine('abc def xyz</A>')).toBe('Expected # found </A>') | ||
}) | ||
}) |
Oops, something went wrong.