Permalink
Browse files

Added proper Qunit tests

* moved old tests to test/examples
* js for tests are .gitignored; must have coffeescript to compile/run tests/contribute.
  • Loading branch information...
1 parent 1b97554 commit 740b51da617fe7f49d820f0c24175408ecd8d3af @coderifous committed Apr 22, 2011
View
@@ -0,0 +1,3 @@
+test/localize_test.js
+test/qunit_setup.js
+
View
@@ -129,7 +129,7 @@ You can provide a callback if you want to augment or replace the default callbac
}
})
-See the tests for working examples.
+See the test/samples for working examples.
# Contributing
@@ -141,6 +141,10 @@ save changes to the coffee code under src/.
If you're interested in contributing, please fork the [repository](https://github.com/coderifous/jquery-localize),
make your changes, and send pull-requests.
+Tests use QUnit. Run them by serving from the root of the project and
+navigating to the test/ directory, which has an index.html that takes it from
+there.
+
Learn more about [how to fork](http://help.github.com/fork-a-repo/) and
[pull-requests](http://help.github.com/pull-requests/).
View
@@ -1,4 +0,0 @@
-TODO
-====
-
-* Replace simple page load tests with proper unit tests using JSUnit or QUnit
View
@@ -1 +1,2 @@
-coffee -wco build src/jquery.localize.coffee
+coffee -wco build src/*.coffee &
+coffee -wc test/*.coffee &
@@ -7,15 +7,13 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Localize Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Test localization...</h1>
- <p rel="localize[test.puts]">puts 2 + 2</p>
- <p rel="localize[test.array]">foo = %w(a b c)</p>
- <p rel="localize[test.iteration]">foo.each { |n| puts "Got #{n}" }</p>
- <p rel="localize[message]">It failed :(</p>
+ <p rel="localize[test.nested]">puts 2 + 2</p>
+ <p rel="localize[basic]">It failed :(</p>
<script type="text/javascript" charset="utf-8">
$(function(){
var opts = { language: "ja", pathPrefix: "/test/lang", skipLanguage: "en-US" };
@@ -7,14 +7,12 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Localize Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Test localization...</h1>
- <p rel="localize[test.puts]">puts 2 + 2</p>
- <p rel="localize[test.array]">foo = %w(a b c)</p>
- <p rel="localize[test.iteration]">foo.each { |n| puts "Got #{n}" }</p>
+ <p rel="localize[test.nested]">puts 2 + 2</p>
<input rel="localize[test.input]" type="text" value="fail" />
<select>
<optgroup rel="localize[test.optgroup]" label="Fail">
@@ -25,10 +23,10 @@
<img src="ruby_square.gif" alt="a square ruby" rel="localize[test.ruby_image]"/>
Ruby image should be round.
</p>
- <p rel="localize[message]">It failed :(</p>
+ <p rel="localize[basic]">It failed :(</p>
<script type="text/javascript" charset="utf-8">
$(function(){
- var opts = { language: "ja", pathPrefix: "lang", skipLanguage: "en-US" };
+ var opts = { language: "ja", pathPrefix: "../lang", skipLanguage: "en-US" };
$("[rel*=localize]").localize("test", opts)
})
</script>
@@ -7,21 +7,19 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Localize Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Test localization...</h1>
- <p rel="localize[test.puts]">puts 2 + 2</p>
- <p rel="localize[test.array]">foo = %w(a b c)</p>
- <p rel="localize[test.iteration]">foo.each { |n| puts "Got #{n}" }</p>
- <p rel="localize[message]">It failed :(</p>
+ <p rel="localize[test.nested]">puts 2 + 2</p>
+ <p rel="localize[basic]">It failed :(</p>
<script type="text/javascript" charset="utf-8">
$(function(){
- var opts = { language: "ja", pathPrefix: "lang", skipLanguage: "en-US" };
+ var opts = { language: "ja", pathPrefix: "../lang", skipLanguage: "en-US" };
opts.callback = function(data, defaultCallback) {
data.message = "Optional call back works."
- $("[rel*=message]").css({color: "#FF8833"})
+ $("[rel*=basic]").css({color: "#FF8833"})
defaultCallback(data);
}
$("[rel*=localize]").localize("test", opts)
@@ -7,18 +7,15 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Localize Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Test localization...</h1>
- <p rel="localize[test.puts]">puts 2 + 2</p>
- <p rel="localize[test.array]">foo = %w(a b c)</p>
- <p rel="localize[test.iteration]">foo.each { |n| puts "Got #{n}" }</p>
<p rel="localize[message]">It failed :(</p>
<script type="text/javascript" charset="utf-8">
$(function(){
- var opts = { language: "ja-XX", pathPrefix: "lang", skipLanguage: "en-US" };
+ var opts = { language: "ja-XX", pathPrefix: "../lang", skipLanguage: "en-US" };
$("[rel*=localize]").localize("test", opts)
})
</script>
File renamed without changes
File renamed without changes
@@ -7,12 +7,13 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Localize Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Test localization...</h1>
- <p rel="localize[message]">Should do nothing.</p>
+ <p rel="localize[en_message]">Should not load en lang file.</p>
+ <p rel="localize[en_us_message]">Should not load en-US lang file.</p>
<script type="text/javascript" charset="utf-8">
$(function(){
var opts = { language: "en", pathPrefix: "lang", skipLanguage: "en" };
View
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html dir="ltr" lang="en-US">
+<head>
+ <meta charset="utf-8" />
+ <title>Localize Test Suite</title>
+
+ <!--Test styles-->
+ <link href="vendor/qunit.css" media="screen" rel="stylesheet" type="text/css" />
+
+ <!--Application scripts-->
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../build/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
+
+ <!--Test scripts-->
+ <script type="text/javascript" src="vendor/qunit.js"></script>
+ <script type="text/javascript" src="qunit_setup.js"></script>
+ <script type="text/javascript" src="localize_test.js"></script>
+
+ <!--<style>-->
+ <!-- p { background-color: #ddd; padding: .5em; line-height: 1.5; }-->
+ <!--</style>-->
+</head>
+
+<body>
+ <h1 id="qunit-header">QUnit Test Suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+</body>
+</html>
+
@@ -0,0 +1 @@
+{ "path_prefix": "pathPrefix success" }
@@ -0,0 +1 @@
+{ "en_us_message": "en-US loaded" }
View
@@ -0,0 +1 @@
+{ "en_message": "en loaded" }
@@ -1,3 +1 @@
-{
- "message": "ja-XX was loaded!"
-}
+{ "message": "country code success" }
View
@@ -1,15 +1,13 @@
{
"test": {
- "puts": "System.out.println(\"foo\")",
- "array": "List foo = new List()",
- "iteration": "Iterator foo = foo.iterator()",
- "input": "success",
- "optgroup": "success",
- "option": "success",
+ "nested": "nested success",
+ "input": "input success",
+ "optgroup": "optgroup success",
+ "option": "option success",
"ruby_image": {
"src": "ruby_round.gif",
"alt": "a round ruby"
}
},
- "message": "It worked!"
+ "basic": "basic success"
}
View
@@ -0,0 +1,103 @@
+localizableTag = (tag, localizeKey, attributes) ->
+ t = $("<#{tag}>").attr("rel", "localize[#{localizeKey}]")
+ if attributes.text?
+ t.text(attributes.text)
+ delete attributes.text
+ if attributes.val?
+ t.val(attributes.val)
+ delete attributes.val
+ t.attr(k,v) for k, v of attributes
+ t
+
+module "Basic Usage"
+
+setup ->
+ @testOpts = language: "ja", pathPrefix: "lang"
+
+test "basic tag text substitution", ->
+ t = localizableTag("p", "basic", text: "basic fail")
+ t.localize("test", @testOpts)
+ equals t.text(), "basic success"
+
+test "basic tag text substitution with nested key", ->
+ t = localizableTag("p", "test.nested", text: "nested fail")
+ t.localize("test", @testOpts)
+ equals t.text(), "nested success"
+
+test "input tag value substitution", ->
+ t = localizableTag("input", "test.input", val: "input fail")
+ t.localize("test", @testOpts)
+ equals t.val(), "input success"
+
+test "image tag src and alt substitution", ->
+ t = localizableTag("img", "test.ruby_image", src: "ruby_square.gif", alt: "a square ruby")
+ t.localize("test", @testOpts)
+ equals t.attr("src"), "ruby_round.gif"
+ equals t.attr("alt"), "a round ruby"
+
+moreSetup ->
+ @t = $('
+ <select>
+ <optgroup rel="localize[test.optgroup]" label="optgroup fail">
+ <option rel="localize[test.option]" value="1">option fail</option>
+ </optgroup>
+ </select>
+ ')
+
+test "optgroup tag label substitution", ->
+ t = @t.find("optgroup")
+ t.localize("test", @testOpts)
+ equals t.attr("label"), "optgroup success"
+
+test "option tag text substitution", ->
+ t = @t.find("option")
+ t.localize("test", @testOpts)
+ equals t.text(), "option success"
+
+module "Options"
+
+test "pathPrefix loads lang files from custom path", ->
+ opts = language: "fo", pathPrefix: "/test/lang/custom"
+ t = localizableTag("p", "path_prefix", text: "pathPrefix fail")
+ t.localize("test", opts)
+ equals t.text(), "pathPrefix success"
+
+test "custom callback is fired", ->
+ opts = language: "ja", pathPrefix: "lang"
+ opts.callback = (data, defaultCallback) ->
+ data.custom_callback = "custom callback success"
+ defaultCallback(data)
+ t = localizableTag("p", "custom_callback", text: "custom callback fail")
+ t.localize("test", opts)
+ equals t.text(), "custom callback success"
+
+test "language with country code", ->
+ opts = language: "ja-XX", pathPrefix: "lang"
+ t = localizableTag("p", "message", text: "country code fail")
+ t.localize("test", opts)
+ equals t.text(), "country code success"
+
+module "Language optimization"
+
+test "skipping language using string match", ->
+ opts = language: "en", pathPrefix: "lang", skipLanguage: "en"
+ t = localizableTag("p", "en_message", text: "en not loaded")
+ t.localize("test", opts)
+ equals t.text(), "en not loaded"
+
+test "skipping language using regex match", ->
+ opts = language: "en-US", pathPrefix: "lang", skipLanguage: /^en/
+ t = localizableTag("p", "en_us_message", text: "en-US not loaded")
+ t.localize("test", opts)
+ equals t.text(), "en-US not loaded"
+
+test "skipping language using array match", ->
+ opts = language: "en", pathPrefix: "lang", skipLanguage: ["en", "en-US"]
+ t = localizableTag("p", "en_message", text: "en not loaded")
+ t.localize("test", opts)
+ equals t.text(), "en not loaded"
+
+ opts = language: "en-US", pathPrefix: "lang", skipLanguage: ["en", "en-US"]
+ t = localizableTag("p", "en_us_message", text: "en-US not loaded")
+ t.localize("test", opts)
+ equals t.text(), "en-US not loaded"
View
@@ -0,0 +1,35 @@
+setupFn = -> null
+teardownFn = -> null
+window.setup = (fn) -> setupFn = fn
+window.teardown = (fn) -> teardownFn = fn
+
+window.moreSetup = (fn) ->
+ origSetup = setupFn
+ setup ->
+ origSetup.call(this)
+ fn.call(this)
+
+window.moreTeardown = (fn) ->
+ origTeardown = teardownFn
+ teardown ->
+ fn.call(this)
+ origTeardown.call(this)
+
+window.clearSetup = ->
+ setup -> null
+ teardown -> null
+
+originalModule = window.module
+window.module = (description) ->
+ clearSetup()
+ originalModule(description)
+
+originalTest = window.test
+window.test = (description, testFn) ->
+ setupSnapshot = setupFn
+ teardownSnapshot = teardownFn
+ originalTest description, ->
+ context = {}
+ setupSnapshot.call(context)
+ testFn.call(context)
+ teardownSnapshot.call(context)
Oops, something went wrong.

0 comments on commit 740b51d

Please sign in to comment.