mirror of
https://github.com/wassname/side-by-side.git
synced 2026-09-09 11:34:24 +08:00
Make test suite nicer
This commit is contained in:
@@ -9,7 +9,7 @@ angular.module("sideBySide").factory("transformer", () ->
|
||||
#
|
||||
# @param translations [array] Translations to check
|
||||
checkLengths = (translations) ->
|
||||
if translations.length < 1
|
||||
if !translations or translations.length < 1
|
||||
throw "No poems found!"
|
||||
lengths = []
|
||||
for translation in translations
|
||||
|
||||
@@ -1,8 +1 @@
|
||||
this.injector = angular.injector(['ng', 'sideBySide'])
|
||||
|
||||
init = {
|
||||
setup: () ->
|
||||
this.$scope = injector.get('$rootScope').$new();
|
||||
}
|
||||
|
||||
this.module('tests', init);
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
test("poems service has default content", () ->
|
||||
poems = injector.get("poems")
|
||||
poems = {}
|
||||
|
||||
module "poems", {
|
||||
setup: () ->
|
||||
poems = injector.get "poems"
|
||||
}
|
||||
|
||||
test "has default content", () ->
|
||||
active = poems.getActive()
|
||||
equal(active[0].content[0].text, 'Please be patient!')
|
||||
equal(active[0].content[0].section, 'Loading...')
|
||||
)
|
||||
equal active[0].content[0].text, 'Please be patient!'
|
||||
equal active[0].content[0].section, 'Loading...'
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
this.testReader = (name, testCases) ->
|
||||
reader = injector.get(name)
|
||||
|
||||
for testCase in testCases
|
||||
deepEqual(reader(testCase.tested), testCase.expected)
|
||||
@@ -1,9 +1,18 @@
|
||||
test("readerFactory", () ->
|
||||
factory = injector.get("readerFactory")
|
||||
factory = {}
|
||||
|
||||
ok(factory("somefile.md"))
|
||||
ok(factory("otherfile.json"))
|
||||
throws(() ->
|
||||
factory("microsoft.doc")
|
||||
, /unknown/i)
|
||||
)
|
||||
module "reader factory", {
|
||||
setup: () ->
|
||||
factory = injector.get "readerFactory"
|
||||
}
|
||||
|
||||
test "reads markdown", () ->
|
||||
ok factory "somefile.md"
|
||||
ok factory "otherfile.markdown"
|
||||
|
||||
test "reads json", () ->
|
||||
ok factory "fried.json"
|
||||
|
||||
test "doesn't read doc", () ->
|
||||
throws () ->
|
||||
factory "microsoft.doc"
|
||||
, /unknown/i
|
||||
|
||||
@@ -1,29 +1,32 @@
|
||||
test("jsonReader", () ->
|
||||
testCases = [{
|
||||
tested: """
|
||||
{
|
||||
meta: {
|
||||
Title: "Test leading text"
|
||||
},
|
||||
content: [
|
||||
"<p><strong>This</strong> is leading text.</p>",
|
||||
{ section: "I", text: "<p>First section.</p>" },
|
||||
{ section: "II", text: "<p>Second section.</p>" },
|
||||
{ text: "<p>Another section.</p>" },
|
||||
],
|
||||
}"""
|
||||
expected: {
|
||||
reader = {}
|
||||
|
||||
module "json reader", {
|
||||
setup: () ->
|
||||
reader = injector.get "jsonReader"
|
||||
}
|
||||
|
||||
test "loads poem", () ->
|
||||
deepEqual reader("""
|
||||
{
|
||||
meta: {
|
||||
Title: "Test leading text"
|
||||
}
|
||||
},
|
||||
content: [
|
||||
{ section: "", text: "<p><strong>This</strong> is leading text.</p>" }
|
||||
{ section: "I", text: "<p>First section.</p>" }
|
||||
{ section: "II", text: "<p>Second section.</p>" }
|
||||
{ section: "", text: "<p>Another section.</p>" }
|
||||
]
|
||||
"<p><strong>This</strong> is leading text.</p>",
|
||||
{ section: "I", text: "<p>First section.</p>" },
|
||||
{ section: "II", text: "<p>Second section.</p>" },
|
||||
{ text: "<p>Another section.</p>" },
|
||||
],
|
||||
}
|
||||
}]
|
||||
|
||||
testReader("jsonReader", testCases)
|
||||
)
|
||||
"""),
|
||||
{
|
||||
meta: {
|
||||
Title: "Test leading text"
|
||||
}
|
||||
content: [
|
||||
{ section: "", text: "<p><strong>This</strong> is leading text.</p>" }
|
||||
{ section: "I", text: "<p>First section.</p>" }
|
||||
{ section: "II", text: "<p>Second section.</p>" }
|
||||
{ section: "", text: "<p>Another section.</p>" }
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,104 +1,108 @@
|
||||
test("markdownReader", () ->
|
||||
testCases = [{
|
||||
tested: """
|
||||
Title: Universal Declaration of Human Rights
|
||||
Author: John Peters Humphrey
|
||||
Date: 1948
|
||||
Source: http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights
|
||||
reader = {}
|
||||
|
||||
## 1
|
||||
All human beings are born *free* and equal in dignity and rights...
|
||||
module "markdown reader", {
|
||||
setup: () ->
|
||||
reader = injector.get "markdownReader"
|
||||
}
|
||||
|
||||
## 2
|
||||
Everyone is entitled to all the rights and freedoms...
|
||||
test "loads poem with section numbers, paragraphs, and a list", () ->
|
||||
deepEqual reader("""
|
||||
Title: Universal Declaration of Human Rights
|
||||
Author: John Peters Humphrey
|
||||
Date: 1948
|
||||
Source: http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights
|
||||
|
||||
Furthermore, no distinction shall be made on the basis of...
|
||||
## 1
|
||||
All human beings are born *free* and equal in dignity and rights...
|
||||
|
||||
## 11.1
|
||||
Everyone charged with a penal offence has the right to be presumed...
|
||||
## 2
|
||||
Everyone is entitled to all the rights and freedoms...
|
||||
|
||||
## 11.2
|
||||
No one shall be held guilty of any penal offence...
|
||||
Furthermore, no distinction shall be made on the basis of...
|
||||
|
||||
## 13
|
||||
Everyone is entitled to:
|
||||
## 11.1
|
||||
Everyone charged with a penal offence has the right to be presumed...
|
||||
|
||||
- sex
|
||||
- drugs
|
||||
- rock & roll
|
||||
"""
|
||||
expected: {
|
||||
meta: {
|
||||
Title: "Universal Declaration of Human Rights"
|
||||
Author: "John Peters Humphrey"
|
||||
Date: "1948"
|
||||
Source: '<a href="http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights">http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights</a>'
|
||||
}
|
||||
content: [
|
||||
{ section: "1", text: "<p>All human beings are born <em>free</em> and equal in dignity and rights…</p>\n" }
|
||||
{ section: "2", text: """
|
||||
<p>Everyone is entitled to all the rights and freedoms…</p>
|
||||
<p>Furthermore, no distinction shall be made on the basis of…</p>\n""" }
|
||||
{ section: "11.1", text: "<p>Everyone charged with a penal offence has the right to be presumed…</p>\n" }
|
||||
{ section: "11.2", text: "<p>No one shall be held guilty of any penal offence…</p>\n" }
|
||||
{ section: "13", text: """
|
||||
<p>Everyone is entitled to:</p>
|
||||
<ul>
|
||||
<li>sex</li>
|
||||
<li>drugs</li>
|
||||
<li>rock & roll</li>
|
||||
</ul>\n""" }
|
||||
]
|
||||
## 11.2
|
||||
No one shall be held guilty of any penal offence...
|
||||
|
||||
## 13
|
||||
Everyone is entitled to:
|
||||
|
||||
- sex
|
||||
- drugs
|
||||
- rock & roll
|
||||
"""),
|
||||
{
|
||||
meta: {
|
||||
Title: "Universal Declaration of Human Rights"
|
||||
Author: "John Peters Humphrey"
|
||||
Date: "1948"
|
||||
Source: '<a href="http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights">http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights</a>'
|
||||
}
|
||||
}, {
|
||||
tested: """
|
||||
Title: Test leading text and mixed separators
|
||||
content: [
|
||||
{ section: "1", text: "<p>All human beings are born <em>free</em> and equal in dignity and rights…</p>\n" }
|
||||
{ section: "2", text: """
|
||||
<p>Everyone is entitled to all the rights and freedoms…</p>
|
||||
<p>Furthermore, no distinction shall be made on the basis of…</p>\n""" }
|
||||
{ section: "11.1", text: "<p>Everyone charged with a penal offence has the right to be presumed…</p>\n" }
|
||||
{ section: "11.2", text: "<p>No one shall be held guilty of any penal offence…</p>\n" }
|
||||
{ section: "13", text: """
|
||||
<p>Everyone is entitled to:</p>
|
||||
<ul>
|
||||
<li>sex</li>
|
||||
<li>drugs</li>
|
||||
<li>rock & roll</li>
|
||||
</ul>\n""" }
|
||||
]
|
||||
}
|
||||
|
||||
**This** is leading text.
|
||||
# I
|
||||
First section.
|
||||
test "loads poem with leading text and mixed separators", () ->
|
||||
deepEqual reader("""
|
||||
Title: Test leading text and mixed separators
|
||||
|
||||
---
|
||||
Second section.
|
||||
"""
|
||||
expected: {
|
||||
meta: {
|
||||
Title: "Test leading text and mixed separators"
|
||||
}
|
||||
content: [
|
||||
{ section: "", text: "<p><strong>This</strong> is leading text.</p>\n" }
|
||||
{ section: "I", text: "<p>First section.</p>\n" }
|
||||
{ section: "", text: "<p>Second section.</p>\n" }
|
||||
]
|
||||
**This** is leading text.
|
||||
# I
|
||||
First section.
|
||||
|
||||
---
|
||||
Second section.
|
||||
"""),
|
||||
{
|
||||
meta: {
|
||||
Title: "Test leading text and mixed separators"
|
||||
}
|
||||
}, {
|
||||
tested: """
|
||||
Title: Separator test
|
||||
Separator: heading
|
||||
content: [
|
||||
{ section: "", text: "<p><strong>This</strong> is leading text.</p>\n" }
|
||||
{ section: "I", text: "<p>First section.</p>\n" }
|
||||
{ section: "", text: "<p>Second section.</p>\n" }
|
||||
]
|
||||
}
|
||||
|
||||
# I
|
||||
First section.
|
||||
test "loads poem using a custom separator", () ->
|
||||
deepEqual reader("""
|
||||
Title: Separator test
|
||||
Separator: heading
|
||||
|
||||
---
|
||||
Second part.
|
||||
# I
|
||||
First section.
|
||||
|
||||
# II
|
||||
Second section.
|
||||
"""
|
||||
expected: {
|
||||
meta: {
|
||||
Title: "Separator test"
|
||||
Separator: "heading"
|
||||
}
|
||||
content: [
|
||||
{ section: "I", text: """
|
||||
<p>First section.</p>
|
||||
<hr>
|
||||
<p>Second part.</p>\n""" }
|
||||
{ section: "II", text: "<p>Second section.</p>\n" }
|
||||
]
|
||||
---
|
||||
Second part.
|
||||
|
||||
# II
|
||||
Second section.
|
||||
"""),
|
||||
{
|
||||
meta: {
|
||||
Title: "Separator test"
|
||||
Separator: "heading"
|
||||
}
|
||||
}]
|
||||
|
||||
testReader("markdownReader", testCases)
|
||||
)
|
||||
content: [
|
||||
{ section: "I", text: """
|
||||
<p>First section.</p>
|
||||
<hr>
|
||||
<p>Second part.</p>\n""" }
|
||||
{ section: "II", text: "<p>Second section.</p>\n" }
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,128 +1,144 @@
|
||||
test("transformer", () ->
|
||||
english = {
|
||||
meta: {
|
||||
Title: "Ten Commandments"
|
||||
Author: "God knows"
|
||||
transformer = {}
|
||||
english = {}
|
||||
czech = {}
|
||||
polish = {}
|
||||
expected = {}
|
||||
|
||||
module "transformer", {
|
||||
setup: () ->
|
||||
transformer = injector.get "transformer"
|
||||
|
||||
english = {
|
||||
meta: {
|
||||
Title: "Ten Commandments"
|
||||
Author: "God knows"
|
||||
}
|
||||
content: [{
|
||||
section: "1"
|
||||
text: "Thou shalt have no other gods before me"
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Thou shalt not take the name of the Lord thy God in vain"
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Remember the sabbath day, to keep it holy"
|
||||
}]
|
||||
}
|
||||
content: [{
|
||||
section: "1"
|
||||
text: "Thou shalt have no other gods before me"
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Thou shalt not take the name of the Lord thy God in vain"
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Remember the sabbath day, to keep it holy"
|
||||
}]
|
||||
}
|
||||
|
||||
czech = {
|
||||
meta: {
|
||||
Title: "Desatero"
|
||||
Author: "Ví bůh"
|
||||
czech = {
|
||||
meta: {
|
||||
Title: "Desatero"
|
||||
Author: "Ví bůh"
|
||||
}
|
||||
content: [{
|
||||
section: "1"
|
||||
text: "V jednoho Boha věřiti budeš."
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Nevezmeš jména Božího nadarmo."
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Pomni, abys den sváteční světil."
|
||||
}]
|
||||
}
|
||||
content: [{
|
||||
section: "1"
|
||||
text: "V jednoho Boha věřiti budeš."
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Nevezmeš jména Božího nadarmo."
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Pomni, abys den sváteční světil."
|
||||
}]
|
||||
}
|
||||
|
||||
polish = {
|
||||
meta: {
|
||||
Title: "Dekalog"
|
||||
Author: "Bóg wie"
|
||||
polish = {
|
||||
meta: {
|
||||
Title: "Dekalog"
|
||||
Author: "Bóg wie"
|
||||
}
|
||||
content: [{
|
||||
section: "1"
|
||||
text: "Nie będziesz miał bogów cudzych przede mną."
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Nie będziesz wzywał imienia Boga twego nadaremno."
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Pamiętaj, abyś dzień święty święcił."
|
||||
}]
|
||||
}
|
||||
content: [{
|
||||
section: "1"
|
||||
text: "Nie będziesz miał bogów cudzych przede mną."
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Nie będziesz wzywał imienia Boga twego nadaremno."
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Pamiętaj, abyś dzień święty święcił."
|
||||
}]
|
||||
}
|
||||
|
||||
expected = {
|
||||
meta: [{
|
||||
Title: "Ten Commandments"
|
||||
Author: "God knows"
|
||||
}, {
|
||||
Title: "Desatero"
|
||||
Author: "Ví bůh"
|
||||
}, {
|
||||
Title: "Dekalog"
|
||||
Author: "Bóg wie"
|
||||
}]
|
||||
expected = {
|
||||
meta: [{
|
||||
Title: "Ten Commandments"
|
||||
Author: "God knows"
|
||||
}, {
|
||||
Title: "Desatero"
|
||||
Author: "Ví bůh"
|
||||
}, {
|
||||
Title: "Dekalog"
|
||||
Author: "Bóg wie"
|
||||
}]
|
||||
|
||||
verses: [[{
|
||||
section: "1"
|
||||
text: "Thou shalt have no other gods before me"
|
||||
}, {
|
||||
section: "1"
|
||||
text: "V jednoho Boha věřiti budeš."
|
||||
}, {
|
||||
section: "1"
|
||||
text: "Nie będziesz miał bogów cudzych przede mną."
|
||||
}], [{
|
||||
section: "2"
|
||||
text: "Thou shalt not take the name of the Lord thy God in vain"
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Nevezmeš jména Božího nadarmo."
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Nie będziesz wzywał imienia Boga twego nadaremno."
|
||||
}], [{
|
||||
section: "3"
|
||||
text: "Remember the sabbath day, to keep it holy"
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Pomni, abys den sváteční světil."
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Pamiętaj, abyś dzień święty święcił."
|
||||
}]]
|
||||
}
|
||||
verses: [[{
|
||||
section: "1"
|
||||
text: "Thou shalt have no other gods before me"
|
||||
}, {
|
||||
section: "1"
|
||||
text: "V jednoho Boha věřiti budeš."
|
||||
}, {
|
||||
section: "1"
|
||||
text: "Nie będziesz miał bogów cudzych przede mną."
|
||||
}], [{
|
||||
section: "2"
|
||||
text: "Thou shalt not take the name of the Lord thy God in vain"
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Nevezmeš jména Božího nadarmo."
|
||||
}, {
|
||||
section: "2"
|
||||
text: "Nie będziesz wzywał imienia Boga twego nadaremno."
|
||||
}], [{
|
||||
section: "3"
|
||||
text: "Remember the sabbath day, to keep it holy"
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Pomni, abys den sváteční světil."
|
||||
}, {
|
||||
section: "3"
|
||||
text: "Pamiętaj, abyś dzień święty święcił."
|
||||
}]]
|
||||
}
|
||||
}
|
||||
|
||||
# Test transformation works as it should
|
||||
transformer = injector.get("transformer")
|
||||
deepEqual(transformer([english, czech, polish]), expected)
|
||||
test "transforms", () ->
|
||||
deepEqual transformer([english, czech, polish]), expected
|
||||
|
||||
# Test transformation still works when we remove Polish
|
||||
test "transforms with 2 items", () ->
|
||||
expected.meta = expected.meta.slice(0, 2)
|
||||
expected.verses = _.map(expected.verses, (translations) ->
|
||||
return translations.slice(0, 2)
|
||||
)
|
||||
deepEqual(transformer([english, czech]), expected)
|
||||
deepEqual transformer([english, czech]), expected
|
||||
|
||||
# Test empty throws an error
|
||||
throws(() ->
|
||||
test "throws error on no param", () ->
|
||||
throws () ->
|
||||
transformer()
|
||||
)
|
||||
, /no poems found/i
|
||||
|
||||
# Czech is shortest
|
||||
test "throws error on empty array", () ->
|
||||
throws () ->
|
||||
transformer []
|
||||
, /no poems found/i
|
||||
|
||||
test "throws error on poem being too short", () ->
|
||||
czech.content.pop()
|
||||
throws(() ->
|
||||
transformer([english, czech, polish])
|
||||
, /missing.*nevezmeš jména/i)
|
||||
throws () ->
|
||||
transformer [english, czech, polish]
|
||||
, /missing.*nevezmeš jména/i
|
||||
|
||||
# English is longest
|
||||
test "throws error on poem being too long", () ->
|
||||
czech.content.pop()
|
||||
polish.content.pop()
|
||||
throws(() ->
|
||||
transformer([english, czech, polish])
|
||||
, /superfluous.*sabbath/i)
|
||||
throws () ->
|
||||
transformer [english, czech, polish]
|
||||
, /superfluous.*sabbath/i
|
||||
|
||||
# Czech and Polish too short plus different lengths
|
||||
test "throws error on poems too short plus different lengths", () ->
|
||||
czech.content.pop()
|
||||
polish.content.pop()
|
||||
throws(() ->
|
||||
transformer([english, czech, polish])
|
||||
, /missing.*bogów cudzych.*nevezmeš/i)
|
||||
)
|
||||
polish.content.pop()
|
||||
throws () ->
|
||||
transformer [english, czech, polish]
|
||||
, /missing.*bogów cudzych.*nevezmeš/i
|
||||
|
||||
Reference in New Issue
Block a user