diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/assertions.js | 13 | ||||
-rw-r--r-- | test/books/conrefs/README.md | 6 | ||||
-rw-r--r-- | test/books/highlight/README.md | 15 | ||||
-rw-r--r-- | test/books/highlight/SUMMARY.md | 1 | ||||
-rw-r--r-- | test/books/init/SUMMARY.md | 3 | ||||
-rw-r--r-- | test/codehighlighting.js | 58 | ||||
-rw-r--r-- | test/configuration.js | 8 | ||||
-rw-r--r-- | test/conrefs.js | 31 | ||||
-rw-r--r-- | test/ebook.js | 19 | ||||
-rw-r--r-- | test/helper.js | 12 | ||||
-rw-r--r-- | test/init.js | 1 | ||||
-rw-r--r-- | test/json.js | 2 | ||||
-rw-r--r-- | test/navigation.js | 61 | ||||
-rw-r--r-- | test/plugins.js | 42 | ||||
-rw-r--r-- | test/plugins/replace_highlight/index.js | 11 | ||||
-rw-r--r-- | test/plugins/replace_highlight/package.json | 9 | ||||
-rw-r--r-- | test/resolve.js | 61 |
17 files changed, 340 insertions, 13 deletions
diff --git a/test/assertions.js b/test/assertions.js index 7a34380..f81645b 100644 --- a/test/assertions.js +++ b/test/assertions.js @@ -26,14 +26,27 @@ should.Assertion.add('html', function(rules, description) { _.each(rules, function(validations, query) { validations = _.defaults(validations || {}, { + // Select a specific element in the list of matched elements + index: null, + + // Check that there is the correct count of elements count: 1, + + // Check attribute values attributes: {}, + + // Trim inner text trim: false, + + // Check inner text text: undefined }); var $el = $(query); + // Select correct element + if (_.isNumber(validations.index)) $el = $($el.get(validations.index)); + // Test number of elements $el.length.should.be.equal(validations.count); diff --git a/test/books/conrefs/README.md b/test/books/conrefs/README.md index 7f56eed..804a77a 100644 --- a/test/books/conrefs/README.md +++ b/test/books/conrefs/README.md @@ -3,8 +3,10 @@ ### Relative <p id="t1">{% include "./hello.md" %}</p> +<p id="t2">{% 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> +<p id="t3">{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md" %}</p> +<p id="t4">{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test2.md" %}</p> +<p id="t5">{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test3.md" %}</p> diff --git a/test/books/highlight/README.md b/test/books/highlight/README.md new file mode 100644 index 0000000..417fabc --- /dev/null +++ b/test/books/highlight/README.md @@ -0,0 +1,15 @@ +# Readme + +Block without language + +``` +test 1 +``` + +Block with a language + +```lang +test 2 +``` + +Inline code: `test 3`
\ No newline at end of file diff --git a/test/books/highlight/SUMMARY.md b/test/books/highlight/SUMMARY.md new file mode 100644 index 0000000..ac9323c --- /dev/null +++ b/test/books/highlight/SUMMARY.md @@ -0,0 +1 @@ +# Summary diff --git a/test/books/init/SUMMARY.md b/test/books/init/SUMMARY.md index 1e63aed..31c1561 100644 --- a/test/books/init/SUMMARY.md +++ b/test/books/init/SUMMARY.md @@ -4,3 +4,6 @@ * [Hello 2](hello2.md) * Hello 3 * [Hello 4](hello3/hello4.md) + * Hello 5 + * [Hello 6](hello3/hello5/hello6.md) + diff --git a/test/codehighlighting.js b/test/codehighlighting.js new file mode 100644 index 0000000..e35f37e --- /dev/null +++ b/test/codehighlighting.js @@ -0,0 +1,58 @@ +var _ = require('lodash'); +var should = require('should'); +var path = require('path'); +var fs = require('fs'); + +var Plugin = require('../lib/plugin'); +var PLUGINS_ROOT = path.resolve(__dirname, 'plugins'); + +describe('Code Highlighting', function () { + var book, PAGE; + + before(function() { + return books.generate('highlight', 'website', { + prepare: function(_book) { + book = _book; + + var plugin = new Plugin(book, "replace_highlight"); + plugin.load("./replace_highlight", PLUGINS_ROOT); + + book.plugins.load(plugin); + } + }) + .then(function() { + PAGE = fs.readFileSync( + path.join(book.options.output, "index.html"), + { encoding: "utf-8" } + ); + }); + }); + + it('should correctly replace highlighting', function() { + PAGE.should.be.html({ + "code": { + index: 0, + text: 'code_test 1\n_code' + } + }); + }); + + it('should correctly replace highlighting with language', function() { + PAGE.should.be.html({ + "code": { + index: 1, + text: 'lang_test 2\n_lang' + } + }); + }); + + it('should correctly replace highlighting for inline code', function() { + PAGE.should.be.html({ + "code": { + index: 2, + text: 'code_test 3_code' + } + }); + }); +}); + diff --git a/test/configuration.js b/test/configuration.js index eedec49..2cff26e 100644 --- a/test/configuration.js +++ b/test/configuration.js @@ -29,4 +29,12 @@ describe('Configuration', function () { book.options.title.should.be.equal("js-config"); }); }); + + it('should provide configuration on book.config.get', function() { + return books.parse("basic") + .then(function(book) { + book.config.get('description').should.be.equal("Default description for the book."); + book.getConfig('description').should.be.equal("Default description for the book."); + }); + }); }); diff --git a/test/conrefs.js b/test/conrefs.js index 8d6a181..7e044f5 100644 --- a/test/conrefs.js +++ b/test/conrefs.js @@ -26,15 +26,40 @@ describe('ConRefs', function () { }); }); - it('should handle git references', function() { + it('should handle local references with absolute paths', function() { readme.should.be.html({ ".page-inner p#t2": { count: 1, - text: "Hello from git", + text: "Hello World", trim: true - }, + } + }); + }); + + it('should correctly include file from git reference', function() { + readme.should.be.html({ ".page-inner p#t3": { count: 1, + text: "Hello from git", + trim: true + } + }); + }); + + it('should correctly handle deep include in git reference', function() { + readme.should.be.html({ + ".page-inner p#t4": { + count: 1, + text: "First Hello. Hello from git", + trim: true + } + }); + }); + + it('should correctly handle absolute include in git reference', function() { + readme.should.be.html({ + ".page-inner p#t5": { + count: 1, text: "First Hello. Hello from git", trim: true } diff --git a/test/ebook.js b/test/ebook.js index 9b353d2..033fd04 100644 --- a/test/ebook.js +++ b/test/ebook.js @@ -37,12 +37,23 @@ describe('eBook generator', function () { path.join(book.options.output, "index.html"), { encoding: "utf-8" } ); + + // There are 2 styles (one from plugin-highlight and the new style) PAGE.should.be.html({ "link": { - count: 1, - attributes: { - href: "./styles/print.css" - } + count: 2 + } + }); + + PAGE.should.be.html({ + "link[href='./styles/print.css']": { + count: 1 + } + }); + + PAGE.should.be.html({ + "link[href='gitbook/plugins/gitbook-plugin-highlight/ebook.css']": { + count: 1 } }); }); diff --git a/test/helper.js b/test/helper.js index f6b671b..f4432a9 100644 --- a/test/helper.js +++ b/test/helper.js @@ -17,10 +17,18 @@ var TMPDIR = os.tmpdir(); // Generate and return a book -function generateBook(bookId, test) { +function generateBook(bookId, test, opts) { + opts = _.defaults(opts || {}, { + prepare: function() {} + }); + return parseBook(bookId, test) .then(function(book) { - return book.generate(test) + + return Q(opts.prepare(book)) + .then(function() { + return book.generate(test); + }) .thenResolve(book); }); } diff --git a/test/init.js b/test/init.js index 3ba701f..8415b20 100644 --- a/test/init.js +++ b/test/init.js @@ -19,5 +19,6 @@ describe('Init Books', function () { should(fs.existsSync(path.resolve(initRoot, "hello.md"))).be.ok; should(fs.existsSync(path.resolve(initRoot, "hello2.md"))).be.ok; should(fs.existsSync(path.resolve(initRoot, "hello3/hello4.md"))).be.ok; + should(fs.existsSync(path.resolve(initRoot, "hello3/hello5/hello6.md"))).be.ok; }); }); diff --git a/test/json.js b/test/json.js index 0e50237..758cfd7 100644 --- a/test/json.js +++ b/test/json.js @@ -35,7 +35,7 @@ describe('JSON generator', function () { 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"); + page.sections[0].should.have.property("type", "normal"); }); it('should contains valid progress', function() { diff --git a/test/navigation.js b/test/navigation.js new file mode 100644 index 0000000..ddcc86e --- /dev/null +++ b/test/navigation.js @@ -0,0 +1,61 @@ +var should = require('should'); + +describe('Navigation', function () { + var book; + + before(function() { + return books.parse("summary") + .then(function(_book) { + book = _book; + }); + }); + + it('should correctly parse navigation as a map', function() { + book.should.have.property("navigation"); + book.navigation.should.have.property("README.md"); + book.navigation.should.have.property("README.md"); + }); + + it('should correctly include filenames', function() { + book.navigation.should.have.property("README.md"); + book.navigation.should.have.property("PAGE1.md"); + book.navigation.should.have.property("folder/PAGE2.md"); + book.navigation.should.not.have.property("NOTFOUND.md"); + }); + + it('should correctly detect next/prev for README', function() { + var README = book.navigation['README.md']; + + README.index.should.equal(0); + README.should.have.property('next'); + should(README.prev).not.be.ok(); + + README.next.should.have.property('path'); + README.next.path.should.equal('PAGE1.md'); + }); + + it('should correctly detect next/prev a page', function() { + var PAGE = book.navigation['PAGE1.md']; + + PAGE.index.should.equal(1); + PAGE.should.have.property('next'); + PAGE.should.have.property('prev'); + + PAGE.prev.should.have.property('path'); + PAGE.prev.path.should.equal('README.md'); + + PAGE.next.should.have.property('path'); + PAGE.next.path.should.equal('folder/PAGE2.md'); + }); + + it('should correctly detect next/prev for last page', function() { + var PAGE = book.navigation['folder/PAGE2.md']; + + PAGE.index.should.equal(2); + PAGE.should.have.property('prev'); + should(PAGE.next).not.be.ok(); + + PAGE.prev.should.have.property('path'); + PAGE.prev.path.should.equal('PAGE1.md'); + }); +}); diff --git a/test/plugins.js b/test/plugins.js index e3d0c49..6d5b9de 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -1,8 +1,10 @@ var _ = require('lodash'); var should = require('should'); var path = require('path'); +var fs = require('fs'); var Plugin = require('../lib/plugin'); +var parsers = require("gitbook-parsers"); var PLUGINS_ROOT = path.resolve(__dirname, 'plugins'); describe('Plugins', function () { @@ -88,7 +90,12 @@ describe('Plugins', function () { it('should extend books plugins', function() { var resources = book.plugins.resources("ebook"); - resources["css"].should.have.lengthOf(1); + + // There is resources from highlight plugin and this plugin + resources["css"].should.have.lengthOf(2); + should.exist(_.find(resources["css"], { + path: './resources/test' + })); }); }); }); @@ -200,5 +207,38 @@ describe('Plugins', function () { }); }); }); + + describe('Blocks without parsing', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "blocks"); + plugin.load("./blocks", PLUGINS_ROOT); + + return book.plugins.load(plugin); + }); + + var testTpl = function(markup, str, args, options) { + var filetype = parsers.get(markup); + + return book.template.renderString(str, args, options) + .then(filetype.page).get('sections').get(0).get('content') + .then(book.template.postProcess) + }; + + it('should correctly process unparsable for markdown', function() { + return testTpl('.md', '{% test %}**hello**{% endtest %}') + .then(function(content) { + content.should.equal("<p>test**hello**test</p>\n"); + }); + }); + + it('should correctly process unparsable for asciidoc', function() { + return testTpl('.adoc', '{% test %}**hello**{% endtest %}') + .then(function(content) { + content.should.equal('<div class="paragraph">\n<p>test**hello**test</p>\n</div>'); + }); + }); + }); }); diff --git a/test/plugins/replace_highlight/index.js b/test/plugins/replace_highlight/index.js new file mode 100644 index 0000000..25f9642 --- /dev/null +++ b/test/plugins/replace_highlight/index.js @@ -0,0 +1,11 @@ +module.exports = { + blocks: { + "code": { + process: function(blk) { + var lang = blk.kwargs.language || 'code'; + + return lang+"_"+blk.body+"_"+lang; + } + } + } +};
\ No newline at end of file diff --git a/test/plugins/replace_highlight/package.json b/test/plugins/replace_highlight/package.json new file mode 100644 index 0000000..72d1033 --- /dev/null +++ b/test/plugins/replace_highlight/package.json @@ -0,0 +1,9 @@ +{ + "name": "gitbook-plugin-replace_highlight", + "description": "Test replacing default code highlighter", + "main": "index.js", + "version": "0.0.1", + "engines": { + "gitbook": "*" + } +}
\ No newline at end of file diff --git a/test/resolve.js b/test/resolve.js new file mode 100644 index 0000000..b31a10d --- /dev/null +++ b/test/resolve.js @@ -0,0 +1,61 @@ +var fs = require('fs'); +var path = require('path'); + +describe('Resolve Files', function () { + var book; + + before(function() { + return books.parse("basic") + .then(function(_book) { + book = _book; + }); + }); + + describe('book.fileIsInBook', function() { + it('should return true for correct paths', function() { + book.fileIsInBook(path.join(book.root, 'README.md')).should.equal(true); + book.fileIsInBook(path.join(book.root, 'styles/website.css')).should.equal(true); + }); + + it('should return true for root folder', function() { + book.fileIsInBook(path.join(book.root, './')).should.equal(true); + book.fileIsInBook(book.root).should.equal(true); + }); + + it('should return false for files out of scope', function() { + book.fileIsInBook(path.join(book.root, '../')).should.equal(false); + book.fileIsInBook('README.md').should.equal(false); + book.fileIsInBook(path.resolve(book.root, '../README.md')).should.equal(false); + }); + + it('should correctly handle windows paths', function() { + book.fileIsInBook(path.join(book.root, '\\styles\\website.css')).should.equal(true); + }); + }); + + describe('book.resolve', function() { + it('should resolve a file to its absolute path', function() { + book.resolve('README.md').should.equal(path.resolve(book.root, 'README.md')); + book.resolve('website/README.md').should.equal(path.resolve(book.root, 'website/README.md')); + }); + + it('should correctly handle windows paths', function() { + book.resolve('styles\\website.css').should.equal(path.resolve(book.root, 'styles\\website.css')); + }); + + it('should correctly resolve all arguments', function() { + book.resolve('test', 'hello', '..', 'README.md').should.equal(path.resolve(book.root, 'test/README.md')); + }); + + it('should correctly resolve to root folder', function() { + book.resolve('test', '/README.md').should.equal(path.resolve(book.root, 'README.md')); + book.resolve('test', '\\README.md').should.equal(path.resolve(book.root, 'README.md')); + }); + + it('should throw an error for file out of book', function() { + (function() { + return book.resolve('../README.md'); + }).should.throw(); + }); + }); +}); |