mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
node conversion finished, narwhal removed.
This commit is contained in:
10
test/fixtures/execution/test_arguments.coffee
vendored
10
test/fixtures/execution/test_arguments.coffee
vendored
@@ -4,12 +4,12 @@ area: (x, y, x1, y1) ->
|
||||
x: y: 10
|
||||
x1: y1: 20
|
||||
|
||||
print area(x, y, x1, y1) is 100
|
||||
puts area(x, y, x1, y1) is 100
|
||||
|
||||
print(area(x, y,
|
||||
puts(area(x, y,
|
||||
x1, y1) is 100)
|
||||
|
||||
print(area(
|
||||
puts(area(
|
||||
x
|
||||
y
|
||||
x1
|
||||
@@ -19,7 +19,7 @@ print(area(
|
||||
|
||||
# Arguments are turned into arrays.
|
||||
curried: ->
|
||||
print area.apply(this, arguments.concat(20, 20)) is 100
|
||||
puts area.apply(this, arguments.concat(20, 20)) is 100
|
||||
|
||||
curried 10, 10
|
||||
|
||||
@@ -29,4 +29,4 @@ func: ->
|
||||
arguments: 25
|
||||
arguments
|
||||
|
||||
print func(100) is 25
|
||||
puts func(100) is 25
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
nums: n * n for n in [1, 2, 3] when n % 2 isnt 0
|
||||
results: n * 2 for n in nums
|
||||
|
||||
print results.join(',') is '2,18'
|
||||
puts results.join(',') is '2,18'
|
||||
|
||||
|
||||
obj: {one: 1, two: 2, three: 3}
|
||||
names: prop + '!' for prop of obj
|
||||
odds: prop + '!' for prop, value of obj when value % 2 isnt 0
|
||||
|
||||
print names.join(' ') is "one! two! three!"
|
||||
print odds.join(' ') is "one! three!"
|
||||
puts names.join(' ') is "one! two! three!"
|
||||
puts odds.join(' ') is "one! three!"
|
||||
|
||||
|
||||
evens: for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0
|
||||
@@ -17,12 +17,12 @@ evens: for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0
|
||||
num -= 2
|
||||
num * -1
|
||||
|
||||
print evens.join(', ') is '4, 6, 8'
|
||||
puts evens.join(', ') is '4, 6, 8'
|
||||
|
||||
|
||||
# Make sure that the "in" operator still works.
|
||||
|
||||
print 2 in evens
|
||||
puts 2 in evens
|
||||
|
||||
|
||||
# When functions are being defined within the body of a comprehension, make
|
||||
@@ -37,6 +37,6 @@ for method in methods
|
||||
obj[name]: ->
|
||||
"I'm " + name
|
||||
|
||||
print obj.one() is "I'm one"
|
||||
print obj.two() is "I'm two"
|
||||
print obj.three() is "I'm three"
|
||||
puts obj.one() is "I'm one"
|
||||
puts obj.two() is "I'm two"
|
||||
puts obj.three() is "I'm three"
|
||||
|
||||
@@ -7,7 +7,7 @@ catch error
|
||||
|
||||
result2: try nonexistent * missing catch error then true
|
||||
|
||||
print result is true and result2 is true
|
||||
puts result is true and result2 is true
|
||||
|
||||
|
||||
# Assign to conditional.
|
||||
@@ -16,8 +16,8 @@ get_x: -> 10
|
||||
|
||||
if x: get_x() then 100
|
||||
|
||||
print x is 10
|
||||
puts x is 10
|
||||
|
||||
x: if get_x() then 100
|
||||
|
||||
print x is 100
|
||||
puts x is 100
|
||||
2
test/fixtures/execution/test_blocks.coffee
vendored
2
test/fixtures/execution/test_blocks.coffee
vendored
@@ -1,4 +1,4 @@
|
||||
results: [1, 2, 3].map (x) ->
|
||||
x * x
|
||||
|
||||
print results.join(' ') is '1 4 9'
|
||||
puts results.join(' ') is '1 4 9'
|
||||
@@ -20,7 +20,7 @@ ThirdChild::func: (string) ->
|
||||
|
||||
result: (new ThirdChild()).func 'four'
|
||||
|
||||
print result is 'zero/one/two/three/four'
|
||||
puts result is 'zero/one/two/three/four'
|
||||
|
||||
|
||||
TopClass: (arg) ->
|
||||
@@ -35,4 +35,4 @@ SubClass: ->
|
||||
SuperClass extends TopClass
|
||||
SubClass extends SuperClass
|
||||
|
||||
print((new SubClass()).prop is 'top-super-sub')
|
||||
puts((new SubClass()).prop is 'top-super-sub')
|
||||
@@ -3,7 +3,7 @@ identity_wrap: (x) ->
|
||||
|
||||
result: identity_wrap(identity_wrap(true))()()
|
||||
|
||||
print result
|
||||
puts result
|
||||
|
||||
|
||||
str: 'god'
|
||||
@@ -14,7 +14,7 @@ result: str.
|
||||
reverse().
|
||||
reverse()
|
||||
|
||||
print result.join('') is 'dog'
|
||||
puts result.join('') is 'dog'
|
||||
|
||||
result: str
|
||||
.split('')
|
||||
@@ -22,4 +22,4 @@ result: str
|
||||
.reverse()
|
||||
.reverse()
|
||||
|
||||
print result.join('') is 'dog'
|
||||
puts result.join('') is 'dog'
|
||||
@@ -3,26 +3,26 @@ b: -2
|
||||
|
||||
[a, b]: [b, a]
|
||||
|
||||
print a is -2
|
||||
print b is -1
|
||||
puts a is -2
|
||||
puts b is -1
|
||||
|
||||
|
||||
arr: [1, 2, 3]
|
||||
|
||||
[a, b, c]: arr
|
||||
|
||||
print a is 1
|
||||
print b is 2
|
||||
print c is 3
|
||||
puts a is 1
|
||||
puts b is 2
|
||||
puts c is 3
|
||||
|
||||
|
||||
obj: {x: 10, y: 20, z: 30}
|
||||
|
||||
{x: a, y: b, z: c}: obj
|
||||
|
||||
print a is 10
|
||||
print b is 20
|
||||
print c is 30
|
||||
puts a is 10
|
||||
puts b is 20
|
||||
puts c is 30
|
||||
|
||||
|
||||
person: {
|
||||
@@ -42,8 +42,8 @@ person: {
|
||||
|
||||
{name: a, family: {brother: {addresses: [one, {city: b}]}}}: person
|
||||
|
||||
print a is "Bob"
|
||||
print b is "Moquasset NY, 10021"
|
||||
puts a is "Bob"
|
||||
puts b is "Moquasset NY, 10021"
|
||||
|
||||
|
||||
test: {
|
||||
@@ -59,4 +59,4 @@ test: {
|
||||
|
||||
{person: {address: [ignore, addr...]}}: test
|
||||
|
||||
print addr.join(', ') is "Street 101, Apt 101, City 101"
|
||||
puts addr.join(', ') is "Street 101, Apt 101, City 101"
|
||||
@@ -26,4 +26,4 @@ func: ->
|
||||
|
||||
c.single: c.list[1..1][0]
|
||||
|
||||
print func() is '-'
|
||||
puts func() is '-'
|
||||
|
||||
24
test/fixtures/execution/test_existence.coffee
vendored
24
test/fixtures/execution/test_existence.coffee
vendored
@@ -1,8 +1,8 @@
|
||||
print(if my_special_variable? then false else true)
|
||||
puts(if my_special_variable? then false else true)
|
||||
|
||||
my_special_variable: false
|
||||
|
||||
print(if my_special_variable? then true else false)
|
||||
puts(if my_special_variable? then true else false)
|
||||
|
||||
|
||||
# Existential assignment.
|
||||
@@ -12,7 +12,7 @@ a: null
|
||||
a ?= 10
|
||||
b ?= 10
|
||||
|
||||
print a is 10 and b is 10
|
||||
puts a is 10 and b is 10
|
||||
|
||||
|
||||
# The existential operator.
|
||||
@@ -20,7 +20,7 @@ print a is 10 and b is 10
|
||||
z: null
|
||||
x: z ? "EX"
|
||||
|
||||
print z is null and x is "EX"
|
||||
puts z is null and x is "EX"
|
||||
|
||||
|
||||
# Only evaluate once.
|
||||
@@ -30,7 +30,7 @@ get_next_node: ->
|
||||
throw "up" if counter
|
||||
counter++
|
||||
|
||||
print(if get_next_node()? then true else false)
|
||||
puts(if get_next_node()? then true else false)
|
||||
|
||||
|
||||
# Existence chains, soaking up undefined properties:
|
||||
@@ -39,17 +39,17 @@ obj: {
|
||||
prop: "hello"
|
||||
}
|
||||
|
||||
print obj?.prop is "hello"
|
||||
puts obj?.prop is "hello"
|
||||
|
||||
print obj?.prop?.non?.existent?.property is undefined
|
||||
puts obj?.prop?.non?.existent?.property is undefined
|
||||
|
||||
|
||||
# Soaks and caches method calls as well.
|
||||
|
||||
arr: ["--", "----"]
|
||||
|
||||
print arr.pop()?.length is 4
|
||||
print arr.pop()?.length is 2
|
||||
print arr.pop()?.length is undefined
|
||||
print arr[0]?.length is undefined
|
||||
print arr.pop()?.length?.non?.existent()?.property is undefined
|
||||
puts arr.pop()?.length is 4
|
||||
puts arr.pop()?.length is 2
|
||||
puts arr.pop()?.length is undefined
|
||||
puts arr[0]?.length is undefined
|
||||
puts arr.pop()?.length?.non?.existent()?.property is undefined
|
||||
|
||||
@@ -9,7 +9,7 @@ findit: (items) ->
|
||||
for item in items
|
||||
return item if item is "bacon"
|
||||
|
||||
print findit(items) is "bacon"
|
||||
puts findit(items) is "bacon"
|
||||
|
||||
|
||||
# When when a closure wrapper is generated for expression conversion, make sure
|
||||
@@ -26,5 +26,5 @@ obj: {
|
||||
this.num
|
||||
}
|
||||
|
||||
print obj.num is obj.func()
|
||||
print obj.num is obj.result
|
||||
puts obj.num is obj.func()
|
||||
puts obj.num is obj.result
|
||||
@@ -7,10 +7,10 @@ result: if a
|
||||
if d
|
||||
true
|
||||
|
||||
print result
|
||||
puts result
|
||||
|
||||
|
||||
first: if false then false else second: if false then false else true
|
||||
|
||||
print first
|
||||
print second
|
||||
puts first
|
||||
puts second
|
||||
36
test/fixtures/execution/test_functions.coffee
vendored
36
test/fixtures/execution/test_functions.coffee
vendored
@@ -2,11 +2,11 @@ x: 1
|
||||
y: {}
|
||||
y.x: -> 3
|
||||
|
||||
print x is 1
|
||||
print typeof(y.x) is 'function'
|
||||
print y.x instanceof Function
|
||||
print y.x() is 3
|
||||
print y.x.name is 'x'
|
||||
puts x is 1
|
||||
puts typeof(y.x) is 'function'
|
||||
puts y.x instanceof Function
|
||||
puts y.x() is 3
|
||||
puts y.x.name is 'x'
|
||||
|
||||
|
||||
# The empty function should not cause a syntax error.
|
||||
@@ -17,10 +17,10 @@ obj: {
|
||||
name: "Fred"
|
||||
|
||||
bound: ->
|
||||
(=> print(this.name is "Fred"))()
|
||||
(=> puts(this.name is "Fred"))()
|
||||
|
||||
unbound: ->
|
||||
(-> print(!this.name?))()
|
||||
(-> puts(!this.name?))()
|
||||
}
|
||||
|
||||
obj.unbound()
|
||||
@@ -44,18 +44,18 @@ Math: {
|
||||
FastAdd: memoize (a, b) -> a + b
|
||||
}
|
||||
|
||||
print Math.Add(5, 5) is 10
|
||||
print Math.AnonymousAdd(10, 10) is 20
|
||||
print Math.FastAdd(20, 20) is 40
|
||||
puts Math.Add(5, 5) is 10
|
||||
puts Math.AnonymousAdd(10, 10) is 20
|
||||
puts Math.FastAdd(20, 20) is 40
|
||||
|
||||
|
||||
# Parens are optional on simple function calls.
|
||||
print 100 > 1 if 1 > 0
|
||||
print true unless false
|
||||
print true for i in [1..3]
|
||||
puts 100 > 1 if 1 > 0
|
||||
puts true unless false
|
||||
puts true for i in [1..3]
|
||||
|
||||
print_func: (f) -> print(f())
|
||||
print_func -> true
|
||||
puts_func: (f) -> puts(f())
|
||||
puts_func -> true
|
||||
|
||||
# Optional parens can be used in a nested fashion.
|
||||
call: (func) -> func()
|
||||
@@ -64,7 +64,7 @@ result: call ->
|
||||
inner: call ->
|
||||
Math.Add(5, 5)
|
||||
|
||||
print result is 10
|
||||
puts result is 10
|
||||
|
||||
|
||||
# And even with strange things like this:
|
||||
@@ -72,8 +72,8 @@ print result is 10
|
||||
funcs: [(x) -> x, (x) -> x * x]
|
||||
result: funcs[1] 5
|
||||
|
||||
print result is 25
|
||||
puts result is 25
|
||||
|
||||
result: ("hello".slice) 3
|
||||
|
||||
print result is 'lo'
|
||||
puts result is 'lo'
|
||||
@@ -18,4 +18,4 @@ switch 'string'
|
||||
code()
|
||||
# comment
|
||||
|
||||
print func()
|
||||
puts func()
|
||||
|
||||
12
test/fixtures/execution/test_heredocs.coffee
vendored
12
test/fixtures/execution/test_heredocs.coffee
vendored
@@ -3,7 +3,7 @@ a: """
|
||||
on two lines
|
||||
"""
|
||||
|
||||
print a is "basic heredoc\non two lines"
|
||||
puts a is "basic heredoc\non two lines"
|
||||
|
||||
|
||||
a: '''
|
||||
@@ -12,12 +12,12 @@ a: '''
|
||||
c
|
||||
'''
|
||||
|
||||
print a is "a\n \"b\nc"
|
||||
puts a is "a\n \"b\nc"
|
||||
|
||||
|
||||
a: '''one-liner'''
|
||||
|
||||
print a is 'one-liner'
|
||||
puts a is 'one-liner'
|
||||
|
||||
|
||||
a: """
|
||||
@@ -25,7 +25,7 @@ a: """
|
||||
here
|
||||
"""
|
||||
|
||||
print a is "out\nhere"
|
||||
puts a is "out\nhere"
|
||||
|
||||
|
||||
a: '''
|
||||
@@ -34,7 +34,7 @@ a: '''
|
||||
c
|
||||
'''
|
||||
|
||||
print a is " a\n b\nc"
|
||||
puts a is " a\n b\nc"
|
||||
|
||||
a: '''
|
||||
a
|
||||
@@ -43,4 +43,4 @@ a
|
||||
b c
|
||||
'''
|
||||
|
||||
print a is "a\n\n\nb c"
|
||||
puts a is "a\n\n\nb c"
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
num: 1 + 2 + (a: 3)
|
||||
|
||||
print num is 6
|
||||
puts num is 6
|
||||
|
||||
|
||||
result: if true
|
||||
false
|
||||
other: "result"
|
||||
|
||||
print result is "result" and other is "result"
|
||||
puts result is "result" and other is "result"
|
||||
14
test/fixtures/execution/test_literals.coffee
vendored
14
test/fixtures/execution/test_literals.coffee
vendored
@@ -1,37 +1,37 @@
|
||||
a: [(x) -> x, (x) -> x * x]
|
||||
|
||||
print a.length is 2
|
||||
puts a.length is 2
|
||||
|
||||
|
||||
regex: /match/i
|
||||
words: "I think there is a match in here."
|
||||
|
||||
print !!words.match(regex)
|
||||
puts !!words.match(regex)
|
||||
|
||||
|
||||
neg: (3 -4)
|
||||
|
||||
print neg is -1
|
||||
puts neg is -1
|
||||
|
||||
|
||||
func: ->
|
||||
return if true
|
||||
|
||||
print func() is null
|
||||
puts func() is null
|
||||
|
||||
|
||||
str: "\\"
|
||||
reg: /\\/
|
||||
|
||||
print reg(str) and str is '\\'
|
||||
puts reg(str) and str is '\\'
|
||||
|
||||
|
||||
i: 10
|
||||
while i -= 1
|
||||
|
||||
print i is 0
|
||||
puts i is 0
|
||||
|
||||
|
||||
money$: 'dollars'
|
||||
|
||||
print money$ is 'dollars'
|
||||
puts money$ is 'dollars'
|
||||
@@ -6,6 +6,6 @@ multi_liner:
|
||||
single_liner:
|
||||
[x, y] for y in [3..5] for x in [3..5]
|
||||
|
||||
print multi_liner.length is single_liner.length
|
||||
print 5 is multi_liner[2][2][1]
|
||||
print 5 is single_liner[2][2][1]
|
||||
puts multi_liner.length is single_liner.length
|
||||
puts 5 is multi_liner[2][2][1]
|
||||
puts 5 is single_liner[2][2][1]
|
||||
|
||||
@@ -3,4 +3,4 @@ six:
|
||||
2 +
|
||||
3
|
||||
|
||||
print six is 6
|
||||
puts six is 6
|
||||
10
test/fixtures/execution/test_operations.coffee
vendored
10
test/fixtures/execution/test_operations.coffee
vendored
@@ -1,12 +1,12 @@
|
||||
# CoffeeScript's operations should be chainable, like Python's.
|
||||
|
||||
print 500 > 50 > 5 > -5
|
||||
puts 500 > 50 > 5 > -5
|
||||
|
||||
print true is not false is true is not false
|
||||
puts true is not false is true is not false
|
||||
|
||||
print 10 < 20 > 10
|
||||
puts 10 < 20 > 10
|
||||
|
||||
print 50 > 10 > 5 is parseInt('5', 10)
|
||||
puts 50 > 10 > 5 is parseInt('5', 10)
|
||||
|
||||
|
||||
# Make sure that each argument is only evaluated once, even if used
|
||||
@@ -15,4 +15,4 @@ print 50 > 10 > 5 is parseInt('5', 10)
|
||||
i: 0
|
||||
func: -> i++
|
||||
|
||||
print 1 > func() < 1
|
||||
puts 1 > func() < 1
|
||||
|
||||
@@ -5,16 +5,16 @@ negs: negs[0..2]
|
||||
|
||||
result: nums.concat(negs).join(', ')
|
||||
|
||||
print result is '3, 6, 9, -20, -19, -18'
|
||||
puts result is '3, 6, 9, -20, -19, -18'
|
||||
|
||||
# Ensure that ranges are safe. This used to infinite loop:
|
||||
j = 5
|
||||
result: for j in [j..(j+3)]
|
||||
j
|
||||
|
||||
print result.join(' ') is '5 6 7 8'
|
||||
puts result.join(' ') is '5 6 7 8'
|
||||
|
||||
# With range comprehensions, you can loop in steps.
|
||||
results: x for x in [0..25] by 5
|
||||
|
||||
print results.join(' ') is '0 5 10 15 20 25'
|
||||
puts results.join(' ') is '0 5 10 15 20 25'
|
||||
@@ -5,7 +5,7 @@ b: array[2...4]
|
||||
|
||||
result: a.concat(b).join(' ')
|
||||
|
||||
print result is "7 8 9 2 3"
|
||||
puts result is "7 8 9 2 3"
|
||||
|
||||
countdown: [10..1].join(' ')
|
||||
print countdown is "10 9 8 7 6 5 4 3 2 1"
|
||||
puts countdown is "10 9 8 7 6 5 4 3 2 1"
|
||||
10
test/fixtures/execution/test_splats.coffee
vendored
10
test/fixtures/execution/test_splats.coffee
vendored
@@ -3,7 +3,7 @@ func: (first, second, rest...) ->
|
||||
|
||||
result: func 1, 2, 3, 4, 5
|
||||
|
||||
print result is "3 4 5"
|
||||
puts result is "3 4 5"
|
||||
|
||||
|
||||
gold: silver: bronze: the_field: null
|
||||
@@ -29,7 +29,7 @@ contenders: [
|
||||
|
||||
medalists "Mighty Mouse", contenders...
|
||||
|
||||
print gold is "Mighty Mouse"
|
||||
print silver is "Michael Phelps"
|
||||
print bronze is "Liu Xiang"
|
||||
print the_field.length is 8
|
||||
puts gold is "Mighty Mouse"
|
||||
puts silver is "Michael Phelps"
|
||||
puts bronze is "Liu Xiang"
|
||||
puts the_field.length is 8
|
||||
2
test/fixtures/execution/test_splices.coffee
vendored
2
test/fixtures/execution/test_splices.coffee
vendored
@@ -2,4 +2,4 @@ array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
array[5..10]: [0, 0, 0]
|
||||
|
||||
print array.join(' ') is '0 1 2 3 4 0 0 0'
|
||||
puts array.join(' ') is '0 1 2 3 4 0 0 0'
|
||||
10
test/fixtures/execution/test_switch.coffee
vendored
10
test/fixtures/execution/test_switch.coffee
vendored
@@ -14,7 +14,7 @@ result: switch num
|
||||
when 11 then false
|
||||
else false
|
||||
|
||||
print result
|
||||
puts result
|
||||
|
||||
func: (num) ->
|
||||
switch num
|
||||
@@ -24,7 +24,7 @@ func: (num) ->
|
||||
false
|
||||
else false
|
||||
|
||||
print func(2)
|
||||
print func(6)
|
||||
print !func(3)
|
||||
print !func(8)
|
||||
puts func(2)
|
||||
puts func(6)
|
||||
puts !func(3)
|
||||
puts !func(8)
|
||||
|
||||
6
test/fixtures/execution/test_while.coffee
vendored
6
test/fixtures/execution/test_while.coffee
vendored
@@ -1,17 +1,17 @@
|
||||
i: 100
|
||||
while i -= 1
|
||||
|
||||
print i is 0
|
||||
puts i is 0
|
||||
|
||||
|
||||
i: 5
|
||||
list: while i -= 1
|
||||
i * 2
|
||||
|
||||
print list.join(' ') is "8 6 4 2"
|
||||
puts list.join(' ') is "8 6 4 2"
|
||||
|
||||
|
||||
i: 5
|
||||
list: (i * 3 while i -= 1)
|
||||
|
||||
print list.join(' ') is "12 9 6 3"
|
||||
puts list.join(' ') is "12 9 6 3"
|
||||
@@ -3,14 +3,14 @@
|
||||
result: while sunny?
|
||||
go_outside()
|
||||
|
||||
print(3 + try
|
||||
puts(3 + try
|
||||
nonexistent.no_way
|
||||
catch error
|
||||
print(error)
|
||||
puts(error)
|
||||
3
|
||||
)
|
||||
|
||||
func: (x) ->
|
||||
return throw x
|
||||
|
||||
print(x * x for x in [1..100])
|
||||
puts(x * x for x in [1..100])
|
||||
Reference in New Issue
Block a user