| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
std.assertEqual((function(x) x * x)(5), 5 * 5) && |
| 18 |
|
| 19 |
// Correct self binding from within a function. |
| 20 |
std.assertEqual({ x: 1, y: { x: 0, y: function() self.x }.y() }, { x: 1, y: 0 }) && |
| 21 |
|
| 22 |
local max = function(a, b) if a > b then a else b; |
| 23 |
local inv(d) = if d != 0 then 1 / d else 0; |
| 24 |
|
| 25 |
std.assertEqual(max(4, 8), 8) && |
| 26 |
std.assertEqual(inv(0.5), 2) && |
| 27 |
|
| 28 |
local is_even = function(n) if n == 0 then true else is_odd(n - 1), |
| 29 |
is_odd = function(n) if n == 1 then true else is_even(n - 1); |
| 30 |
|
| 31 |
std.assertEqual(is_even(42), true) && |
| 32 |
std.assertEqual(is_odd(41), true) && |
| 33 |
|
| 34 |
std.assertEqual((function(a, b) [a, b])(b=1, a=2), [2, 1]) && |
| 35 |
std.assertEqual((function(a, b=2) [a, b])(3), [3, 2]) && |
| 36 |
|
| 37 |
// Mutually recursive default arguments. |
| 38 |
std.assertEqual((function(a=[1, b[1]], b=[a[0], 2]) [a, b])(), [[1, 2], [1, 2]]) && |
| 39 |
std.assertEqual((local a = 'no1', b = 'no2'; function(a=[1, b[1]], b=[a[0], 2]) [a, b])(), |
| 40 |
[[1, 2], [1, 2]]) && |
| 41 |
std.assertEqual((local x = 3; function(a=[x, b[1]], b=[a[0], 2]) [a, b])(), |
| 42 |
[[3, 2], [3, 2]]) && |
| 43 |
std.assertEqual({ g: 3, f(a=[self.g, b[1]], b=[a[0], 2]): [a, b] }.f(), |
| 44 |
[[3, 2], [3, 2]]) && |
| 45 |
|
| 46 |
local url(host, port=80, protocol='http', url='%s://%s:%d/' % [protocol, host, port]) = url; |
| 47 |
|
| 48 |
std.assertEqual(url('myhost'), 'http://myhost:80/') && |
| 49 |
std.assertEqual(url('mybucket', 8080, protocol='gs'), 'gs://mybucket:8080/') && |
| 50 |
std.assertEqual(url(null, url='wat'), 'wat') && |
| 51 |
|
| 52 |
local test(a=error 'Need a', alt="'" + a + "'") = alt; |
| 53 |
std.assertEqual(test(a='Q'), "'Q'") && |
| 54 |
std.assertEqual(test(alt='|Q|'), '|Q|') && |
| 55 |
|
| 56 |
local X = 3; |
| 57 |
std.assertEqual((function(X=4) X)(), 4) && |
| 58 |
std.assertEqual((function(X=4, Y=X) Y)(), 4) && |
| 59 |
|
| 60 |
true |