【问题标题】:How to create spock-style DSL for dataset description?如何为数据集描述创建 spock 风格的 DSL?
【发布时间】:2012-12-19 22:46:31
【问题描述】:

我想要一个 spock 数据驱动规范格式的数据集描述:

'Key'   |    'Value'    |    'Comments'
1       |    'foo'      |    'something'
2       |    'bar'      |    'something else'

这必须转换成二维数组之类的东西(或任何可能实现的东西)。

任何想法如何实现此数据描述?

附言最大的问题是换行检测,剩下的可以通过在Object的metaClass上重载or来实现。

【问题讨论】:

    标签: java groovy spock


    【解决方案1】:

    | 运算符是左关联的,因此该表中的一行将被解析为:

    ('Key' | 'Value') | 'Comments'
    

    然后,您可以检测每一行的开始和结束位置是让| 操作符返回一个包含其操作数的列表,然后为每个| 询问左侧操作数(即this)是否为一个列表。如果是,则表示它是一行的延续;如果不是列表,则表示这是一个新行。

    这是一个完整的 DSL 示例,它使用 Category 解析这些数据集以避免覆盖 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']
    ]
    

    在本例中,我将表解析为地图列表,但在 DSL 闭包运行后,您应该能够对 DatasetCategory 的结果执行任何操作。

    【讨论】:

      猜你喜欢
      • 2014-08-06
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      相关资源
      最近更新 更多