Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

More function tests #7

Merged
merged 4 commits into from

2 participants

@jakubmal

Hey,
Could you give me some initial feedback, so I know your expectations and how to contribute?

Btw, cool project, looking forward to testing someone ;)

tests/app/functions.js
@@ -34,5 +34,57 @@ define([ 'use!underscore' ], function(_) {
var partial = fn(sayIt, 'Hello', 'Ellie');
expect(partial('!!!')).to.be('Hello, Ellie!!!');
});
+
+ it("you should be able to use arguments", function () {
+ // define a function for fn so that the following will pass
@rmurphey Owner

You may want to be clear that you're expecting that a single function will meet all of the expectations below.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/app/functions.js
@@ -34,5 +34,57 @@ define([ 'use!underscore' ], function(_) {
var partial = fn(sayIt, 'Hello', 'Ellie');
expect(partial('!!!')).to.be('Hello, Ellie!!!');
});
+
+ it("you should be able to use arguments", function () {
+ // define a function for fn so that the following will pass
+
+ //sum all arguments that are passed to the function
+ expect(fn(1)).to.be(1);
+ expect(fn(1, 2)).to.be(3);
+ expect(fn(1, 2, 3)).to.be(6);
+ expect(fn(1, 2, 3, 4)).to.be(10);
+ expect(fn(1, 2, 3, 4, 5)).to.be(15);
+ expect(fn(1, 2, 3, 4, 5, 6)).to.be(21);
+ expect(fn(54, 6, 543, 6543, 32, 321)).to.be(7499);
+ });
+
+ it("you should be able to curry existing functions", function () {
@rmurphey Owner

Ditto here, it wasn't clear at a glance that the same function should cause all tests to pass. Maybe move curryMe2 up to the top and put all the expectations together?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/app/functions.js
((26 lines not shown))
+ expect(fn(curryMe1, "Mark")("Zuckerberg", "Facebook")).to.be("Hey! I'm Mark Zuckerberg from Facebook");
+ expect(fn(curryMe1, "Mark", "Zuckerberg")("Facebook")).to.be("Hey! I'm Mark Zuckerberg from Facebook");
+ expect(fn(curryMe1, "Mark", "Zuckerberg", "Facebook")()).to.be("Hey! I'm Mark Zuckerberg from Facebook");
+
+ var curryMe2 = function (x, y, z) {
+ return x / y * z;
+ };
+
+ expect(fn(curryMe2)(12, 3, 5)).to.be(20);
+ expect(fn(curryMe2, 12)(3, 5)).to.be(20);
+ expect(fn(curryMe2, 12, 3)(5)).to.be(20);
+ expect(fn(curryMe2, 12, 3, 5)()).to.be(20);
+ });
+
+ it('you should be able to use closure', function () {
+ var arr = [54, 2345, 32, 45, 127];
@rmurphey Owner

Super nitpicky: If the actual numbers aren't important here, might be better to use strings like 'a', b', ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/app/functions.js
((25 lines not shown))
+ expect(fn(curryMe1)("Mark", "Zuckerberg", "Facebook")).to.be("Hey! I'm Mark Zuckerberg from Facebook");
+ expect(fn(curryMe1, "Mark")("Zuckerberg", "Facebook")).to.be("Hey! I'm Mark Zuckerberg from Facebook");
+ expect(fn(curryMe1, "Mark", "Zuckerberg")("Facebook")).to.be("Hey! I'm Mark Zuckerberg from Facebook");
+ expect(fn(curryMe1, "Mark", "Zuckerberg", "Facebook")()).to.be("Hey! I'm Mark Zuckerberg from Facebook");
+
+ var curryMe2 = function (x, y, z) {
+ return x / y * z;
+ };
+
+ expect(fn(curryMe2)(12, 3, 5)).to.be(20);
+ expect(fn(curryMe2, 12)(3, 5)).to.be(20);
+ expect(fn(curryMe2, 12, 3)(5)).to.be(20);
+ expect(fn(curryMe2, 12, 3, 5)()).to.be(20);
+ });
+
+ it('you should be able to use closure', function () {
@rmurphey Owner

I think this should say "closures" not "closure"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rmurphey
Owner

Minor comments but this is great stuff. Thanks!

@rmurphey
Owner

I have some tiny language tweaks, but I'm going to go ahead and merge this. Thanks!

@rmurphey rmurphey merged commit 2145b5f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 13, 2012
  1. @jakubmal
  2. @jakubmal

    Added closure example

    jakubmal authored
Commits on Apr 14, 2012
  1. @jakubmal
  2. @jakubmal
This page is out of date. Refresh to see the latest.
Showing with 79 additions and 0 deletions.
  1. +79 −0 tests/app/functions.js
View
79 tests/app/functions.js
@@ -34,5 +34,84 @@ define([ 'use!underscore' ], function(_) {
var partial = fn(sayIt, 'Hello', 'Ellie');
expect(partial('!!!')).to.be('Hello, Ellie!!!');
});
+
+ it("you should be able to use arguments", function () {
+ fn = function () {
+ // you can only edit function body here
+ };
+
+ var a = Math.random(), b = Math.random(), c = Math.random(), d = Math.random();
+ expect(fn(a)).to.be(a);
+ expect(fn(a, b)).to.be(a + b);
+ expect(fn(a, b, c)).to.be(a + b + c);
+ expect(fn(a, b, c, d)).to.be(a + b + c + d);
+ });
+
+ it("you should be able to apply functions", function () {
+ fn = function (fun) {
+ // you can only edit function body here
+ };
+
+ (function () {
+ var a = Math.random(), b = Math.random(), c = Math.random();
+
+ var wasITake2ArgumentsCalled = false;
+ var iTake2Arguments = function (firstArgument, secondArgument) {
+ expect(arguments.length).to.be(2);
+ expect(firstArgument).to.be(a);
+ expect(secondArgument).to.be(b);
+
+ wasITake2ArgumentsCalled = true;
+ };
+
+ var wasITake3ArgumentsCalled = false;
+ var iTake3Arguments = function (firstArgument, secondArgument, thirdArgument) {
+ expect(arguments.length).to.be(3);
+ expect(firstArgument).to.be(a);
+ expect(secondArgument).to.be(b);
+ expect(thirdArgument).to.be(c);
+
+ wasITake3ArgumentsCalled = true;
+ };
+
+ fn(iTake2Arguments, a, b);
+ fn(iTake3Arguments, a, b, c);
+ })();
+ });
+
+ it("you should be able to curry existing functions", function () {
+ fn = function (fun) {
+ // you can only edit function body here
+ };
+
+ var curryMe = function (x, y, z) {
+ return x / y * z;
+ };
+
+ var a = Math.random(), b = Math.random(), c = Math.random();
+ expect(fn(curryMe)(a, b, c)).to.be(curryMe(a, b, c));
+ expect(fn(curryMe, a)(b, c)).to.be(curryMe(a, b, c));
+ expect(fn(curryMe, a, b)(c)).to.be(curryMe(a, b, c));
+ expect(fn(curryMe, a, b, c)()).to.be(curryMe(a, b, c));
+ expect(fn(curryMe, a, b, c)()).to.be(curryMe(a, b, c));
+ expect(fn(curryMe, b, a, c)()).to.be(curryMe(b, a, c));
+ });
+
+ it('you should be able to use closures', function () {
+ var arr = [Math.random(), Math.random(), Math.random(), Math.random()];
+ var doSomeStuff;
+
+ fn = function (vals) {
+ // you can only edit function body here
+ };
+
+ doSomeStuff = function (x) { return x * x; };
+
+ var funcs = fn(arr);
+ expect(funcs).to.have.length(5);
+ for (var i = funcs.length - 1; i >= 0; i--) {
+ expect(funcs[i]()).to.be(doSomeStuff(arr[i]));
+ };
+ });
});
});
Something went wrong with that request. Please try again.