Commit f78eca9d authored by Matěj Cepl's avatar Matěj Cepl

More tests and deleted yamlishwriter-php (there is really nothing there).

parent 7d08baca
def TODO(func):
"""unittest test method decorator that ignores
exceptions raised by test
Used to annotate test methods for code that may
not be written yet. Ignores failures in the
annotated test method; fails if the text
unexpectedly succeeds.
"""
def wrapper(*args, **kw):
try:
func(*args, **kw)
succeeded = True
except:
succeeded = False
assert succeeded is False, \
"%s marked TODO but passed" % func.__name__
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper
import unittest
import load, input, reader, output, writer
import test_load
import test_input
#import test_reader
import test_output
#import test_writer
add_tests_from_class = unittest.TestLoader().loadTestsFromTestCase
if __name__ == "__main__":
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(test_load)
suite.addTests(loader.loadTestsFromModule(test_input))
#suite.addTests(loader.loadTestsFromModule(test_reader))
suite.addTests(loader.loadTestsFromModule(test_output))
#suite.addTests(loader.loadTestsFromModule(test_writer))
suite = unittest.TestSuite()
suite.addTest(add_tests_from_class(load.TestBasics))
suite.addTest(add_tests_from_class(TestHappyPath))
suite.addTest(add_tests_from_class(TestBadPath))
suite.addTest(add_tests_from_class(TestPiglitData))
suite.addTest(add_tests_from_class(TestMainArgsMgmt))
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
use strict;
use warnings;
use Test::More tests => 9;
use Data::Dumper;
use Data::YAML::Writer;
my $out = [
"---",
"bill-to:",
" address:",
" city: 'Royal Oak'",
" lines: \"458 Walkman Dr.\\nSuite #292\\n\"",
" postal: 48046",
" state: MI",
" family: Dumars",
" given: Chris",
"comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"",
"date: 2001-01-23",
"invoice: 34843",
"product:",
" -",
" description: Basketball",
" price: 450.00",
" quantity: 4",
" sku: BL394D",
" -",
" description: 'Super Hoop'",
" price: 2392.00",
" quantity: 1",
" sku: BL4438H",
"tax: 251.42",
"total: 4443.52",
"...",
];
my $in = {
'bill-to' => {
'given' => 'Chris',
'address' => {
'city' => 'Royal Oak',
'postal' => '48046',
'lines' => "458 Walkman Dr.\nSuite #292\n",
'state' => 'MI'
},
'family' => 'Dumars'
},
'invoice' => '34843',
'date' => '2001-01-23',
'tax' => '251.42',
'product' => [
{
'sku' => 'BL394D',
'quantity' => '4',
'price' => '450.00',
'description' => 'Basketball'
},
{
'sku' => 'BL4438H',
'quantity' => '1',
'price' => '2392.00',
'description' => 'Super Hoop'
}
],
'comments' =>
"Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n",
'total' => '4443.52'
};
my @buf1 = ();
my @buf2 = ();
my $buf3 = '';
my @destination = (
{
name => 'Array reference',
destination => \@buf1,
normalise => sub { return \@buf1 },
},
{
name => 'Closure',
destination => sub { push @buf2, shift },
normalise => sub { return \@buf2 },
},
{
name => 'Scalar',
destination => \$buf3,
normalise => sub {
my @ar = split( /\n/, $buf3 );
return \@ar;
},
},
);
for my $dest ( @destination ) {
my $name = $dest->{name};
ok my $yaml = Data::YAML::Writer->new, "$name: Created";
isa_ok $yaml, 'Data::YAML::Writer';
$yaml->write( $in, $dest->{destination} );
my $got = $dest->{normalise}->();
unless ( is_deeply $got, $out, "$name: Result matches" ) {
local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1;
diag( Data::Dumper->Dump( [$got], ['$got'] ) );
diag( Data::Dumper->Dump( [$out], ['$expected'] ) );
}
}
#!perl -T
use Test::More;
eval "use Test::Pod 1.14";
plan skip_all => "Test::Pod 1.14 required for testing POD" if $@;
all_pod_files_ok();
#!perl -T
use Test::More;
eval "use Test::Pod::Coverage 1.04";
plan skip_all =>
"Test::Pod::Coverage 1.04 required for testing POD coverage"
if $@;
all_pod_coverage_ok(
{ private => [ qr{^BUILD|DEMOLISH|AUTOMETHOD|START$}, qr{^_} ] } );
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import logging
import unittest
import yamlish
logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
level=logging.INFO)
IN = """
---
......@@ -87,7 +90,7 @@ class TestInput(unittest.TestCase):
for src in source:
name = src['name']
yaml = yamlish.Reader()
self.assert_(True, "$name: Created")
self.assert_(True, "%s: Created" % name)
self.assert_(isinstance(yaml, yamlish.Reader))
#my $got = eval { $yaml -> read($src -> {source}) };
......@@ -97,6 +100,3 @@ class TestInput(unittest.TestCase):
observed = %s
""" % (name, OUT, got))
if __name__ == "__main__":
unittest.main()
......@@ -5,6 +5,11 @@ class TestBasics(unittest.TestCase):
def test_import(self):
import yamlish
from yamlish import Reader
self.assert_(True, "Importing Reader.")
from yamlish import Writer
self.assert_(True, "Importing Writer.")
self.assert_(True,
"Testing import of yamlish, version %s." % yamlish.__version__)
if __name__ == "__main__":
unittest.main()
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import re
import unittest
import yamlish
from . import TODO
OUT = [
"---",
"bill-to:",
" address:",
" city: 'Royal Oak'",
" lines: \"458 Walkman Dr.\\nSuite #292\\n\"",
" postal: 48046",
" state: MI",
" family: Dumars",
" given: Chris",
"comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"",
"date: 2001-01-23",
"invoice: 34843",
"product:",
" -",
" description: Basketball",
" price: 450.00",
" quantity: 4",
" sku: BL394D",
" -",
" description: 'Super Hoop'",
" price: 2392.00",
" quantity: 1",
" sku: BL4438H",
"tax: 251.42",
"total: 4443.52",
"...",
]
IN = {
'bill-to': {
'given': 'Chris',
'address': {
'city': 'Royal Oak',
'postal': '48046',
'lines': "458 Walkman Dr.\nSuite #292\n",
'state': 'MI'
},
'family': 'Dumars'
},
'invoice': '34843',
'date': '2001-01-23',
'tax': '251.42',
'product': [
{
'sku': 'BL394D',
'quantity': '4',
'price': '450.00',
'description': 'Basketball'
},
{
'sku': 'BL4438H',
'quantity': '1',
'price': '2392.00',
'description': 'Super Hoop'
}
],
'comments': "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n",
'total': '4443.52'
}
buf1 = []
buf2 = []
buf3 = ""
destination = [
{
"name": 'Array reference',
"destination": buf1,
"normalise": (lambda x: buf1),
},
# {
# "name": 'Closure',
# "destination": sub { push @buf2, shift },
# "normalise": sub { return \@buf2 },
# },
{
"name": 'Scalar',
"destination": buf3,
"normalise": (lambda : re.split(r" \n ", buf3))
}
]
class TestOuptut(unittest.TestCase):
@TODO
def test_output(self):
for dest in destination:
name = dest['name']
yaml = yamlish.Writer()
self.assert_(True, "%s: Created" % name)
self.assert_(isinstance(yaml, yamlish.Writer))
yaml.write(IN, dest[destination])
got = dest['normalise']()
self.assertEqual(got, OUT, """%s: Result matches
expected = %s
observed = %s
""" % (name, OUT, got))
This diff is collapsed.
#!/usr/bin/perl
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
use strict;
use warnings;
use Test::More;
use Data::Dumper;
use Data::YAML::Reader;
use Data::YAML::Writer;
import unittest
import yamlish
my @SCHEDULE;
BEGIN {
@SCHEDULE = (
{
name => 'Simple scalar',
in => 1,
out => [ '--- 1', '...', ],
name= > 'Simple scalar',
in= > 1,
out= > [ '--- 1', '...', ],
},
{
name => 'Undef',
in => undef,
out => [ '--- ~', '...', ],
name= > 'Undef',
in= > undef,
out= > [ '--- ~', '...', ],
},
{
name => 'Unprintable',
in => "\x01\n\t",
out => [ '--- "\x01\n\t"', '...', ],
name= > 'Unprintable',
in= > "\x01\n\t",
out= > [ '--- "\x01\n\t"', '...', ],
},
{
name => 'Simple array',
in => [ 1, 2, 3 ],
out => [ '---', '- 1', '- 2', '- 3', '...', ],
name= > 'Simple array',
in= > [ 1, 2, 3 ],
out= > [ '---', '- 1', '- 2', '- 3', '...', ],
},
{
name => 'Array, two elements, undef',
in => [ undef, undef ],
out => [ '---', '- ~', '- ~', '...', ],
name= > 'Array, two elements, undef',
in= > [ undef, undef ],
out= > [ '---', '- ~', '- ~', '...', ],
},
{
name => 'Nested array',
in => [ 1, 2, [ 3, 4 ], 5 ],
out =>
name= > 'Nested array',
in= > [ 1, 2, [ 3, 4 ], 5 ],
out= >
[ '---', '- 1', '- 2', '-', ' - 3', ' - 4', '- 5', '...', ],
},
{
name => 'Simple hash',
in => { one => '1', two => '2', three => '3' },
out => [ '---', 'one: 1', 'three: 3', 'two: 2', '...', ],
name= > 'Simple hash',
in= > { one= > '1', two= > '2', three= > '3' },
out= > [ '---', 'one: 1', 'three: 3', 'two: 2', '...', ],
},
{
name => 'Nested hash',
in => {
one => '1',
two => '2',
more => { three => '3', four => '4' }
name= > 'Nested hash',
in= > {
one= > '1',
two= > '2',
more= > { three= > '3', four= > '4' }
},
out => [
out= > [
'---',
'more:',
' four: 4',
......@@ -65,55 +62,55 @@ BEGIN {
],
},
{
name => 'Unprintable key',
in => { one => '1', "\x02" => '2', three => '3' },
out => [ '---', '"\x02": 2', 'one: 1', 'three: 3', '...', ],
name= > 'Unprintable key',
in= > { one= > '1', "\x02"= > '2', three= > '3' },
out= > [ '---', '"\x02": 2', 'one: 1', 'three: 3', '...', ],
},
{
name => 'Empty key',
in => { '' => 'empty' },
out => [ '---', "'': empty", '...', ],
name= > 'Empty key',
in= > { ''= > 'empty' },
out= > [ '---', "'': empty", '...', ],
},
{
name => 'Empty value',
in => { '' => '' },
out => [ '---', "'': ''", '...', ],
name= > 'Empty value',
in= > { ''= > '' },
out= > [ '---', "'': ''", '...', ],
},
{
name => 'Complex',
in => {
'bill-to' => {
'given' => 'Chris',
'address' => {
'city' => 'Royal Oak',
'postal' => '48046',
'lines' => "458 Walkman Dr.\nSuite #292\n",
'state' => 'MI'
name= > 'Complex',
in= > {
'bill-to'= > {
'given'= > 'Chris',
'address'= > {
'city'= > 'Royal Oak',
'postal'= > '48046',
'lines'= > "458 Walkman Dr.\nSuite #292\n",
'state'= > 'MI'
},
'family' => 'Dumars'
'family'= > 'Dumars'
},
'invoice' => '34843',
'date' => '2001-01-23',
'tax' => '251.42',
'product' => [
'invoice'= > '34843',
'date'= > '2001-01-23',
'tax'= > '251.42',
'product'= > [
{
'sku' => 'BL394D',
'quantity' => '4',
'price' => '450.00',
'description' => 'Basketball'
'sku'= > 'BL394D',
'quantity'= > '4',
'price'= > '450.00',
'description'= > 'Basketball'
},
{
'sku' => 'BL4438H',
'quantity' => '1',
'price' => '2392.00',
'description' => 'Super Hoop'
'sku'= > 'BL4438H',
'quantity'= > '1',
'price'= > '2392.00',
'description'= > 'Super Hoop'
}
],
'comments' =>
'comments'= >
"Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n",
'total' => '4443.52'
'total'= > '4443.52'
},
out => [
out= > [
"---",
"bill-to:",
" address:",
......@@ -144,7 +141,7 @@ BEGIN {
},
);
plan tests => @SCHEDULE * 5;
plan tests = > @SCHEDULE * 5;
}
sub iter {
......@@ -154,46 +151,46 @@ sub iter {
};
}
for my $test ( @SCHEDULE ) {
my $name = $test->{name};
ok my $yaml = Data::YAML::Writer->new, "$name: Created";
for my $test (@SCHEDULE) {
my $name = $test -> {name};
ok my $yaml = Data::YAML::Writer -> new, "$name: Created";
isa_ok $yaml, 'Data::YAML::Writer';
my $got = [];
my $writer = sub { push @$got, shift };
my $data = $test->{in};
my $data = $test -> {in};
eval { $yaml->write( $data, $writer ) };
eval { $yaml -> write($data, $writer) };
if ( my $err = $test->{error} ) {
unless ( like $@, $err, "$name: Error message" ) {
if (my $err=$test -> {error}) {
unless (like $@, $err, "$name: Error message") {
diag "Error: $@\n";
}
is_deeply $got, [], "$name: No result";
pass;
}
else {
my $want = $test->{out};
unless ( ok !$@, "$name: No error" ) {
my $want = $test -> {out};
unless (ok ! $@, "$name: No error") {
diag "Error: $@\n";
}
unless ( is_deeply $got, $want, "$name: Result matches" ) {
unless (is_deeply $got, $want, "$name: Result matches") {
local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1;
diag( Data::Dumper->Dump( [$got], ['$got'] ) );
diag( Data::Dumper->Dump( [$want], ['$expected'] ) );
diag(Data::Dumper -> Dump([$got], ['$got']));
diag(Data::Dumper -> Dump([$want], ['$expected']));
}
my $yr = Data::YAML::Reader->new;
my $yr = Data::YAML::Reader -> new;
# Now try parsing it
my $reader = sub { shift @$got };
my $parsed = $yr->read( $reader );
my $parsed = $yr -> read($reader);
unless ( is_deeply $parsed, $data, "$name: Reparse OK" ) {
unless (is_deeply $parsed, $data, "$name: Reparse OK") {
local $Data::Dumper::Useqq = $Data::Dumper::Useqq = 1;
diag( Data::Dumper->Dump( [$parsed], ['$parsed'] ) );
diag( Data::Dumper->Dump( [$data], ['$data'] ) );
diag(Data::Dumper -> Dump([$parsed], ['$parsed']));
diag(Data::Dumper -> Dump([$data], ['$data']));
}
}
}
......
......@@ -3,6 +3,8 @@
import yaml
import pprint
__version__ = "0.1"
IN = """
---
bill-to:
......@@ -72,6 +74,11 @@ class Reader(object):
def read(self, source):
pass
class Writer(object):
def __init__(self):
pass
def write(self, source, destination):
pass
#print yaml.dump(OUT, canonical=False, default_flow_style=False, default_style=False)
def read():
pass
0.0.1 2007-03-19
- Initial release
\ No newline at end of file
./Changes
./dist.sh
./lib/yamlishwriter.php
./Makefile
./README
./t/00-load.php
./t/10-writer.php
./t/harness.t
./t/lib/TestLite.php
./MANIFEST
# Makefile for YAMLish
PHP = /usr/bin/php
PROVE = prove -r t
VERSION = 0.0.1
DISTNAME = yamlishwriter-php-v$(VERSION)
default:
test:
PHP=$(PHP) $(PROVE)
dist: manifest
./dist.sh $(DISTNAME)
manifest:
rm -f MANIFEST
find . -type f | grep -vE '\.svn|\.DS_Store|\.tmproj' > MANIFEST
echo './MANIFEST' >> MANIFEST
\ No newline at end of file
YAMLishWriter.php Version 0.0.1
This is a simple implementation of a YAMLish writer for PHP. See
http://testanything.org/wiki/index.php/YAMLish
for more information about YAMLish.
You can find the YAMLishWriter class in lib/yamlishwriter.php in this
distribution. YAMLishWriter is a self contained PHP class. To use it
// Or wherever it is on your include path
include('yamlishwriter.php');
// And then later
$writer = new YAMLishWriter;
$data = array( 'got' => 1, 'expected' => 2 );
$output = $writer->write($data);
The returned value is an array containing the lines of YAMLish.
This distribution contains a rudimentary test harness for which you'll
need Perl prove command. To run the tests edit Makefile to reflect the
location of your php command and then
make test
Copyright 2007 Andy Armstrong, andy@hexten.net.
YAMLishWriter is distributed under Perl Artistic Licence:
http://www.opensource.org/licenses/artistic-license.php
#!/bin/sh
distname=$1
distarc="$distname.tar.gz"
echo "Making $distarc"
for f in `cat MANIFEST`
do
d=`dirname $f`
mkdir -p "$distname/$d"
echo "Adding $f"
cp $f "$distname/$f"
done
tar zcf $distarc $distname
rm -rf $distname
<?php
// Would be nice if this weren't a global function - but I can't find
// out how to make a class member work in preg_replace_callback
function __escape_unprintable($matched) {
$unprintable = array(
'z', 'x01', 'x02', 'x03', 'x04', 'x05', 'x06', 'a',
'x08', 't', 'n', 'v', 'f', 'r', 'x0e', 'x0f',
'x10', 'x11', 'x12', 'x13', 'x14', 'x15', 'x16', 'x17',
'x18', 'x19', 'x1a', 'e', 'x1c', 'x1d', 'x1e', 'x1f',
);
return '\\' . $unprintable[ ord( $matched[0] ) ];
}
class YAMLishWriter {
function YAMLishWriter() {
}
function write( $obj ) {
$out = array();
$this->_write_obj( $out, '---', $obj );
$out[] = '...';
return $out;
}
function _write_hash( &$out, $obj, $indent, $pad ) {
while ( list($k, $v) = each( $obj ) ) {
$this->_write_