rspec-given
rspec-given copied to clipboard
Displays unicode escapes when using hex strings
This code:
context 'annoying unicode conversion' do
Given(:io) { StringIO.new "\x00\x00" }
When(:read) { io.read 1 }
Then { read == "\x00\x00" }
end
Outputs the following:
Then expression failed at [...]
expected: "\x00"
to equal: "\u0000\u0000"
false <- read == "\x00\x00"
"\x00" <- read
However, it is hard to see the difference between the strings (more so with larger strings of non-nulls) when one is displayed in hex and the other uses unicode escaping. Optimally rspec-given should display both exactly like they are written in the spec (in this case both in hex), like this:
Then expression failed at [...]
expected: "\x00"
to equal: "\x00\x00"
false <- read == "\x00\x00"
"\x00" <- read
After deeper investigation it turns out this is not a problem related to rspec-given.
The "problem" is that strings has the encoding of the source file in which they are contained (in my case UTF8). On the other hand IO.read
returns strings with ASCII-8BIT encoding when the length
parameter is used. As a result, in the above example, read.encoding
is ASCII-8BIT while "\x00\x00".encoding
is UTF-8. This causes Ruby to output the strings differently, at least when inspect
is used (which is what rspec-given/rspec uses to output with I guess).
Are you still having this issue? I ran into something similar, and just had to explicitly specify the right encoding (ASCII_8BIT) in my test case. Otherwise they were UTF-8, which is ruby's default encoding. This matches what ruby uses for binary strings, like the return value of Array#pack.
I've found two ways to do this on a string literal:
binary_str = "\x00\x00".b
binary_str = "\x00\x00".force_encoding('ASCII_8BIT')
Hope that helps. You might be able to close this issue.