Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jphpsf committed Aug 29, 2012
0 parents commit 916baf8
Show file tree
Hide file tree
Showing 9 changed files with 3,371 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Jasmine data provider demo

## Background

This project is a demo for the data provider pattern applied to Jasmine.

It allows to DRY up Jasmine tests that needs to be run with multiple values.

```javascript
function using(name, values, func){
for (var i = 0, count = values.length; i < count; i++) {
if (Object.prototype.toString.call(values[i]) !== '[object Array]') {
values[i] = [values[i]];
}
func.apply(this, values[i]);
jasmine.currentEnv_.currentSpec.description += ' (with "' + name + '" using ' + values[i].join(', ') + ')';
}
}
```

# License

WTFPL
43 changes: 43 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="stylesheet" type="text/css" href="vendor/jasmine-1.2.0/jasmine.css">
<script type="text/javascript" src="vendor/jasmine-1.2.0/jasmine.js"></script>
<script type="text/javascript" src="vendor/jasmine-1.2.0/jasmine-html.js"></script>
<script type="text/javascript" src="lib/Demo.js"></script>
<script type="text/javascript" src="spec/SpecHelper.js"></script>
<script type="text/javascript" src="spec/DemoSpec.js"></script>
<script type="text/javascript">
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;

window.htmlReporter = new jasmine.HtmlReporter();

jasmineEnv.addReporter(htmlReporter);

jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};

var currentWindowOnload = window.onload;

window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};

function execJasmine() {
jasmineEnv.execute();
}

})();
</script>
</head>
<body>
</body>
</html>
16 changes: 16 additions & 0 deletions lib/Demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// to validate a username
function validateUserName(username) {
return typeof username === 'string' && username.match(/^[a-zA-Z0-9_]{3,12}$/);
}

// to convert "dashed-strings" to "DashedStrings"
function dashToCamelCase(string) {
return ("-"+string).replace(/\-([a-z])/g, function(match) {
return match.toUpperCase().replace('-','');
});
}

// to convert "DashedStrings" to "dashed-strings"
function camelCaseToDash(string) {
return string.replace(/([\w])([A-Z])/g,'$1-$2').toLowerCase();
}
33 changes: 33 additions & 0 deletions spec/DemoSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// A first simple test to demonstrate the data provider pattern

describe("username validation", function() {
using("valid values", ["abc", "longusername", "john_doe"], function(value){
it("should return true for valid usernames", function() {
expect(validateUserName(value)).toBeTruthy();
})
})

using("invalid values", ["ab", "name_too_long", "no spaces", "inv*alid"], function(value){
it("should return false for invalid usernames", function() {
expect(validateUserName(value)).toBeFalsy();
})
})
})


// A second example using multiple values

describe("camel case converter", function() {
using("dashes", [ ["foo", "Foo"], ["foo-bar", "FooBar"], ["foo-bar-baz", "FooBarBaz"]], function(value, expected){
it("should return camel case string", function() {
expect(dashToCamelCase(value)).toEqual(expected);
})
})

using("camel case", [ ["Foo", "foo"], ["FooBar", "foo-bar"], ["FooBarBaz", "foo-bar-baz"]], function(value, expected){
it("should return dashed string", function() {
expect(camelCaseToDash(value)).toEqual(expected);
})
})
})

10 changes: 10 additions & 0 deletions spec/SpecHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Data provider code
function using(name, values, func){
for (var i = 0, count = values.length; i < count; i++) {
if (Object.prototype.toString.call(values[i]) !== '[object Array]') {
values[i] = [values[i]];
}
func.apply(this, values[i]);
jasmine.currentEnv_.currentSpec.description += ' (with "' + name + '" using ' + values[i].join(', ') + ')';
}
}
20 changes: 20 additions & 0 deletions vendor/jasmine-1.2.0/MIT.LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2008-2011 Pivotal Labs

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit 916baf8

Please sign in to comment.