From b3f8eaccbb5ac88c2bdd53052736f2865db64ea9 Mon Sep 17 00:00:00 2001 From: Vit Brunner Date: Sun, 12 Oct 2014 17:27:14 +0200 Subject: [PATCH] Make test suite nicer --- .../scripts/services/transformer.js.coffee | 2 +- source/documents/tests/_setup.js.coffee | 7 - .../documents/tests/services/poems.js.coffee | 15 +- .../tests/services/reader/_abstract.js.coffee | 5 - .../tests/services/reader/_factory.js.coffee | 25 +- .../tests/services/reader/json.js.coffee | 53 ++-- .../tests/services/reader/markdown.js.coffee | 182 +++++++------- .../tests/services/transformer.js.coffee | 230 ++++++++++-------- 8 files changed, 272 insertions(+), 247 deletions(-) delete mode 100644 source/documents/tests/services/reader/_abstract.js.coffee diff --git a/source/documents/scripts/services/transformer.js.coffee b/source/documents/scripts/services/transformer.js.coffee index 05ae5c8..bd255cc 100644 --- a/source/documents/scripts/services/transformer.js.coffee +++ b/source/documents/scripts/services/transformer.js.coffee @@ -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 diff --git a/source/documents/tests/_setup.js.coffee b/source/documents/tests/_setup.js.coffee index 42cf8c7..2693c01 100644 --- a/source/documents/tests/_setup.js.coffee +++ b/source/documents/tests/_setup.js.coffee @@ -1,8 +1 @@ this.injector = angular.injector(['ng', 'sideBySide']) - -init = { - setup: () -> - this.$scope = injector.get('$rootScope').$new(); -} - -this.module('tests', init); diff --git a/source/documents/tests/services/poems.js.coffee b/source/documents/tests/services/poems.js.coffee index 517a9ee..c85ccfe 100644 --- a/source/documents/tests/services/poems.js.coffee +++ b/source/documents/tests/services/poems.js.coffee @@ -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...' diff --git a/source/documents/tests/services/reader/_abstract.js.coffee b/source/documents/tests/services/reader/_abstract.js.coffee deleted file mode 100644 index 55dee12..0000000 --- a/source/documents/tests/services/reader/_abstract.js.coffee +++ /dev/null @@ -1,5 +0,0 @@ -this.testReader = (name, testCases) -> - reader = injector.get(name) - - for testCase in testCases - deepEqual(reader(testCase.tested), testCase.expected) diff --git a/source/documents/tests/services/reader/_factory.js.coffee b/source/documents/tests/services/reader/_factory.js.coffee index 364a0a6..d19265d 100644 --- a/source/documents/tests/services/reader/_factory.js.coffee +++ b/source/documents/tests/services/reader/_factory.js.coffee @@ -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 diff --git a/source/documents/tests/services/reader/json.js.coffee b/source/documents/tests/services/reader/json.js.coffee index eb79a54..ab5bb36 100644 --- a/source/documents/tests/services/reader/json.js.coffee +++ b/source/documents/tests/services/reader/json.js.coffee @@ -1,29 +1,32 @@ -test("jsonReader", () -> - testCases = [{ - tested: """ - { - meta: { - Title: "Test leading text" - }, - content: [ - "

This is leading text.

", - { section: "I", text: "

First section.

" }, - { section: "II", text: "

Second section.

" }, - { text: "

Another section.

" }, - ], - }""" - expected: { +reader = {} + +module "json reader", { + setup: () -> + reader = injector.get "jsonReader" +} + +test "loads poem", () -> + deepEqual reader(""" + { meta: { Title: "Test leading text" - } + }, content: [ - { section: "", text: "

This is leading text.

" } - { section: "I", text: "

First section.

" } - { section: "II", text: "

Second section.

" } - { section: "", text: "

Another section.

" } - ] + "

This is leading text.

", + { section: "I", text: "

First section.

" }, + { section: "II", text: "

Second section.

" }, + { text: "

Another section.

" }, + ], } - }] - - testReader("jsonReader", testCases) -) + """), + { + meta: { + Title: "Test leading text" + } + content: [ + { section: "", text: "

This is leading text.

" } + { section: "I", text: "

First section.

" } + { section: "II", text: "

Second section.

" } + { section: "", text: "

Another section.

" } + ] + } diff --git a/source/documents/tests/services/reader/markdown.js.coffee b/source/documents/tests/services/reader/markdown.js.coffee index d3d35ad..92c298c 100644 --- a/source/documents/tests/services/reader/markdown.js.coffee +++ b/source/documents/tests/services/reader/markdown.js.coffee @@ -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: 'http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights' - } - content: [ - { section: "1", text: "

All human beings are born free and equal in dignity and rights…

\n" } - { section: "2", text: """ -

Everyone is entitled to all the rights and freedoms…

-

Furthermore, no distinction shall be made on the basis of…

\n""" } - { section: "11.1", text: "

Everyone charged with a penal offence has the right to be presumed…

\n" } - { section: "11.2", text: "

No one shall be held guilty of any penal offence…

\n" } - { section: "13", text: """ -

Everyone is entitled to:

- \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: 'http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights' } - }, { - tested: """ - Title: Test leading text and mixed separators + content: [ + { section: "1", text: "

All human beings are born free and equal in dignity and rights…

\n" } + { section: "2", text: """ +

Everyone is entitled to all the rights and freedoms…

+

Furthermore, no distinction shall be made on the basis of…

\n""" } + { section: "11.1", text: "

Everyone charged with a penal offence has the right to be presumed…

\n" } + { section: "11.2", text: "

No one shall be held guilty of any penal offence…

\n" } + { section: "13", text: """ +

Everyone is entitled to:

+ \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: "

This is leading text.

\n" } - { section: "I", text: "

First section.

\n" } - { section: "", text: "

Second section.

\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: "

This is leading text.

\n" } + { section: "I", text: "

First section.

\n" } + { section: "", text: "

Second section.

\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: """ -

First section.

-
-

Second part.

\n""" } - { section: "II", text: "

Second section.

\n" } - ] + --- + Second part. + + # II + Second section. + """), + { + meta: { + Title: "Separator test" + Separator: "heading" } - }] - - testReader("markdownReader", testCases) -) + content: [ + { section: "I", text: """ +

First section.

+
+

Second part.

\n""" } + { section: "II", text: "

Second section.

\n" } + ] + } diff --git a/source/documents/tests/services/transformer.js.coffee b/source/documents/tests/services/transformer.js.coffee index c1d1187..0d8b600 100644 --- a/source/documents/tests/services/transformer.js.coffee +++ b/source/documents/tests/services/transformer.js.coffee @@ -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