summaryrefslogtreecommitdiff
path: root/lib/querypath/test/test.php
blob: a5fdd32810824297f80083e1353d37a067d2b744 (plain)
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
#!/usr/bin/env php
<?php
/**
 * Generic CLI parser tests.
 *
 * These are not unit tests. They are just plain parser tests.
 * @author M Butcher <matt@aleph-null.tv>
 * @license The GNU Lesser GPL (LGPL) or an MIT-like license.
 */
require '../src/QueryPath/QueryPath.php';
//$str = 'abc > def.g |: hi(jk)[lmn]*op';
//$str = '&abc.def';

/**
 * Testing harness for the CssEventHandler.
 * @ingroup querypath_tests
 */
class SimpleTestCssEventHandler implements CssEventHandler {
  var $stack = NULL;
  var $expect = array();
  
  public function __construct() {
    $this->stack = array();
  }
  
  public function getStack() {
    return $this->stack;
  }
  
  public function dumpStack() {
    print "\nExpected:\n";
    $format = "Element %d: %s\n";
    foreach ($this->expect as $item) {
      printf($format, $item->eventType(), implode(',', $item->params()));
    }
    
    print "Got:\n";
    foreach($this->stack as $item){
      printf($format, $item->eventType(), implode(',', $item->params()));
    }
  }
  
  public function expects($stack) {
    $this->expect = $stack;
  }
  
  public function success() {
    return ($this->expect == $this->stack);
  }
  
  public function elementID($id) {
    $this->stack[] = new TestEvent(TestEvent::elementID, $id);
  }
  public function element($name) {
    $this->stack[] = new TestEvent(TestEvent::element, $name);
  }
  public function elementNS($name, $namespace = NULL){
    $this->stack[] = new TestEvent(TestEvent::elementNS, $name, $namespace);
  }
  public function anyElement(){
    $this->stack[] = new TestEvent(TestEvent::anyElement);
  }
  public function anyElementInNS($ns){
    $this->stack[] = new TestEvent(TestEvent::anyElementInNS, $ns);
  }
  public function elementClass($name){
    $this->stack[] = new TestEvent(TestEvent::elementClass, $name);
  }
  public function attribute($name, $value = NULL, $operation = CssEventHandler::isExactly){
    $this->stack[] = new TestEvent(TestEvent::attribute, $name, $value, $operation);
  }
  public function attributeNS($name, $ns, $value = NULL, $operation = CssEventHandler::isExactly){
    $this->stack[] = new TestEvent(TestEvent::attributeNS, $name, $ns, $value, $operation);
  }
  public function pseudoClass($name, $value = NULL){
    $this->stack[] = new TestEvent(TestEvent::pseudoClass, $name, $value);
  }
  public function pseudoElement($name){
    $this->stack[] = new TestEvent(TestEvent::pseudoElement, $name);
  }
  public function directDescendant(){
    $this->stack[] = new TestEvent(TestEvent::directDescendant);
  }
  public function anyDescendant() {
    $this->stack[] = new TestEvent(TestEvent::anyDescendant);
  }
  public function adjacent(){
    $this->stack[] = new TestEvent(TestEvent::adjacent);
  }
  public function anotherSelector(){
    $this->stack[] = new TestEvent(TestEvent::anotherSelector);
  }
  public function sibling(){
    $this->stack[] = new TestEvent(TestEvent::sibling);
  }
}

/**
 * Simple utility object for use with the TestCssEventHandler.
 * @ingroup querypath_tests
 */
class TestEvent {
  const elementID = 0;
  const element = 1;
  const elementNS = 2;
  const anyElement = 3;
  const elementClass = 4;
  const attribute = 5;
  const attributeNS = 6;
  const pseudoClass = 7;
  const pseudoElement = 8;
  const directDescendant = 9;
  const adjacent = 10;
  const anotherSelector = 11;
  const sibling = 12;
  const anyElementInNS = 13;
  const anyDescendant = 14;
  
  var $type = NULL;
  var $params = NULL;
  
  public function __construct($event_type) {
    

    
    $this->type = $event_type;
    $args = func_get_args();
    array_shift($args);
    $this->params = $args;
    
    print "Event " . $event_type;
    print_r($args);
  }
  
  public function eventType() {
    return $this->type;
  }
  
  public function params() {
    return $this->params;
  }
}

print ord('"');
#$str = 'tag.class #id :test (test) + anotherElement > yetAnother[test] more[test="ing"]';
$str = 'tag.class #id :test (test)';
print "Now testing: $str\n";

$c = new SimpleTestCssEventHandler();

$p = new CssParser($str, $c);
$p->parse();