code.coffee 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are met:
  5. #
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  13. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. # ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  16. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. 'use strict'
  23. expect = require('chai').expect
  24. esutils = require '../'
  25. describe 'code', ->
  26. describe 'isDecimalDigit', ->
  27. it 'returns true if provided code is decimal digit', ->
  28. for ch in [0..9]
  29. expect(esutils.code.isDecimalDigit((ch + '').charCodeAt(0))).to.be.true
  30. it 'returns false if provided code is not decimal digit', ->
  31. for code in ['a'.charCodeAt(0)..'z'.charCodeAt(0)]
  32. expect(esutils.code.isDecimalDigit(code)).to.be.false
  33. for code in ['A'.charCodeAt(0)..'Z'.charCodeAt(0)]
  34. expect(esutils.code.isDecimalDigit(code)).to.be.false
  35. describe 'isHexDigit', ->
  36. it 'returns true if provided code is hexadecimal digit', ->
  37. for ch in [0..9]
  38. expect(esutils.code.isHexDigit((ch + '').charCodeAt(0))).to.be.true
  39. for code in ['a'.charCodeAt(0)..'f'.charCodeAt(0)]
  40. expect(esutils.code.isHexDigit(code)).to.be.true
  41. for code in ['A'.charCodeAt(0)..'F'.charCodeAt(0)]
  42. expect(esutils.code.isHexDigit(code)).to.be.true
  43. it 'returns false if provided code is not hexadecimal digit', ->
  44. for code in ['g'.charCodeAt(0)..'z'.charCodeAt(0)]
  45. expect(esutils.code.isHexDigit(code)).to.be.false
  46. for code in ['G'.charCodeAt(0)..'Z'.charCodeAt(0)]
  47. expect(esutils.code.isHexDigit(code)).to.be.false
  48. describe 'isOctalDigit', ->
  49. it 'returns true if provided code is octal digit', ->
  50. for ch in [0..7]
  51. expect(esutils.code.isOctalDigit((ch + '').charCodeAt(0))).to.be.true
  52. it 'returns false if provided code is not octal digit', ->
  53. for ch in [8..9]
  54. expect(esutils.code.isOctalDigit((ch + '').charCodeAt(0))).to.be.false
  55. for code in ['a'.charCodeAt(0)..'z'.charCodeAt(0)]
  56. expect(esutils.code.isOctalDigit(code)).to.be.false
  57. for code in ['A'.charCodeAt(0)..'Z'.charCodeAt(0)]
  58. expect(esutils.code.isOctalDigit(code)).to.be.false
  59. describe 'isWhiteSpace', ->
  60. it 'returns true if provided code is white space', ->
  61. codes = [
  62. 0x0009 # TAB
  63. 0x000B # VT
  64. 0x000C # FF
  65. 0x0020 # SP
  66. 0x00A0 # NBSP
  67. 0xFEFF # BOM
  68. # Zs
  69. 0x1680
  70. 0x180E
  71. 0x2000
  72. 0x2001
  73. 0x2002
  74. 0x2003
  75. 0x2004
  76. 0x2005
  77. 0x2006
  78. 0x2007
  79. 0x2008
  80. 0x2009
  81. 0x200A
  82. 0x202F
  83. 0x205F
  84. 0x3000
  85. ]
  86. for code in codes
  87. expect(esutils.code.isWhiteSpace(code)).to.be.true
  88. it 'returns false if provided code is not white space', ->
  89. for ch in [0..9]
  90. expect(esutils.code.isWhiteSpace((ch + '').charCodeAt(0))).to.be.false
  91. for code in ['a'.charCodeAt(0)..'z'.charCodeAt(0)]
  92. expect(esutils.code.isWhiteSpace(code)).to.be.false
  93. for code in ['A'.charCodeAt(0)..'Z'.charCodeAt(0)]
  94. expect(esutils.code.isWhiteSpace(code)).to.be.false
  95. describe 'isLineTerminator', ->
  96. it 'returns true if provided code is line terminator', ->
  97. codes = [
  98. 0x000A
  99. 0x000D
  100. 0x2028
  101. 0x2029
  102. ]
  103. for code in codes
  104. expect(esutils.code.isLineTerminator(code)).to.be.true
  105. it 'returns false if provided code is not line terminator', ->
  106. for ch in [0..9]
  107. expect(esutils.code.isLineTerminator((ch + '').charCodeAt(0))).to.be.false
  108. for code in ['a'.charCodeAt(0)..'z'.charCodeAt(0)]
  109. expect(esutils.code.isLineTerminator(code)).to.be.false
  110. for code in ['A'.charCodeAt(0)..'Z'.charCodeAt(0)]
  111. expect(esutils.code.isLineTerminator(code)).to.be.false
  112. describe 'isIdentifierStart', ->
  113. it 'returns true if provided code can be a start of Identifier', ->
  114. characters = [
  115. 'a'
  116. '$'
  117. '_'
  118. 'ゆ'
  119. ]
  120. for code in characters.map((ch) -> ch.charCodeAt(0))
  121. expect(esutils.code.isIdentifierStart(code)).to.be.true
  122. it 'returns false if provided code cannot be a start of Identifier', ->
  123. for ch in [0..9]
  124. expect(esutils.code.isIdentifierStart((ch + '').charCodeAt(0))).to.be.false
  125. describe 'isIdentifierPart', ->
  126. it 'returns true if provided code can be a part of Identifier', ->
  127. characters = [
  128. 'a'
  129. '_'
  130. '$'
  131. 'ゆ'
  132. ]
  133. for code in characters.map((ch) -> ch.charCodeAt(0))
  134. expect(esutils.code.isIdentifierPart(code)).to.be.true
  135. for ch in [0..9]
  136. expect(esutils.code.isIdentifierPart((ch + '').charCodeAt(0))).to.be.true
  137. it 'returns false if provided code cannot be a part of Identifier', ->
  138. expect(esutils.code.isIdentifierPart('+'.charCodeAt(0))).to.be.false
  139. expect(esutils.code.isIdentifierPart('-'.charCodeAt(0))).to.be.false