黄瓜色输出会根据我在哪里打印文本而有所不同 [英] Cucumber colored output changes depending on where I print text

查看:321
本文介绍了黄瓜色输出会根据我在哪里打印文本而有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从黄瓜的Given / When / Then语句直接打印输出时,它打印出一个漂亮的蓝色文本,但是如果我调用任何其他函数并在其中打印,它打印为红色(而不是缩进) / p>

示例功能:

 功能:Foo 
场景: Foo2
给定东西
然后别的东西

范例step_def:

 给定/ ^ something $ / do 
putsPretty blue
end

AnyClass
def doSomething
putScary red
end
end

然后/ ^ something else $ / do
AnyClass.new。 doSomething
end

在下面的示例输出中,Scary red并且实际上在黄瓜步骤之前打印(Then子句)。

 特征:Foo 

场景:Foo2
给定东西
漂亮的蓝色
可怕的红色
然后别的东西

1场景(1次通过)
2步(2 pass)
0m0.001s

黄瓜,所以我甚至不是积极的那些是这个问题的适当标签。有没有办法让其他类中调用的方法的输出匹配漂亮的格式?



编辑:更多的搜索显示,如,如果你的格式不同,它们被包括)比来自其他函数的put(从其他格式化器排除)。所以也许问题是,Ruby如何拦截来自步骤的puts?

解决方案

在编写黄瓜步骤的语法时,黄瓜已经重写(语法)放在被着色和缓冲。颜色取决于步骤是否通过,并且缓冲发生,因为它不会在输出一直运行之前对输出进行着色(因此它知道要打印哪个颜色)。



在语法步骤中传递自我,并调用对象puts将获得我之后的行为。



红色是我的默认字体颜色的副作用。 p>

ie:

  After / Some条件/ do 
some_object .test_something(self)
end

class SomeObject
def test_something(obj)
obj.putsSomeText
end
end


When I print output straight from the Given/When/Then statements of cucumber, it prints in a nice blue text, however if I call into any other function and print within there, it prints in red (and not indented)

Sample feature:

Feature: Foo
  Scenario: Foo2
    Given something
    Then something else

Sample step_def:

Given /^something$/ do
  puts "Pretty blue"
end

class AnyClass
  def doSomething
    puts "Scary red"
  end
end

Then /^something else$/ do
  AnyClass.new.doSomething
end

In the below sample output, "Scary red" is red, not indented at all, and actually printed before the cucumber step (The "Then" clause). "Pretty blue" is blue, indented appropriately, and outputted after the cucumber step.

Feature: Foo

  Scenario: Foo2
    Given something
      Pretty blue
Scary red
    Then something else

1 scenario (1 passed)
2 steps (2 passed)
0m0.001s

I am new to Ruby & cucumber so I am not even positive those are the appropriate tags for this question. Is there a way to make output from methods called within other classes will match the pretty formatting?

Edit: Some more searching has shown that puts from within steps are handled differently (as in, if you format it differently, they are included) than puts from other functions (which get excluded from other formatters). So perhaps the question is, how does Ruby intercept the puts from the steps? I suspect they're implementing their own puts which is part of the step's class then.

解决方案

When writing the grammar for cucumber steps, cucumber has overridden (the grammars) puts to be colored and buffered. The color depends on whether the step passes or not, and the buffering happens because it won't color the output until everything has run (so it knows which color to print).

Passing self from within a grammar step and calling that objects puts gets the behavior I was after.

The red was a side effect of my default font color.

ie:

After /Some Condition/ do
   some_object.test_something(self)
end

class SomeObject
   def test_something(obj)
      obj.puts "SomeText"
   end
end

这篇关于黄瓜色输出会根据我在哪里打印文本而有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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