From: David Brady Date: 2005-08-25T03:35:10+09:00 Subject: Re: The Chainsaw Infanticide Logger Manuever Bill Kelly wrote: >Again, I'd ask to see what sort of test you'd propose that would >catch the problem I described. (My post, the one you quoted, was >about CGI, not the Logger.) > > I find that the hardest tests to write are the ones that I overlooked because I am unaware of my blind spots. Until I read your post, I thought like you did--that to_s flattened an object into a chunk of text. But now I see that it doesn't. It doesn't even return a String object! It returns an object that behaves--dare I say quacks?--like a String. Another assumption you may be making--and once I state it, the test for it should become obvious--is that YAML should output pleasingly human readable text, even if it loses the ability to correctly restore objects. I know, I know. There exists in business the concept of "approprately incorrect". You are using YAML to produce "appropriately incorrect" files. The behavior you want is "take a Stringlike object, and emit pleasingly simplified text". That's easily testable. Take your CGI object, have YAML dump it, then open the YAML file manually and see that the text is correct. Or build a CGI object, then build an equivalent object using Strings, output them to different files and then assert that there is no difference between the files. The second method may seem more robust, because it lets you not care how YAML stores Strings... but that's the rabbit that led us down this hole in the first place. Use the first method, because you don't care if YAML stores CGI the same as a String, you care that the output is pleasing to read. If YAML changes the way it stores String, you'll want this test to break. Your app should shriek until you have verified that the new format is pleasant. Once you have a regression test in place, you will be able to remove the hack to String#to_s and know when your application is fixed. A good alternative to your hack, perhaps, is to replace YAML with a dumper of your own devising that transforms Stringlike-->String on save/reload. Perhaps even a child class of YAML, whose only override is something like: obj = String.new(obj) if obj.respond_to? :to_str && obj.class != String I haven't learned how to see my blind spots yet. I could not have prevented this bug from happening. But having happened, I can immediately write a test that verifies that it is fixed, and that future revisions to CGI, YAML, and even String will be regression tested against this bug. Cheers, -dB -- David Brady ruby_talk@shinybit.com I'm feeling really surreal today... OR AM I?