Indefero

Indefero Commit Details


Date:2011-03-22 18:26:26 (13 years 8 months ago)
Author:Thomas Keller
Branch:develop, feature-issue_links, feature.better-home, feature.content-md5, feature.diff-whitespace, feature.download-md5, feature.issue-links, feature.issue-of-others, feature.issue-summary, feature.search-filter, feature.webrepos, feature.wiki-default-page, release-1.2, release-1.3
Commit:5b5705fe906670b5533fa9f6bd3759695887aeea
Parents: f08b5c5e3ff3b740dc0518dce22040933d7fc6c5
Message:Add a unit test for monotone's basicio parser and compiler. Also note a couple of quirks and be less strict (for now) when it comes to hash parsing and stanza lines without values.

Changes:

File differences

src/IDF/Scm/Monotone/BasicIO.php
2121
2222
2323
24
25
2426
2527
2628
......
3133
3234
3335
36
37
38
39
40
3441
3542
3643
......
5461
5562
5663
57
58
59
60
64
65
6166
62
63
64
65
66
67
68
6967
70
71
72
73
74
75
76
77
78
79
80
81
68
69
70
71
72
73
74
8275
83
8476
85
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
8698
87
88
89
90
91
92
93
99
100
101
102
103
104
105
94106
95
96
107
108
97109
98
99
100
110
111
112
113
101114
102115
103116
......
114127
115128
116129
130
131
132
133
134
135
117136
118137
119138
......
129148
130149
131150
132
151
133152
134153
135154
......
157176
158177
159178
160
161
162
163
164
165
166
167179
168180
169181
#
# ***** END LICENSE BLOCK ***** */
require_once 'IDF/Scm/Exception.php';
/**
* Utility class to parse and compile basic_io stanzas
*
/**
* Parses monotone's basic_io format
*
* Known quirks:
* - does not handle multi-values starting with a hash '[]' (no known output)
* - does not validate hashes (should be /[0-9a-f]{40}/i)
* - does not handle forbidden \0
*
* @param string $in
* @return array of arrays
*/
$stanzaLine['key'] .= $ch;
}
// symbol w/o a value list
if ($pos >= $length || $in[$pos] == "\n") break;
if ($in[$pos] == '[') {
// ensure we don't look at a symbol w/o a value list
if ($pos >= $length || $in[$pos] == "\n") {
unset($stanzaLine['values']);
++$pos; // opening square bracket
$stanzaLine['hash'] = substr($in, $pos, 40);
$pos += 40;
++$pos; // closing square bracket
}
else
{
unset($stanzaLine['hash']);
$valCount = 0;
// if hashs and plain values are encountered in the same
// value list, we add the hash values as simple values as well
while ($in[$pos] == '"' || $in[$pos] == '[') {
$isHashValue = $in[$pos] == '[';
++$pos; // opening quote / bracket
$stanzaLine['values'][$valCount] = '';
while ($pos < $length) {
$ch = $in[$pos]; $pr = $in[$pos-1];
if (($isHashValue && $ch == ']')
||(!$isHashValue && $ch == '"' && $pr != '\\'))
break;
}
else {
if ($in[$pos] == '[') {
unset($stanzaLine['values']);
++$pos; // opening square bracket
while ($pos < $length && $in[$pos] != ']') {
$stanzaLine['hash'] .= $in[$pos];
++$pos;
$stanzaLine['values'][$valCount] .= $ch;
}
++$pos; // closing quote
++$pos; // closing square bracket
}
else
{
unset($stanzaLine['hash']);
$valCount = 0;
// if hashs and plain values are encountered in the same
// value list, we add the hash values as simple values as well
while ($in[$pos] == '"' || $in[$pos] == '[') {
$isHashValue = $in[$pos] == '[';
++$pos; // opening quote / bracket
$stanzaLine['values'][$valCount] = '';
while ($pos < $length) {
$ch = $in[$pos]; $pr = $in[$pos-1];
if (($isHashValue && $ch == ']')
||(!$isHashValue && $ch == '"' && $pr != '\\'))
break;
++$pos;
$stanzaLine['values'][$valCount] .= $ch;
}
++$pos; // closing quote
if (!$isHashValue) {
$stanzaLine['values'][$valCount] = str_replace(
array("\\\\", "\\\""),
array("\\", "\""),
$stanzaLine['values'][$valCount]
);
}
if (!$isHashValue) {
$stanzaLine['values'][$valCount] = str_replace(
array("\\\\", "\\\""),
array("\\", "\""),
$stanzaLine['values'][$valCount]
);
}
if ($pos >= $length)
break;
if ($pos >= $length)
break;
if ($in[$pos] == ' ') {
++$pos; // space
++$valCount;
if ($in[$pos] == ' ') {
++$pos; // space
++$valCount;
}
}
}
}
/**
* Compiles monotone's basicio format
*
* Known quirks:
* - does not validate keys for /[a-z_]+/
* - does not validate hashes (should be /[0-9a-f]{40}/i)
* - does not support intermixed value / hash formats
* - does not handle forbidden \0
*
* @param array $in Array of arrays
* @return string
*/
$maxkeylength = 0;
foreach ((array)$stanza as $lx => $line) {
if (!array_key_exists('key', $line)) {
if (!array_key_exists('key', $line) || empty($line['key'])) {
throw new IDF_Scm_Exception(
'"key" not found in basicio stanza '.$sx.', line '.$lx
);
$value).'"';
}
}
else
{
throw new IDF_Scm_Exception(
'neither "hash" nor "values" found in basicio '.
'stanza '.$sx.', line '.$lx
);
}
$out .= "\n";
}
test/IDF/Scm/Monotone/BasicIOTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
include 'IDF/Scm/Monotone/BasicIO.php';
class IDF_Scm_Monotone_BasicIOTest extends PHPUnit_Framework_TestCase
{
public function testParse()
{
$stanzas = IDF_Scm_Monotone_BasicIO::parse(null);
$this->assertTrue(is_array($stanzas) && count($stanzas) == 0);
// single stanza, single line, only key
$stanzas = IDF_Scm_Monotone_BasicIO::parse('foo');
$this->assertEquals(1, count($stanzas));
$stanza = $stanzas[0];
$this->assertEquals(1, count($stanza));
$entry = $stanza[0];
$this->assertEquals('foo', $entry['key']);
$this->assertTrue(!array_key_exists('hash', $entry));
$this->assertTrue(!array_key_exists('values', $entry));
// single stanza, single line, key with hash
$stanzas = IDF_Scm_Monotone_BasicIO::parse("foo [0123456789012345678901234567890123456789]");
$this->assertEquals(1, count($stanzas));
$stanza = $stanzas[0];
$this->assertEquals(1, count($stanza));
$entry = $stanza[0];
$this->assertEquals('foo', $entry['key']);
$this->assertEquals("0123456789012345678901234567890123456789", $entry['hash']);
$this->assertTrue(!array_key_exists('values', $entry));
// single stanza, single line, key with two values
$stanzas = IDF_Scm_Monotone_BasicIO::parse("foo \"bar\n\nbaz\" \"bla\"");
$this->assertEquals(1, count($stanzas));
$stanza = $stanzas[0];
$this->assertEquals(1, count($stanza));
$entry = $stanza[0];
$this->assertEquals('foo', $entry['key']);
$this->assertTrue(!array_key_exists('hash', $entry));
$this->assertEquals(array("bar\n\nbaz", "bla"), $entry['values']);
// single stanza, single line, key with a value and a hash
$stanzas = IDF_Scm_Monotone_BasicIO::parse("foo \"bar\n\nbaz\" [0123456789012345678901234567890123456789]");
$this->assertEquals(1, count($stanzas));
$stanza = $stanzas[0];
$this->assertEquals(1, count($stanza));
$entry = $stanza[0];
$this->assertEquals('foo', $entry['key']);
$this->assertTrue(!array_key_exists('hash', $entry));
$this->assertEquals(array("bar\n\nbaz", "0123456789012345678901234567890123456789"), $entry['values']);
// single stanza, two lines, keys with single value / hash
$stanzas = IDF_Scm_Monotone_BasicIO::parse("foo \"bar\"\nbaz [0123456789012345678901234567890123456789]");
$this->assertEquals(1, count($stanzas));
$stanza = $stanzas[0];
$this->assertEquals(2, count($stanza));
$entry = $stanza[0];
$this->assertEquals('foo', $entry['key']);
$this->assertTrue(!array_key_exists('hash', $entry));
$this->assertEquals(array("bar"), $entry['values']);
$entry = $stanza[1];
$this->assertEquals('baz', $entry['key']);
$this->assertTrue(!array_key_exists('values', $entry));
$this->assertEquals("0123456789012345678901234567890123456789", $entry['hash']);
// two stanza, one two liner, one one liner
$stanzas = IDF_Scm_Monotone_BasicIO::parse("foo \"bar\"\nbaz [0123456789012345678901234567890123456789]\n\nbla \"blub\"");
$this->assertEquals(2, count($stanzas));
$stanza = $stanzas[0];
$this->assertEquals(2, count($stanza));
$entry = $stanza[0];
$this->assertEquals('foo', $entry['key']);
$this->assertTrue(!array_key_exists('hash', $entry));
$this->assertEquals(array("bar"), $entry['values']);
$entry = $stanza[1];
$this->assertEquals('baz', $entry['key']);
$this->assertTrue(!array_key_exists('values', $entry));
$this->assertEquals("0123456789012345678901234567890123456789", $entry['hash']);
$stanza = $stanzas[1];
$this->assertEquals(1, count($stanza));
$entry = $stanza[0];
$this->assertEquals('bla', $entry['key']);
$this->assertTrue(!array_key_exists('hash', $entry));
$this->assertEquals(array("blub"), $entry['values']);
// (un)escaping tests
$stanzas = IDF_Scm_Monotone_BasicIO::parse('foo "bar\\baz" "bla\"blub"');
$this->assertEquals(1, count($stanzas));
$stanza = $stanzas[0];
$this->assertEquals(1, count($stanza));
$entry = $stanza[0];
$this->assertEquals('foo', $entry['key']);
$this->assertTrue(!array_key_exists('hash', $entry));
$this->assertEquals(array('bar\baz', 'bla"blub'), $entry['values']);
}
public function testCompile()
{
$stanzas = array(
array(
array('key' => 'foo'),
array('key' => 'bar', 'values' => array('one', "two\nthree")),
),
array(
array('key' => 'baz', 'hash' => '0123456789012345678901234567890123456789'),
array('key' => 'blablub', 'values' => array('one"two', 'three\four')),
),
);
$ex =<<<END
foo
bar "one" "two
three"
baz [0123456789012345678901234567890123456789]
blablub "one\"two" "three\\\\four"
END;
$this->assertEquals($ex, IDF_Scm_Monotone_BasicIO::compile($stanzas));
// keys must not be null
$stanzas = array(
array(
array('key' => null, 'values' => array('foo')),
),
);
$thrown = false;
try {
IDF_Scm_Monotone_BasicIO::compile($stanzas);
} catch (IDF_Scm_Exception $e) {
$this->assertRegExp('/^"key" not found in basicio stanza/', $e->getMessage());
$thrown = true;
}
$this->assertTrue($thrown);
// ...nor completly non-existing
$stanzas = array(
array(
array('values' => array('foo')),
),
);
$thrown = false;
try {
IDF_Scm_Monotone_BasicIO::compile($stanzas);
} catch (IDF_Scm_Exception $e) {
$this->assertRegExp('/^"key" not found in basicio stanza/', $e->getMessage());
$thrown = true;
}
$this->assertTrue($thrown);
}
}

Archive Download the corresponding diff file

Page rendered in 0.11641s using 13 queries.