| | 35 | ### ArgumentMatchers |
|---|
| | 36 | ### |
|---|
| | 37 | ### These matchers are used to test argument handling. |
|---|
| | 38 | ### |
|---|
| | 39 | ### * ValidationMatcher -- lets us check for acceptance and rejection of input values |
|---|
| | 40 | module ArgumentMatchers |
|---|
| | 41 | class ValidationMatcher |
|---|
| | 42 | def initialize( input, mode = :accept ) |
|---|
| | 43 | @input = input |
|---|
| | 44 | @mode = mode |
|---|
| | 45 | end |
|---|
| | 46 | |
|---|
| | 47 | def matches?( argument ) |
|---|
| | 48 | @argument = argument |
|---|
| | 49 | |
|---|
| | 50 | begin |
|---|
| | 51 | argument.process( @input ) |
|---|
| | 52 | rescue Linen::Plugin::ArgumentError |
|---|
| | 53 | result = :rejected |
|---|
| | 54 | # the exception means the argument was rejected, so return true |
|---|
| | 55 | # in reject mode and false in accept mode. |
|---|
| | 56 | return true if @mode == :reject |
|---|
| | 57 | return false |
|---|
| | 58 | else |
|---|
| | 59 | # no exception means the argument was accepted |
|---|
| | 60 | return false if @mode == :reject |
|---|
| | 61 | return true |
|---|
| | 62 | end |
|---|
| | 63 | end |
|---|
| | 64 | |
|---|
| | 65 | def failure_message |
|---|
| | 66 | "expected argument #{@argument} to accept '#{@input}', but it was rejected." |
|---|
| | 67 | end |
|---|
| | 68 | |
|---|
| | 69 | def negative_failure_message |
|---|
| | 70 | "expected argument #{@argument} to reject '#{@input}', but it was accepted." |
|---|
| | 71 | end |
|---|
| | 72 | end |
|---|
| | 73 | |
|---|
| | 74 | def accept( input ) |
|---|
| | 75 | ArgumentMatchers::ValidationMatcher.new( input, :accept ) |
|---|
| | 76 | end |
|---|
| | 77 | |
|---|
| | 78 | def reject( input ) |
|---|
| | 79 | ArgumentMatchers::ValidationMatcher.new( input, :reject ) |
|---|
| | 80 | end |
|---|
| | 81 | end |
|---|