1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/video_engine/test/auto_test/primitives/choice_helpers.h" 12 13 #include <assert.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 17 #include <algorithm> 18 #include <sstream> 19 20 namespace webrtc { 21 22 ChoiceBuilder::ChoiceBuilder(const std::string& title, const Choices& choices) 23 : choices_(choices), 24 input_helper_(TypedInput(title)) { 25 input_helper_.WithInputValidator( 26 new IntegerWithinRangeValidator(1, choices.size())); 27 input_helper_.WithAdditionalInfo(MakeHumanReadableOptions()); 28 } 29 30 int ChoiceBuilder::Choose() { 31 std::string input = input_helper_.AskForInput(); 32 return atoi(input.c_str()); 33 } 34 35 ChoiceBuilder& ChoiceBuilder::WithDefault(const std::string& default_choice) { 36 Choices::const_iterator iterator = std::find( 37 choices_.begin(), choices_.end(), default_choice); 38 assert(iterator != choices_.end() && "No such choice."); 39 40 // Store the value as the choice number, e.g. its index + 1. 41 int choice_index = (iterator - choices_.begin()) + 1; 42 char number[16]; 43 sprintf(number, "%d", choice_index); 44 45 input_helper_.WithDefault(number); 46 return *this; 47 } 48 49 ChoiceBuilder& ChoiceBuilder::WithInputSource(FILE* input_source) { 50 input_helper_.WithInputSource(input_source); 51 return *this; 52 } 53 54 std::string ChoiceBuilder::MakeHumanReadableOptions() { 55 std::string result = ""; 56 Choices::const_iterator iterator = choices_.begin(); 57 for (int number = 1; iterator != choices_.end(); ++iterator, ++number) { 58 std::ostringstream os; 59 os << "\n " << number << ". " << (*iterator).c_str(); 60 result += os.str(); 61 } 62 return result; 63 } 64 65 Choices SplitChoices(const std::string& raw_choices) { 66 return Split(raw_choices, "\n"); 67 } 68 69 ChoiceBuilder FromChoices( 70 const std::string& title, const std::string& raw_choices) { 71 return ChoiceBuilder(title, SplitChoices(raw_choices)); 72 } 73 74 } // namespace webrtc 75