有什么方法可以测试句子与柏树的每种可能组合?
假设有三个数组。
const initialArray = ['He', 'She', 'It']
const middleArray = ['is', 'was', 'were']
const lastArray = ['a dog', 'a cat', 'a wizard']
我的意思是,如果这三种组合中的任何一个句子是有效的,但是句子需要为
'initialarray middlearray lastarray'
只有这个序列组合正确的其他是错误的,
我试图写下这样的东西它是硬编码的,但是当变量数量增加时它将无法工作。
cy.get(`.${className} .legend_title`).should((key) => {
let mytext = key.text();
expect(mytext.trim()).to.be.oneOf([
'He is a dog',
"He is a cat",
'He is a wizard',
'She is a dog', .......
]);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个简单的方法是使用正则态度与句子匹配。如果选择,则可以通过编程方式创建匹配器。
这是一个工作
An easier way is to use regex to match against the sentence. You can programmatically create the matcher if you choose.
Here is a working example.
因此,如果您在所有三个阵列中进行嵌套的foreach循环,则可以检查所有这样的组合:
组合:
添加所有内容,您的代码应该看起来像这样:
So if you do a nested forEach Loop for all three arrays, you can check all the combinations like this:
Combinations:
Adding all this, your code should look like this:
我喜欢@jjhelguero的答案,但这是使用
.flatmap()
创建排列的另一种方法。I like @jjhelguero answer but here's another way using
.flatMap()
to create the permutations.要从一系列价值观构建正发行,请使用 REGEXP构造函数
无法通过
.trim()
来处理这一点请注意,使用Regex您可以更灵活地处理边缘案例,例如,在REGEX CHANGE中, 。 /code>(一个空间)到
[\ s]+
(一个或多个空格)。To build a regex from the array of values, use the RegExp constructor
Note with regex you get more flexibility for handling edge cases, for example this can't be handled by
.trim()
In the regex change
\s
(one space) to[\s]+
(one or more spaces).