0

I have a failing mocha test that outputs my string with the "Actual" and "Expected" highlighting... except that nothing's highlighted.

no highlighting

After some head-bashing, I think I've determined that my actual string contains some whacky UTF-8 characters that are completely hidden from me, and Mocha doesn't seem to know to highlight them.

I figured this out by writing out my expected and actual values to raw text files and loading them up in Kaleidoscope, which shows that they differ by highlighting what appears to be empty spaces between words.

enter image description here

I tried loading the utf8 library (on npm) and encoding one of the strings with utf8.encode str, and that still failed, but now the characters appear as something more than blank spaces, and Mocha does highlighting:

some highlighting

But either way, my tests are failing. How can I encode/decode/whatever these strings so that they match and my tests pass?

Btw, the comparison string I'm using in my test looks like this:

comparison string

neezer
  • 19,720
  • 33
  • 121
  • 220
  • How did you get the contents of the expected body? If you copy/pasted it then you probably need to convert the utf8 chars that you pasted into proper string literal utf8 format like `\u0065` or something. – loganfsmyth Jul 10 '12 at 04:05
  • Hmm, yeah, I did copy/paste that... is there something I can put the string through that will convert these characters to string literals? – neezer Jul 10 '12 at 04:14
  • Since you pasted it into your text editor, what editor do you use? Maybe it saved your JS file as the incorrect encoding? Try running `file -bi filename.js` on your mocha file and make sure it us utf-8. Just copy-pasting should be fine since UTF8 charaters inside a string literal should be fine. – loganfsmyth Jul 10 '12 at 04:37
  • It looks like your console font can't display the particular characters involved; you might try setting it to use a more comprehensive font (how to do this depends on your OS/desktop). – ebohlman Jul 10 '12 at 04:37
  • I use Vim through iTerm2 (not MacVim), so I had copy/pasted it directly into a Vim buffer. I checked the filetype of my spec and it looks to be set correctly: `text/plain; charset=utf-8`. I'd love to know how to convert this copy/pasted text into escaped literals--how can I do that? – neezer Jul 10 '12 at 05:14

1 Answers1

0

Make sure that either your text editor is saving your source code as proper UTF-8, or convert those copy/pasted chars to escaped literals as @loganfsmyth correctly comments.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • I've tried copy/pasting with several editors I know support UTF-8 (TextMate, SublimeText2), and I've checked the file type with `file -bi` and the charset is set correctly to `text/plain; charset=utf-8`. I think my best bet would be to convert all the chars to escaped literals, as you suggest, but how do I do that? – neezer Jul 10 '12 at 05:29
  • If you are confident the binary data on disk is properly encoded UTF-8, open it with a hex editor (or tell your editor to open it as hex), then convert the hex to unicode escaped literals. This answer has an example: http://stackoverflow.com/a/8005329/266795 – Peter Lyons Jul 10 '12 at 05:36
  • That seems like a lot of work... is there anyway I can pass this text through some program and have it filter offending characters to literals while leaving the rest of the string intact? I got my test passing by changing my expectation to `it "gets the full body", -> escape(@parsed.body).should.equal escape(page.body)`, but now my diff is a massive block of escaped characters, which makes it really hard to have a nice visual diff during TDD... – neezer Jul 10 '12 at 05:40
  • Sure, if you write "some program" to do that. :-) In JavaScript you can use String.charCodeAt and just ignore anything higher than ASCII (127). – Peter Lyons Jul 10 '12 at 05:54