From ee521a52a84be00bab5f83f045bcf1e92416ac94 Mon Sep 17 00:00:00 2001 From: Henning Peters Date: Thu, 24 Sep 2015 16:57:11 +0200 Subject: [PATCH] proposal for doctests --- .travis.yml | 2 +- website/Makefile | 7 ++- website/run_jade | 59 +++++++++++++++++++ website/src/jade/home/_usage.jade | 19 +----- .../tests/test_get_tokens_and_sentences.txt | 8 +++ .../test_load_resources_and_process_text.txt | 5 ++ .../test_use_interger_ids_for_any_strings.txt | 10 ++++ 7 files changed, 91 insertions(+), 19 deletions(-) create mode 100755 website/run_jade create mode 100644 website/tests/test_get_tokens_and_sentences.txt create mode 100644 website/tests/test_load_resources_and_process_text.txt create mode 100644 website/tests/test_use_interger_ids_for_any_strings.txt diff --git a/.travis.yml b/.travis.yml index f21301db1..1ea1f8375 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,4 +24,4 @@ install: # run tests script: - - "py.test tests/ -x" + - "py.test tests/ website/tests/ -x" diff --git a/website/Makefile b/website/Makefile index e2002c97f..87c9077ec 100644 --- a/website/Makefile +++ b/website/Makefile @@ -1,9 +1,12 @@ -all: site +all: dir site + +dir: + mkdir -p site site: site/index.html site/blog/ site/docs/ site/license/ site/blog/introducing-spacy/ site/blog/parsing-english-in-python/ site/blog/part-of-speech-POS-tagger-in-python/ site/tutorials/twitter-filter/ site/tutorials/syntax-search/ site/tutorials/mark-adverbs/ site/blog/writing-c-in-cython/ site/blog/how-spacy-works/ site/index.html: src/jade/header.jade src/jade/*.jade - jade -P src/jade/home/index.jade --out site/ + ./run_jade src/jade/home/index.jade $@ site/docs/: src/jade/docs/*.jade src/jade/header.jade jade -P src/jade/docs/index.jade --out $@ diff --git a/website/run_jade b/website/run_jade new file mode 100755 index 000000000..83ac87ef7 --- /dev/null +++ b/website/run_jade @@ -0,0 +1,59 @@ +#!/usr/bin/env node +'use strict'; + +var fs = require('fs'); +var jade = require('jade'); + +// returns all: code and return value (default) +jade.filters.doctest_all = function (html, _, use_rv) { + use_rv = use_rv === undefined ? true : use_rv; + + var lines = html.trim().split(/\n/), + block = [], + res = ''; + + lines.forEach(function (line) { + if (line.indexOf('>>> ') === 0) { + // we use ### to suppress lines + if (line.indexOf("###") === -1) { + block.push(line.replace(/^>>> /gm, '')); + } + } else if (block.length > 0) { + res += '
' + block.join('\n') + '
'; + block = []; + + if (use_rv) { + res += '

Which produces:

'; + res += '
' + line + '
'; + } + } + }); + + if (block.length > 0) { + res += '
' + block.join('\n') + '
'; + } + + return res; +}; + +// returns only code +jade.filters.doctest = function (html) { + return jade.filters.doctest_all(html, null, false); +}; + +if (process.argv[0] === "node") { + process.argv.shift(); +} + +var in_file = process.argv[1]; +var out_file = process.argv[2]; + +var html = jade.renderFile(in_file, { + pretty: true +}); + +fs.writeFile(out_file, html, function (err) { + if (err) { + throw err; + } +}); diff --git a/website/src/jade/home/_usage.jade b/website/src/jade/home/_usage.jade index a26823358..12dc9e2a5 100644 --- a/website/src/jade/home/_usage.jade +++ b/website/src/jade/home/_usage.jade @@ -6,26 +6,13 @@ mixin example(name) +example("Load resources and process text") - pre.language-python: code - | from __future__ import unicode_literals, print_function - | from spacy.en import English - | nlp = English() - | doc = nlp('Hello, world. Here are two sentences.') + include:doctest_all ../../../tests/test_load_resources_and_process_text.txt +example("Get tokens and sentences") - pre.language-python: code - | token = doc[0] - | sentence = doc.sents.next() - | assert token is sentence[0] - | assert sentence.text == 'Hello, world.' + include:doctest ../../../tests/test_get_tokens_and_sentences.txt +example("Use integer IDs for any string") - pre.language-python: code - | hello_id = nlp.vocab.strings['Hello'] - | hello_str = nlp.vocab.strings[hello_id] - | - | assert token.orth == hello_id == 469755 - | assert token.orth_ == hello_str == 'Hello' + include:doctest ../../../tests/test_use_interger_ids_for_any_strings.txt +example("Get and set string views and flags") pre.language-python: code diff --git a/website/tests/test_get_tokens_and_sentences.txt b/website/tests/test_get_tokens_and_sentences.txt new file mode 100644 index 000000000..e649a6f6d --- /dev/null +++ b/website/tests/test_get_tokens_and_sentences.txt @@ -0,0 +1,8 @@ +>>> from spacy.en import English ### +>>> nlp = English() ### +>>> doc = nlp(u'Hello, world. Here are two sentences.') ### +>>> +>>> token = doc[0] +>>> sentence = doc.sents.next() +>>> assert token is sentence[0] +>>> assert sentence.text == 'Hello, world.' diff --git a/website/tests/test_load_resources_and_process_text.txt b/website/tests/test_load_resources_and_process_text.txt new file mode 100644 index 000000000..9937be1b0 --- /dev/null +++ b/website/tests/test_load_resources_and_process_text.txt @@ -0,0 +1,5 @@ +>>> from spacy.en import English +>>> nlp = English() +>>> doc = nlp(u'Hello, world. Here are two sentences.') +>>> print([s.string for s in doc.sents]) +[u'Hello, world. ', u'Here are two sentences.'] diff --git a/website/tests/test_use_interger_ids_for_any_strings.txt b/website/tests/test_use_interger_ids_for_any_strings.txt new file mode 100644 index 000000000..ec66c2729 --- /dev/null +++ b/website/tests/test_use_interger_ids_for_any_strings.txt @@ -0,0 +1,10 @@ +>>> from spacy.en import English ### +>>> nlp = English() ### +>>> doc = nlp(u'Hello, world. Here are two sentences.') ### +>>> token = doc[0] ### +>>> +>>> hello_id = nlp.vocab.strings['Hello'] +>>> hello_str = nlp.vocab.strings[hello_id] +>>> +>>> assert token.orth == hello_id == 469755 +>>> assert token.orth_ == hello_str == 'Hello'