Skip to content

Commit

Permalink
better Array tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 27, 2020
1 parent 9519d2c commit c18ff3f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
2 changes: 2 additions & 0 deletions dtslint/ts3.5/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ interface X1 {
declare const x1s: Array<X1>

_.sort(ord1)(x1s) // $ExpectType X1[]
pipe(x1s, _.sort(ord1)) // $ExpectType X1[]

//
// sortBy
Expand All @@ -162,3 +163,4 @@ interface X2 {
declare const x2s: Array<X2>

_.sortBy([ord2, ord3])(x2s) // $ExpectType X2[]
pipe(x2s, _.sortBy([ord2, ord3])) // $ExpectType X2[]
2 changes: 2 additions & 0 deletions dtslint/ts3.5/ReadonlyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ interface X1 {
declare const x1s: ReadonlyArray<X1>

_.sort(ord1)(x1s) // $ExpectType ReadonlyArray<X1>
pipe(x1s, _.sort(ord1)) // $ExpectType ReadonlyArray<X1>

//
// sortBy
Expand All @@ -153,3 +154,4 @@ interface X2 {
declare const x2s: ReadonlyArray<X2>

_.sortBy([ord2, ord3])(x2s) // $ExpectType ReadonlyArray<X2>
pipe(x2s, _.sortBy([ord2, ord3])) // $ExpectType ReadonlyArray<X2>
73 changes: 38 additions & 35 deletions test/ReadonlyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,24 +594,26 @@ describe('ReadonlyArray', () => {
})

it('sort', () => {
assert.deepStrictEqual(_.sort(Ord.ordNumber)([3, 2, 1]), [1, 2, 3])
assert.strictEqual(_.sort(Ord.ordNumber)(_.empty), _.empty)
const byName = pipe(
Ord.ordString,
Ord.contramap((x: { readonly name: string }) => x.name)
const O = pipe(
Ord.ordNumber,
Ord.contramap((x: { readonly a: number }) => x.a)
)
assert.deepStrictEqual(
_.sort(byName)([
{ name: 'b', age: 0 },
{ name: 'a', age: 1 },
{ name: 'c', age: 2 }
]),
pipe(
[
{ a: 3, b: 'b1' },
{ a: 2, b: 'b2' },
{ a: 1, b: 'b3' }
],
_.sort(O)
),
[
{ name: 'a', age: 1 },
{ name: 'b', age: 0 },
{ name: 'c', age: 2 }
{ a: 1, b: 'b3' },
{ a: 2, b: 'b2' },
{ a: 3, b: 'b1' }
]
)
assert.strictEqual(_.sort(Ord.ordNumber)(_.empty), _.empty)
})

it('zipWith', () => {
Expand Down Expand Up @@ -746,40 +748,41 @@ describe('ReadonlyArray', () => {
})

it('sortBy', () => {
interface Person {
readonly name: string
readonly age: number
interface X {
readonly a: string
readonly b: number
readonly c: boolean
}
const byName = pipe(
Ord.ordString,
Ord.contramap((p: Person) => p.name)
Ord.contramap((p: { readonly a: string; readonly b: number }) => p.a)
)
const byAge = pipe(
Ord.ordNumber,
Ord.contramap((p: Person) => p.age)
Ord.contramap((p: { readonly a: string; readonly b: number }) => p.b)
)
const sortByNameByAge = _.sortBy([byName, byAge])
const persons: ReadonlyArray<Person> = [
{ name: 'a', age: 1 },
{ name: 'b', age: 3 },
{ name: 'c', age: 2 },
{ name: 'b', age: 2 }
const f = _.sortBy([byName, byAge])
const xs: ReadonlyArray<X> = [
{ a: 'a', b: 1, c: true },
{ a: 'b', b: 3, c: true },
{ a: 'c', b: 2, c: true },
{ a: 'b', b: 2, c: true }
]
assert.deepStrictEqual(sortByNameByAge(persons), [
{ name: 'a', age: 1 },
{ name: 'b', age: 2 },
{ name: 'b', age: 3 },
{ name: 'c', age: 2 }
assert.deepStrictEqual(f(xs), [
{ a: 'a', b: 1, c: true },
{ a: 'b', b: 2, c: true },
{ a: 'b', b: 3, c: true },
{ a: 'c', b: 2, c: true }
])
const sortByAgeByName = _.sortBy([byAge, byName])
assert.deepStrictEqual(sortByAgeByName(persons), [
{ name: 'a', age: 1 },
{ name: 'b', age: 2 },
{ name: 'c', age: 2 },
{ name: 'b', age: 3 }
assert.deepStrictEqual(sortByAgeByName(xs), [
{ a: 'a', b: 1, c: true },
{ a: 'b', b: 2, c: true },
{ a: 'c', b: 2, c: true },
{ a: 'b', b: 3, c: true }
])

assert.deepStrictEqual(_.sortBy([])(persons), persons)
assert.deepStrictEqual(_.sortBy([])(xs), xs)
})

it('chop', () => {
Expand Down

0 comments on commit c18ff3f

Please sign in to comment.