| 17 | | ### This test suite tests the stage1 (static) parser and the metagrammar it |
| 18 | | ### parses in which the actual parser-generator's behaviour is defined. |
| 19 | | class EnglishConjunctionsTestCase < Linguistics::TestCase |
| 20 | | |
| 21 | | Linguistics::use( :en, :installProxy => true ) |
| 22 | | include Linguistics::EN |
| | 25 | describe Linguistics::EN do |
| | 26 | |
| | 27 | before( :all ) do |
| | 28 | Linguistics::use( :en ) |
| | 29 | include Linguistics::EN |
| | 30 | end |
| | 31 | |
| | 32 | |
| | 33 | describe "conjunctions with an Array of a single element" do |
| | 34 | |
| | 35 | before( :each ) do |
| | 36 | @array = ['cat'] |
| | 37 | end |
| | 38 | |
| | 39 | it "results in a phrase with indefinite article" do |
| | 40 | @array.en.conjunction.should == "a cat" |
| | 41 | end |
| | 42 | |
| | 43 | end |
| | 44 | |
| | 45 | |
| | 46 | describe "conjunction with an Array of two different words" do |
| | 47 | |
| | 48 | before( :each ) do |
| | 49 | @array = ['cat', 'dog'] |
| | 50 | end |
| | 51 | |
| | 52 | it "results in a phrase joined with 'and' with default options" do |
| | 53 | @array.en.conjunction.should == "a cat and a dog" |
| | 54 | end |
| | 55 | |
| | 56 | it "results in a phrase joined with 'plus' if 'plus' is set as the conjunctive" do |
| | 57 | @array.en.conjunction(:conjunctive => 'plus').should == "a cat plus a dog" |
| | 58 | end |
| | 59 | |
| | 60 | it "results in a phrase joined with a space if an empty string is set as the conjunctive" do |
| | 61 | @array.en.conjunction(:conjunctive => '').should == "a cat a dog" |
| | 62 | end |
| | 63 | |
| | 64 | end |
| | 65 | |
| | 66 | |
| | 67 | describe "conjunction with an Array of two words that differ only in case" do |
| | 68 | |
| | 69 | before( :each ) do |
| | 70 | @array = ['cat', 'Cat'] |
| | 71 | end |
| | 72 | |
| | 73 | it "combines them into their downcased equivalents with default options" do |
| | 74 | @array.en.conjunction.should == "two cats" |
| | 75 | end |
| | 76 | |
| | 77 | it "lists them separately if :combine is set to false" do |
| | 78 | @array.en.conjunction(:combine => false).should == "a cat and a Cat" |
| | 79 | end |
| | 80 | |
| | 81 | it "doesn't combine them if :casefold is turned off" do |
| | 82 | @array.en.conjunction(:casefold => false).should == "a cat and a Cat" |
| | 83 | end |
| | 84 | |
| | 85 | it "combines and lists them with a non-specific count if :generalize is set" do |
| | 86 | @array.en.conjunction(:generalize => true).should == "several cats" |
| | 87 | end |
| | 88 | |
| | 89 | end |
| | 90 | |
| | 91 | |
| | 92 | describe "conjunction with an Array of many (more than two) words of varying cases" do |
| | 93 | |
| | 94 | before( :each ) do |
| | 95 | @array = %w{cat dog fox dog chicken chicken Fox chicken goose Dog goose} |
| | 96 | end |
| | 97 | |
| | 98 | it "combines them into their downcased equivalents and lists them in order of amount " + |
| | 99 | "with default options" do |
| | 100 | @array.en.conjunction.should == |
| | 101 | 'three dogs, three chickens, two foxes, two geese, and a cat' |
| | 102 | end |
| | 103 | |
| | 104 | it "lists them separately if :combine is set to false" do |
| | 105 | @array.en.conjunction(:combine => false).should == |
| | 106 | 'a cat, a dog, a fox, a dog, a chicken, a chicken, a Fox, a '\ |
| | 107 | 'chicken, a goose, a Dog, and a goose' |
| | 108 | end |
| | 109 | |
| | 110 | it "doesn't combine the differently-cased ones if :casefold is turned off" do |
| | 111 | @array.en.conjunction(:casefold => false).should == |
| | 112 | 'three chickens, two dogs, two geese, a cat, a fox, a Fox, '\ |
| | 113 | 'and a Dog' |
| | 114 | end |
| | 115 | |
| | 116 | it "combines and lists them with a non-specific count if :generalize is set" do |
| | 117 | @array.en.conjunction(:generalize => true).should == |
| | 118 | 'several dogs, several chickens, several foxes, several '\ |
| | 119 | 'geese, and a cat' |
| | 120 | end |
| | 121 | |
| | 122 | end |
| 24 | | Tests = { |
| 25 | | # Test name => { |
| 26 | | # target => { |
| 27 | | # {<options>} => <expected output> |
| 28 | | # }, |
| 29 | | :singleWord => { |
| 30 | | ['cat'] => { |
| 31 | | {} => %{a cat} |
| 32 | | }, |
| 33 | | }, |
| 34 | | |
| 35 | | :twoWord => { |
| 36 | | %w{cat dog} => { |
| 37 | | {} => 'a cat and a dog', |
| 38 | | {:conjunctive => 'plus'} => 'a cat plus a dog', |
| 39 | | {:conjunctive => ''} => 'a cat a dog', |
| 40 | | }, |
| 41 | | %w{cat Cat} => { |
| 42 | | {} => %{two cats}, |
| 43 | | {:combine => false} => 'a cat and a Cat', |
| 44 | | {:casefold => false} => 'a cat and a Cat', |
| 45 | | {:generalize => true} => 'several cats', |
| 46 | | }, |
| 47 | | }, |
| 48 | | |
| 49 | | :manyWord => { |
| 50 | | %w{cat dog fox dog chicken chicken Fox chicken goose Dog goose} => { |
| 51 | | {} => 'three dogs, three chickens, two foxes, two geese, and a cat', |
| 52 | | {:combine => false} => |
| 53 | | 'a cat, a dog, a fox, a dog, a chicken, a chicken, a Fox, a '\ |
| 54 | | 'chicken, a goose, a Dog, and a goose', |
| 55 | | {:casefold => false} => |
| 56 | | 'three chickens, two dogs, two geese, a cat, a fox, a Fox, '\ |
| 57 | | 'and a Dog', |
| 58 | | {:generalize => true} => |
| 59 | | 'several dogs, several chickens, several foxes, several '\ |
| 60 | | 'geese, and a cat', |
| | 124 | |
| | 125 | describe "conjunction with an object-transform block" do |
| | 126 | |
| | 127 | it "doesn't still have #6: #conjunction doesn't invoke supplied block under some conditions" |
| | 128 | before( :each ) do |
| | 129 | # Create a new class, as we need to guarantee that this will be the |
| | 130 | # first #conjunction call to it. |
| | 131 | @collection = Class::new { |
| | 132 | include Enumerable, Linguistics |
| | 133 | def initialize( *ary ) |
| | 134 | @ary = ary.flatten |
| | 135 | end |
| | 136 | |
| | 137 | # Delegate #each to the contained Array |
| | 138 | def each( &block ) |
| | 139 | @ary.each( &block ) |
| | 140 | end |
| 62 | | }, |
| 63 | | } |
| 64 | | |
| 65 | | # Auto-generate tests for the dataset |
| 66 | | Tests.each_with_index {|test,i| |
| 67 | | name = test[0] |
| 68 | | methname = "test_%03d_%s" % |
| 69 | | [ i + 50, name ] |
| 70 | | define_method( methname ) { |
| 71 | | printTestHeader "Conjunction: #{name}" |
| 72 | | Tests[name].each {|target, tests| |
| 73 | | rval = nil |
| 74 | | tests.each {|opts, output| |
| 75 | | op = "conjunction of %s with options=%s" % |
| 76 | | [ target.inspect, opts.inspect ] |
| 77 | | |
| 78 | | # Method interface |
| 79 | | assert_nothing_raised( op ) { |
| 80 | | rval = target.en.conjunction( opts ) |
| 81 | | } |
| 82 | | assert_equal output, rval, op |
| 83 | | |
| 84 | | # Function interface |
| 85 | | assert_nothing_raised( op ) { |
| 86 | | rval = conjunction( target, opts ) |
| 87 | | } |
| 88 | | assert_equal output, rval, op |
| 89 | | } |
| 90 | | } |
| 91 | | } |
| 92 | | } |
| 93 | | |
| 94 | | ### Overridden initializer: auto-generated test methods have an arity of 1 |
| 95 | | ### even though they don't require an argument (as of the current Ruby CVS), |
| 96 | | ### and the default initializer throws an :invalid_test for methods with |
| 97 | | ### arity != 0. |
| 98 | | def initialize( test_method_name ) |
| 99 | | if !respond_to?( test_method_name ) |
| 100 | | throw :invalid_test |
| 101 | | end |
| 102 | | @method_name = test_method_name |
| 103 | | @test_passed = true |
| 104 | | end |
| 105 | | |
| 106 | | |
| 107 | | ################################################################# |
| 108 | | ### T E S T S |
| 109 | | ################################################################# |
| 110 | | |
| 111 | | Items = %w{cow chicken blancmange cyclist} |
| 112 | | |
| 113 | | # Test for defect #6 |
| 114 | | def test_conjunction_should_use_supplied_block_for_object_transform_on_first_invocation |
| 115 | | rval = nil |
| 116 | | |
| 117 | | # Create a new class, as we need to guarantee that this will be the |
| 118 | | # first #conjunction call to it. |
| 119 | | collection = Class::new { |
| 120 | | include Enumerable, Linguistics |
| 121 | | def initialize( *ary ) |
| 122 | | @ary = ary.flatten |
| 123 | | end |
| 124 | | |
| 125 | | # Delegate #each to the contained Array |
| 126 | | def each( &block ) |
| 127 | | @ary.each( &block ) |
| 128 | | end |
| 129 | | } |
| 130 | | |
| 131 | | obj = collection.new( 'foo', 'bar', 'baz' ) |
| 132 | | |
| 133 | | assert_nothing_raised do |
| 134 | | rval = obj.en.conjunction {|word| "%d-letter word" % word.length } |
| 135 | | end |
| 136 | | end |
| | 142 | |
| | 143 | @obj = @collection.new( 'foo', 'bar', 'baz', 'tree', 'node', 'sonogram' ) |
| | 144 | end |
| | 145 | |
| | 146 | it "uses supplied block for object transform on first invocation" do |
| | 147 | @obj.en.conjunction {|word| "%s-letter word" % word.length.en.numwords }.should == |
| | 148 | "three three-letter words, two four-letter words, and an eight-letter word" |
| | 149 | end |
| | 150 | end |
| | 151 | |