Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displays unicode escapes when using hex strings #40

Open
olerass opened this issue Feb 7, 2014 · 2 comments
Open

Displays unicode escapes when using hex strings #40

olerass opened this issue Feb 7, 2014 · 2 comments

Comments

@olerass
Copy link

olerass commented Feb 7, 2014

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
@olerass
Copy link
Author

olerass commented Feb 8, 2014

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).

@ansonhoyt
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants