# HG changeset patch # User Steve Losh # Date 1259349575 18000 # Node ID 22fefa553086bb5387f1b952f2785a755deaef1f # Parent 0fbe174dbb700885f77c4567f59806ad34868ca6 Start adding some unit tests. diff -r 0fbe174dbb70 -r 22fefa553086 tests/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/__init__.py Fri Nov 27 14:19:35 2009 -0500 @@ -0,0 +1,5 @@ +"""Unit tests for hg-prompt. + +The tests require nose: pip install nose && nosetests --with-doctest -v + +""" \ No newline at end of file diff -r 0fbe174dbb70 -r 22fefa553086 tests/test_branch.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_branch.py Fri Nov 27 14:19:35 2009 -0500 @@ -0,0 +1,42 @@ +'''Test output of {branch}.''' + +from nose import * +from util import * +from mercurial import commands + + +@with_setup(setup_sandbox, teardown_sandbox) +def test_default_branch(): + output = prompt(fs='{branch}') + assert output == 'default' + + output = prompt(fs='{on {branch}}') + assert output == 'on default' + + +@with_setup(setup_sandbox, teardown_sandbox) +def test_non_default_branch(): + commands.branch(get_sandbox_ui(), get_sandbox_repo(), 'test') + + output = prompt(fs='{branch}') + assert output == 'test' + + output = prompt(fs='{on the {branch} branch}') + assert output == 'on the test branch' + + +@with_setup(setup_sandbox, teardown_sandbox) +def test_quiet_filter(): + output = prompt(fs='{branch|quiet}') + assert output == '' + + output = prompt(fs='{on {branch|quiet}}') + assert output == '' + + commands.branch(get_sandbox_ui(), get_sandbox_repo(), 'test') + + output = prompt(fs='{branch|quiet}') + assert output == 'test' + + output = prompt(fs='{on the {branch|quiet} branch}') + assert output == 'on the test branch' diff -r 0fbe174dbb70 -r 22fefa553086 tests/test_none.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_none.py Fri Nov 27 14:19:35 2009 -0500 @@ -0,0 +1,22 @@ +'''Test output without keywords.''' + +from nose import * +from util import * + + +@with_setup(setup_sandbox, teardown_sandbox) +def test_blank(): + output = prompt(fs='') + assert output == '' + + +@with_setup(setup_sandbox, teardown_sandbox) +def test_text(): + output = prompt(fs='test one two three') + assert output == 'test one two three' + + +@with_setup(setup_sandbox, teardown_sandbox) +def test_invalid_keyword(): + output = prompt(fs='{invalidkeyword}') + assert output == '{invalidkeyword}' diff -r 0fbe174dbb70 -r 22fefa553086 tests/util.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/util.py Fri Nov 27 14:19:35 2009 -0500 @@ -0,0 +1,35 @@ +"""Utilities for writing unit tests for hg-prompt.""" + +import os, shutil, sys +from mercurial import cmdutil, commands, hg, ui + +pkg_path = os.path.realpath(__file__) +sys.path =[os.path.split(os.path.split(pkg_path)[0])[0]] + sys.path +from prompt import prompt as _prompt + +_ui = ui.ui() +def prompt(fs=''): + _ui.pushbuffer() + _prompt(_ui, get_sandbox_repo(), fs=fs) + output = _ui.popbuffer() + + print output + return output + +sandbox_path = os.path.join(os.path.realpath('.'), 'sandbox') + +def setup_sandbox(): + os.mkdir(sandbox_path) + os.chdir(sandbox_path) + + commands.init(_ui) + +def teardown_sandbox(): + os.chdir(os.path.realpath(os.path.join(sandbox_path, os.pardir))) + shutil.rmtree(sandbox_path) + +def get_sandbox_repo(): + return hg.repository(_ui, sandbox_path) + +def get_sandbox_ui(): + return _ui \ No newline at end of file