我想要一个 spock 数据驱动规范格式的数据集描述:
'Key' | 'Value' | 'Comments'
1 | 'foo' | 'something'
2 | 'bar' | 'something else'
这必须转换为二维数组之类的东西(或任何可能实现的东西)。
有什么想法可以实现这个数据描述吗?
附注最大的问题是换行检测,其余的可以通过在Object的metaClass
上重载或
来实现。
最佳答案
|
运算符是左关联的,因此该表中的一行将被解析为:
('Key' | 'Value') | 'Comments'
为了检测每一行的开始和结束位置,您可以做的是让 |
运算符返回一个包含其操作数的列表,然后针对每个 |
询问左边是否- 手操作数(即 this
)是一个列表。如果是,则意味着它是一行的延续;如果它不是列表,则意味着它是一个新行。
这是使用 Category 解析这些数据集的 DSL 的完整示例避免覆盖 Object 元类上的内容:
@Category(Object)
class DatasetCategory {
// A static property for holding the results of the DSL.
static results
def or(other) {
if (this instanceof List) {
// Already in a row. Add the value to the row.
return this << other
} else {
// A new row.
def row = [this, other]
results << row
return row
}
}
}
// This is the method that receives a closure with the DSL and returns the
// parsed result.
def dataset(Closure cl) {
// Initialize results and execute closure using the DatasetCategory.
DatasetCategory.results = []
use (DatasetCategory) { cl() }
// Convert the 2D results array into a list of objects:
def table = DatasetCategory.results
def header = table.head()
def rows = table.tail()
rows.collect { row ->
[header, row].transpose().collectEntries { it }
}
}
// Example usage.
def data = dataset {
'Key' | 'Value' | 'Comments'
1 | 'foo' | 'something'
2 | 'bar' | 'something else'
}
// Correcness test :)
assert data == [
[Key: 1, Value: 'foo', Comments: 'something'],
[Key: 2, Value: 'bar', Comments: 'something else']
]
在此示例中,我将表解析为 map 列表,但在 DSL 闭包运行后,您应该能够对 DatasetCategory 的结果执行任何您想做的事情。
关于java - 如何为数据集描述创建 spock 风格的 DSL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13962536/