Skip to content

Instantly share code, notes, and snippets.

@rachelmyers
Created January 4, 2011 07:51
Show Gist options
  • Save rachelmyers/764513 to your computer and use it in GitHub Desktop.
Save rachelmyers/764513 to your computer and use it in GitHub Desktop.
Beginnings of an introduction to test-first javascript development inspired by Jim Weirich's Ruby Koans.
// Written by Rachel Myers as a introduction to test-first javascript development.
// Inspired by and very nearly lifted from Jim Weirich's Ruby Koans, which are awesome.
describe("Strings", function() {
describe("Creating Strings", function() {
var double_quote_string;
var single_quote_string;
var talking_string;
var apostrophe_string;
it("should create a new string with double quotes", function(){
expect(double_quote_string).toEqual("We have acrobats!");
});
it("should create a string with single quotes", function(){
expect(single_quote_string).toEqual('We have clowns!');
});
it("should use single quotes to contain double quotes", function(){
expect(talking_string).toEqual('I said, "We have clowns!"');
});
it("should use double quotes to contain single quotes", function(){
expect(apostrophe_string).toEqual("It's a limited engagement.");
});
});
describe("Character and Substring Access", function() {
var character;
var no_such_character;
var index;
var no_such_index;
var none_such_index;
beforeEach(function() {
double_quote_string = "We have acrobats!"; // Unnecessary if previous tests passed.
});
it("should use charAt to access a character from a string", function(){
expect(character).toEqual("a");
// looking for: double_quote_string.charAt(4)
});
it("returns empty string if index is larger than string", function(){
expect(no_such_character).toEqual("");
// looking for something like: double_quote_string.charAt(17)
});
it("should use indexOf to find index of 'bats' substring", function(){
expect(index).toEqual(12);
// looking for: double_quote_string.indexOf("bats")
});
it ("should return -1 if there's substring doesn't exist", function(){
expect(no_such_index).toEqual(-1);
//looking for something like: double_quote_string.indexOf(birds)
});
it ("show indexOf is will not find 'Bats", function(){
expect(none_such_index).toEqual(-1);
//looking for: double_quote_string.indexOf(Bats)
});
});
describe("Concatenation and Splitting", function() {
var question = "And do you know what else?";
var answer = "We also have lots of clowns.";
var elaboration = "How do they all fit in that car?";
var concatenation;
var long_concatenation;
var split_answer;
var no_such_split;
var strangely_split_answer;
it ("should concatenate two strings", function(){
expect(concatenation).toEqual("And do you know what else? We also have lots of clowns.");
//looking for: concat(question, answer)
});
it ("should concatenate many strings", function(){
expect(long_concatenation).toEqual("And do you know what else? We also have lots of clowns. How do they all fit in that car?");
//looking for: concat(question, answer, elaboration)
});
it ("should split strings", function(){
expect(split_answer).toEqual(["We", "also", "have", "lots", "of", "clowns."]);
//looking for: answer.split()
});
it ("should split an empty string into an array containing an empty string", function(){
expect(no_such_split).toEqual([""]);
//looking for: "".split()
});
it ("should split strings by any character we want", function(){
expect(answer_split_by_o).toEqual(["We als", "have l", "ts", "fcl", "wns."]);
//looking for: answer.split(["o"])
});
});
describe("Formating Strings", function() {
var statement = " I love the circus! ";
var screaming;
var whispering;
var trimmed;
it ("should make a statement uppercase", function(){
expect(screaming).toEqual(" I LOVE THE CIRCUS! ");
//looking for: sentence.toUpperCase()
});
it ("should make a statement lowercase", function(){
expect(whispering).toEqual(" i love the circus! ");
//looking for: sentence.toLowerCase()
});
it ("should remove white space", function(){
expect(trimmed).toEqual("I love the circus!");
//looking for: sentence.trim()
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment