diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-03-24 23:14:17 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-03-24 23:14:17 +0100 |
commit | c9af209a9335ea1219c3149eb12c2daa271c9403 (patch) | |
tree | 72df3edf7a67db465ce2dacbbf204974253b818a | |
parent | 63ee94ff89d10e56d82079183c494f8129b92eae (diff) | |
parent | 48ab44a776b665b1d3627192cf82e9220ec74678 (diff) | |
download | gitbook-c9af209a9335ea1219c3149eb12c2daa271c9403.zip gitbook-c9af209a9335ea1219c3149eb12c2daa271c9403.tar.gz gitbook-c9af209a9335ea1219c3149eb12c2daa271c9403.tar.bz2 |
Merge pull request #667 from GitbookIO/better-testing
Better Unit Tests
73 files changed, 771 insertions, 568 deletions
diff --git a/lib/book.js b/lib/book.js index a9bc40c..bdd4dfc 100644 --- a/lib/book.js +++ b/lib/book.js @@ -94,6 +94,11 @@ var Book = function(root, context, parent) { _.bindAll(this); }; +// Return string representation +Book.prototype.toString = function() { + return "[Book "+this.root+"]"; +}; + // Initialize and parse the book: config, summary, glossary Book.prototype.parse = function() { var that = this; diff --git a/package.json b/package.json index f5e29ae..c1bf71d 100644 --- a/package.json +++ b/package.json @@ -12,14 +12,14 @@ "resolve": "0.6.3", "fs-extra": "0.16.5", "fstream-ignore": "1.0.2", - "gitbook-parsers": "0.6.0", + "gitbook-parsers": "0.7.1", "nunjucks": "git+https://github.com/mozilla/nunjucks.git#0f8b21b8df7e8e852b2e1889388653b7075f0d09", "nunjucks-autoescape": "0.1.1", "nunjucks-filter": "0.1.0", "nunjucks-i18n": "1.0.0", "semver": "2.2.1", "npmi": "0.1.1", - "cheerio": "0.18.0", + "cheerio": "0.19.0", "gitbook-plugin-livereload": "0.0.1", "gaze": "~0.5.1", "send": "0.2.0", @@ -36,6 +36,7 @@ }, "devDependencies": { "mocha": "2.2.1", + "should": "5.2.0", "grunt": "~0.4.2", "grunt-cli": "0.1.11", "grunt-contrib-copy": "0.5.0", @@ -44,7 +45,7 @@ "grunt-bower-install-simple": "0.9.2" }, "scripts": { - "test": "node_modules/.bin/mocha --reporter list --timeout 15000" + "test": "node_modules/.bin/mocha --reporter spec --timeout 15000" }, "repository": { "type": "git", diff --git a/test/assertions.js b/test/assertions.js new file mode 100644 index 0000000..443929a --- /dev/null +++ b/test/assertions.js @@ -0,0 +1,52 @@ +var _ = require('lodash'); +var fs = require('fs'); +var path = require('path'); +var should = require('should'); +var cheerio = require('cheerio'); + +should.Assertion.add('file', function(file, description) { + this.params = { actual: this.obj.toString(), operator: 'have file ' + file, message: description }; + + this.obj.should.have.property('options').which.is.an.Object; + this.obj.options.should.have.property('output').which.is.a.String; + this.assert(fs.existsSync(path.resolve(this.obj.options.output, file))); +}); + +should.Assertion.add('jsonfile', function(file, description) { + this.params = { actual: this.obj.toString(), operator: 'have valid jsonfile ' + file, message: description }; + + this.obj.should.have.property('options').which.is.an.Object; + this.obj.options.should.have.property('output').which.is.a.String; + this.assert(JSON.parse(fs.readFileSync(path.resolve(this.obj.options.output, file), { encoding: "utf-8" }))); +}); + +should.Assertion.add('html', function(rules, description) { + this.params = { actual: "HTML string", operator: 'valid html', message: description }; + var $ = cheerio.load(this.obj); + + _.each(rules, function(validations, query) { + validations = _.defaults(validations || {}, { + count: 1, + attributes: {}, + trim: false, + text: undefined + }); + + var $el = $(query); + + // Test number of elements + $el.should.have.lengthOf(validations.count); + + // Test text + if (validations.text !== undefined) { + var text = $el.text(); + if (validations.trim) text = text.trim(); + text.should.be.equal(validations.text); + } + + // Test attributes + _.each(validations.attributes, function(value, name) { + $el.attr(name).should.be.equal(value); + }); + }); +}); diff --git a/test/books/basic/README.md b/test/books/basic/README.md new file mode 100644 index 0000000..09ade40 --- /dev/null +++ b/test/books/basic/README.md @@ -0,0 +1,3 @@ +# Readme + +Default description for the book. diff --git a/test/fixtures/test2/en/SUMMARY.md b/test/books/basic/SUMMARY.md index ac9323c..ac9323c 100644 --- a/test/fixtures/test2/en/SUMMARY.md +++ b/test/books/basic/SUMMARY.md diff --git a/test/books/config-js/README.md b/test/books/config-js/README.md new file mode 100644 index 0000000..f395431 --- /dev/null +++ b/test/books/config-js/README.md @@ -0,0 +1 @@ +# Readme diff --git a/test/fixtures/test2/fr/SUMMARY.md b/test/books/config-js/SUMMARY.md index ac9323c..ac9323c 100644 --- a/test/fixtures/test2/fr/SUMMARY.md +++ b/test/books/config-js/SUMMARY.md diff --git a/test/books/config-js/book.js b/test/books/config-js/book.js new file mode 100644 index 0000000..8731343 --- /dev/null +++ b/test/books/config-js/book.js @@ -0,0 +1,3 @@ +module.exports = { + "title": "js-config" +}; diff --git a/test/books/config-json/README.md b/test/books/config-json/README.md new file mode 100644 index 0000000..f395431 --- /dev/null +++ b/test/books/config-json/README.md @@ -0,0 +1 @@ +# Readme diff --git a/test/fixtures/test5/SUMMARY.md b/test/books/config-json/SUMMARY.md index ac9323c..ac9323c 100644 --- a/test/fixtures/test5/SUMMARY.md +++ b/test/books/config-json/SUMMARY.md diff --git a/test/books/config-json/book.json b/test/books/config-json/book.json new file mode 100644 index 0000000..eda10bb --- /dev/null +++ b/test/books/config-json/book.json @@ -0,0 +1,3 @@ +{ + "title": "json-config" +} diff --git a/test/books/conrefs/README.md b/test/books/conrefs/README.md new file mode 100644 index 0000000..7f56eed --- /dev/null +++ b/test/books/conrefs/README.md @@ -0,0 +1,10 @@ +# Readme + +### Relative + +<p id="t1">{% include "./hello.md" %}</p> + +### Git + +<p id="t2">{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md" %}</p> +<p id="t3">{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test2.md" %}</p> diff --git a/test/books/conrefs/SUMMARY.md b/test/books/conrefs/SUMMARY.md new file mode 100644 index 0000000..ac9323c --- /dev/null +++ b/test/books/conrefs/SUMMARY.md @@ -0,0 +1 @@ +# Summary diff --git a/test/books/conrefs/hello.md b/test/books/conrefs/hello.md new file mode 100644 index 0000000..557db03 --- /dev/null +++ b/test/books/conrefs/hello.md @@ -0,0 +1 @@ +Hello World diff --git a/test/books/glossary/GLOSSARY.md b/test/books/glossary/GLOSSARY.md new file mode 100644 index 0000000..fa706f1 --- /dev/null +++ b/test/books/glossary/GLOSSARY.md @@ -0,0 +1,9 @@ +# Glossary + +## test + +Just a simple and easy to understand test. + +## hakunamatata + +This word is not present in the content. diff --git a/test/books/glossary/README.md b/test/books/glossary/README.md new file mode 100644 index 0000000..50e8d4c --- /dev/null +++ b/test/books/glossary/README.md @@ -0,0 +1,8 @@ +# Readme + +The word test should be replaced by a glossary link. + +``` +But the word test should not be replaced in code blocks +``` + diff --git a/test/books/glossary/SUMMARY.md b/test/books/glossary/SUMMARY.md new file mode 100644 index 0000000..deaea20 --- /dev/null +++ b/test/books/glossary/SUMMARY.md @@ -0,0 +1,3 @@ +# Summary + +* [Page](folder/PAGE.md) diff --git a/test/books/glossary/folder/PAGE.md b/test/books/glossary/folder/PAGE.md new file mode 100644 index 0000000..33b17c2 --- /dev/null +++ b/test/books/glossary/folder/PAGE.md @@ -0,0 +1,3 @@ +# Page + +Just a page in a sub-directory to test relative link to glossary. diff --git a/test/books/images/README.md b/test/books/images/README.md new file mode 100644 index 0000000..484f410 --- /dev/null +++ b/test/books/images/README.md @@ -0,0 +1,18 @@ +# Tests for Images + +# SVG relative image + + +### Convert SVG to PNG + + +### Should only convert it once + + +### Download remote images + + +### Remote images with same filename + + + diff --git a/test/books/images/SUMMARY.md b/test/books/images/SUMMARY.md new file mode 100644 index 0000000..ec0c4fc --- /dev/null +++ b/test/books/images/SUMMARY.md @@ -0,0 +1,3 @@ +# Summary + +* [Page](./folder/PAGE.md) diff --git a/test/books/images/folder/PAGE.md b/test/books/images/folder/PAGE.md new file mode 100644 index 0000000..8beb060 --- /dev/null +++ b/test/books/images/folder/PAGE.md @@ -0,0 +1,3 @@ +### Images from other folder + + diff --git a/test/fixtures/test4/test.svg b/test/books/images/test.svg index 48bba70..48bba70 100644 --- a/test/fixtures/test4/test.svg +++ b/test/books/images/test.svg diff --git a/test/books/languages/LANGS.md b/test/books/languages/LANGS.md new file mode 100644 index 0000000..4267b3c --- /dev/null +++ b/test/books/languages/LANGS.md @@ -0,0 +1,4 @@ +# Language + +* [English](en) +* [French](fr) diff --git a/test/books/languages/README.md b/test/books/languages/README.md new file mode 100644 index 0000000..f395431 --- /dev/null +++ b/test/books/languages/README.md @@ -0,0 +1 @@ +# Readme diff --git a/test/books/languages/en/README.md b/test/books/languages/en/README.md new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/test/books/languages/en/README.md @@ -0,0 +1 @@ +Hello diff --git a/test/books/languages/en/SUMMARY.md b/test/books/languages/en/SUMMARY.md new file mode 100644 index 0000000..ac9323c --- /dev/null +++ b/test/books/languages/en/SUMMARY.md @@ -0,0 +1 @@ +# Summary diff --git a/test/books/languages/fr/README.md b/test/books/languages/fr/README.md new file mode 100644 index 0000000..632e4fe --- /dev/null +++ b/test/books/languages/fr/README.md @@ -0,0 +1 @@ +Bonjour diff --git a/test/books/languages/fr/SUMMARY.md b/test/books/languages/fr/SUMMARY.md new file mode 100644 index 0000000..ac9323c --- /dev/null +++ b/test/books/languages/fr/SUMMARY.md @@ -0,0 +1 @@ +# Summary diff --git a/test/books/summary/PAGE1.md b/test/books/summary/PAGE1.md new file mode 100644 index 0000000..9608001 --- /dev/null +++ b/test/books/summary/PAGE1.md @@ -0,0 +1 @@ +# Page 1 diff --git a/test/books/summary/README.md b/test/books/summary/README.md new file mode 100644 index 0000000..f395431 --- /dev/null +++ b/test/books/summary/README.md @@ -0,0 +1 @@ +# Readme diff --git a/test/books/summary/SUMMARY.md b/test/books/summary/SUMMARY.md new file mode 100644 index 0000000..38d0f6b --- /dev/null +++ b/test/books/summary/SUMMARY.md @@ -0,0 +1,5 @@ +# Summary + +* [Page 1](./PAGE1.md) +* [Page 2](./folder/PAGE2.md) +* [Don't exists](./NOTFOUND.md) diff --git a/test/books/summary/folder/PAGE2.md b/test/books/summary/folder/PAGE2.md new file mode 100644 index 0000000..f310be3 --- /dev/null +++ b/test/books/summary/folder/PAGE2.md @@ -0,0 +1 @@ +# Page 2 diff --git a/test/configuration.js b/test/configuration.js index e690f2c..eedec49 100644 --- a/test/configuration.js +++ b/test/configuration.js @@ -1,14 +1,32 @@ +var fs = require('fs'); var path = require('path'); -var assert = require('assert'); -var Book = require('../').Book; +describe('Configuration', function () { + it('should extract default title from README', function() { + return books.parse("basic") + .then(function(book) { + book.options.title.should.be.equal("Readme"); + }); + }); + + it('should extract default description from README', function() { + return books.parse("basic") + .then(function(book) { + book.options.description.should.be.equal("Default description for the book."); + }); + }); -describe('Configuration parsing', function () { - it('should correctly load from json', function() { - assert(books[0].options.title == "Test"); + it('should correctly load from json (book.json)', function() { + return books.parse("config-json") + .then(function(book) { + book.options.title.should.be.equal("json-config"); + }); }); - it('should correctly load from javascript', function() { - assert(books[4].options.title == "Test 2"); + it('should correctly load from JavaScript (book.js)', function() { + return books.parse("config-js") + .then(function(book) { + book.options.title.should.be.equal("js-config"); + }); }); }); diff --git a/test/conrefs.js b/test/conrefs.js new file mode 100644 index 0000000..8d6a181 --- /dev/null +++ b/test/conrefs.js @@ -0,0 +1,43 @@ +var fs = require('fs'); +var path = require('path'); + +describe('ConRefs', function () { + var book, readme; + + before(function() { + return books.generate("conrefs", "website") + .then(function(_book) { + book = _book; + + readme = fs.readFileSync( + path.join(book.options.output, "index.html"), + { encoding: "utf-8" } + ); + }); + }); + + it('should handle local references', function() { + readme.should.be.html({ + ".page-inner p#t1": { + count: 1, + text: "Hello World", + trim: true + } + }); + }); + + it('should handle git references', function() { + readme.should.be.html({ + ".page-inner p#t2": { + count: 1, + text: "Hello from git", + trim: true + }, + ".page-inner p#t3": { + count: 1, + text: "First Hello. Hello from git", + trim: true + } + }); + }); +}); diff --git a/test/ebook.js b/test/ebook.js index d02e7cd..c9bb924 100644 --- a/test/ebook.js +++ b/test/ebook.js @@ -1,36 +1,24 @@ +var fs = require('fs'); var path = require('path'); -var _ = require('lodash'); -var assert = require('assert'); -var cheerio = require('cheerio'); -var fs = require("fs"); -var fsUtil = require("../lib/utils/fs"); +describe('eBook generator', function () { + describe('Basic Book', function() { + var book; + before(function() { + return books.generate("basic", "ebook") + .then(function(_book) { + book = _book; + }); + }); -describe('eBook Generator', function () { - it('should correctly generate ebook pages', function(done) { - testGeneration(books[1], "ebook", function(output) { - assert(fs.existsSync(path.join(output, "SUMMARY.html"))); - }, done); - }); - - it('should correctly convert svg images to png', function(done) { - testGeneration(books[4], "ebook", function(output) { - // Check that all images exists - _.each([ - "index.html", - "sub/PAGE.html" - ], function(pageName) { - var pageFile = path.join(output, pageName); - var pageFolder = path.dirname(pageFile); - var pageContent = fs.readFileSync(pageFile, {encoding: "utf8"}); - var $ = cheerio.load(pageContent); + it('should correctly output a SUMMARY.html', function() { + book.should.have.file("SUMMARY.html"); + }); - $("img").each(function() { - var src = $(this).attr("src"); - assert(fs.existsSync(path.resolve(pageFolder, src)), src+" not found for page "+pageName); - }) - }); - }, done); + it('should correctly copy assets', function() { + book.should.have.file("gitbook"); + book.should.have.file("gitbook/style.css"); + }); }); }); diff --git a/test/fixtures/test0/GLOSSARY.md b/test/fixtures/test0/GLOSSARY.md deleted file mode 100644 index ad07f86..0000000 --- a/test/fixtures/test0/GLOSSARY.md +++ /dev/null @@ -1,6 +0,0 @@ -# Glossary - -## Description - -This is the dexcription of a description. - diff --git a/test/fixtures/test0/README.md b/test/fixtures/test0/README.md deleted file mode 100644 index a2fdfea..0000000 --- a/test/fixtures/test0/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Other Title - -This is a description, it's simple, no ? diff --git a/test/fixtures/test0/SUMMARY.md b/test/fixtures/test0/SUMMARY.md deleted file mode 100644 index 27870e5..0000000 --- a/test/fixtures/test0/SUMMARY.md +++ /dev/null @@ -1 +0,0 @@ -# Summary
\ No newline at end of file diff --git a/test/fixtures/test0/book.json b/test/fixtures/test0/book.json deleted file mode 100644 index 41faa63..0000000 --- a/test/fixtures/test0/book.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "title": "Test" -}
\ No newline at end of file diff --git a/test/fixtures/test0/styles/website.css b/test/fixtures/test0/styles/website.css deleted file mode 100644 index 5c5e887..0000000 --- a/test/fixtures/test0/styles/website.css +++ /dev/null @@ -1,3 +0,0 @@ -body { - background: red; -} diff --git a/test/fixtures/test1/GLOSSARY.md b/test/fixtures/test1/GLOSSARY.md deleted file mode 100644 index dc14550..0000000 --- a/test/fixtures/test1/GLOSSARY.md +++ /dev/null @@ -1,9 +0,0 @@ - -# Test - -a test text - -# Test 2 - -a second test - diff --git a/test/fixtures/test1/README.md b/test/fixtures/test1/README.md deleted file mode 100644 index 2f7a8d4..0000000 --- a/test/fixtures/test1/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Test - -This file is not parsed by gitbook because the structure is defined in book.json. - diff --git a/test/fixtures/test1/SUMMARY.md b/test/fixtures/test1/SUMMARY.md deleted file mode 100644 index d05fc4a..0000000 --- a/test/fixtures/test1/SUMMARY.md +++ /dev/null @@ -1,6 +0,0 @@ -# Summary - -* [Chapter 1](test.md) - * [Article 1](sub/test1.md) -* [Chapter 2](test2.md) -* [Google](https://www.google.com) diff --git a/test/fixtures/test1/book.json b/test/fixtures/test1/book.json deleted file mode 100644 index edaa4fc..0000000 --- a/test/fixtures/test1/book.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "structure": { - "readme": "intro.md" - } -}
\ No newline at end of file diff --git a/test/fixtures/test1/intro.md b/test/fixtures/test1/intro.md deleted file mode 100644 index bf9a1cf..0000000 --- a/test/fixtures/test1/intro.md +++ /dev/null @@ -1,4 +0,0 @@ -# My Book - -Test description - diff --git a/test/fixtures/test1/sub/test1.md b/test/fixtures/test1/sub/test1.md deleted file mode 100644 index d45a4dd..0000000 --- a/test/fixtures/test1/sub/test1.md +++ /dev/null @@ -1,8 +0,0 @@ -# This file is used to etst links transformations: - -This is a relative link [test](../intro.md). - - - -This is a glossary link. - diff --git a/test/fixtures/test2/LANGS.md b/test/fixtures/test2/LANGS.md deleted file mode 100644 index a501d22..0000000 --- a/test/fixtures/test2/LANGS.md +++ /dev/null @@ -1,4 +0,0 @@ -# Languages - -* [English](en/) -* [French](fr/) diff --git a/test/fixtures/test2/README.md b/test/fixtures/test2/README.md deleted file mode 100644 index c6186ac..0000000 --- a/test/fixtures/test2/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# Multi-Languages test - diff --git a/test/fixtures/test2/en/README.md b/test/fixtures/test2/en/README.md deleted file mode 100644 index 95bc71c..0000000 --- a/test/fixtures/test2/en/README.md +++ /dev/null @@ -1 +0,0 @@ -# English Book diff --git a/test/fixtures/test2/fr/README.md b/test/fixtures/test2/fr/README.md deleted file mode 100644 index c7a4103..0000000 --- a/test/fixtures/test2/fr/README.md +++ /dev/null @@ -1 +0,0 @@ -# French Book diff --git a/test/fixtures/test3/README.adoc b/test/fixtures/test3/README.adoc deleted file mode 100644 index ea295ee..0000000 --- a/test/fixtures/test3/README.adoc +++ /dev/null @@ -1,4 +0,0 @@ -= My Book - -Test description - diff --git a/test/fixtures/test3/SUMMARY.adoc b/test/fixtures/test3/SUMMARY.adoc deleted file mode 100644 index 29fcb65..0000000 --- a/test/fixtures/test3/SUMMARY.adoc +++ /dev/null @@ -1,5 +0,0 @@ -= Summary - -. link:test.adoc[Chapter 1] -.. link:test1.adoc[Article 1] -. link:test2.adoc[Chapter 2] diff --git a/test/fixtures/test3/test.adoc b/test/fixtures/test3/test.adoc deleted file mode 100644 index e69de29..0000000 --- a/test/fixtures/test3/test.adoc +++ /dev/null diff --git a/test/fixtures/test3/test1.adoc b/test/fixtures/test3/test1.adoc deleted file mode 100644 index e69de29..0000000 --- a/test/fixtures/test3/test1.adoc +++ /dev/null diff --git a/test/fixtures/test3/test2.adoc b/test/fixtures/test3/test2.adoc deleted file mode 100644 index e69de29..0000000 --- a/test/fixtures/test3/test2.adoc +++ /dev/null diff --git a/test/fixtures/test4/README.md b/test/fixtures/test4/README.md deleted file mode 100644 index d73be69..0000000 --- a/test/fixtures/test4/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Other Title - -A description - - - - - -# Test with youtube videos that have the same filename: - - diff --git a/test/fixtures/test4/SUMMARY.md b/test/fixtures/test4/SUMMARY.md deleted file mode 100644 index 3cda114..0000000 --- a/test/fixtures/test4/SUMMARY.md +++ /dev/null @@ -1,3 +0,0 @@ -# Summary - -* [Page](sub/PAGE.md) diff --git a/test/fixtures/test4/book.js b/test/fixtures/test4/book.js deleted file mode 100644 index 4642b56..0000000 --- a/test/fixtures/test4/book.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - title: "Test 2" -}; diff --git a/test/fixtures/test4/sub/PAGE.md b/test/fixtures/test4/sub/PAGE.md deleted file mode 100644 index a98a942..0000000 --- a/test/fixtures/test4/sub/PAGE.md +++ /dev/null @@ -1,18 +0,0 @@ -## - -## Image from root page - - - - -## Inline svg - -{% html %} -<svg xmlns="http://www.w3.org/2000/svg"> - <path d="M97.008198003228,6.103238498249268A97.2,97.2 0 0,1 87.9491894996971,41.385747140125076L54.289623147961166,25.546757493904366A60,60 0 0,0 59.88160370569629,3.7674311717588074Z"></path> -</svg> -{% endhtml %} - -## Remote image - - diff --git a/test/fixtures/test5/README.md b/test/fixtures/test5/README.md deleted file mode 100644 index 3db21b2..0000000 --- a/test/fixtures/test5/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Test Content inclusion - -{% include "./test.md" %} -Git1:{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md" %} -Git2:{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test2.md" %}
\ No newline at end of file diff --git a/test/fixtures/test5/test.md b/test/fixtures/test5/test.md deleted file mode 100644 index 5e1c309..0000000 --- a/test/fixtures/test5/test.md +++ /dev/null @@ -1 +0,0 @@ -Hello World
\ No newline at end of file diff --git a/test/git.js b/test/git.js index 9d48606..f03bec2 100644 --- a/test/git.js +++ b/test/git.js @@ -1,32 +1,31 @@ -var path = require('path'); -var _ = require('lodash'); -var assert = require('assert'); - -var fs = require("fs"); -var git = require("../lib/utils/git"); +var should = require('should'); +var git = require('../lib/utils/git'); describe('GIT parser and getter', function () { it('should correctly parse an https url', function() { var parts = git.parseUrl("git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md"); - assert(parts); - assert.equal(parts.host, "https://gist.github.com/69ea4542e4c8967d2fa7.git"); - assert.equal(parts.ref, "master"); - assert.equal(parts.filepath, "test.md"); + + should.exist(parts); + parts.host.should.be.equal("https://gist.github.com/69ea4542e4c8967d2fa7.git"); + parts.ref.should.be.equal("master"); + parts.filepath.should.be.equal("test.md"); }); it('should correctly parse an https url with a reference', function() { var parts = git.parseUrl("git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md#0.1.2"); - assert(parts); - assert.equal(parts.host, "https://gist.github.com/69ea4542e4c8967d2fa7.git"); - assert.equal(parts.ref, "0.1.2"); - assert.equal(parts.filepath, "test.md"); + + should.exist(parts); + parts.host.should.be.equal("https://gist.github.com/69ea4542e4c8967d2fa7.git"); + parts.ref.should.be.equal("0.1.2"); + parts.filepath.should.be.equal("test.md"); }); it('should correctly parse an ssh url', function() { var parts = git.parseUrl("git+git@github.com:GitbookIO/gitbook.git/directory/README.md#e1594cde2c32e4ff48f6c4eff3d3d461743d74e1"); - assert(parts); - assert.equal(parts.host, "git@github.com:GitbookIO/gitbook.git"); - assert.equal(parts.ref, "e1594cde2c32e4ff48f6c4eff3d3d461743d74e1"); - assert.equal(parts.filepath, "directory/README.md"); + + should.exist(parts); + parts.host.should.be.equal("git@github.com:GitbookIO/gitbook.git"); + parts.ref.should.be.equal("e1594cde2c32e4ff48f6c4eff3d3d461743d74e1"); + parts.filepath.should.be.equal("directory/README.md"); }); }); diff --git a/test/glossary.js b/test/glossary.js index 5deb04c..631967e 100644 --- a/test/glossary.js +++ b/test/glossary.js @@ -1,35 +1,78 @@ +var fs = require('fs'); var path = require('path'); -var _ = require('lodash'); -var assert = require('assert'); -var cheerio = require('cheerio'); -var fs = require("fs"); -var fsUtil = require("../lib/utils/fs"); +describe('Glossary', function () { + describe('Parsing', function() { + var book; + before(function() { + return books.parse("glossary") + .then(function(_book) { + book = _book; + }); + }); -describe('Glossary Generation', function () { - it('should correctly replace glossary terms', function(done) { - testGeneration(books[0], "website", function(output) { - var content = fs.readFileSync(path.join(output, "index.html"), { encoding: "utf8" }); - var $ = cheerio.load(content); - - var $body = $(".page-inner"); - var $a = $("a[href='GLOSSARY.html#description']"); - assert($a.length == 1); - assert($a.text() == "description"); - }, done); + it('should correctly list items', function() { + book.should.have.property("glossary"); + book.glossary.should.have.lengthOf(2); + }); }); - it('should correctly replace glossary terms in sub pages', function(done) { - testGeneration(books[1], "website", function(output) { - var content = fs.readFileSync(path.join(output, "sub/test1.html"), { encoding: "utf8" }); - var $ = cheerio.load(content); - - var $body = $(".page-inner"); - var $a = $("a[href='../GLOSSARY.html#test']"); - assert($a.length == 1); - assert($a.text() == "test"); - assert($a.attr("title") == "a test text"); - }, done); + describe('Generation', function() { + var book; + + before(function() { + return books.generate("glossary", "website") + .then(function(_book) { + book = _book; + }); + }); + + it('should correctly generate a GLOSSARY.html', function() { + book.should.have.file("GLOSSARY.html"); + }); + + describe('Page Integration', function() { + var readme, page; + + before(function() { + readme = fs.readFileSync( + path.join(book.options.output, "index.html"), + { encoding: "utf-8" } + ); + page = fs.readFileSync( + path.join(book.options.output, "folder/PAGE.html"), + { encoding: "utf-8" } + ); + }); + + it('should correctly replaced terms by links', function() { + readme.should.be.html({ + ".page-inner a[href='GLOSSARY.html#test']": { + count: 1, + text: "test", + attributes: { + title: "Just a simple and easy to understand test." + } + } + }); + }); + + it('should correctly replaced terms by links (relative)', function() { + page.should.be.html({ + ".page-inner a[href='../GLOSSARY.html#test']": { + count: 1 + } + }); + }); + + it('should not replace terms in codeblocks', function() { + readme.should.be.html({ + ".page-inner code a": { + count: 0 + } + }); + }); + }); }); }); diff --git a/test/helper.js b/test/helper.js index 528d491..f6b671b 100644 --- a/test/helper.js +++ b/test/helper.js @@ -1,62 +1,68 @@ +var os = require('os'); var path = require('path'); var Q = require('q'); var fs = require('fs'); var _ = require('lodash'); +var should = require('should'); -var fsUtil = require("../lib/utils/fs"); +var fsUtil = require('../lib/utils/fs'); var Book = require('../').Book; var LOG_LEVELS = require('../').LOG_LEVELS; -// Nicety for mocha / Q -global.qdone = function qdone(promise, done) { - return promise.then(function() { - return done(); - }, function(err) { - return done(err); - }).done(); -}; +require("./assertions"); -// Test generation of a book -global.testGeneration = function(book, type, func, done) { - var OUTPUT_PATH = book.options.output; - qdone( - book.generate(type) - .then(function() { - func(OUTPUT_PATH); - }) - .fin(function() { - return fsUtil.remove(OUTPUT_PATH); - }), - done); +var BOOKS = {}; +var TMPDIR = os.tmpdir(); + + +// Generate and return a book +function generateBook(bookId, test) { + return parseBook(bookId, test) + .then(function(book) { + return book.generate(test) + .thenResolve(book); + }); +} + +// Generate and return a book +function parseBook(bookId, test) { + test = test || "website"; + BOOKS[bookId] = BOOKS[bookId] || {}; + if (BOOKS[bookId][test]) return Q(BOOKS[bookId][test]); + + BOOKS[bookId][test] = new Book(path.resolve(__dirname, "books", bookId), { + logLevel: LOG_LEVELS.DISABLED, + config: { + output: path.resolve(TMPDIR, bookId+"-"+test) + } + }); + + return BOOKS[bookId][test].parse() + .then(function() { + return BOOKS[bookId][test]; + }); +} + + +global.books = { + parse: parseBook, + generate: generateBook }; -// Books for testings -var books = fs.readdirSync(path.join(__dirname, './fixtures/')); - -global.books = _.chain(books) - .sortBy() - .map(function(book) { - if (book.indexOf("test") !== 0) return null; - return new Book(path.join(__dirname, './fixtures/', book), { - logLevel: LOG_LEVELS.DISABLED - }); - }) - .compact() - .value(); - -// Init before doing tests -before(function(done) { - - qdone( - _.reduce(global.books, function(prev, book) { +// Cleanup all tests +after(function() { + return _.chain(BOOKS) + .map(function(types, bookId) { + return _.values(types); + }) + .flatten() + .reduce(function(prev, book) { return prev.then(function() { - return fsUtil.remove(path.join(book.root, "_book")); + return fsUtil.remove(book.options.output); }) - .then(function() { - return book.parse(); - }); - }, Q()), - done - ); + }, Q()) + .value(); }); + + diff --git a/test/images.js b/test/images.js new file mode 100644 index 0000000..75bc529 --- /dev/null +++ b/test/images.js @@ -0,0 +1,56 @@ +var fs = require('fs'); +var _ = require('lodash'); +var path = require('path'); +var cheerio = require('cheerio'); + +describe('Images', function () { + var book, readme, $, $img, srcs; + + before(function() { + return books.generate("images", "ebook") + .then(function(_book) { + book = _book; + + readme = fs.readFileSync( + path.join(book.options.output, "index.html"), + { encoding: "utf-8" } + ); + $ = cheerio.load(readme); + $img = $("img"); + srcs = $img.map(function() { return $(this).attr("src"); }) + }); + }); + + it('should detect all images', function() { + _.uniq(srcs).should.have.lengthOf(4); + }); + + it('should keep image tags', function() { + srcs.should.have.lengthOf(5); + }); + + it('should not have .svg files', function() { + _.each(srcs, function(src) { + path.extname(src).should.not.equal(".svg"); + }); + }); + + it('should correctly convert svg images to png', function() { + _.each(srcs, function(src) { + book.should.have.file(src); + }); + }); + + it('should handle relative paths', function() { + var PAGE = fs.readFileSync( + path.join(book.options.output, "folder/PAGE.html"), + { encoding: "utf-8" } + ); + + PAGE.should.be.html({ + "img[src='../test.png']": { + count: 1 + } + }); + }); +}); diff --git a/test/json.js b/test/json.js index d7f6aac..0e50237 100644 --- a/test/json.js +++ b/test/json.js @@ -1,59 +1,92 @@ +var fs = require('fs'); var path = require('path'); -var _ = require('lodash'); -var assert = require('assert'); - -var fs = require("fs"); describe('JSON generator', function () { - it('should correctly generate a book to json with glossary', function(done) { - testGeneration(books[0], "json", function(output) { - assert(fs.existsSync(path.join(output, "README.json"))); + describe('Basic Book', function() { + var book; - var readme = JSON.parse(fs.readFileSync(path.join(output, "README.json"))); - assert(readme.sections[0].content.indexOf('class="glossary-term"') > 0); - }, done); - }); + before(function() { + return books.generate("basic", "json") + .then(function(_book) { + book = _book; + }); + }); - it('should correctly generate a book to json with sub folders', function(done) { - testGeneration(books[1], "json", function(output) { - assert(fs.existsSync(path.join(output, "README.json"))); - assert(fs.existsSync(path.join(output, "intro.json"))); - assert(fs.existsSync(path.join(output, "sub/test1.json"))); + it('should correctly output a README.json', function() { + book.should.have.file("README.json"); + }); - var test1 = JSON.parse(fs.readFileSync(path.join(output, "sub/test1.json"))); - assert(test1.sections[0].content.indexOf("index.html") > 0); - }, done); - }); + it('should output a valid json', function() { + book.should.have.jsonfile("README.json"); + }); - it('should correctly generate a multilingual book to json', function(done) { - testGeneration(books[2], "json", function(output) { - assert(fs.existsSync(path.join(output, "README.json"))); - assert(fs.existsSync(path.join(output, "en/README.json"))); - assert(fs.existsSync(path.join(output, "fr/README.json"))); - }, done); - }); + describe('Page Format', function() { + var page; - it('should correctly generate an asciidoc book to json', function(done) { - testGeneration(books[3], "json", function(output) { - assert(fs.existsSync(path.join(output, "README.json"))); - assert(fs.existsSync(path.join(output, "test.json"))); - assert(fs.existsSync(path.join(output, "test1.json"))); - assert(fs.existsSync(path.join(output, "test2.json"))); - }, done); - }); + before(function() { + page = JSON.parse( + fs.readFileSync( + path.join(book.options.output, "README.json"), + { encoding: "utf-8" } + ) + ); + }); + + it('should contains valid section', function() { + page.should.have.property("sections").with.lengthOf(1); + page.sections[0].should.have.property("content").which.is.a.String; + page.sections[0].should.have.property("type").which.is.a.String.which.equal("normal"); + }); + + it('should contains valid progress', function() { + page.should.have.property("progress"); + page.progress.should.have.property("chapters").with.lengthOf(1); + page.progress.should.have.property("current"); + }); - it('should correctly generate a book with local inclusion', function(done) { - testGeneration(books[5], "json", function(output) { - var readme = JSON.parse(fs.readFileSync(path.join(output, "README.json"))); - assert(readme.sections[0].content.indexOf('Hello World') > 0); - }, done); + it('should contains no languages', function() { + page.should.have.property("langs").with.lengthOf(0); + }); + }); }); - it('should correctly generate a book with external inclusion', function(done) { - testGeneration(books[5], "json", function(output) { - var readme = JSON.parse(fs.readFileSync(path.join(output, "README.json"))); - assert(readme.sections[0].content.indexOf('Git1:Hello from git') > 0); - assert(readme.sections[0].content.indexOf('Git2:First Hello. Hello from git') > 0); - }, done); + describe('Multilingual Book', function() { + var book; + + before(function() { + return books.generate("languages", "json") + .then(function(_book) { + book = _book; + }); + }); + + it('should correctly output READMEs', function() { + book.should.have.file("README.json"); + book.should.have.file("en/README.json"); + book.should.have.file("fr/README.json"); + }); + + it('should output valid json', function() { + book.should.have.jsonfile("README.json"); + book.should.have.jsonfile("en/README.json"); + book.should.have.jsonfile("fr/README.json"); + }); + + describe('Page Format', function() { + var page; + + before(function() { + page = JSON.parse( + fs.readFileSync( + path.join(book.options.output, "README.json"), + { encoding: "utf-8" } + ) + ); + }); + + it('should contains no languages', function() { + page.should.have.property("langs").with.lengthOf(2); + }); + }); }); }); diff --git a/test/languages.js b/test/languages.js new file mode 100644 index 0000000..abdc5dd --- /dev/null +++ b/test/languages.js @@ -0,0 +1,40 @@ +var fs = require('fs'); +var path = require('path'); + +describe('Languages', function () { + describe('Parsing', function() { + var book; + + before(function() { + return books.parse("languages") + .then(function(_book) { + book = _book; + }); + }); + + it('should correctly list languages', function() { + book.should.have.property("books"); + book.books.should.have.lengthOf(2); + + book.books[0].options.language.should.be.equal("en"); + book.books[1].options.language.should.be.equal("fr"); + }); + }); + + describe('Generation', function() { + var book; + + before(function() { + return books.generate("languages", "website") + .then(function(_book) { + book = _book; + }); + }); + + it('should correctly create books', function() { + book.should.have.file("index.html"); + book.should.have.file("en/index.html"); + book.should.have.file("fr/index.html"); + }); + }); +}); diff --git a/test/links.js b/test/links.js index 2c68a23..5c7823d 100644 --- a/test/links.js +++ b/test/links.js @@ -1,40 +1,37 @@ -var path = require('path'); -var _ = require('lodash'); -var assert = require('assert'); - +var should = require('should'); var links = require("../lib/utils/links"); describe('Links', function () { it('should correctly test external links', function() { - assert(links.isExternal("http://google.fr")); - assert(links.isExternal("https://google.fr")); - assert(!links.isExternal("test.md")); - assert(!links.isExternal("folder/test.md")); - assert(!links.isExternal("/folder/test.md")); + links.isExternal("http://google.fr").should.be.exactly(true); + links.isExternal("https://google.fr").should.be.exactly(true); + links.isExternal("test.md").should.be.exactly(false); + links.isExternal("folder/test.md").should.be.exactly(false); + links.isExternal("/folder/test.md").should.be.exactly(false); }); - it('should correctly test anchor links', function() { - assert(links.isAnchor("#test")); - assert(links.isAnchor(" #test")); - assert(!links.isAnchor("https://google.fr#test")); - assert(!links.isAnchor("test.md#test")); + it('should correctly detect anchor links', function() { + links.isAnchor("#test").should.be.exactly(true); + links.isAnchor(" #test").should.be.exactly(true); + links.isAnchor("https://google.fr#test").should.be.exactly(false); + links.isAnchor("test.md#test").should.be.exactly(false); }); describe('toAbsolute', function() { it('should correctly transform as absolute', function() { - assert.equal(links.toAbsolute("http://google.fr"), "http://google.fr"); - assert.equal(links.toAbsolute("test.md", "./", "./"), "test.md"); - assert.equal(links.toAbsolute("folder/test.md", "./", "./"), "folder/test.md"); + links.toAbsolute("http://google.fr").should.be.equal("http://google.fr"); + links.toAbsolute("test.md", "./", "./").should.be.equal("test.md"); + links.toAbsolute("folder/test.md", "./", "./").should.be.equal("folder/test.md"); }); it('should correctly handle windows path', function() { - assert.equal(links.toAbsolute("folder\\test.md", "./", "./"), "folder/test.md"); + links.toAbsolute("folder\\test.md", "./", "./").should.be.equal("folder/test.md"); }); it('should correctly handle absolute path', function() { - assert.equal(links.toAbsolute("/test.md", "./", "./"), "test.md"); - assert.equal(links.toAbsolute("/test.md", "test", "test"), "../test.md"); - assert.equal(links.toAbsolute("/sub/test.md", "test", "test"), "../sub/test.md"); + links.toAbsolute("/test.md", "./", "./").should.be.equal("test.md"); + links.toAbsolute("/test.md", "test", "test").should.be.equal("../test.md"); + links.toAbsolute("/sub/test.md", "test", "test").should.be.equal("../sub/test.md"); }); }); }); diff --git a/test/parsing.js b/test/parsing.js deleted file mode 100644 index 498de9f..0000000 --- a/test/parsing.js +++ /dev/null @@ -1,82 +0,0 @@ -var path = require('path'); -var _ = require('lodash'); -var assert = require('assert'); - -describe('Book parsing', function () { - it('should correctly parse the readme', function() { - assert.equal(books[1].options.title, 'My Book'); - assert.equal(books[1].options.description, 'Test description'); - }); - - it('should correctly parse the readme with asciidoc', function() { - assert.equal(books[3].options.title, 'My Book'); - assert.equal(books[3].options.description, 'Test description'); - }); - - it('should correctly parse the summary', function() { - var LEXED = books[1].summary; - - assert.equal(LEXED.chapters[0].path, 'intro.md'); - assert.equal(LEXED.chapters[0].exists, true); - assert.equal(LEXED.chapters[0].introduction, true); - assert.equal(LEXED.chapters[0].external, false); - - assert.equal(LEXED.chapters[1].path, 'test.md'); - assert.equal(LEXED.chapters[1].exists, false); - assert.equal(LEXED.chapters[1].introduction, false); - assert.equal(LEXED.chapters[1].external, false); - - assert.equal(LEXED.chapters[2].path, 'test2.md'); - assert.equal(LEXED.chapters[2].exists, false); - assert.equal(LEXED.chapters[2].introduction, false); - assert.equal(LEXED.chapters[2].external, false); - - assert.equal(LEXED.chapters[3].path, 'https://www.google.com'); - assert.equal(LEXED.chapters[3].exists, true); - assert.equal(LEXED.chapters[3].introduction, false); - assert.equal(LEXED.chapters[3].external, true); - }); - - it('should correctly parse the glossary', function() { - var LEXED = books[1].glossary; - - assert.equal(LEXED[0].id, "test"); - assert.equal(LEXED[0].name, "Test"); - assert.equal(LEXED[0].description, "a test text"); - - assert.equal(LEXED[1].id, "test_2"); - assert.equal(LEXED[1].name, "Test 2"); - assert.equal(LEXED[1].description, "a second test"); - }); - - it('should correctly parse list of files', function() { - var FILES = books[1].files; - - assert.deepEqual(FILES, [ 'README.md', 'intro.md', 'sub/', 'sub/test1.md' ]); - }); - - it('should correctly parse the languages', function() { - assert.equal(books[2].books.length, 2); - assert(books[2].isMultilingual()); - - assert.equal(books[2].books[0].options.language, "en"); - assert.equal(books[2].books[0].options.title, "English Book"); - - assert.equal(books[2].books[1].options.language, "fr"); - assert.equal(books[2].books[1].options.title, "French Book"); - }); - - it('should correctly parse the navigation', function() { - var NAVIGATION = books[1].navigation; - - assert.equal(_.size(NAVIGATION), 2); - assert(NAVIGATION["intro.md"]) - assert.equal(NAVIGATION["intro.md"].title, ""); - assert.equal(NAVIGATION["intro.md"].prev, null); - assert.equal(NAVIGATION["intro.md"].next.title, "Article 1"); - - assert.equal(NAVIGATION["sub/test1.md"].title, "Article 1"); - assert.equal(NAVIGATION["sub/test1.md"].prev.title, ""); - assert.equal(NAVIGATION["sub/test1.md"].next, null); - }); -}); diff --git a/test/plugins.js b/test/plugins.js index c7d1f90..14f4a2f 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -1,189 +1,190 @@ -var path = require('path'); var _ = require('lodash'); -var assert = require('assert'); -var fs = require("fs"); - -var Plugin = require("../lib/plugin"); +var should = require('should'); +var path = require('path'); -var PLUGINS_ROOT = path.resolve(__dirname, "plugins"); +var Plugin = require('../lib/plugin'); +var PLUGINS_ROOT = path.resolve(__dirname, 'plugins'); describe('Plugins', function () { - describe('invalid plugin', function() { - it('should signal as invalid', function() { - var plugin = new Plugin(books[0], "invalid"); + var book; + + before(function() { + return books.parse("basic") + .then(function(_book) { + book = _book; + }); + }); + + describe('Invalid', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "invalid"); plugin.load("./invalid", PLUGINS_ROOT); - assert(!plugin.isValid()); + }); + + it('should be detected', function() { + should(plugin.isValid()).be.exactly(false); }); }); - describe('empty plugin', function() { - var plugin = new Plugin(books[0], "invalid"); - plugin.load("./empty", PLUGINS_ROOT); + describe('Empty', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "empty"); + plugin.load("./empty", PLUGINS_ROOT); + }); it('should valid a plugin', function() { - assert(plugin.isValid()); + should(plugin.isValid()).be.exactly(true); }); - it('should return an empty list of resources', function(done) { - qdone( - plugin.getResources() - .then(function(resources) { - _.each(Plugin.RESOURCES, function(resName) { - assert.equal(resources[resName].length, 0); - }); - }), - done); + it('should return an empty list of resources', function() { + return plugin.getResources() + .then(function(resources) { + _.each(Plugin.RESOURCES, function(resName) { + resources[resName].should.have.lengthOf(0); + }); + }); }); }); - describe('resources plugin', function() { - var plugin = new Plugin(books[0], "resources"); - plugin.load("./resources", PLUGINS_ROOT); + describe('Resources', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "resources"); + plugin.load("./resources", PLUGINS_ROOT); - before(function(done) { - qdone(books[0].plugins.load(plugin), done); + return book.plugins.load(plugin); }); it('should valid a plugin', function() { - assert(plugin.isValid()); + should(plugin.isValid()).be.exactly(true); }); - it('should return a valid list of resources (website)', function(done) { - qdone( - plugin.getResources("website") - .then(function(resources) { - assert.equal(resources["js"].length, 1); - }), - done); - }); + describe('Website', function() { + it('should return a valid list of resources', function() { + return plugin.getResources("website") + .then(function(resources) { + resources["js"].should.have.lengthOf(1); + }); + }); - it('should return a valid list of resources (ebook)', function(done) { - qdone( - plugin.getResources("ebook") - .then(function(resources) { - assert.equal(resources["css"].length, 1); - }), - done); + it('should extend books plugins', function() { + var resources = book.plugins.resources("website"); + resources["js"].should.have.lengthOf(1); + }); }); - it('should extend books plugins (website)', function() { - var resources = books[0].plugins.resources("website"); - assert.equal(resources["js"].length, 1); - }); + describe('eBook', function() { + it('should return a valid list of resources', function() { + return plugin.getResources("ebook") + .then(function(resources) { + resources["css"].should.have.lengthOf(1); + }); + }); - it('should extend books plugins (ebook)', function() { - var resources = books[0].plugins.resources("ebook"); - assert.equal(resources["css"].length, 1); + it('should extend books plugins', function() { + var resources = book.plugins.resources("ebook"); + resources["css"].should.have.lengthOf(1); + }); }); }); - describe('filters', function() { - var plugin = new Plugin(books[0], "filters"); - plugin.load("./filters", PLUGINS_ROOT); + describe('Filters', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "filters"); + plugin.load("./filters", PLUGINS_ROOT); - before(function(done) { - qdone(books[0].plugins.load(plugin), done); + return book.plugins.load(plugin); }); it('should valid a plugin', function() { - assert(plugin.isValid()); + should(plugin.isValid()).be.exactly(true); }); it('should return a map of filters', function() { var filters = plugin.getFilters(); - assert.equal(_.size(filters), 2); - assert(filters["hello"]); - assert(filters["helloCtx"]); + + _.size(filters).should.equal(2); + filters.should.have.property("hello"); + filters.should.have.property("helloCtx"); }); - it('should correctly extend template filters', function(done) { - qdone( - books[0].template.renderString('{{ "World"|hello }}') + it('should correctly extend template filters', function() { + return book.template.renderString('{{ "World"|hello }}') .then(function(content) { - assert.equal(content, "Hello World"); - }), - done - ); + content.should.equal("Hello World"); + }); }); - it('should correctly set book as context', function(done) { - qdone( - books[0].template.renderString('{{ "root"|helloCtx }}') + it('should correctly set book as context', function() { + return book.template.renderString('{{ "root"|helloCtx }}') .then(function(content) { - assert.equal(content, "root:"+books[0].root); - }), - done - ); + content.should.equal("root:"+book.root); + }); }); }); - describe('blocks', function() { - var plugin = new Plugin(books[0], "blocks"); - plugin.load("./blocks", PLUGINS_ROOT); + describe('Blocks', function() { + var plugin; - var testTpl = function(str, args, options) { - return books[0].template.renderString(str, args, options) - .then(books[0].template.postProcess) - }; + before(function() { + plugin = new Plugin(book, "blocks"); + plugin.load("./blocks", PLUGINS_ROOT); - before(function(done) { - qdone(books[0].plugins.load(plugin), done); + return book.plugins.load(plugin); }); + var testTpl = function(str, args, options) { + return book.template.renderString(str, args, options) + .then(book.template.postProcess) + }; + it('should valid a plugin', function() { - assert(plugin.isValid()); + should(plugin.isValid()).be.exactly(true); }); - it('should correctly extend template blocks', function(done) { - qdone( - testTpl('{% test %}hello{% endtest %}') + it('should correctly extend template blocks', function() { + return testTpl('{% test %}hello{% endtest %}') .then(function(content) { - assert.equal(content, "testhellotest"); - }), - done - ); + content.should.equal("testhellotest"); + }); }); - it('should correctly accept shortcuts', function(done) { - qdone( - testTpl('$$hello$$', {}, { + it('should correctly accept shortcuts', function() { + return testTpl('$$hello$$', {}, { type: "markdown" }) .then(function(content) { - assert.equal(content, "testhellotest"); - }), - done - ); + content.should.equal("testhellotest"); + }); }); - it('should correctly extend template blocks with defined end', function(done) { - qdone( - testTpl('{% test2 %}hello{% endtest2end %}') + it('should correctly extend template blocks with defined end', function() { + return testTpl('{% test2 %}hello{% endtest2end %}') .then(function(content) { - assert.equal(content, "test2hellotest2"); - }), - done - ); + content.should.equal("test2hellotest2"); + }); }); - it('should correctly extend template blocks with sub-blocks', function(done) { - qdone( - testTpl('{% test3join separator=";" %}hello{% also %}world{% endtest3join %}') + it('should correctly extend template blocks with sub-blocks', function() { + return testTpl('{% test3join separator=";" %}hello{% also %}world{% endtest3join %}') .then(function(content) { - assert.equal(content, "hello;world"); - }), - done - ); + content.should.equal("hello;world"); + }); }); - it('should correctly extend template blocks with different sub-blocks', function(done) { - qdone( - testTpl('{% test4join separator=";" %}hello{% also %}the{% finally %}world{% endtest4join %}') + it('should correctly extend template blocks with different sub-blocks', function() { + return testTpl('{% test4join separator=";" %}hello{% also %}the{% finally %}world{% endtest4join %}') .then(function(content) { - assert.equal(content, "hello;the;world"); - }), - done - ); + content.should.equal("hello;the;world"); + }); }); }); }); + diff --git a/test/summary.js b/test/summary.js new file mode 100644 index 0000000..99cdf6b --- /dev/null +++ b/test/summary.js @@ -0,0 +1,45 @@ +var fs = require('fs'); +var path = require('path'); + +describe('Summary', function () { + describe('Parsing', function() { + var book; + + before(function() { + return books.parse("summary") + .then(function(_book) { + book = _book; + }); + }); + + it('should correctly list items', function() { + book.should.have.property("summary"); + book.summary.should.have.property("chapters"); + book.summary.chapters.should.have.lengthOf(4); + }); + + it('should correctly mark non-existant entries', function() { + book.summary.chapters[0].exists.should.have.equal(true); + book.summary.chapters[1].exists.should.have.equal(true); + book.summary.chapters[2].exists.should.have.equal(true); + book.summary.chapters[3].exists.should.have.equal(false); + }); + }); + + describe('Generation', function() { + var book; + + before(function() { + return books.generate("summary", "website") + .then(function(_book) { + book = _book; + }); + }); + + it('should create files according to summary', function() { + book.should.have.file("index.html"); + book.should.have.file("PAGE1.html"); + book.should.have.file("folder/PAGE2.html"); + }); + }); +}); diff --git a/test/templating.js b/test/templating.js index 9165d7a..af29d25 100644 --- a/test/templating.js +++ b/test/templating.js @@ -1,37 +1,33 @@ -var path = require('path'); -var _ = require('lodash'); -var assert = require('assert'); -var fs = require("fs"); - var pkg = require("../package.json"); describe('Templating', function () { - before(function(done) { - testGeneration(books[0], "website", function(output) {}, done); + var book; + + before(function() { + return books.parse("basic") + .then(function(_book) { + book = _book; + }); }); var testTpl = function(str, args, options) { - return books[0].template.renderString(str, args, options) - .then(books[0].template.postProcess) + return book.template.renderString(str, args, options) + .then(book.template.postProcess) }; - it('should correctly have access to generator', function(done) { - qdone( - testTpl('{{ gitbook.generator }}') - .then(function(content) { - assert.equal(content, "website"); - }), - done - ); - }); + describe('Context', function() { + it('should correctly have access to generator', function() { + return testTpl('{{ gitbook.generator }}') + .then(function(content) { + content.should.equal("website"); + }); + }); - it('should correctly have access to gitbook version', function(done) { - qdone( - testTpl('{{ gitbook.version }}') - .then(function(content) { - assert.equal(content, pkg.version.toString()); - }), - done - ); + it('should correctly have access to gitbook version', function() { + return testTpl('{{ gitbook.version }}') + .then(function(content) { + content.should.equal(pkg.version.toString()); + }); + }); }); }); diff --git a/test/website.js b/test/website.js index afbe7aa..91340a0 100644 --- a/test/website.js +++ b/test/website.js @@ -1,40 +1,25 @@ +var fs = require('fs'); var path = require('path'); -var _ = require('lodash'); -var assert = require('assert'); -var fs = require("fs"); -var fsUtil = require("../lib/utils/fs"); +describe('Website generator', function () { + describe('Basic Book', function() { + var book; + before(function() { + return books.generate("basic", "website") + .then(function(_book) { + book = _book; + }); + }); -describe('Website Generator', function () { - it('should correctly generate a book to website', function(done) { - testGeneration(books[1], "website", function(output) { - assert(fs.existsSync(path.join(output, "index.html"))); - assert(fs.existsSync(path.join(output, "search_index.json"))); - }, done); - }); - - it('should correctly include styles in website', function(done) { - testGeneration(books[0], "website", function(output) { - assert(fs.existsSync(path.join(output, "styles/website.css"))); - - var INDEX = fs.readFileSync(path.join(output, "index.html")).toString(); - assert(INDEX.indexOf("styles/website.css") > 0); - }, done); - }); - - it('should correctly include glossary in website', function(done) { - testGeneration(books[0], "website", function(output) { - assert(fs.existsSync(path.join(output, "GLOSSARY.html"))); - }, done); - }); + it('should correctly output an index.html', function() { + book.should.have.file("index.html"); + }); - it('should correctly generate a multilingual book to website', function(done) { - testGeneration(books[2], "website", function(output) { - assert(fs.existsSync(path.join(output, "index.html"))); - assert(fs.existsSync(path.join(output, "gitbook"))); - assert(fs.existsSync(path.join(output, "fr/index.html"))); - assert(fs.existsSync(path.join(output, "en/index.html"))); - }, done); + it('should correctly copy assets', function() { + book.should.have.file("gitbook"); + book.should.have.file("gitbook/app.js"); + book.should.have.file("gitbook/style.css"); + }); }); }); |