Skip to content

Instantly share code, notes, and snippets.

@jeffbowman
Last active August 22, 2018 15:57
Show Gist options
  • Save jeffbowman/f098dbdb2810a936b2c557642f4ad15f to your computer and use it in GitHub Desktop.
Save jeffbowman/f098dbdb2810a936b2c557642f4ad15f to your computer and use it in GitHub Desktop.
DW Rudimentary unit testing example
<words>
<word>123</word>
<word>mom</word>
<word>12321</word>
<word>madam im adam</word>
<word>Mom</word>
<word>Madam I'm Adam</word>
<word>FooBar</word>
<word>Eva, Can I Stab Bats In A Cave?</word>
</words>
{
"reverseTest": "PASS",
"isNonCharacterTest": "PASS",
"isCharacterTest": "PASS",
"nonAlphaRemovalTest": "PASS",
"eqIngoreCaseWithEqualStringsTest": "PASS",
"eqIngoreCaseWithInequalStringsTest": "PASS",
"words": [
{
"word": "123",
"isPalindrome": false
},
{
"word": "mom",
"isPalindrome": true
},
{
"word": "12321",
"isPalindrome": true
},
{
"word": "madam im adam",
"isPalindrome": true
},
{
"word": "Mom",
"isPalindrome": true
},
{
"word": "Madam I'm Adam",
"isPalindrome": true
},
{
"word": "FooBar",
"isPalindrome": false
},
{
"word": "Eva, Can I Stab Bats In A Cave?",
"isPalindrome": true
}
]
}
%dw 1.0
%input payload application/xml
%output application/json
%var testing = true
%function reverse(word) ((rmNonAlphaCharacters(word) splitBy '')[-1..0]) joinBy ''
%function isCharacter(c) c matches /[a-zA-Z0-9]/
%function rmNonAlphaCharacters(word) ((word splitBy '') filter isCharacter($)) joinBy ''
%function eqIgnoreCase(w1, w2) (lower w1) == lower (rmNonAlphaCharacters(w2))
%function wtest(name, actual, expectedResult, failMessage)
log(failMessage, name ++ ": got [" ++ actual ++ "] but expected [" ++ expectedResult ++ "]") when actual != expectedResult otherwise log(name ++ " : PASS", "PASS")
---
{
(reverseTest: wtest("reverse test", reverse('foo'), 'oof', 'failed to match on reverse')) when testing,
(isNonCharacterTest: wtest("non alphanumeric character test", isCharacter("?"), false, "failed to identify non-alphanumeric character")) when testing,
(isCharacterTest: wtest("non alphanumeric character test", isCharacter("a"), true, "failed to identify alphanumeric character")) when testing,
(nonAlphaRemovalTest: wtest("remove non alpha characters", rmNonAlphaCharacters("abc123',?"), "abc123", "failed to correctly remove non-alphanumerics")) when testing,
(eqIngoreCaseWithEqualStringsTest: wtest("equals ignore case for equal strings", eqIgnoreCase('aBc', 'AbC'), true, "failed to match strings ignoring case")) when testing,
(eqIngoreCaseWithInequalStringsTest: wtest("equals ignore case for inequal strings", eqIgnoreCase('aBcD', 'AbC'), false, "matched inequal strings ignoring case, but should not")) when testing,
words: payload.words pluck ((word) -> {
word: word,
isPalindrome: eqIgnoreCase(reverse(word), word)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment