.net - Unit testing with NUnit and Console -
i've been playing around curses sharp library (a c# wrapper pdcurses), writing unit test code handle on api , how works, , i've come question.
i can run curses sharp within dll (so nunit can test it), using following code:
bool consoleallocated = allocconsole(); if (!consoleallocated) throw new exception("unable allocate new console."); curses.initscr(); stdscr.add(4, 6, "this test title"); curses.endwin(); freeconsole();
allocconsole , freeconsole extern's imported kernel32.
what able read console output position 4,6 string programmatically check string i've entered has been output correctly. pretty important able checks in order create curses-style app using tdd instance.
i've looked on curses , stdscr objects (both curses sharp objects), , console object (from windows library) , haven't been able find way yet. have ideas?
i managed find answer, in case interested i've included code below. it's messy, haven't cleaned yet, should serve example how this.
thanks pinvoke.net excellent signature collection.
[dllimport("kernel32", setlasterror = true)] static extern bool allocconsole(); [dllimport("kernel32", setlasterror = true)] static extern bool freeconsole(); [dllimport("kernel32", setlasterror = true)] static extern intptr getstdhandle(int nstdhandle); [dllimport("kernel32", setlasterror = true)] static extern bool readconsoleoutputcharacter(intptr hconsoleoutput, [out]stringbuilder lpcharacter, uint nlength, coord dwreadcoord, out uint lpnumberofcharsread); const int std_output_handle = -11; [structlayout(layoutkind.sequential)] struct coord { public short x; public short y; } [test] public void writetitle() { bool consoleallocated = allocconsole(); if (!consoleallocated) throw new exception("unable allocate new console."); curses.initscr(); stdscr.add(4, 6, "this test title"); stdscr.refresh(); intptr stdout = getstdhandle(std_output_handle); uint length = 20; stringbuilder consoleoutput = new stringbuilder((int)length); coord readcoord; readcoord.x = 6; readcoord.y = 4; uint numofcharsread = 0; readconsoleoutputcharacter(stdout, consoleoutput, length, readcoord, out numofcharsread); string outputstring = consoleoutput.tostring(); assert.that(outputstring, is.equalto("this test title")); curses.endwin(); freeconsole(); }
Comments
Post a Comment