02.13.06
Pitati pametno
Svi koji koristimo open souce (Ruby, Watir…) (i ne samo mi) trebali bi znati kako pitati pametno (tako da dobiješ odgovor).
Chris McMahon je to spomenuo na wtr-general listi.
The only exhaustive testing there is is so much testing that the tester is exhausted. - William C. Hetzel
Svi koji koristimo open souce (Ruby, Watir…) (i ne samo mi) trebali bi znati kako pitati pametno (tako da dobiješ odgovor).
Chris McMahon je to spomenuo na wtr-general listi.
All of us using open source (Ruby, Watir…) (and not only us) should know how to ask questions the smart way (the way that you will get an answer).
Chris McMahon mentioned it at wtr-general mailing list.
I wanted to make an object. Then copy it. Then change that copy and leave original object intact. But I did not know how to do it. When I do not know how to do something, I always ask wtr-general mailing list. I posted my question. Then I though it would be nice if I explained why I want to copy an object. But it got pretty long, so I put it at my blog.
I got a reply. “Try Object#clone”. Could it be that easy?! It could be. After all, this is Ruby. But no, it is not that easy. It did not work. Original object was changing with it’s duplicate.
Output:
#<Page:0x2820ae0 @url="app.com/view.aspx", @texts=["File > View", "File added."]>
#<Page:0×2820a98 @url=”app.com/view.aspx”, @texts=["File > View", "File added."]>
But now I new where to look. So I typed “ri Object#clone” and “ri Object#dup” at Command Prompt (and even looked at http://www.ruby-doc.org/core/). It said (at both places, for both Object#clone and Object#dup):
“Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference.”
So, it creates a “shallow” copy. I do not need this. I need a “real” copy.
Google helped me. I searched for “ruby copy object” and found http://www.rubygarden.org/ruby?Make_A_Deep_Copy_Of_An_Object. Secret word is “deep” copy.
I added this to class Page (and replaced page_view.clone with page_view.deep_clone) and got what I needed!
Output:
#<Page:0x4c1dd10 @url="app.com/view.aspx", @texts=["File > View"]>
#<Page:0×4c1dc50 @url=”app.com/view.aspx”, @texts=["File > View", "File added."]>
I do not understand how it works, and I do not care, as long as it works.
Želio sam napraviti objekt. Zatim ga kopirati. Zatim promijeniti kopiju bez mijenjanja izvornika. Ali nisam znao kako. Kad god nešto ne znam, pitam wtr-general listu. Poslao sam pitanje. Pa sam pomislio da bi bilo lijepo kad bi objasnio zašto želim kopirati objekt. Ali, objašnjenje se odužilo, pa sam ga stavio na moj blog.
Dobio sam odgovor. “Probaj Object#clone”. Može li biti tako jednostavno?! Može. Ipak, to je Ruby. Ali ne, nije tako jednostavno. Nije radilo. Izvorni objekt se mijenjao sa svojim duplikatom.
Rezulat:
#<Page:0x2820ae0 @url="app.com/view.aspx", @texts=["File > View", "File added."]>
#<Page:0×2820a98 @url=”app.com/view.aspx”, @texts=["File > View", "File added."]>
Ali sad sam znao gdje gledati. Upisao sam “ri Object#clone” i “ri Object#dup” u Command Prompt (čak sam pogledao i na http://www.ruby-doc.org/core/). Kaže (na oba mjesta, i za Object#clone i za Object#dup):
“Proizvodi plitku kopiju objekta - kopiraju se objektove varijable instance (kako ovo prevesti?), ali ne i objekt na kojeg se odnose.”
Dakle, stvara “plitku” kopiju. To mi ne treba. Trebam “pravu” kopiju.
Google mi je pomogao. Potražio sam “ruby copy object” i našao http://www.rubygarden.org/ruby?Make_A_Deep_Copy_Of_An_Object.
Tajna riječ je “dubinsko” kopiranje.
Dodao sam ovo klasi Page (i zamijenio page_view.clone sa page_view.deep_clone) i dobio sam što sam trebao!
Rezulat:
#<Page:0x4c1dd10 @url="app.com/view.aspx", @texts=["File > View"]>
#<Page:0×4c1dc50 @url=”app.com/view.aspx”, @texts=["File > View", "File added."]>
Ne razumijem kako radi, i nije me briga, sve dok radi.
Živim od testiranja web aplikacija. Služim se Rubyjem i Watirom za automatiziranje testova funkcionalnosti. Moje skripte su narasle, pa sam napravio mali framework (ne znam kako ovo pametno prevesti) kako bi ih pojednostavnio (sav kod u ovom članku je pojednostavljen i neisproban).
Klasa Page olakšava provjeravanje url-a i tekst(ov)a stranice.
“Pogledaj element” stranica u aplikaciji koju testiram ima tekst (breadcrumb - opet ne znam kako bi ovo smisleno preveo) {vrsta_elementa} > View (primjer: File > View).”
Nakon što je element dodan/promijenjen, na toj stranici se nalazi i tekst {vrsta_elementa} {akcija}. (primjer: File added.).
Klasa File, naravno, testira datoteke.
I sve baš lijepo radi. Sve što trebam napisati je ovo.
Postoje tri objekta. Jedan za svaku (@page_view…) varijablu. “id” u “Page:{id}” je različit za svaku varijablu.
#<Page:0×2b5ac28 @url=”app.com/view.aspx”, @texts=["File > View"]>
#<Page:0×2b5ab98 @url=”app.com/view.aspx”, @texts=["File > View", "File added."]>
#<Page:0×2b5ab08 @url=”app.com/view.aspx”, @texts=["File > View", "File edited."]>
Ali, dupliciranje me muči.
Želio bi da File#initialize izgleda ovako.
Ali (iznenađenje!), to ne radi. Pa, prilično sam novi u objektno orijentiranom svijetu, pa mi to i nije iznenađenje.
Na ovaj način stvoren je samo jedan Page objekt, i sve tri varijable pokazuju na njega. Bez obzira koju varijablu promijenim, ona promijeni objekt na koji pokazuje. Rezultat je da su sve tri varijable iste. To nije ono što sam želio.
#<Page:0×2820048 @url=”app.com/view.aspx”, @texts=["File > View", "File added.", "File edited."]>
#<Page:0×2820048 @url=”app.com/view.aspx”, @texts=["File > View", "File added.", "File edited."]>
#<Page:0×2820048 @url=”app.com/view.aspx”, @texts=["File > View", "File added.", "File edited."]>
Isprobao sam nekoliko alternativa, ali niti jedna nije uspjela. Za sad mogu živjeti s duplikacijom, ali bi je volio ukloniti iz mojih skripti.
I test web applications for living. I use Ruby and Watir for automating functional tests. My scripts have grown, so I made a little framework to make them simpler (all code in this post is simplified and not tested).
Class Page makes checking page url and text(s) easier.
View item page at application under test has text (breadcrumb) {item_type} > View (example: File > View).
After item is added/edited, at that page there is also text {item_type} {action}. (example: File added.).
Class File tests files, of course.
And it works just nice. All I have to write is this.
There are three objects. One for each (@page_view…) variable. “id” in “Page:{id}” is different for each variable.
#<Page:0×2b5ac28 @url=”app.com/view.aspx”, @texts=["File > View"]>
#<Page:0×2b5ab98 @url=”app.com/view.aspx”, @texts=["File > View", "File added."]>
#<Page:0×2b5ab08 @url=”app.com/view.aspx”, @texts=["File > View", "File edited."]>
But, duplication bugs me.
I would like that File#initialize looks like this.
But (surprise!), it does not work. Well, I am pretty new in object oriented world, so this was not really a surprise for me.
This way only one Page object is created, and all three variables point to it. No matter which variable I change, it changes the object it points to. Result is that all three variables are the same. Not what I wanted to do.
#<Page:0×2820048 @url=”app.com/view.aspx”, @texts=["File > View", "File added.", "File edited."]>
#<Page:0×2820048 @url=”app.com/view.aspx”, @texts=["File > View", "File added.", "File edited."]>
#<Page:0×2820048 @url=”app.com/view.aspx”, @texts=["File > View", "File added.", "File edited."]>
I have tried a few alternatives, but none worked. I will live with duplication for now, but I would like to remove it from my scripts.