Skip to content

Commit c5fc353

Browse files
authored
Added tests for Strings algorithms (#390)
* test: added tests for check anagram function
1 parent e156fe3 commit c5fc353

13 files changed

+8250
-22
lines changed

String/CheckAnagram.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,4 @@ const checkAnagram = (str1, str2) => {
4444
return 'Anagrams'
4545
}
4646

47-
console.log(checkAnagram('abcd', 'bcad')) // should print anagram
48-
console.log(checkAnagram('abcd', 'abef')) // should print not anagram
49-
console.log(checkAnagram(10, 'abcd'))// should print Not String(s).
50-
console.log(checkAnagram('abs', 'abds'))// should print not anagram
47+
export { checkAnagram }

String/CheckAnagram.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { checkAnagram } from './CheckAnagram'
2+
3+
describe('checkAnagram', () => {
4+
it.each`
5+
inputOne | inputTwo
6+
${123456} | ${'abcd'}
7+
${[1, 2, 3, 4, 5, 6]} | ${'abcd'}
8+
${{ test: 'test' }} | ${'abcd'}
9+
${'abcd'} | ${123456}
10+
${'abcd'} | ${[1, 2, 3, 4, 5, 6]}
11+
${'abcd'} | ${{ test: 'test' }}
12+
`(
13+
'expects to return "Not string(s)" given values $inputOne and $inputTwo',
14+
({ inputOne, inputTwo }) => {
15+
const SUT = checkAnagram(inputOne, inputTwo)
16+
expect(SUT).toBe('Not string(s)')
17+
}
18+
)
19+
it('expects to return "Not anagram" if the arguments have different lengths', () => {
20+
const SUT = checkAnagram('abs', 'abds')
21+
expect(SUT).toBe('Not Anagram')
22+
})
23+
it('expects to return "Not anagram" if the arguments are not anagrams', () => {
24+
const SUT = checkAnagram('abcs', 'abds')
25+
expect(SUT).toBe('Not anagrams')
26+
})
27+
it('expects to return "Anagram" if the arguments are anagram', () => {
28+
const SUT = checkAnagram('abcd', 'bcad')
29+
expect(SUT).toBe('Anagrams')
30+
})
31+
})

String/CheckPalindrome.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ const checkPalindrome = (str) => {
2121
return 'Palindrome'
2222
}
2323

24-
console.log(checkPalindrome('madam'))
25-
console.log(checkPalindrome('abcd'))
24+
export { checkPalindrome }

String/CheckPalindrome.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { checkPalindrome } from './CheckPalindrome'
2+
3+
describe('checkPalindrome', () => {
4+
it('expects to return "Palindrome" if the given string is a palindrome', () => {
5+
const SUT = checkPalindrome('madam')
6+
expect(SUT).toBe('Palindrome')
7+
})
8+
it('expects to return "Empty string" if the given string is empty', () => {
9+
const SUT = checkPalindrome('')
10+
expect(SUT).toBe('Empty string')
11+
})
12+
it('expects to return "Not a string" if the given string is not a string', () => {
13+
const SUT = checkPalindrome(123)
14+
expect(SUT).toBe('Not a string')
15+
})
16+
})

String/PatternMatching.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ return the starting index if the given pattern is
88
available in the text
99
*/
1010
const checkIfPatternExists = (text, pattern) => {
11+
if (typeof text !== 'string' || typeof pattern !== 'string') {
12+
throw new TypeError('Given input is not a string')
13+
}
1114
const textLength = text.length // Store the length of the text in a variable
1215
const patternLength = pattern.length // Store the length of the pattern in a variable
1316

@@ -22,15 +25,10 @@ const checkIfPatternExists = (text, pattern) => {
2225
// j + 1 is equal to the length of the pattern
2326
if (j + 1 === patternLength) {
2427
console.log(`Given pattern is found at index ${i}`)
28+
return `Given pattern is found at index ${i}`
2529
}
2630
}
2731
}
2832
}
2933

30-
const main = () => {
31-
const text = 'AABAACAADAABAAAABAA'
32-
const pattern = 'AABA'
33-
checkIfPatternExists(text.toLowerCase(), pattern.toLowerCase())
34-
}
35-
36-
main()
34+
export { checkIfPatternExists }

String/PatternMatching.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { checkIfPatternExists } from './PatternMatching'
2+
describe('checkIfPatternExists', () => {
3+
it('expects to find a pattern with correct input', () => {
4+
const text = 'AABAACAADAABAAAABAA'
5+
const pattern = 'AABA'
6+
const SUT = checkIfPatternExists(text.toLowerCase(), pattern.toLowerCase())
7+
expect(SUT).toBe('Given pattern is found at index 0')
8+
})
9+
it('expects to return a message when there is no pattern', () => {
10+
const text = 'ABCDEFG'
11+
const pattern = 'AEG'
12+
const SUT = checkIfPatternExists(text.toLowerCase(), pattern.toLowerCase())
13+
expect(SUT).toBe(undefined)
14+
})
15+
it('expects to find a pattern independent of casing', () => {
16+
const text = 'AbCAAAAAAB'
17+
const pattern = 'abc'
18+
const SUT = checkIfPatternExists(text, pattern)
19+
expect(SUT).toBe(undefined)
20+
})
21+
it('expects to throw an error message when given inpuut is not a string', () => {
22+
const text = 123444456
23+
const pattern = 123
24+
expect(() => checkIfPatternExists(text, pattern)).toThrow('Given input is not a string')
25+
})
26+
})

