Skip to content

Instantly share code, notes, and snippets.

@Saneyan
Created November 27, 2012 16:16
Show Gist options
  • Select an option

  • Save Saneyan/4155172 to your computer and use it in GitHub Desktop.

Select an option

Save Saneyan/4155172 to your computer and use it in GitHub Desktop.
###
CoffeeScript Tests for ME!
I found CoffeeScript is very very simple...
@author Saneyuki Tadokoro (@Saneyan or @jSaneyan) <post@saneyuki.gfunction.com>
###
#
# Util
#
log = ( target ) ->
console.log( target )
target
log log log 'LOG'
#
# Test 1
#
log( '\n-------------Test 1-------------' );
func1 = ( name ) -> "Hello, #{name}"
log func1( 'Saneyan' )
log( func1( 'John' ) )
log( func1 'Smith' )
log func1 'Moon'
log func1()
log func1
func12 = ( a, b ) -> "#{ a }, #{ b }"
log func12( 'A', 'B' )
log func12 'A', 'B'
#
# Test 2
#
log( '\n-------------Test 2-------------' );
list1 = { name: 'Saneyan', age: 18 }
list2 =
computer1:
cpu: 'Core 2 Duo E8500'
memory: '8GB'
hdd: '300GB'
computer2:
cpu: 'Core i7'
memory: '16GB'
hdd: '3TB'
list3 =
cup:
coffee: {
sugar: false
}
tea:
sugar: true
log list1
log list2
log list3
#
# Test 3
#
log( '\n-------------Test 3-------------' );
array1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
array2 = [
1, 2, 3
4, 5, 6
7, 8, 9
]
array3 = [
[ 0, 1, 2 ]
[ 0, 1, 2 ]
[ 0, 1, 2 ]
]
log array1
log array2
log array3
#
# Test 4
#
log( '\n-------------Test 4-------------' );
al1 = [
numbers: [
0, 1
2, 3, 4, 5, 6, 7, 8
9
]
# DO NOT forget comma if you want define as a list
, numbers: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
]
al2 = {
array: [
list:
value: 0
inner: {
mask: false
}
, list:
value: 1
inner: {
mask: true
}
]
}
log al1
log al1[ 0 ].numbers[ 9 ]
log al2
log al2[ 'array' ][ 1 ].list.inner.mask
#
# Test 5
#
log( '\n-------------Test 5-------------' );
log
nest:
test: '1'
log(
nest:
test: '2'
)
#
# Test 6
#
log( '\n-------------Test 6-------------' );
log "9 is 9? -> #{ 9 is 9 }"
log "10 isnt 9? -> #{ 10 isnt 9 }"
log '\n' + "true, yes, on: #{ [ true, yes, on ] }"
log "false, no, off: #{ [ false, no, off ] }"
log '\n' + "true isnt false? -> #{ true isnt false }"
log "yes isnt no? -> #{ yes isnt false }"
log "on isnt off? -> #{ on isnt off }"
log '\n' + "true is yes? -> #{ true is yes }"
log "true is on? -> #{ true is on }"
log "yes is on? -> #{ yes is on }"
log '\n' + "false is no? -> #{ false is no }"
log "false is off? -> #{ false is off }"
log "no is off? -> #{ no is off }"
#
# Test 7
#
log( '\n-------------Test 7-------------' );
outer = 1
func2 = ->
inner = 5
outer = 10
log func2() is outer
func3 = ( outer ) ->
outer = "Argument 'outer' is in func3 scope"
log func3()
log outer
#
# Test 8
#
log( '\n-------------Test 8-------------' );
open = -> on
it = '10am'
log status = open() if it is '10am'
it = '12am'
if( it is '12am' and status is on )
log 'Open'
else
log 'Close'
close = -> off
it = '9pm'
log status = if status is on and it is '9pm' then close() else off
#
# Test 9
#
log( '\n-------------Test 9-------------' );
first = second = third = others = 'Unmarked'
adapt = ( fir, sec, thi, oth... ) ->
first = fir
second = sec
third = thi
others = oth
number = [
'first', 'second', 'third', 'forth'
'fifth', 'sixth', 'seventh', 'eighth'
'nineth', 'tenth'
]
adapt number...
log "First: #{ first }"
log "Second: #{ second }"
log "Third: #{ third }"
log "Others: #{ others }"
#
# Test 10
#
log( '\n-------------Test 10-------------' );
array4 = [ 'Coke', 'Lemon', 'Vitamin' ]
log value1 for value1 in array4
for value1 in array4
log value1
log '\n'
array5 = [ 'Camera', 'Software', 'Display' ]
log i + 1, log value2 for value2, i in array5
for value2, i in array5
log i + 1
log value2
log '\n'
array6 = [ 'Time', 'Space', 'Universe' ]
log value3 for value3 in array6 when value3 isnt 'Space'
for value3 in array6 when value3 isnt 'Space'
log value3
obj1 =
marked: yes
number: 3
log '\n'
log "Key: #{ key }, Value: #{ value }" for key, value of obj1
for key, value of obj1
log "Key: #{ key }, Value: #{ value }"
#
# Test 11
#
log( '\n-------------Test 11-------------' );
log [ 1..10 ]
log [ -10..1 ]
log num for num in [ 0..20 ] by 4 when num isnt 8
log [ 9..100 ][ 10..20 ]
a
b
ret = -> log "A: #{ a }, B: #{ b }"
[ a, b ] = [ 'Japan', 'Japanese' ]
ret()
[ b, a ] = [ a, b ]
ret()
#
# Test 12
#
log( '\n-------------Test 12-------------' );
text = " Paragraph 1 - Contents
Paragraph 2 - Contents
Paragraph 3 - Contents"
log text
log( '\n' )
html = """
<!DOCTYPE html>
<html>
<head><title>Test - Home</title></head>
<body><p><strong>Page 1</strong></p></body>
</html>
"""
log html
#
# Test 13
#
log( '\n-------------Test 13-------------' );
embeddedText = 'hello'
embeddedFunction = `
function(){
console.log( embeddedText );
}
`
embeddedFunction()
#
# Test 14
#
log( '\n-------------Test 14-------------' );
class Person
constructor: ->
walk: ->
log 'Walking'
speak: ->
log 'Hello'
class Employee extends Person
_private_job = undefined
constructor: ( job )->
_private_job = job
show: ->
log _private_job
speak: ->
log 'Good morning'
super()
Person::walk()
Employee::speak()
employee = new Employee( 'Programmer' )
employee.walk()
employee.show()
employee.speak()
#
# Test 15
#
log( '\n-------------Test 15-------------' );
log do ->
i = 10
while i > 0
i--
log do ->
stop = no
j = 10
until stop
if j < 2 then stop = yes
j--
#
# Test 16
#
log( '\n-------------Test 16-------------' );
class Car
name = undefined
module = undefined
speed = 0
constructor: ( n ) ->
name = n
pushAccel: ->
speed = 50
showSpeed: ->
log speed
addModule: ( module ) ->
module = module.apply @
log "#{ module if module? } is added!"
car = new Car( 'MyCar' )
do ->
@unit = 'Radio'
car.addModule ->
@unit if log @unit?
car.addModule =>
@unit if log @unit?
// Generated by CoffeeScript 1.4.0
/*
CoffeeScript Tests for ME!
I found CoffeeScript is very very simple...
@author Saneyuki Tadokoro (@Saneyan or @jSaneyan) <post@saneyuki.gfunction.com>
*/
(function() {
var Car, Employee, Person, a, adapt, al1, al2, array1, array2, array3, array4, array5, array6, b, car, close, embeddedFunction, embeddedText, employee, first, func1, func12, func2, func3, html, i, it, key, list1, list2, list3, log, num, number, obj1, open, others, outer, ret, second, status, text, third, value, value1, value2, value3, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n, _o, _p, _ref, _ref1, _results,
__slice = [].slice,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
log = function(target) {
console.log(target);
return target;
};
log(log(log('LOG')));
log('\n-------------Test 1-------------');
func1 = function(name) {
return "Hello, " + name;
};
log(func1('Saneyan'));
log(func1('John'));
log(func1('Smith'));
log(func1('Moon'));
log(func1());
log(func1);
func12 = function(a, b) {
return "" + a + ", " + b;
};
log(func12('A', 'B'));
log(func12('A', 'B'));
log('\n-------------Test 2-------------');
list1 = {
name: 'Saneyan',
age: 18
};
list2 = {
computer1: {
cpu: 'Core 2 Duo E8500',
memory: '8GB',
hdd: '300GB'
},
computer2: {
cpu: 'Core i7',
memory: '16GB',
hdd: '3TB'
}
};
list3 = {
cup: {
coffee: {
sugar: false
},
tea: {
sugar: true
}
}
};
log(list1);
log(list2);
log(list3);
log('\n-------------Test 3-------------');
array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
array3 = [[0, 1, 2], [0, 1, 2], [0, 1, 2]];
log(array1);
log(array2);
log(array3);
log('\n-------------Test 4-------------');
al1 = [
{
numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}, {
numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}
];
al2 = {
array: [
{
list: {
value: 0,
inner: {
mask: false
}
}
}, {
list: {
value: 1,
inner: {
mask: true
}
}
}
]
};
log(al1);
log(al1[0].numbers[9]);
log(al2);
log(al2['array'][1].list.inner.mask);
log('\n-------------Test 5-------------');
log({
nest: {
test: '1'
}
});
log({
nest: {
test: '2'
}
});
log('\n-------------Test 6-------------');
log("9 is 9? -> " + (9 === 9));
log("10 isnt 9? -> " + (10 !== 9));
log('\n' + ("true, yes, on: " + [true, true, true]));
log("false, no, off: " + [false, false, false]);
log('\n' + ("true isnt false? -> " + (true !== false)));
log("yes isnt no? -> " + (true !== false));
log("on isnt off? -> " + (true !== false));
log('\n' + ("true is yes? -> " + (true === true)));
log("true is on? -> " + (true === true));
log("yes is on? -> " + (true === true));
log('\n' + ("false is no? -> " + (false === false)));
log("false is off? -> " + (false === false));
log("no is off? -> " + (false === false));
log('\n-------------Test 7-------------');
outer = 1;
func2 = function() {
var inner;
inner = 5;
return outer = 10;
};
log(func2() === outer);
func3 = function(outer) {
return outer = "Argument 'outer' is in func3 scope";
};
log(func3());
log(outer);
log('\n-------------Test 8-------------');
open = function() {
return true;
};
it = '10am';
if (it === '10am') {
log(status = open());
}
it = '12am';
if (it === '12am' && status === true) {
log('Open');
} else {
log('Close');
}
close = function() {
return false;
};
it = '9pm';
log(status = status === true && it === '9pm' ? close() : false);
log('\n-------------Test 9-------------');
first = second = third = others = 'Unmarked';
adapt = function() {
var fir, oth, sec, thi;
fir = arguments[0], sec = arguments[1], thi = arguments[2], oth = 4 <= arguments.length ? __slice.call(arguments, 3) : [];
first = fir;
second = sec;
third = thi;
return others = oth;
};
number = ['first', 'second', 'third', 'forth', 'fifth', 'sixth', 'seventh', 'eighth', 'nineth', 'tenth'];
adapt.apply(null, number);
log("First: " + first);
log("Second: " + second);
log("Third: " + third);
log("Others: " + others);
log('\n-------------Test 10-------------');
array4 = ['Coke', 'Lemon', 'Vitamin'];
for (_i = 0, _len = array4.length; _i < _len; _i++) {
value1 = array4[_i];
log(value1);
}
for (_j = 0, _len1 = array4.length; _j < _len1; _j++) {
value1 = array4[_j];
log(value1);
}
log('\n');
array5 = ['Camera', 'Software', 'Display'];
for (i = _k = 0, _len2 = array5.length; _k < _len2; i = ++_k) {
value2 = array5[i];
log(i + 1, log(value2));
}
for (i = _l = 0, _len3 = array5.length; _l < _len3; i = ++_l) {
value2 = array5[i];
log(i + 1);
log(value2);
}
log('\n');
array6 = ['Time', 'Space', 'Universe'];
for (_m = 0, _len4 = array6.length; _m < _len4; _m++) {
value3 = array6[_m];
if (value3 !== 'Space') {
log(value3);
}
}
for (_n = 0, _len5 = array6.length; _n < _len5; _n++) {
value3 = array6[_n];
if (value3 !== 'Space') {
log(value3);
}
}
obj1 = {
marked: true,
number: 3
};
log('\n');
for (key in obj1) {
value = obj1[key];
log("Key: " + key + ", Value: " + value);
}
for (key in obj1) {
value = obj1[key];
log("Key: " + key + ", Value: " + value);
}
log('\n-------------Test 11-------------');
log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
log([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1]);
for (num = _o = 0; _o <= 20; num = _o += 4) {
if (num !== 8) {
log(num);
}
}
log((function() {
_results = [];
for (_p = 9; _p <= 100; _p++){ _results.push(_p); }
return _results;
}).apply(this).slice(10, 21));
a;
b;
ret = function() {
return log("A: " + a + ", B: " + b);
};
_ref = ['Japan', 'Japanese'], a = _ref[0], b = _ref[1];
ret();
_ref1 = [a, b], b = _ref1[0], a = _ref1[1];
ret();
log('\n-------------Test 12-------------');
text = " Paragraph 1 - Contents Paragraph 2 - Contents Paragraph 3 - Contents";
log(text);
log('\n');
html = "<!DOCTYPE html>\n<html>\n <head><title>Test - Home</title></head>\n <body><p><strong>Page 1</strong></p></body>\n</html>";
log(html);
log('\n-------------Test 13-------------');
embeddedText = 'hello';
embeddedFunction =
function(){
console.log( embeddedText );
}
;
embeddedFunction();
log('\n-------------Test 14-------------');
Person = (function() {
function Person() {}
Person.prototype.walk = function() {
return log('Walking');
};
Person.prototype.speak = function() {
return log('Hello');
};
return Person;
})();
Employee = (function(_super) {
var _private_job;
__extends(Employee, _super);
_private_job = void 0;
function Employee(job) {
_private_job = job;
}
Employee.prototype.show = function() {
return log(_private_job);
};
Employee.prototype.speak = function() {
log('Good morning');
return Employee.__super__.speak.call(this);
};
return Employee;
})(Person);
Person.prototype.walk();
Employee.prototype.speak();
employee = new Employee('Programmer');
employee.walk();
employee.show();
employee.speak();
log('\n-------------Test 15-------------');
log((function() {
var _results1;
i = 10;
_results1 = [];
while (i > 0) {
_results1.push(i--);
}
return _results1;
})());
log((function() {
var j, stop, _results1;
stop = false;
j = 10;
_results1 = [];
while (!stop) {
if (j < 2) {
stop = true;
}
_results1.push(j--);
}
return _results1;
})());
log('\n-------------Test 16-------------');
Car = (function() {
var module, name, speed;
name = void 0;
module = void 0;
speed = 0;
function Car(n) {
name = n;
}
Car.prototype.pushAccel = function() {
return speed = 50;
};
Car.prototype.showSpeed = function() {
return log(speed);
};
Car.prototype.addModule = function(module) {
module = module.apply(this);
return log("" + (module != null ? module : void 0) + " is added!");
};
return Car;
})();
car = new Car('MyCar');
(function() {
var _this = this;
this.unit = 'Radio';
car.addModule(function() {
if (log(this.unit != null)) {
return this.unit;
}
});
return car.addModule(function() {
if (log(_this.unit != null)) {
return _this.unit;
}
});
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment