有什么方法可以测试句子与柏树的每种可能组合?

发布于 2025-02-08 23:28:23 字数 659 浏览 2 评论 0 原文

假设有三个数组。

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', .......
            ]);
          });

Let say there are three array.

const initialArray = ['He', 'She', 'It']

const middleArray = ['is', 'was', 'were']

const lastArray = ['a dog', 'a cat', 'a wizard']

What I mean is if any of these 3 combinations comes the sentence is valid but the sentence need to be

'initialArray middleArray lastArray' only this sequence combination is correct others are false

I am trying to write something like this which is hardcoded but it won't work when the number of variables increase.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笔芯 2025-02-15 23:28:24

一个简单的方法是使用正则态度与句子匹配。如果选择,则可以通过编程方式创建匹配器。

这是一个工作

// regex for simpilicity
const matcher = /(He|She|It)\s(is|was|were)\s(a)\s(dog|cat|wizard)/i;
cy.get("#sentence").invoke("text").should("match", matcher);

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.

// regex for simpilicity
const matcher = /(He|She|It)\s(is|was|were)\s(a)\s(dog|cat|wizard)/i;
cy.get("#sentence").invoke("text").should("match", matcher);
生生漫 2025-02-15 23:28:24

因此,如果您在所有三个阵列中进行嵌套的foreach循环,则可以检查所有这样的组合:

initialArray.forEach((init) => {
  middleArray.forEach((mid) => {
    lastArray.forEach((last) => {
      console.log(init + ' ' + mid + ' ' + last)
    })
  })
})

组合:

添加所有内容,您的代码应该看起来像这样:

let sentenceArray = [] //initialize an empty array

initialArray.forEach((init) => {
  middleArray.forEach((mid) => {
    lastArray.forEach((last) => {
      sentenceArray.push(init + ' ' + mid + ' ' + last) //populate array with all combinations
    })
  })
})

cy.get(`.${className} .legend_title`)
  .invoke('text')
  .then((myText) => {
    expect(mytext.trim()).to.be.oneOf(sentenceArray)
  })

So if you do a nested forEach Loop for all three arrays, you can check all the combinations like this:

initialArray.forEach((init) => {
  middleArray.forEach((mid) => {
    lastArray.forEach((last) => {
      console.log(init + ' ' + mid + ' ' + last)
    })
  })
})

Combinations:

Combinations

Adding all this, your code should look like this:

let sentenceArray = [] //initialize an empty array

initialArray.forEach((init) => {
  middleArray.forEach((mid) => {
    lastArray.forEach((last) => {
      sentenceArray.push(init + ' ' + mid + ' ' + last) //populate array with all combinations
    })
  })
})

cy.get(`.${className} .legend_title`)
  .invoke('text')
  .then((myText) => {
    expect(mytext.trim()).to.be.oneOf(sentenceArray)
  })
夏尔 2025-02-15 23:28:24

我喜欢@jjhelguero的答案,但这是使用 .flatmap()创建排列的另一种方法。

const initialArray = ['He', 'She', 'It']
const middleArray = ['is', 'was', 'were']
const lastArray = ['a dog', 'a cat', 'a wizard']

const permutations = initialArray.flatMap(i => 
  middleArray.flatMap(m => 
    lastArray.flatMap(l => `${i} ${m} ${l}`)
  )
)
console.log(permutations)
console.assert(permutations.includes('She was a cat'))

expect(mytext.trim()).to.be.oneOf(permutations);

I like @jjhelguero answer but here's another way using .flatMap() to create the permutations.

const initialArray = ['He', 'She', 'It']
const middleArray = ['is', 'was', 'were']
const lastArray = ['a dog', 'a cat', 'a wizard']

const permutations = initialArray.flatMap(i => 
  middleArray.flatMap(m => 
    lastArray.flatMap(l => `${i} ${m} ${l}`)
  )
)
console.log(permutations)
console.assert(permutations.includes('She was a cat'))

expect(mytext.trim()).to.be.oneOf(permutations);
离去的眼神 2025-02-15 23:28:24

要从一系列价值观构建正发行,请使用 REGEXP构造函数

const initialArray = ['He', 'She', 'It']
const middleArray = ['is', 'was', 'were']
const lastArray = ['a dog', 'a cat', 'a wizard']

const initialChoices = initialArray.join('|')
const middleChoices = middleArray.join('|')
const lastChoices = lastArray.join('|')

let re = new RegExp(`(${initialChoices})\s(${middleChoices})\s(${lastChoices})`, 'i')
 
cy.get("#sentence").invoke("text").should("match", re);

无法通过 .trim()来处理这一点

"She          was     a cat"

请注意,使用Regex您可以更灵活地处理边缘案例,例如,在REGEX CHANGE中, 。 /code>(一个空间)到 [\ s]+(一个或多个空格)。

To build a regex from the array of values, use the RegExp constructor

const initialArray = ['He', 'She', 'It']
const middleArray = ['is', 'was', 'were']
const lastArray = ['a dog', 'a cat', 'a wizard']

const initialChoices = initialArray.join('|')
const middleChoices = middleArray.join('|')
const lastChoices = lastArray.join('|')

let re = new RegExp(`(${initialChoices})\s(${middleChoices})\s(${lastChoices})`, 'i')
 
cy.get("#sentence").invoke("text").should("match", re);

Note with regex you get more flexibility for handling edge cases, for example this can't be handled by .trim()

"She          was     a cat"

In the regex change \s (one space) to [\s]+ (one or more spaces).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文