String/ReverseString.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010

1111
function ReverseStringIterative (string) {
12+
if (typeof string !== 'string') {
13+
throw new TypeError('The given value is not a string')
14+
}
1215
let reversedString = ''
1316
let index
1417

@@ -28,6 +31,9 @@ function ReverseStringIterative (string) {
2831
*/
2932

3033
function ReverseStringIterativeInplace (string) {
34+
if (typeof string !== 'string') {
35+
throw new TypeError('The given value is not a string')
36+
}
3137
const _string = string.split('')
3238

3339
for (let i = 0; i < Math.floor(_string.length / 2); i++) {
@@ -40,6 +46,4 @@ function ReverseStringIterativeInplace (string) {
4046
return _string.join('')
4147
}
4248

43-
// testing
44-
console.log(ReverseStringIterative('Javascript'))
45-
console.log(ReverseStringIterativeInplace('Javascript'))
49+
export { ReverseStringIterative, ReverseStringIterativeInplace }

String/ReverseString.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
ReverseStringIterative,
3+
ReverseStringIterativeInplace
4+
} from './ReverseString'
5+
6+
describe('ReverseStringIterative', () => {
7+
it('expects to reverse a simple string', () => {
8+
const SUT = ReverseStringIterative('reverse')
9+
expect(SUT).toEqual('esrever')
10+
})
11+
it('expects to reverse a string with spaces in between', () => {
12+
const SUT = ReverseStringIterative('reverse me')
13+
expect(SUT).toEqual('em esrever')
14+
})
15+
it('expects to reverse a simple string without capitalizing the first letter', () => {
16+
const SUT = ReverseStringIterative('Javascript')
17+
expect(SUT).toEqual('tpircsavaJ')
18+
})
19+
it.each`
20+
input
21+
${123456}
22+
${[1, 2, 3, 4, 5, 6]}
23+
${{ test: 'test' }}
24+
`(
25+
'expects to throw a type error given a value that is $input',
26+
({ input }) => {
27+
expect(() => {
28+
ReverseStringIterative(input)
29+
}).toThrow('The given value is not a string')
30+
}
31+
)
32+
it('expects to return a empty string with an empty string is given', () => {
33+
const SUT = ReverseStringIterative('')
34+
expect(SUT).toEqual('')
35+
})
36+
})
37+
describe('ReverseStringIterativeInplace', () => {
38+
it('expects to reverse a simple string', () => {
39+
const SUT = ReverseStringIterativeInplace('reverse')
40+
expect(SUT).toEqual('esrever')
41+
})
42+
it('expects to reverse a simple string without capitalizing the first letter', () => {
43+
const SUT = ReverseStringIterativeInplace('Javascript')
44+
expect(SUT).toEqual('tpircsavaJ')
45+
})
46+
it('expects to return an empty string given an empty string', () => {
47+
const SUT = ReverseStringIterativeInplace('Javascript')
48+
expect(SUT).toEqual('tpircsavaJ')
49+
})
50+
it.each`
51+
input
52+
${123456}
53+
${[1, 2, 3, 4, 5, 6]}
54+
${{ test: 'test' }}
55+
`(
56+
'expects to throw a type error given a value that is $input',
57+
({ input }) => {
58+
expect(() => {
59+
ReverseStringIterativeInplace(input)
60+
}).toThrow('The given value is not a string')
61+
}
62+
)
63+
})

String/ReverseWords.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const reverseWords = (str) => {
2+
if (typeof str !== 'string') {
3+
throw new TypeError('The given value is not a string')
4+
}
25
// Split string into words
36
// Ex. "I Love JS" => ["I", "Love", "JS"]
47
const words = str.split(' ')
@@ -10,6 +13,4 @@ const reverseWords = (str) => {
1013
return reversedWords.join(' ')
1114
}
1215

13-
// testing
14-
console.log(reverseWords('I Love JS'))
15-
console.log(reverseWords('My Name Is JavaScript'))
16+
export { reverseWords }

String/ReverseWords.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { reverseWords } from './ReverseWords'
2+
3+
describe('reverseWords', () => {
4+
it('expects to reverse words to return a joined word', () => {
5+
const SUT = reverseWords('I Love JS')
6+
expect(SUT).toBe('JS Love I')
7+
})
8+
it.each`
9+
input
10+
${123456}
11+
${[1, 2, 3, 4, 5, 6]}
12+
${{ test: 'test' }}
13+
`(
14+
'expects to throw a type error given a value that is $input',
15+
({ input }) => {
16+
expect(() => {
17+
reverseWords(input)
18+
}).toThrow('The given value is not a string')
19+
}
20+
)
21+
})

babel.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
esmodules: true
8+
}
9+
}
10+
]
11+
]
12+
}

0 commit comments

Comments
 (0)