Karate: match with parametrized regex

徘徊边缘 提交于 2019-12-24 07:26:05

问题


I didn't find the right way to write a match with regexp cointaining a variable:

* def getYear =
  """
  function() {
    var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
    var sdf = new SimpleDateFormat('yyyy');
    var date = new java.util.Date();
    return sdf.format(date);
  } 
  """
* def currentYear = getYear()
* def testmatch = { gencode: '#("000" + currentYear + "0000012345")' }
* match testmatch == { gencode: '#regex "[0-9]{3}" + currentYear + "[0-9]{10}"' }

There is a way to do this?

Thanks, Lorenzo


回答1:


First, normally when you are doing matches like this, regex is un-necessary, because you might as well do an exact match.

But anyway this is the solution, refer: https://github.com/intuit/karate#self-validation-expressions

* def isValidYear = function(x){ return new RegExp("[0-9]{3}" + currentYear + "[0-9]{10}").test(x) }
* assert isValidYear('00020190000012345')
* match testmatch == { gencode: '#? isValidYear(_)' }


来源:https://stackoverflow.com/questions/54767743/karate-match-with-parametrized-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!