lang_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var _ = require('../../../../src/util')
  2. describe('Util - Language Enhancement', function () {
  3. it('toString', function () {
  4. expect(_.toString('hi')).toBe('hi')
  5. expect(_.toString(1.234)).toBe('1.234')
  6. expect(_.toString(null)).toBe('')
  7. expect(_.toString(undefined)).toBe('')
  8. })
  9. it('toNumber', function () {
  10. expect(_.toNumber('12')).toBe(12)
  11. expect(_.toNumber('1e5')).toBe(1e5)
  12. expect(_.toNumber('0x2F')).toBe(0x2F)
  13. expect(_.toNumber(null)).toBe(null)
  14. expect(_.toNumber(true)).toBe(true)
  15. expect(_.toNumber('hello')).toBe('hello')
  16. })
  17. it('strip quotes', function () {
  18. expect(_.stripQuotes('"123"')).toBe('123')
  19. expect(_.stripQuotes("'fff'")).toBe('fff')
  20. expect(_.stripQuotes("'fff")).toBe(false)
  21. })
  22. it('bind', function () {
  23. var original = function (a) {
  24. return this.a + a
  25. }
  26. var ctx = { a: 'ctx a ' }
  27. var bound = _.bind(original, ctx)
  28. var res = bound('arg a')
  29. expect(res).toBe('ctx a arg a')
  30. })
  31. it('toArray', function () {
  32. // should make a copy of original array
  33. var arr = [1,2,3]
  34. var res = _.toArray(arr)
  35. expect(Array.isArray(res)).toBe(true)
  36. expect(res.toString()).toEqual('1,2,3')
  37. expect(res).not.toBe(arr)
  38. // should work on arguments
  39. ;(function () {
  40. var res = _.toArray(arguments)
  41. expect(Array.isArray(res)).toBe(true)
  42. expect(res.toString()).toEqual('1,2,3')
  43. })(1,2,3)
  44. })
  45. it('extend', function () {
  46. var from = {a:1,b:2}
  47. var to = {}
  48. _.extend(to, from)
  49. expect(to.a).toBe(from.a)
  50. expect(to.b).toBe(from.b)
  51. })
  52. it('isObject', function () {
  53. expect(_.isObject({})).toBe(true)
  54. expect(_.isObject([])).toBe(true)
  55. expect(_.isObject(null)).toBeFalsy()
  56. expect(_.isObject(123)).toBeFalsy()
  57. expect(_.isObject(true)).toBeFalsy()
  58. expect(_.isObject('hi')).toBeFalsy()
  59. expect(_.isObject(undefined)).toBeFalsy()
  60. expect(_.isObject(function(){})).toBeFalsy()
  61. })
  62. it('isPlainObject', function () {
  63. expect(_.isPlainObject({})).toBe(true)
  64. expect(_.isPlainObject([])).toBe(false)
  65. expect(_.isPlainObject(null)).toBe(false)
  66. expect(_.isPlainObject(null)).toBeFalsy()
  67. expect(_.isPlainObject(123)).toBeFalsy()
  68. expect(_.isPlainObject(true)).toBeFalsy()
  69. expect(_.isPlainObject('hi')).toBeFalsy()
  70. expect(_.isPlainObject(undefined)).toBeFalsy()
  71. expect(_.isPlainObject(function(){})).toBe(false)
  72. if (_.inBrowser) {
  73. expect(_.isPlainObject(window)).toBe(false)
  74. }
  75. })
  76. it('isArray', function () {
  77. expect(_.isArray([])).toBe(true)
  78. expect(_.isArray({})).toBe(false)
  79. expect(_.isArray(arguments)).toBe(false)
  80. })
  81. it('define', function () {
  82. var obj = {}
  83. _.define(obj, 'test', 123)
  84. expect(obj.test).toBe(123)
  85. var desc = Object.getOwnPropertyDescriptor(obj, 'test')
  86. expect(desc.enumerable).toBe(false)
  87. _.define(obj, 'test2', 123, true)
  88. expect(obj.test2).toBe(123)
  89. desc = Object.getOwnPropertyDescriptor(obj, 'test2')
  90. expect(desc.enumerable).toBe(true)
  91. })
  92. })