Skip to content

Commit e4ec488

Browse files
Merge pull request #11 from TotalTechGeek/tests/add-tests-for-#10
Contribute tests for PR #10
2 parents 5755464 + bfaf066 commit e4ec488

File tree

3 files changed

+106
-8
lines changed

3 files changed

+106
-8
lines changed

async.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,78 @@ modes.forEach((logic) => {
657657
expect(answer2).toStrictEqual([])
658658
})
659659

660+
test('length string', async () => {
661+
const answer = await logic.run({
662+
length: 'hello'
663+
})
664+
665+
expect(answer).toStrictEqual(5)
666+
})
667+
668+
test('length array', async () => {
669+
const answer = await logic.run({
670+
length: ['hello']
671+
})
672+
673+
expect(answer).toStrictEqual(1)
674+
})
675+
676+
test('length object (2 keys)', async () => {
677+
const answer = await logic.run({
678+
length: { preserve: { a: 1, b: 2 } }
679+
})
680+
681+
expect(answer).toStrictEqual(2)
682+
})
683+
684+
test('length object (1 keys)', async () => {
685+
const answer = await logic.run({
686+
length: { preserve: { a: 1 } }
687+
})
688+
689+
expect(answer).toStrictEqual(1)
690+
})
691+
692+
test('get from array', async () => {
693+
const answer = await logic.run({
694+
get: [['hi'], 'length']
695+
})
696+
697+
expect(answer).toStrictEqual(1)
698+
})
699+
700+
test('get from object', async () => {
701+
const answer = await logic.run({
702+
get: [{ preserve: { a: 1 } }, 'a']
703+
})
704+
705+
expect(answer).toStrictEqual(1)
706+
})
707+
708+
test('get from object default', async () => {
709+
const answer = await logic.run({
710+
get: [{ preserve: {} }, 'a', 5]
711+
})
712+
713+
expect(answer).toStrictEqual(5)
714+
})
715+
716+
test('length object (0 keys)', async () => {
717+
const answer = await logic.run({
718+
length: { preserve: {} }
719+
})
720+
721+
expect(answer).toStrictEqual(0)
722+
})
723+
724+
test('length object (null)', async () => {
725+
const answer = await logic.run({
726+
length: { preserve: null }
727+
})
728+
729+
expect(answer).toStrictEqual(0)
730+
})
731+
660732
test('merge', async () => {
661733
const answer = await logic.run({
662734
merge: [{ preserve: ['b'] }, { preserve: ['c'] }]

defaultMethods.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,8 @@ const defaultMethods = {
154154
return string.substr(from, end)
155155
},
156156
length: (i) => {
157-
if ((typeof i === 'string' || Array.isArray(i))) {
158-
return i.length
159-
}
160-
161-
if (i && typeof i === 'object') {
162-
return Object.keys(i).length
163-
}
164-
157+
if (typeof i === 'string' || Array.isArray(i)) return i.length
158+
if (i && typeof i === 'object') return Object.keys(i).length
165159
return 0
166160
},
167161
get: {

test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,38 @@ modes.forEach((logic) => {
708708
expect(answer).toStrictEqual(1)
709709
})
710710

711+
test('length object (2 keys)', () => {
712+
const answer = logic.run({
713+
length: { preserve: { a: 1, b: 2 } }
714+
})
715+
716+
expect(answer).toStrictEqual(2)
717+
})
718+
719+
test('length object (1 keys)', () => {
720+
const answer = logic.run({
721+
length: { preserve: { a: 1 } }
722+
})
723+
724+
expect(answer).toStrictEqual(1)
725+
})
726+
727+
test('length object (0 keys)', () => {
728+
const answer = logic.run({
729+
length: { preserve: {} }
730+
})
731+
732+
expect(answer).toStrictEqual(0)
733+
})
734+
735+
test('length object (null)', () => {
736+
const answer = logic.run({
737+
length: { preserve: null }
738+
})
739+
740+
expect(answer).toStrictEqual(0)
741+
})
742+
711743
test('get from array', () => {
712744
const answer = logic.run({
713745
get: [['hi'], 'length']

0 commit comments

Comments
 (0)