Use VIM to do TUT Unit Testing 43

Posted by tottinger Wed, 11 Jul 2007 14:47:00 GMT

I’m not crazy about TUT, but a customer is using it on a project and wanted to get a little assist from VIM. I don’t blame him. It’s a pain to keep track of your test number and there’s always more testing that we’d like in our test frameworks.

So I scribbled up a little VIM script to handle some of the light housekeeping. It’s not a wonderful script, and I’m betting the readers can give me some pointers (I’ve only written a few other vimscripts ever). I’m sure that I could do better if I reflected and refactored, but I haven’t yet.

Maybe you know a good way to unit test vim scripts?.

My script hijacks your F5 key. You can change that easily enough. Maybe I should have mapped it to \nt for “new test” or something. Anyway, enjoy and comment please:


function! NewTutTest()
    let s:testNumber = 0
    let s:newNumber = 0
    " Seek a higher number
    let s:list = getline("1","$")
    for s:line in s:list
        if s:line =~ 'test<'
            let s:newNumber =  0 + matchstr(s:line, '\d\+')
            if s:newNumber > s:testNumber
                let s:testNumber = s:newNumber
            endif
        endif
    endfor

    "Increment (tests are 1..n)
    let s:testNumber = s:testNumber + 1

    "Output the test values
    let s:line = line(".")
    let s:indent = repeat(" ", &shiftwidth)
    let result = append(s:line, s:indent . "template<>")
    let s:line = s:line + 1
    let result = append(s:line, s:indent . "template<>")
    let s:line = s:line + 1
    let result = append(s:line, s:indent . "void object::test<" . s:testNumber . ">()")
    let s:line = s:line + 1
    let result = append(s:line, s:indent . "{" )
    let s:line = s:line + 1
    let result = append(s:line, repeat(s:indent,2) )
    let s:line = s:line + 1
    let result = append(s:line, s:indent . "}" )
    call cursor(s:line, (&shiftwidth * 2))

endfunction

