-1

I have written a console application that waits for the user char input.

application has function:

    typedef char *ProcessedDataType;

    extern ProcessedDataType askUserInput(void){
        int i = getchar();
        ProcessedDataType local_var = userInputProcessed(i);
        return ProcessedDataType;
    }

    static ProcessedDataType userInputProcessed(int i){
        ...
        return PocessedDataType;
    }

Now, I would like to test this function for several inputs. So where normally the cursor would be blinking for user input, now, automatically, entries from a file that contains the choice letters returns. This would then be asserted against the known result.

    runTest{

        // when console gets input 'A'
        ProcessedDataType local_var = askUserInput();
        assert('ProcessedA' == local_var);        }

        // when console gets input 'B'
        ProcessedDataType local_var = askUserInput();
        assert('ProcessedB' == local_var);        }

        // when console gets input 'C'
        ProcessedDataType local_var = askUserInput();
        assert('ProcessedC' == local_var);        }

How do I provide the same string parameters to this function when testing? How do I 'feed' the console the inputs A, B, C, etc ...

I do not want to use Preprocessors as here: How to unit test c functions involving IO?

Community
  • 1
  • 1
hewi
  • 1,274
  • 3
  • 17
  • 32
  • How will `getchar` get 9 characters? – aschepler May 23 '16 at 19:16
  • `return ProcessedDataType;` WTF? Please supply compilable code. And what is `assert('ProcessedA' == local_var);` suppoosed to do? Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) which shows what you have tried, not some fantasy. – Weather Vane May 23 '16 at 19:27
  • does the typedef clear the confusion? – hewi May 24 '16 at 12:56
  • Avoid *typedeffing* pointers, it just lends to confusion throughout the code. How in the hell is `typedef char *ProcessedDataType;` shorter than `char *`? – David C. Rankin May 24 '16 at 12:56
  • it is what they would call 'an abstract data type'!? Had this discussion at length, could you give indication why abstracting like this is not a good idea? – hewi May 25 '16 at 05:52
  • @hewi, If you found an answer provided here helpful, please upvote and/or mark as accepted, so that other users with a similar question can benefit. – Wayne Booth May 29 '16 at 08:50

1 Answers1

0

Other than the obvious that "getchar" is only going to collect one character.... I've therefore replaced this method with a method "getchars", but you can eventually replace with what you actually need.

You're going to need to mock the method that would collect user input, in your test.

#define getchars mockgetchars

const char* expected;
char mockgetchars() {
    return expected;
}

expected = "AAAAAAA\n";
ProcessedDataType local_var = askUserInput();
assert("AAAAAAA", local_var);
Wayne Booth
  • 424
  • 2
  • 8
  • If you wNt to use getchars, you can (and your going to update your app to actually call getchars more than once. You can replace mock getchars return with "expected++" – Wayne Booth May 23 '16 at 19:39
  • Possible, but your losing control between your SUT and the test harness. You could read from a file in your test harness to populate the "expected" value. – Wayne Booth May 23 '16 at 19:56