Skip to content

Commit

Permalink
Merge pull request #78 from typekit/bs-jasmine
Browse files Browse the repository at this point in the history
Convert unit tests to Jasmine
  • Loading branch information
bramstein committed Feb 20, 2013
2 parents d507d71 + d865b6b commit 90b3933
Show file tree
Hide file tree
Showing 51 changed files with 6,719 additions and 3,734 deletions.
10 changes: 2 additions & 8 deletions Gemfile
@@ -1,9 +1,3 @@
source :gemcutter
source :rubygems

gem "rake"

group :demos do
gem "rack"
gem "sinatra"
gem "vegas"
end
gemspec
63 changes: 31 additions & 32 deletions README.md
Expand Up @@ -5,7 +5,6 @@ WebFont Loader gives you added control when using linked fonts via
the source, then adds a standard set of events you may use to control the
loading experience.


## Get Started

(These samples use WebFont Loader hosted on [Google's AJAX Libraries][gajax].)
Expand Down Expand Up @@ -36,7 +35,6 @@ Alternatively, load fonts from Typekit. Just specify your Kit ID.
Learn more about the
[modules][mod].


## Do More

WebFont Loader gives you control over how fonts are loaded. If you're
Expand Down Expand Up @@ -91,65 +89,67 @@ WebFont Loader aims to provide a common interface for font loading. Today it
works with Google, Typekit, Ascender, Fontdeck, Fonts.com Web fonts and your own
CSS. Learn more in [transitions][trn].

## Problems?

## More to see
Please open [an issue][issues]. Sample pages are greatly appreciated.

A full suite of demo pages is included in this source. Here you can find some
live examples using the JS and CSS events.
## Developing

To view the demos, just boot up our demo server and start browsing.
Is there something else WebFont Loader should do? Did you find a bug and want
to fix it?

$ rake demo
### Installing development requirements

You may need a few rubygems to run the server. Get them with [Bundler](http://gembundler.com/).
You'll need a few rubygems to run the tests, demo server, and other rake tasks, which should be installed with [Bundler](http://gembundler.com/).

$ gem install bundler
$ bundle install

Browse the demos [source code][demos].


## Problems?
To run the tests in a headless WebKit you will also need to have [PhantomJS](http://www.phantomjs.org) installed. You can install PhantomJS by downloading a binary of using HomeBrew.

Please open [an issue][issues]. Sample pages are greatly appreciated.
$ brew install phantomjs

### Building

## Developing
To build a JS file from source, just run rake:

Is there something else WebFont Loader should do? Did you find a bug and want
to fix it?
$ rake

### Building
### Demos

Run rake:
A full suite of demo pages is included in this source. Here you can find some
live examples using the JS and CSS events. Run the demo server with:

rake
$ rake demo

### Testing
You can also run the demos with uncompressed, debuggable code to aid in
development. Just start the server in dev mode:

You can run uncompressed, debuggable code by starting the demo server in dev mode:
$ rake demodev

rake demodev
Browse the demos [source code][demos].

WebFont Loader has an extensive test suite that runs via
[jsTestDriver][jstestdriver]. Please add tests for any changes.
### Testing

To run tests, first boot the test server. Then open a browser and navigate to
the test server url listed by `rake test` to start listing for test executions.
You can register multiple browsers.
WebFont Loader has an extensive test suite that runs via Jasmine. The test suite
should be passing before submitting a pull request, and new tests should be added
for any new functionality.

rake test:boot
To run tests, open up `spec/index.html` in a browser and check the results. The
test suite will run automatically. Again, before submitting a pull request
please run the test suite in multiple browsers and list them in the pull request.

Then, run the tests.
To run tests in a headless WebKit using [PhantomJS](http://www.phantomjs.org) run:

rake test
$ rake test

## Contributing

* Fork webfontloader
* Create a topic branch - `git checkout -b my_branch`
* Push to your branch - `git push origin my_branch`
* Create an [issue][issues] with a link to your branch
* Make sure all tests are passing
* Create a pull request for your branch
* That's it!


Expand All @@ -173,5 +173,4 @@ WebFont Loader is released under the [Apache 2.0][lic] license.
[demos]: http://github.com/typekit/webfontloader/blob/master/lib/webfontloader/demo/public
[gfontapi]: https://code.google.com/apis/webfonts/
[gajax]: http://code.google.com/apis/ajaxlibs/
[jstestdriver]: http://code.google.com/p/js-test-driver/
[issues]: https://github.com/typekit/webfontloader/issues
35 changes: 2 additions & 33 deletions Rakefile
Expand Up @@ -63,11 +63,6 @@ require 'webfontloader'
CLEAN.include("target")
CLEAN.include("tmp")

# JsTestDriver
JsTestPort = "9876"
JsTestServer = "http://localhost:#{JsTestPort}"
JsTestJar = "tools/jstestdriver/JsTestDriver-1.2.1.jar"

# JsCompiler
JsCompilerJar = "tools/compiler/compiler.jar"

Expand All @@ -85,15 +80,6 @@ SourceJs = FileList["src/**/*"]
directory "target"
directory "tmp"

file "tmp/jsTestDriver.conf" => AllJs + ["tmp"] do |t|
require 'yaml'
config = {
"server" => JsTestServer,
"load" => (@modules.all_source_files + @modules.all_test_globs).map { |f| "../#{f}" }
}
File.open(t.name, "w") { |f| YAML.dump(config, f) }
end

desc "Compile the JavaScript into target/webfont.js"
task :compile => "target/webfont.js"

Expand Down Expand Up @@ -143,26 +129,9 @@ end
desc "Test everything"
task :default => [:clean, :gzipbytes, :test]

namespace :test do
task :server do
system "java -jar #{JsTestJar} --port #{JsTestPort}"
end
task :capture do
system "open #{JsTestServer}/capture?strict"
end
desc "Execute tests against a running server"
task :run => ["tmp/jsTestDriver.conf"] do |t|
config = t.prerequisites.first
system "java -jar #{JsTestJar} --config #{config} --server #{JsTestServer} --tests all --captureConsole --verbose"
end
desc "Boot the test server and capture a browser"
multitask :boot => ['test:server', 'test:capture']
end

desc "Run all tests"
task :test => ["tmp/jsTestDriver.conf"] do |t|
config = t.prerequisites.first
system "java -jar #{JsTestJar} --port #{JsTestPort} --config #{config} --server #{JsTestServer} --browser open --tests all --captureConsole --verbose"
task :test do |t|
exec "phantomjs tools/jasmine-phantomjs/jasmine-phantomjs.js spec/index.html"
end

desc "Start the demo server"
Expand Down
43 changes: 43 additions & 0 deletions spec/ascender/ascenderscript_spec.js
@@ -0,0 +1,43 @@
describe('AscenderScript', function () {
var AscenderScript = webfont.AscenderScript;

var configuration = {
key: 'ec2de397-11ae-4c10-937f-bf94283a70c1',
families: ['AndyBold', 'Arial:bold,regular']
};

describe('load family and variations', function () {
var fakeDomHelper = null,
fakeOnReady = null,
ascenderScript = null;

beforeEach(function () {
fakeDomHelper = {
insertInto: jasmine.createSpy('insertInto'),
createCssLink: jasmine.createSpy('createCssLink'),
getProtocol: jasmine.createSpy('getProtocol').andReturn('http:')
};

fakeOnReady = jasmine.createSpy('onReady');

ascenderScript = new AscenderScript(fakeDomHelper, configuration);
ascenderScript.load(fakeOnReady);
});

it('should create the link correctly', function () {
expect(fakeDomHelper.createCssLink).toHaveBeenCalledWith('http://webfonts.fontslive.com/css/ec2de397-11ae-4c10-937f-bf94283a70c1.css');
expect(fakeDomHelper.insertInto.calls[0].args[0]).toEqual('head');
});

it('should parse variations correctly', function () {
expect(ascenderScript.parseVariations('regular')).toEqual(['n4']);
expect(ascenderScript.parseVariations('bold')).toEqual(['n7']);
expect(ascenderScript.parseVariations('italic')).toEqual(['i4']);
expect(ascenderScript.parseVariations('bolditalic')).toEqual(['i7']);
expect(ascenderScript.parseVariations('regular,')).toEqual(['n4']);
expect(ascenderScript.parseVariations('regular,bold')).toEqual(['n4', 'n7']);
expect(ascenderScript.parseVariations('regular,,bold')).toEqual(['n4', 'n7']);
expect(ascenderScript.parseVariations('n4,n7')).toEqual(['n4', 'n7']);
});
})
});
42 changes: 42 additions & 0 deletions spec/core/cssclassname_spec.js
@@ -0,0 +1,42 @@
describe('CssClassName', function () {
var CssClassName = webfont.CssClassName,
sanitizer = new CssClassName();

describe('#sanitize', function () {
it('should sanitize spaces in names', function () {
expect(sanitizer.sanitize(' My Family ')).toEqual('myfamily');
});

it('should sanitize numbers in names', function () {
expect(sanitizer.sanitize('99 My Family 99')).toEqual('99myfamily99');;
});

it('should sanitize other characters', function () {
expect(sanitizer.sanitize('_My+Family!-')).toEqual('myfamily');
});
});

describe('#build', function () {
it('should build many parts', function () {
expect(sanitizer.build('pre_', 'My Family', '_post')).toEqual('pre-myfamily-post');
});

it('should build some parts', function () {
expect(sanitizer.build('pre!', 'My Family')).toEqual('pre-myfamily');
});
});

describe('#constructor', function () {
it('should use a hyphen as a default separator', function () {
var sanitizer = new CssClassName();

expect(sanitizer.build('pre', 'post')).toEqual('pre-post');
});

it('should use the configured separator', function () {
var sanitizer = new CssClassName('_');

expect(sanitizer.build('pre', 'post')).toEqual('pre_post');
});
});
});
38 changes: 38 additions & 0 deletions spec/core/cssfontfamilyname_spec.js
@@ -0,0 +1,38 @@
describe('CssFontFamilyName', function () {
var CssFontFamilyName = webfont.CssFontFamilyName,
sanitizer = new CssFontFamilyName();

describe('#quote', function () {
it('should quote names with spaces', function () {
expect(sanitizer.quote('My Family')).toEqual("'My Family'");
});

it('should quote names with spaces and double quotes', function () {
expect(sanitizer.quote('"My Family"')).toEqual("'My Family'");
});

it('should quote names with spaces and single quotes', function () {
expect(sanitizer.quote("'My Family'")).toEqual("'My Family'");
});

it('should quote multiple single quoted names separated with a comma', function () {
expect(sanitizer.quote("'family 1','family 2'")).toEqual("'family 1','family 2'");
});

it('should quote multiple single quoted names separated with a comma and space', function () {
expect(sanitizer.quote("'family 1', 'family 2'")).toEqual("'family 1','family 2'");
});

it('should not quote when there is no space', function () {
expect(sanitizer.quote('MyFamily')).toEqual('MyFamily');
});

it('should remove quotes when they are unnecesssary', function () {
expect(sanitizer.quote('"MyFamily"')).toEqual('MyFamily');
});

it('should not quote multiple names when there is no space', function () {
expect(sanitizer.quote("'family-1', 'family-2'")).toEqual('family-1,family-2');
});
});
});

0 comments on commit 90b3933

Please sign in to comment.