1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/test/numbers.coffee

77 lines
1.8 KiB
CoffeeScript
Raw Normal View History

2010-12-29 14:06:57 -05:00
# Number Literals
# ---------------
# * Decimal Integer Literals
# * Octal Integer Literals
# * Hexadecimal Integer Literals
# * Scientific Notation Integer Literals
# * Scientific Notation Non-Integer Literals
# * Non-Integer Literals
2011-10-21 14:44:56 -04:00
# * Binary Integer Literals
2010-12-30 22:48:31 -05:00
2011-10-21 14:44:56 -04:00
# Binary Integer Literals
# Binary notation is understood as would be decimal notation.
test "Parser recognises binary numbers", ->
2011-10-21 15:27:08 -04:00
eq 4, 0b100
2011-10-21 14:44:56 -04:00
2011-03-11 21:41:12 -05:00
# Decimal Integer Literals
2010-12-30 22:48:31 -05:00
test "call methods directly on numbers", ->
eq 4, 4.valueOf()
eq '11', 4.toString 3
eq -1, 3 -4
#764: Numbers should be indexable
eq Number::toString, 42['toString']
eq Number::toString, 42.toString
2011-03-11 21:41:12 -05:00
# Non-Integer Literals
2010-12-30 22:48:31 -05:00
# Decimal number literals.
value = .25 + .75
ok value is 1
value = 0.0 + -.25 - -.75 + 0.0
ok value is 0.5
#764: Numbers should be indexable
eq Number::toString, 4['toString']
2010-12-30 22:48:31 -05:00
eq Number::toString, 4.2['toString']
eq Number::toString, .42['toString']
eq Number::toString, (4)['toString']
2010-12-30 22:48:31 -05:00
eq Number::toString, 4.toString
2010-12-30 22:48:31 -05:00
eq Number::toString, 4.2.toString
eq Number::toString, .42.toString
eq Number::toString, (4).toString
2011-02-27 02:11:12 -05:00
test '#1168: leading floating point suppresses newline', ->
2012-02-03 19:33:03 -05:00
eq 1, do ->
1
.5 + 0.5
test "Python-style octal literal notation '0o777'", ->
eq 511, 0o777
eq 1, 0o1
eq 1, 0o00001
eq parseInt('0777', 8), 0o777
eq '777', 0o777.toString 8
eq 4, 0o4.valueOf()
eq Number::toString, 0o777['toString']
eq Number::toString, 0o777.toString
test "#2060: Disallow uppercase radix prefixes and exponential notation", ->
for char in ['b', 'o', 'x', 'e']
program = "0#{char}0"
doesNotThrow -> CoffeeScript.compile program, bare: yes
throws -> CoffeeScript.compile program.toUpperCase(), bare: yes
test "#2224: hex literals with 0b or B or E", ->
eq 176, 0x0b0
eq 177, 0x0B1
eq 225, 0xE1