Skip to content

Commit 3d48cbf

Browse files
committed
Migrate doctest for ReverseString.js
(also remove inline test driver code)
1 parent 942f9fb commit 3d48cbf

File tree

2 files changed

+29
-65
lines changed

2 files changed

+29
-65
lines changed

String/ReverseString.js

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
/**
2-
* A short example showing how to reverse a string
3-
* @flow
4-
*/
5-
6-
/**
7-
* Create a new string and append
8-
* @complexity O(n)
9-
*
10-
* Doctests
11-
*
12-
* > ReverseStringIterative('some')
13-
* 'emos'
14-
* > ReverseStringIterative('string')
15-
* 'gnirts'
16-
* > ReverseStringIterative('The Algorithms Javascript')
17-
* 'tpircsavaJ smhtiroglA ehT'
18-
* > ReverseStringIterative([])
19-
* ! TypeError
20-
* > ReverseStringIterative({})
21-
* ! TypeError
22-
* > ReverseStringIterative(null)
23-
* ! TypeError
2+
* A short example showing how to reverse a string.
243
*/
254
function ReverseStringIterative (string) {
265
if (typeof string !== 'string') {
@@ -40,34 +19,17 @@ function ReverseStringIterative (string) {
4019
* JS disallows string mutation so we're actually a bit slower.
4120
*
4221
* @complexity O(n)
43-
*
44-
* 'some' -> 'eoms' -> 'emos'
45-
*
46-
* Doctests
47-
*
48-
* > ReverseStringIterativeInplace('some')
49-
* 'emos'
50-
* > ReverseStringIterativeInplace('string')
51-
* 'gnirts'
52-
* > ReverseStringIterativeInplace('The Algorithms Javascript')
53-
* 'tpircsavaJ smhtiroglA ehT'
54-
* > ReverseStringIterativeInplace([])
55-
* ! TypeError
56-
* > ReverseStringIterativeInplace({})
57-
* ! TypeError
58-
* > ReverseStringIterativeInplace(null)
59-
* ! TypeError
6022
*/
6123
function ReverseStringIterativeInplace (string) {
6224
if (typeof string !== 'string') {
6325
throw new TypeError('The given value is not a string')
6426
}
27+
6528
const _string = string.split('')
6629

6730
for (let i = 0; i < Math.floor(_string.length / 2); i++) {
6831
const first = _string[i]
69-
const second = _string[_string.length - 1 - i]
70-
_string[i] = second
32+
_string[i] = _string[_string.length - 1 - i]
7133
_string[_string.length - 1 - i] = first
7234
}
7335

String/test/ReverseString.test.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,65 @@
1-
import {
2-
ReverseStringIterative,
3-
ReverseStringIterativeInplace
4-
} from '../ReverseString'
1+
import { ReverseStringIterative, ReverseStringIterativeInplace } from '../ReverseString'
52

63
describe('ReverseStringIterative', () => {
74
it('expects to reverse a simple string', () => {
8-
const SUT = ReverseStringIterative('reverse')
9-
expect(SUT).toEqual('esrever')
5+
expect(ReverseStringIterative('reverse')).toEqual('esrever')
6+
expect(ReverseStringIterative('some')).toEqual('emos')
7+
expect(ReverseStringIterative('string')).toEqual('gnirts')
8+
expect(ReverseStringIterative('The Algorithms Javascript')).toEqual('tpircsavaJ smhtiroglA ehT')
109
})
10+
1111
it('expects to reverse a string with spaces in between', () => {
12-
const SUT = ReverseStringIterative('reverse me')
13-
expect(SUT).toEqual('em esrever')
12+
expect(ReverseStringIterative('reverse me')).toEqual('em esrever')
1413
})
14+
1515
it('expects to reverse a simple string without capitalizing the first letter', () => {
16-
const SUT = ReverseStringIterative('Javascript')
17-
expect(SUT).toEqual('tpircsavaJ')
16+
expect(ReverseStringIterative('Javascript')).toEqual('tpircsavaJ')
1817
})
18+
1919
it.each`
2020
input
2121
${123456}
2222
${[1, 2, 3, 4, 5, 6]}
2323
${{ test: 'test' }}
24+
${null}
2425
`(
2526
'expects to throw a type error given a value that is $input',
2627
({ input }) => {
27-
expect(() => {
28-
ReverseStringIterative(input)
29-
}).toThrow('The given value is not a string')
28+
expect(() => ReverseStringIterative(input)).toThrow('The given value is not a string')
3029
}
3130
)
31+
3232
it('expects to return a empty string with an empty string is given', () => {
33-
const SUT = ReverseStringIterative('')
34-
expect(SUT).toEqual('')
33+
expect(ReverseStringIterative('')).toEqual('')
3534
})
3635
})
36+
3737
describe('ReverseStringIterativeInplace', () => {
3838
it('expects to reverse a simple string', () => {
39-
const SUT = ReverseStringIterativeInplace('reverse')
40-
expect(SUT).toEqual('esrever')
39+
expect(ReverseStringIterativeInplace('reverse')).toEqual('esrever')
40+
expect(ReverseStringIterativeInplace('some')).toEqual('emos')
41+
expect(ReverseStringIterativeInplace('string')).toEqual('gnirts')
42+
expect(ReverseStringIterativeInplace('The Algorithms Javascript')).toEqual('tpircsavaJ smhtiroglA ehT')
4143
})
44+
4245
it('expects to reverse a simple string without capitalizing the first letter', () => {
43-
const SUT = ReverseStringIterativeInplace('Javascript')
44-
expect(SUT).toEqual('tpircsavaJ')
46+
expect(ReverseStringIterativeInplace('Javascript')).toEqual('tpircsavaJ')
4547
})
48+
4649
it('expects to return an empty string given an empty string', () => {
47-
const SUT = ReverseStringIterativeInplace('Javascript')
48-
expect(SUT).toEqual('tpircsavaJ')
50+
expect(ReverseStringIterativeInplace('Javascript')).toEqual('tpircsavaJ')
4951
})
52+
5053
it.each`
5154
input
5255
${123456}
5356
${[1, 2, 3, 4, 5, 6]}
5457
${{ test: 'test' }}
58+
${null}
5559
`(
5660
'expects to throw a type error given a value that is $input',
5761
({ input }) => {
58-
expect(() => {
59-
ReverseStringIterativeInplace(input)
60-
}).toThrow('The given value is not a string')
62+
expect(() => ReverseStringIterativeInplace(input)).toThrow('The given value is not a string')
6163
}
6264
)
6365
})

0 commit comments

Comments
 (0)