如何阻止JBehave修剪多行参数? [英] How to stop JBehave from trimming multiline parameters?

查看:83
本文介绍了如何阻止JBehave修剪多行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个步骤需要检查某些消息.我使用了多行参数:

I have a step where I need to check certain message. I used a multi-line parameter:

Then I see welcome message: 'Welcome to our site! 
Some more text 
Even more text'

问题是消息的第一行和第二行的末尾应该有空格(为什么是IDK,但应该如此). JBehave会修剪每行的空格.因此测试失败.

Problem is that message should have spaces at the end of first and second lines (IDK why, but it should). And JBehave trims spaces from each line. So test fails.

如何设置不修剪此参数?

How can I set it not to trim this parameter?

我尝试编写\n而不是实际的换行符,但是并没有用换行符代替

I've tried writing \n instead of actual line-breaks, but it wasn't replaced with line-breaks

还尝试在参数的开头添加{trim = false},但它将其读取为参数的一部分.

Also tried adding {trim=false} in the beginning of parameter, but it reads it as part of the parameter.

做了一些带引号的实验(使用了,",无"),也没有帮助...

Made some experiments with quotation marks (used ", ', none), it didn't help either...

推荐答案

我已经测试过了,看来JBehave实际上在测试结束时会截断空格:

I have tested that and it seems that JBehave actually truncates spaces at the end of the test:

When some step with multitline parameter:first line has 10 spaces at the end          
Second line has 6 spaces at the end      
Third line has no spaces at the end, but fourth line has 8 spaces

Fifth line has 3 spaces, and Sixth line has only 7 spaces   


    @When("some step with multitline parameter:$lines")
    public void stepWithMultilneString(String s) {

        String lines [] = s.split("\\r?\\n");

        for(int i = 0; i < lines.length; i++) {
            System.out.format("Line %d is: >>%s<<\n", i+1, lines[i]);
        }
    }

Line 1 is: >>first line has 10 spaces at the end          <<
Line 2 is: >>Second line has 6 spaces at the end      <<
Line 3 is: >>Third line has no spaces at the end, but fourth line has 8 spaces<<
Line 4 is: >>        <<
Line 5 is: >>Fifth line has 3 spaces, and Sixth line has only 7 spaces<<
When some step with multitline parameter:first line has 10 spaces at the end          
Second line has 6 spaces at the end      
Third line has no spaces at the end, but fourth line has 8 spaces

Fifth line has 3 spaces, and Sixth line has only 7 spaces


我建议绕过-在文本的开头和结尾处使用定界符!的步骤:


I suggest a bypass - a step with delimiters ! at the beginning and end of the text:

When step with multitline parameter surrounded by !:!first line has 10 spaces at the end          
Second line has 6 spaces at the end      
Third line has no spaces at the end, but fourth line has 8 spaces

Fifth line has 3 spaces, and Sixth line has only 7 spaces   

!


    @When("step with multitline parameter surrounded by !:$string")
    public void stepWithMultilneStringVersion2(String s) {

        if( s.startsWith("!")) {
            s = s.substring(1);
        }
        if( s.endsWith("!")) {
            s = s.substring(0, s.length()-1);
        }

        String lines [] = s.split("\\r?\\n");

        for(int i = 0; i < lines.length; i++) {
            System.out.format("Line %d is: >>%s<<\n", i+1, lines[i]);
        }
    }

Line 1 is: >>first line has 10 spaces at the end          <<
Line 2 is: >>Second line has 6 spaces at the end      <<
Line 3 is: >>Third line has no spaces at the end, but fourth line has 8 spaces<<
Line 4 is: >>        <<
Line 5 is: >>Fifth line has 3 spaces, and Sixth line has only 7 spaces   <<
Line 6 is: >>       <<
When step with multitline parameter surrounded by !:!first line has 10 spaces at the end          
Second line has 6 spaces at the end      
Third line has no spaces at the end, but fourth line has 8 spaces

Fifth line has 3 spaces, and Sixth line has only 7 spaces   

!

这篇关于如何阻止JBehave修剪多行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