Skip to content

Commit

Permalink
Merge pull request #15 from cirosantilli/add-tests
Browse files Browse the repository at this point in the history
Automated tests. Thanks to @cirosantilli
  • Loading branch information
karlcow committed Mar 28, 2014
2 parents a76c0ce + 735a029 commit 20c113f
Show file tree
Hide file tree
Showing 19 changed files with 436 additions and 29 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1 +1,3 @@
/inputs.tmp.md
/all.tmp.md
/config_local.py
*.pyc
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -4,6 +4,23 @@ Inspired by questions on [W3C Markdown Community Group](http://www.w3.org/commun

Pull Requests are welcome.

## Test Scripts

Markdown Test Suite already includes tests for important markdown engines listed under:

ls tests/extensions

Install script dependencies with:

sudo pip install -r requirements.txt

The scripts are:

- `run-tests.py`
- `cat-all.py`

Read the file level docstring of each script to see what it does.

## Markdown Extensions

Markdown extension tests are accepted. Use the following rules:
Expand Down Expand Up @@ -63,3 +80,9 @@ If the input is the same, and outputs are DOM different, but represent the same
tests/extensions/gfm/fenced-code-block.out
tests/extensions/fenced-code-block.md
tests/extensions/fenced-code-block.out

### New Extensions

Tests are modular, and if you want to test a new engine for your project, that should be easy to do.

If you feel that the new engine is reasonably popular, please send a pull request adding it.
31 changes: 31 additions & 0 deletions cat-all.py
@@ -0,0 +1,31 @@
#!/usr/bin/env python

"""
Concatenate all input output pairs into a single markdown file with format:
# Path
Input
---
Output
# Path2
...
"""

import itertools

import md_testsuite

output_path = u"all.tmp.md"

assert [i for i in itertools.chain(xrange(2), xrange(2))] == [0, 1, 0, 1]

with open(output_path, "w") as output_file: pass
for path, input, output in itertools.chain(md_testsuite.io_iterator(),
md_testsuite.io_iterator_all_engines()):
with open(output_path, "a") as output_file:
output_file.write("# {0}\n\n{1}\n\n---\n\n{2}\n\n".format(path, input, output))
25 changes: 0 additions & 25 deletions cat-input.py

This file was deleted.

16 changes: 16 additions & 0 deletions config_local.py.example
@@ -0,0 +1,16 @@
# This dict contains configurations which users may want to, or are required to personalize.
# Some values can be omitted and have defaults set for them on `md_testsuite.py`.
config = dict(

# Required for GFM.
# Generate token on GitHub website on Profile > Applications > Generate
# Turn off all abilities by unchecking all boxes: none are necessary for markdown compilation.
#gfm_oauth_token = '',

# Listed methods will be excluded from the `run-tests.py` all tests invocation.
# `[]` to run all engines.
#run_all_disable = [],

# Timeout in seconds.
#timeout = 5
)
107 changes: 107 additions & 0 deletions md_testsuite.py
@@ -0,0 +1,107 @@
"""
Shared functionality.
"""

import imp
import os

# Default config values here.
config = dict(
gfm_oauth_token = '',
# GFM off by default because it is too slow.
run_all_disable = ['gfm'],
timeout = 5
)
try:
config_custom = imp.load_source('config_local', 'config_local.py').config
config.update(config_custom)
except IOError:
# Config file not present.
pass

# Encoding of all out outputs when we can chose it, hopefully equal to most inputs.
encoding = 'utf-8'
in_ext = u".md"
out_ext = u".out"
test_dir = u"tests"
engines_dir = os.path.join(test_dir, u"extensions")

# Output UTF-8 by default.
import sys
reload(sys)
sys.setdefaultencoding(encoding)

def same_basename_on_parent(path):
path_split = path.split(os.sep)
return os.sep.join(path_split[0:-2] + [path_split[-1]])

def io_iterator():
"""
Iterator over all input output pairs of the Original markdown, no engines.
Each yield returns a 3-tuple `(path, input, output)` where:
- `path` is the path of the test relative to the `test_dir` and without engine (`.md` or `.out`).
- `input` and `output` are the content of the input and output files.
"""
for basename in sorted(os.listdir(test_dir)):
input_path = os.path.join(test_dir, basename)
if os.path.isfile(input_path):
path_noext, ext = os.path.splitext(input_path)
if ext == in_ext:
with open(input_path, "r") as input_file:
input = input_file.read().decode(encoding)
output_path = path_noext + out_ext
with open(output_path, "r") as output_file:
output = output_file.read().decode(encoding)
yield (path_noext[(len(test_dir)+1):], input, output)

def io_iterator_engine(id):
"""
Iterator over all input output pairs of a given engine.
Original markdown is not included.
"""
engine_dir = os.path.join(engines_dir, id)
for basename in sorted(os.listdir(engine_dir)):
input_path = os.path.join(engine_dir, basename)
if os.path.isfile(input_path):
path_noext, ext = os.path.splitext(input_path)
if ext == in_ext:
# Get input
with open(input_path, "r") as input_file:
input = input_file.read().decode(encoding)
if not input:
with open(same_basename_on_parent(input_path), "r") as input_file:
input = input_file.read().decode(encoding)

# Get output
output_path = path_noext + out_ext
if not os.path.exists(output_path):
output_path = same_basename_on_parent(output_path)
with open(output_path, "r") as output_file:
output = output_file.read().decode(encoding)

yield (path_noext[(len(test_dir)+1):], input, output)

def get_engine_ids():
"""
Returns a sorted list of ids of all supported engines
based on the directories present under the engines directory.
"""
output = []
for basename in sorted(os.listdir(engines_dir)):
path = os.path.join(engines_dir, basename)
if os.path.isdir(path):
output.append(basename)
return output

def io_iterator_all_engines():
"""
Iterator over all input output pairs of a all engines
Original markdown is not included.
"""
for id in get_engine_ids():
for io in io_iterator_engine(id):
yield io
1 change: 1 addition & 0 deletions requirements.txt
@@ -0,0 +1 @@
beautifulsoup4==4.3.2

0 comments on commit 20c113f

Please sign in to comment.