|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var throws = require('assert').throws; |
| 4 | + |
| 5 | +var eq = require('./utils').eq; |
| 6 | +var errorEq = require('./utils').errorEq; |
| 7 | +var S = require('..'); |
| 8 | + |
| 9 | + |
| 10 | +describe('toPairs', function() { |
| 11 | + |
| 12 | + var comparePairsAsc = function(a, b) { |
| 13 | + return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; |
| 14 | + }; |
| 15 | + |
| 16 | + it('is a unary function', function() { |
| 17 | + eq(typeof S.pairs, 'function'); |
| 18 | + eq(S.pairs.length, 1); |
| 19 | + }); |
| 20 | + |
| 21 | + it('type checks its arguments', function() { |
| 22 | + throws(function() { S.pairs('xxx'); }, |
| 23 | + errorEq(TypeError, |
| 24 | + 'Invalid value\n' + |
| 25 | + '\n' + |
| 26 | + 'pairs :: StrMap a -> Array (Pair String a)\n' + |
| 27 | + ' ^^^^^^^^\n' + |
| 28 | + ' 1\n' + |
| 29 | + '\n' + |
| 30 | + '1) "xxx" :: String\n' + |
| 31 | + '\n' + |
| 32 | + 'The value at position 1 is not a member of ‘StrMap a’.\n')); |
| 33 | + |
| 34 | + throws(function() { S.pairs({a: '1', b: 2, c: '3'}); }, |
| 35 | + errorEq(TypeError, |
| 36 | + 'Type-variable constraint violation\n' + |
| 37 | + '\n' + |
| 38 | + 'pairs :: StrMap a -> Array (Pair String a)\n' + |
| 39 | + ' ^\n' + |
| 40 | + ' 1\n' + |
| 41 | + '\n' + |
| 42 | + '1) "1" :: String\n' + |
| 43 | + ' 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' + |
| 44 | + ' "3" :: String\n' + |
| 45 | + '\n' + |
| 46 | + 'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n')); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns an array with the key value pairs of each property of the object', function() { |
| 50 | + eq(S.pairs({}), []); |
| 51 | + eq(S.pairs({a: 1, b: 2, c: 3}).sort(comparePairsAsc), [['a', 1], ['b', 2], ['c', 3]]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not include prototype properties', function() { |
| 55 | + var proto = {a: 1, b: 2}; |
| 56 | + var obj = Object.create(proto); |
| 57 | + obj.c = 3; |
| 58 | + obj.d = 4; |
| 59 | + |
| 60 | + eq(S.pairs(obj).sort(comparePairsAsc), [['c', 3], ['d', 4]]); |
| 61 | + }); |
| 62 | + |
| 63 | +}); |
0 commit comments