我正在尝试使用PHPunit创建\ SplObserver的模拟对象,并将模拟对象附加到\ SplSubject.当我尝试将模拟对象附加到实现\ SplSubject的类时,我得到一个可捕获的致命错误,说明模拟对象没有实现\ SplObserver:
PHP Catchable fatal error: Argument 1 passed to ..\AbstractSubject::attach() must implement interface SplObserver, instance of PHPUnit_Framework_MockObject_Builder_InvocationMocker given, called in ../Decorator/ResultCacheTest.php on line 44 and defined in /users/.../AbstractSubject.php on line 49
Run Code Online (Sandbox Code Playgroud)
或多或少,这是代码:
// Edit: Using the fully qualified name doesn't work either
$observer = $this->getMock('SplObserver', array('update'))
->expects($this->once())
->method('update');
// Attach the mock object to the cache object and listen for the results to be set on cache
$this->_cache->attach($observer);
doSomethingThatSetsCache();
Run Code Online (Sandbox Code Playgroud)
我不确定它是否有所作为,但我使用的是PHP 5.3和PHPUnit 3.4.9
我开始使用Rust了,我正在玩正则表达式箱子以便我可以创建一个词法分析器.
词法分析器使用包含大量命名捕获组的大型正则表达式.我正在尝试获取正则表达式的结果并创建一个Vec<&str, &str>捕获名称和捕获值,但是在映射和过滤结果时迭代返回的值的生命周期一直存在问题.
我认为这与懒惰以及迭代器在超出范围时没有被消耗的事实有关,但我不确定如何真正解决问题.
extern crate regex;
use regex::Regex;
fn main() {
// Define a regular expression with a bunch of named capture groups
let expr = "((?P<num>[0-9]+)|(?P<str>[a-zA-Z]+))";
let text = "0ab123cd";
let re = Regex::new(&expr).unwrap();
let tokens: Vec<(&str, &str)> = re.captures_iter(text)
.flat_map(|t| t.iter_named())
.filter(|t| t.1.is_some())
.map(|t| (t.0, t.1.unwrap()))
.collect();
for token in tokens {
println!("{:?}", token);
}
}
Run Code Online (Sandbox Code Playgroud)
运行上面的代码会导致以下错误:
$ cargo run
Compiling hello_world v0.0.1 (file:///Users/dowling/projects/rust_hello_world)
src/main.rs:14:23: 14:24 error: `t` does not live long enough …Run Code Online (Sandbox Code Playgroud)