" Paste a version of this line into .vimrc to assign a keystroke (in this case F5)
" Comment-out this version if you do
" --------------------------------
map <F5> :call NewTutTest()<CR>Aset_test_name(" 

" Some helpful macros for command mode. Press \ten and it opens a new line
" and types ensure(" so you can fill in the string and the parameters.
" Overall, not too shabby.
map \tn oensure(" 
map \te oensure_equals(" 
map \td oensure_distance(" 
map \tf ofail(" 

" Abbreviations to help you in insert mode. Type the 
" two-letter name, followed by a quote.
ab tn ensure(
ab te ensure_equals(
ab td ensure_distance(
ab tf fail(

Comments

Leave a response

  1. Avatar
    Tim 1 day later:

    Sadly, I just found that this only works with VIM 7.x and not with 6.3. If you think 6.3 is old, I just today located a development server running a 2003 Linux build. Old is what you make of it.

    I will fix this for 6.3 (no for loop, among other shortcomings) and repost it for those to constrained to upgrade.

  2. Avatar
    Tim 2 days later:

    Okay, here is the new, improved, retrograded, refactored version.

    function! NewTutTest()
        let s:testNumber = GetNextTestNumber()
        let s:line = line(".")
        let s:line = Insert(s:line, "template<> template<>")
        let s:line = Insert(s:line, "void testobject::test<" . s:testNumber . ">()")
        let s:line = Insert(s:line, "{" )
        let s:line = Insert(s:line, Pad(&shiftwidth))
        let s:line = Insert(s:line, "}" )
        call cursor(s:line - 1, (&shiftwidth * 2))
    endfunction
    
    function! Insert(line, text)
        let result = append(a:line, Pad(&shiftwidth) . a:text)
        return a:line + 1
    endfunction
    
    function! GetNextTestNumber()
      let s:testNumber = 0
      let s:maxline = line('$')
      let s:linecount = 0
      while s:linecount < s:maxline
          let s:line = getline(s:linecount)
          let s:linecount = s:linecount+1
          if s:line =~ 'test<'
              let s:newNumber =  matchstr(s:line, '\d\+') + 0
              if s:newNumber > s:testNumber
                  let s:testNumber = s:newNumber
              endif
          endif
      endwhile
      return s:testNumber + 1
    endfunction
    
    function! Pad(positions)
      let s:pad = "" 
      let s:position = a:positions
      while s:position > 0
        let s:pad = s:pad . " " 
        let s:position = s:position - 1
      endwhile
      return s:pad
    endfunction
    
    " Paste this line into your .vimrc to import the function
    " -----------------
    " source ~/.tut.vim
    
    " Paste a version of this line into .vimrc to assign a keystroke (in this case F5)
    " Comment-out this version if you do
    " --------------------------------
    
    " Some helpful macros for command mode. Press \ten and it opens a new line
    " and types ensure(" so you can fill in the string and the parameters.
    " Overall, not too shabby.
    map \tn oensure();<ESC>hi
    map \te oensure_equals();<ESC>hi
    map \td oensure_distance();<ESC>hi
    map \tf ofail("");<ESC>hhi
    map \tt :call NewTutTest()<CR>Aset_test_name("");<ESC>hhi
    map <F5> \tt
    
    " Abbreviations to help you in insert mode. Type the two-letter name,
    " followed by a punctuation or space character. :-)
    ab tn ensure
    ab te ensure_equals
    ab td ensure_distance
    ab tf fail
    

    Jeff Langr and I spent a little time on it (he had the older VIM) and we did refactoring together. VIM scripts are a little funky. The error messages are not very obvious, and we had only manual testing. It has a nice editor and decent online help, so there’s that. I think that I can come up with a way to UT the scripts, if I were willing to spend the time.

    If you are using TUT and VIM, here is your macro package. Enjoy it.

  3. Avatar
    Dagfinn Reiersøl 14 days later:

    I came across this a little late. You ask for a good way to test Vim scripts, and I would say the best way is to program Vim in Ruby or Python, or even Perl. I’m just re-implementing my scripts for running tests inside Vim, in Ruby. I was pleasantly surprised that Ruby tests work like a charm inside Vim. That means you can use even the features that control Vim directly from the test suite.

    I’ve blogged a little bit about what I’m doing here

    Of course, this means you need a Vim with Ruby compiled into it, which might mean having to install it. You can’t expect to find it on that venerable 2003 Linux build.

  4. Avatar
    FLV extractor over 3 years later:

    it is good so what

  5. Avatar
    Complete Information On Blog over 3 years later:

    You should go through the on-line tutorial in Vim first if you haven’t done already. It’s usually bundled with Vim as a program called “vimtutor”. This will help you get the hang of the basics of Vim before you start developing in it.

  6. Avatar
    Olivea Copper over 3 years later:

    Thanks for sharing this with all of us. Of course, what a great site and informative posts, I will bookmark this site. Keep doing your great job and always gain my support.

  7. Avatar
    cosplay over 3 years later:

    Thanks for sharing this with all of us. Of course, what a great site and informative posts, I will bookmark this site. Keep doing your great job and always gain my support.

  8. Avatar
    bag manufacturer over 3 years later:

    a project and wanted to get a little assist from VIM. I don’t blame him. It’s a pain to keep track of your test number and there’s always more testing that we’

  9. Avatar
    replica chanel shoes over 3 years later:

    prohibited by

  10. Avatar
    chanel store over 3 years later:

    I hope to leave my comments here. Thank you

  11. Avatar
    iPhone to Mac Transfer over 3 years later:

    The software you can trust to export iPhone music, video and more to Mac.

  12. Avatar
    daisyddan over 3 years later:

    Ugg boot is a legendary brand, you ugg boots can’t understand why uggs online it has a ugly and cunbersome appearance when you have first glimpse of her.But it has been a popular boot in the Eurasian land, now popoular cheap uggs wind blowing all over the world today.The guide of this wind is Euramerian stars. Australia ugg boots snow boots are traditionally made from sheepskin. The wool is tanned into the leather, and the upper part of suede boot the boot is assembled ugg australia with the fleece snow boots on the inside. The soles of the boots uggs are made from rubber, and the uggs outlet stitching is often prominent on the outside of the boot. UK Ugg boots fleece draws away moisture, keeping the uggs outlet feet dry and at body temperature.uk ugg baby boots boots are sold well in ugg boots store.Today they come in a variety of colours, including black, pink, blue, chestnut, sky blue,and fuchsia.They are cheap ugg boots available in both slip-on and lace-up varieties ugg boots and their height can range ugg boots from just above the ankle to above the uggs outlet knee.

  13. Avatar
    puma over 3 years later:

    If you mean to find great shoes for your children puma speed trainers also provides a mixture of finicky and affordable shoes for them. There are a lot of choices, it is up ring call,Ugg Boots, after by people that indigence an incredible quantity of column. This will make the customers happier. If you are often tangled in Singapore womens puma future cat shoes sale at Sainte Marie that could enhance operational efficiency, range visibility and turnaround time,” said Desmond Chan, managing boss, South Asia, Menlo Worldwide Logistics. “Our multi-client facility in Boon Lay Way provides puma trainers with different flag. puma uk’s youngest targets are toddlers. The puma for sale shoes are incredibly affordable, yet they still hold the grace. Wearing comfortable shoes will help children exploit better.

  14. Avatar
    Alix over 3 years later:

    Testkings is appreciative to advertise the new and arising certifications alertness guides and braindumps with appropriate abatement for their registered candidates and for new candidates alone for bound time of period. Don’t decay your time, abstraction by testking’s provided assay alertness kit, save money and access your certifications with accomplished scores. By the Grace of Almighty God, our braindumps are broadly acclimated by the IT able common and humans like to acquirement braindumps and convenance analysis from testking. You will not alone canyon your acceptance assay but, enhance your ability and abilities about the assay artefact you are traveling to pursue. testking 70-536|testking 70-653|testking 70-686|testking 642-357|testking 642-515|testking 000-152|testking 000-105

  15. Avatar
    Silicone Molding over 3 years later:

    Intertech Machinery Inc. provides the most precise Plastic Injection Mold and Rubber Molds from Taiwan. With applying excellent unscrewing device in molds, Intertech is also very professional for making flip top Cap Molds in the world.

  16. Avatar
    axial fan over 4 years later:

    Thanks for writing this blog post, it was informative, enjoyable, and most importantly – a good length!

  17. Avatar
    Criminal Records over 4 years later:

    The error messages are not very obvious, and we had only manual testing. I think that I can come up with a way to UT the scripts, if I were willing to spend the time.

  18. Avatar
    Tenant Screening over 4 years later:

    You ask for a good way to test Vim scripts, and I would say the best way is to program Vim in Ruby or Python, or even Perl.

  19. Avatar
    cable ties over 4 years later:

    informative post!

  20. Avatar
    jaychouchou over 4 years later:

    To be, or not to be- that is a question.Whether ipad bag tis nobler in the mind to suffer The slings and Game Controllers arrows of outrageous fortune Or to take arms against a sea of troubles, And USB Gadgets by opposing end them.???????????????

  21. Avatar
    Jimmy over 4 years later:

    This is an informative post. I like it. Thanks. – roof venice

  22. Avatar
    okey oyunu oyna over 4 years later:

    i need this code. Thanks for sharing…

  23. Avatar
    solo hd over 4 years later:

    This solo hd of headset timbre and its appearance style are very alike—again, very thin. beats solo hd voice very open seem a bit too thin, Fabulous Monster Limited Edition GOLD low frequency partial hard, descend not beautiful but speed feeling good. discount solo hd hf performance is good, accurate and not mellow, intermediate frequency performance is regular. Overall Classic Monster Powered Isolatio black voice more features, more suitable for listening to electronic music or part of the pop.

  24. Avatar
    brand watches sale over 4 years later:

    I am just new to your blog and just spent about 1 hour and 30 minutes lurking and reading. I think I will frequent your blog from now on after going through some of your posts. I will definitely learn a lot from them.

  25. Avatar
    real estate advertising over 4 years later:

    wow very informative post i really like it, thanks a lot.

  26. Avatar
    Cheap Oakleys over 4 years later:

    Life is boring

  27. Avatar
    Monster Beats By Dr. Dre Studio over 4 years later:

    The Monster Beats By Dr. Dre Studio Headphones are classic shoes.You will be proud of owning them. Don’t hesitate!Take Monster Beats By Dr. Dre Solo Headphones home!

  28. Avatar
    christian louboutin shoes on sale over 4 years later:

    Have the christian louboutin patent leather pumps is a happy thing. Here have the most complete kinds of christian louboutin leather platform pumps.

  29. Avatar
    http://www.beatsdrheadphone.com over 4 years later:

    http://www.beatsdrheadphone.com Monster Beats dr. dre Studio Kobe Bryant Limited Edition Headphones on the headset’s design from the good precision. It reveals today’s digital music, including the most complete audio and acoustic requirements of rock, hip hop and R & B With advanced speaker design. Power amplification, and active noise cancellation, the beat to provide all the power, clarity, deep bass and today’s top artists and producers want you to be the ideal dre hear. Beats can be attached to any portable media player and an excellent alternative for the headphones, in the box have the ability to capture the sound, the sound commitment to provide high-definition. Will support and electronic products in the music and some of the most prestigious training support! Music makes the world better!red beats by dre studio| beats limited edition headphones| MONSTER BEATS LAMBORGHINI| Kobe Monster Studio Headphone| Dr. Dre Studio Limited Edition|

  30. Avatar
    beats by dre store over 4 years later:

    , I want to find dinosaur fossils on the museum, I want to ride the dinosaur’s back, quietly awaited the moment of destruction of the world. When the time in the past N years later, when the earth gave birth to new life,high quality headphones new design headphones

  31. Avatar
    zentai suit over 4 years later:

    There are zentai that are loved by many people. At cost effective price vzentai has a large collection of different styles lycra suit ?zentai suits , tiger zentai including spandex bodysuit ,metallic zentai suit ?christian louboutin outlet , shiny metallic zentai suits , pvc catsuit , christian louboutin shoes and black zentai . Vzentais is a reliable online jimmy choo outlet supplier.

  32. Avatar
    beats by dr dre over 4 years later:

    want you to be the ideal dre hear. Beats can be attached to any portable media player and anbeats by dr dre beats by dre sale excellent alternative for the headphones, in the box have the ability to capture the sound, the sound commitment to provide high-definition.

  33. Avatar
    what is an iva over 4 years later:

    Its very informative. What a job well done. Keep it up!

  34. Avatar
    Canada Goose Coats over 4 years later:

    Canada Goose Coats

  35. Avatar
    christianlouboutin over 4 years later:

    All among us realise that if you MBT boots or shoes within get hold of, Jimmy Choo Sandaleseducation-women’ vertisements mbt tunisha providing may easily really encourages lymphatic circulation,Jimmy Choo Bottines you’ chemical in all probability more significant receive boots or shoes clearance retail store as a result of MBT while it a good number of at no cost submitting in combination with MBT boots or shoes are almost always pay for and also profit designed notnaxcanada goose outlet.

    A particular low-priced MBT Nama Boots and shoes out of a number of online space boots and shoes plus boot footwear MBT great bargains preferred now in now would be to simply and even safely mbt sandals pay for consumers pay for progressively more over the internetcanada goose jakke, have MBT footwear and remaining grown to be a sample. MBT boots providing now, ways to explain any one prevent
    the north face

    ? Brand-new assumed to test a person's MBT boots pay for, generate a test? My wife and i reassurance any one, we have a special working experience
    North Face Denali Jakker Kvinder hoodie

    .
  36. Avatar
    ysbearing over 4 years later:

    Slewing bearing called slewing ring bearings, is a comprehensive load to bear a large bearing, can bear large axial, radial load and overturning moment.

  37. Avatar
    Tips For Bowling over 4 years later:

    Do not share the knowledge with which you have been blessed with everyone in general, as you do with some people in particular; and know that there are some men in whom Allah, may He he glorified, has placed hidden secrets, which they are forbidden to reveal.

  38. Avatar
    alwadifa 2012 over 4 years later:

    want you to be the ideal dre hear. Beats can be attached to any portable media player and anbeats by dr dre beats by dre sale excellent alternative

  39. Avatar
    handbags and purses over 5 years later:

    It’s useful to study your blog. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent work.

  40. Avatar
    backup iPhone sms over 5 years later:

    Well. Though I am not a good application developer. And I need do more hard work to improve myself. When I come to here. I know that I have come to the right place to learn something I need. Thanks for your good advice. And I will do the practice as possible as I can. Thanks.

  41. Avatar
    Tiffany Silver over 5 years later:

    Please remember that vintage watches have survived for many decades by being treated Romance God created the brand since 1988, the company, registered in 102 countries around the world omega seamaster ladies watches,jewelry brand,merchandise exports to 70 countries, is a specialized manufacturer of watches and jewelry. Romance of God in 1998 to become a global brand,hire famous Swiss designer Wolfgang Jonsson program development of brand identity company logos,uniform color. Romance God omega seamaster quartz watches Korea Co., Ltd. has become the leading companies in the industry, the reputation is well known South Korean brand abroad. God watches Co.Ltd. has always been committed to technological innovation and excellence in quality romantic and fashion capture,fashion jewelry has become a world dedicated to business goals and tireless efforts.

  42. Avatar
    louboutin sales over 5 years later:

    Use VIM to do TUT Unit Testing 41 hoo,good article!!I like the post!32

  43. Avatar
    bladeless fans over 5 years later:

    Use VIM to do TUT Unit Testing 42 good post170

Comments